【问题标题】:How to add the class properties plugin to webpack如何将类属性插件添加到 webpack
【发布时间】:2019-04-13 02:13:44
【问题描述】:

我正在尝试让 @babel/plugin-proposal-class-properties 与 webpack 一起使用。

我已经使用节点包管理器 (npm) 安装了插件。

我没有.babelrc,所以我假设这个插件应该进入webpack.config.js

我找到了this page,这让我相信以下是在webpack.config.js 文件中包含插件的好设置:

const webpack = require('webpack');
const ClassPropertiesPlugin = require("@babel/plugin-proposal-class-properties"); //installed via npm
const config = {
    entry:  __dirname + '/js/index.jsx',
    output: {
        path: __dirname + '/dist',
        filename: 'bundle.js',
    },
    resolve: {
        extensions: ['.js', '.jsx', '.css']
    },
    module: {
        rules: [
            {
                test: /\.jsx?/,
                exclude: /node_modules/,
                use: 'babel-loader'
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                loader: "file-loader?name=/public/[name].[ext]",
            },
            {
                test: /\.css$/,
                use: ['css-loader'],
            },
        ]
    },
    plugins: [ClassPropertiesPlugin]
};


module.exports = config;

但是,这导致了错误

<personal info>\static\node_modules\webpack\bin\webpack.js:348
                        throw err;
                        ^

TypeError: arguments[i].apply is not a function
    at Compiler.apply (<personal info><personal info>\static\node_modules\tapable\lib\Tapable.js:375:16)
    at webpack (<personal info>\static\node_modules\webpack\lib\webpack.js:33:19)
    at processOptions (<personal info>\static\node_modules\webpack\bin\webpack.js:335:15)
    at yargs.parse (<personal info>\static\node_modules\webpack\bin\webpack.js:397:2)
    at Object.Yargs.self.parse (<personal info>\static\node_modules\yargs\yargs.js:533:18)
    at Object.<anonymous> (<personal info>\static\node_modules\webpack\bin\webpack.js:152:7)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

当我looked into it 时,这似乎意味着我错误地包含了插件,但我不确定如何。我还尝试了以下完全相同的错误:

const webpack = require('webpack');
const config = {
    entry:  __dirname + '/js/index.jsx',
    output: {
        path: __dirname + '/dist',
        filename: 'bundle.js',
    },
    resolve: {
        extensions: ['.js', '.jsx', '.css']
    },
    module: {
        rules: [
            {
                test: /\.jsx?/,
                exclude: /node_modules/,
                use: 'babel-loader'
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                loader: "file-loader?name=/public/[name].[ext]",
            },
            {
                test: /\.css$/,
                use: ['css-loader'],
            },
        ]
    },
    plugins: ["@babel/plugin-proposal-class-properties"]
};


module.exports = config;

我也不能做new ClassPropertiesPlugin(),因为它说它不是构造函数。

如果没有这个插件,我的 webpack(无法构建特定的 .jsx 文件 这就是为什么我需要这个插件,但在其他方面工作正常)看起来像

const webpack = require('webpack');
const config = {
    entry:  __dirname + '/js/index.jsx',
    output: {
        path: __dirname + '/dist',
        filename: 'bundle.js',
    },
    resolve: {
        extensions: ['.js', '.jsx', '.css']
    },
    module: {
        rules: [
            {
                test: /\.jsx?/,
                exclude: /node_modules/,
                use: 'babel-loader'
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                loader: "file-loader?name=/public/[name].[ext]",
            },
            {
                test: /\.css$/,
                use: ['css-loader'],
            },
        ]
    },
};


module.exports = config;

我希望npm run build 能够正常工作而不会引发错误,并让插件正确编译 javascript。正确地说,我的意思是编译器不会在我正在使用的特定文件上抛出this error

【问题讨论】:

    标签: reactjs webpack babeljs


    【解决方案1】:

    理想情况下,您应该在项目的根目录下有一个.babelrc 文件(如果它不存在,只需创建一个)并将其包含在"plugins" 下,否则您可以在 package.json 中包含一个 babel 配置(不推荐)或者在你的 webpack 配置的 babel-loader options.

    【讨论】:

    • 我最终需要使用npm 安装更多内容并更改我的package.json 文件中的一些内容以使其最终工作(不输出任何编译错误)。但是,最终,这奏效了。谢谢。
    • 你们都安装了什么?
    • @Marc 您可以查看我的 React Bare Bones Kit:github.com/mattcarlotta/react-bb-kit,其中包括如何手动设置:Webpack、babel、SCSS、stylelint、eslint 等以及需要哪些包。
    猜你喜欢
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2013-08-27
    • 2018-12-17
    相关资源
    最近更新 更多