@babel/plugin-syntax-dynamic-import 的客户端 babel-loader 仍然抱怨顶级导入

client side babel-loader with @babel/plugin-syntax-dynamic-import still complains about top level imports

这是它抱怨的代码片段:

function something(param) {
  import { whatisthis } from './dir/dir/anotherone';
}

错误输出:

SyntaxError: pathtofile.jsx: 'import' and 'export' may only appear at the top level

webpack 配置:

const baseConfig = {
  resolve: {
    extensions: ['*', '.js', '.jsx'],
    alias: {
      imports: path.resolve(__dirname, 'stuff/'),
      ...other alias
    }
  },
  devServer: {
    hot: true,
  },
};

const clientConfig = Object.assign({}, baseConfig, {
  entry: './client/main.js',
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.js$|\.jsx$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            plugins: [
              '@babel/plugin-syntax-dynamic-import',
              ['@babel/plugin-proposal-decorators', { legacy: true }],
              '@babel/plugin-proposal-function-sent',
              '@babel/plugin-proposal-export-namespace-from',
              '@babel/plugin-proposal-numeric-separator',
              '@babel/plugin-proposal-throw-expressions',
              '@babel/plugin-syntax-import-meta',
              ['@babel/plugin-proposal-class-properties', { loose: false }],
              '@babel/plugin-proposal-json-strings',
              '@babel/plugin-proposal-object-rest-spread',
              '@babel/plugin-transform-runtime',
            ],
            presets: ['@babel/preset-env', '@babel/preset-react'],
          },
        },
      },
      {
        test: /\.css|\.scss$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './client/body.html',
    }),
    new webpack.HotModuleReplacementPlugin(),
  ],
});

我已经尝试过上述配置,也尝试过用 .babelrc 文件替换它,但无论如何,它总是不断抱怨顶级导入。还删除了 node_modules 和锁定文件,重新安装并发生了同样的事情。这不应该工作吗?

PS:我正在使用 webpack4 和 babel7.x。

import语句只能在文件的top-level中使用,这意味着它不能在任何包含{}的范围内使用,例如它不能在

中使用
function something(param) {
  import { whatisthis } from './dir/dir/anotherone';
}

如果需要,可以使用 dynamic import

let whatisthis;
function something(param) {
  import('./dir/dir/anotherone').then(m => (whatisthis = m.whatisthis));
}