webpack definePlugin失败

时间:2018-04-04 23:20:04

标签: webpack vue.js gulp

我尝试使用Webpack为我的Vue项目设置环境变量,但是DefinePlugin不起作用,它不断给我TypeError: Cannot read property 'compilation' of undefined。这是webpack.config.js。

import webpack from 'webpack'; 

module.exports = {
    entry: {
        main: './app'
    },
    output: {
        path: __dirname + '/dist',
        publicPath: '/',
        filename: 'index.js'
    },
    module: {
        rules: [
            {
                test: /\.html$/,
                exclude: /node_modules/,
                use: {loader: 'html-loader'}
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV' : JSON.stringify('production')
        })
    ]
};

,错误是:

TypeError: Cannot read property 'compilation' of undefined
at DefinePlugin.apply (/Users/david/Documents/black-tux-cms/node_modules/webpack/lib/DefinePlugin.js:42:18)
at Compiler.apply (/Users/david/Documents/black-tux-cms/node_modules/webpack-stream/node_modules/tapable/lib/Tapable.js:375:16)
at webpack (/Users/david/Documents/black-tux-cms/node_modules/webpack-stream/node_modules/webpack/lib/webpack.js:33:19)
at Stream.<anonymous> (/Users/david/Documents/black-tux-cms/node_modules/webpack-stream/index.js:134:20)
at _end (/Users/david/Documents/black-tux-cms/node_modules/through/index.js:65:9)
at Stream.stream.end (/Users/david/Documents/black-tux-cms/node_modules/through/index.js:74:5)
at module.exports (/Users/david/Documents/black-tux-cms/node_modules/webpack-stream/index.js:214:12)
at Gulp.<anonymous> (/Users/david/Documents/black-tux-cms/gulp/tasks/scripts.js:8:15)
at module.exports (/Users/david/Documents/black-tux-cms/node_modules/orchestrator/lib/runTask.js:34:7)
at Gulp.Orchestrator._runTask (/Users/david/Documents/black-tux-cms/node_modules/orchestrator/index.js:273:3)

我正在使用gulp来编译我的所有源文件。我是gulp,webpack和vue的新手。

1 个答案:

答案 0 :(得分:0)

根据您使用的 webpack 的版本,您的配置可能会有所不同,请尝试

new webpack.DefinePlugin(
{ 
    PRODUCTION: JSON.stringify(true) 
}
);

而不是

让我们看看结果,您是否也尝试了any other plugins ...也许 IgnorePlugin ,就像这样

new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)

编译没有错误吗?可能你正在混淆版本:D