【问题标题】:ReferenceError: webpack is not definedReferenceError:未定义 webpack
【发布时间】:2015-07-23 16:15:40
【问题描述】:

在我的 webpack 应用程序中,我有一个由“npm run build”触发的基本构建过程,它执行 webpack 二进制文件并将 /app 中的 index.html 复制到 /dist。每当我运行npm run build 时,我都会得到ReferenceError: webpack is not defined,但是当我运行npm start 时,它会启动webpack-dev-server,一切都很好。

这是我的 webpack 配置文件:

var ExtractTextPlugin = require('extract-text-webpack-plugin');

var config = {
    context: __dirname + '/app',
    entry: './index.js',
    output: {
        path: __dirname + '/app',
        filename: 'app.js'
    },
    module: {
        loaders: [
            { test: /\.js$/, loader: 'babel', exclude: /node_modules/ },
            { test: /\.html$/, loader: 'raw', exclude: /node_modules/ },
            { test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 'css!sass'), exclude: /node_modules/}
        ]
    },
    plugins: [
        new ExtractTextPlugin('app.css')
    ]
};

if (process.env.NODE_ENV == 'production') {
    config.output.path = __dirname + '/dist';
    config.plugins.push(new webpack.optimize.UglifyJsPlugin());
}

module.exports = config;

【问题讨论】:

    标签: node.js webpack


    【解决方案1】:

    你不见了

    var webpack = require('webpack');
    

    在文件的开头。如果您想稍微优化执行,可以将其推入您的 if 块中。

    【讨论】:

    • 我还得跑:npm install --save webpack
    • 应该是--save-dev 否则你将运送带有生产代码的webpack
    • 写完那一行后,它说'webpack 已声明但从未使用'。怎么用?
    • 它包含一堆插件。在上面的示例中,使用了 webpack.optimize.UglifyJsPlugin。最好检查 webpack API 以获取确切的详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2019-03-19
    • 2018-10-29
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多