如何添加类的属性插件来的WebPack(How to add the class propertie

2019-10-28 22:49发布

我试图让@babel/plugin-proposal-class-properties与工作的WebPack。

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

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

我发现这个网页这使我相信,下面是一个很好的建立了包括在插件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)

当我看着它 ,它看起来这意味着我包括插件错误,但我不知道怎么样。 我也试着使用完全相同的错误如下:

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。 通过正确的,我的意思是,编译器不会抛出这个错误在我使用一个特定的文件。

Answer 1:

理想情况下,你将有一个.babelrc在项目(只创建一个,如果不存在的话),并包括其下的根文件"plugins" ,否则,您可以在您的package.json通天配置(不推荐)或者在你的WebPack配置的巴贝尔装载机选项 。



文章来源: How to add the class properties plugin to webpack