【问题标题】:Why does Webpack think I’m running a development build?为什么 Webpack 认为我正在运行开发构建?
【发布时间】:2018-12-02 19:02:33
【问题描述】:

这是一个较旧的项目,很快就会得到一些更新,但在那之前我需要使用原始的 Webpack 配置。奇怪的是,自上次发布以来一直没有触及,但存在阻塞问题。

捆绑生产并在浏览器中运行时,我收到以下消息:

未捕获的错误:发生缩小的异常;使用非缩小开发 完整错误消息和其他有用的环境 警告。

似乎 Webpack 认为我在开发模式下运行,但使用的是缩小的 React 文件,因此出现了消息。我跟踪了所有出现的process.env.NODE_ENV,它们都记录了"production"

这是捆绑文件的构建命令:

node --max_old_space_size=3072 node_modules/.bin/webpack --verbose --colors --display-error-details --config webpack/prod.config.js

……以及 Webpack 配置:

require('babel/polyfill');

// Webpack config for creating the production bundle.
const path = require('path');
const webpack = require('webpack');
const strip = require('strip-loader');

const projectRootPath = path.resolve(__dirname, '../');
const assetsPath = path.resolve(projectRootPath, './static/dist');

const webpackPostcssTools = require('webpack-postcss-tools');
const map = webpackPostcssTools.makeVarMap('./src/theme/index.css');

// https://github.com/halt-hammerzeit/webpack-isomorphic-tools
const WebpackIsomorphicToolsPlugin = require('webpack-isomorphic-tools/plugin');
const webpackIsomorphicToolsPlugin = new WebpackIsomorphicToolsPlugin(require('./webpack-isomorphic-tools'));

module.exports = {
    devtool: 'source-map',
    context: path.resolve(__dirname, '..'),
    entry: {
        'main': [
            './src/client.js'
        ]
    },
    output: {
        path: assetsPath,
        filename: '[name]-[chunkhash].js',
        chunkFilename: '[name]-[chunkhash].js',
        publicPath: '/dist/'
    },
    module: {
        loaders: [
            { test: /\.jsx?$/, exclude: /node_modules/, loaders: [strip.loader('debug'), 'babel']},
            { test: /\.json$/, loader: 'json-loader' },
            { test: /\.css$/, loaders: ['style', 'css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss'] },
            { test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10&mimetype=application/font-woff' },
            { test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10&mimetype=application/font-woff' },
            { test: /\.svg$/, loader: 'svg-inline' },
            { test: webpackIsomorphicToolsPlugin.regular_expression('images'), loader: 'url-loader?limit=10' },
            { test: /\.modernizrrc$/, loader: 'modernizr' }
        ]
    },
    progress: true,
    resolve: {
        alias: {
            font: __dirname + '/../src/fonts',
            images: __dirname + '/../static/images',
            modernizr$: path.resolve(__dirname, '../.modernizrrc')
        },
        modulesDirectories: [
            'src',
            'node_modules'
        ],
        extensions: ['', '.json', '.js', '.jsx']
    },
    postcss: () => {
        return [
            require('autoprefixer')({
                browsers: ['last 3 versions']
            }),
            require('precss'),
            require('postcss-custom-media')({
                extensions: map.media
            })
        ];
    },
    plugins: [
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            'Scribe': 'scribe-editor'
        }),

        new webpack.DefinePlugin({
            __CLIENT__: true,
            __SERVER__: false,
            __DEVELOPMENT__: false,
            __DEVTOOLS__: false
        }),

        // ignore dev config
        new webpack.IgnorePlugin(/\.\/dev/, /\/config$/),

        // set global vars
        new webpack.DefinePlugin({
            'process.env': {
                // Useful to reduce the size of client-side libraries, e.g. react
                NODE_ENV: JSON.stringify('production'),
                API_HOST: JSON.stringify(process.env.API_HOST || 'api'),
                WEB_HOST: JSON.stringify(process.env.WEB_HOST || 'https://www.website.com')
            }
        }),

        // optimizations
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            }
        }),

        webpackIsomorphicToolsPlugin
    ]
};

我似乎找不到问题所在。有什么看起来很可疑的东西跳出来?

【问题讨论】:

  • 我相信它正在成功地构建“生产”。它只是警告您浏览器的开发人员工具中可用的错误信息将受到限制,因为一些开发/调试信息已被删除。在使用开发版本时重新创建问题会提供更多信息。

标签: javascript reactjs webpack


【解决方案1】:

由于您的代码已设置为生产环境,因此它正在被缩小并且您会收到该消息。

尝试将其设置为开发(即NODE_ENV: JSON.stringify('development')

React - Minified exception occurred

【讨论】:

  • 但在生产中我想要缩小代码。我是否只是依靠 Webpack 来运行压缩并总是使用 React 的未压缩代码?更重要的是,我在整个应用程序的许多地方都使用了NODE_ENV === 'production'。为什么我要改变这个 var 仅用于 React 的方式?
猜你喜欢
  • 2019-03-26
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 2022-07-12
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
相关资源
最近更新 更多