加载程序不适用于通过别名解析的文件

时间:2019-06-21 15:58:33

标签: webpack webpack-4

我认为webpack加载程序不适用于通过别名解析的文件。 有人遇到过这样的问题吗?还是有人可以反驳这个假设?

完整上下文:

我开发了自己的npm软件包my-package。另外,我有一个使用此my-package的测试项目。为了改善开发经验,我想创建一个单独的Webpack配置,该配置将使用本地软件包src文件替换my-package中的node_modules

它可以工作,但是我在my-package个文件之一中遇到下一个错误:

Support for the experimental syntax 'classProperties' isn't currently enabled (35:20):

  33 | 
  34 | class AddressForm extends PureComponent {
> 35 |   static propTypes = {
     |                    ^
  36 |     // eslint-disable-next-line react/require-default-props
  37 |     onSubmit: (props, propName) => {
  38 |       if (

Add @babel/plugin-proposal-class-properties

我的@babel/plugin-proposal-class-properties文件中有.babelrc,并且在测试项目文件中使用了类属性。

这是我的webpack配置:

const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'development',
  devtool: 'inline-source-map',
  entry: {
    'integration-test-page': './src/integration-test-page/index.jsx',
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
          },
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: './src/integration-test-page/index.html',
      filename: './index.html',
    }),
  ],
  resolve: {
    alias: {
      'my-package': path.resolve(
        __dirname,
        '../my-package/src/index.js'
      ),
    },
    extensions: ['.js', '.jsx'],
  },
};

1 个答案:

答案 0 :(得分:0)

我找到了一种解决方法。我必须将babelrcRoots: ['.', '../'],添加到babel.config.js

以下是有关monorepos https://babeljs.io/docs/en/config-files#monorepos的babel配置的详细信息

相关问题