javascript - 一旦通过 npm 脚本运行,Webpack 就无法工作

标签 javascript webpack webpack-dev-server

一旦我在 cli (cmd) 中运行以下命令,一切都很好:

SET NODE_ENV=production webpack --config webpack.config.js

如果我通过npm脚本运行它,什么也不会发生——既没有输出也没有错误消息。我尝试添加 --display-error-details,但结果是一样的。

请记住,我使用的是 Windows。

这是webpack.config.js:

var fs = require('fs');
var path = require('path');
var webpack = require('webpack');

// Project configuration
var entries = {
    'js/application': ['./app/main']
};

var appPath = path.resolve(__dirname, 'app');
var buildPath = path.join(__dirname, 'build');
var modulesPath = path.resolve(__dirname, 'node_modules');

// We'll bundle some more files for dev purposes, hot-loader and stuff
if (process.env.NODE_ENV != 'production') {
    entries = {
        'js/application': [
            'webpack-hot-middleware/client?https://localhost:3000',
            './app/main',
            './app/styles/main.less'
        ]
    };
}

// Webpack configuration
module.exports =
{
    devtool: 'source-map',
    entry: entries,
    output: {
        path: buildPath,
        filename: '[name].js'
    },
    resolve: {
        root: [modulesPath, appPath],
        extensions: ['', '.js', '.jsx']
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        // needed for UIkit
        new webpack.ProvidePlugin({ // http://webpack.github.io/docs/shimming-modules.html
            $: "jquery",
            jQuery: "jquery",
            L:"leaflet"
        })
    ],
    module: {
        noParse: [],
        loaders: [
            {
                test: /\.jsx?$/,
                loader: 'babel',
                include: appPath
            }, {
                test: /\.json/,
                loader: "json-loader"
            }, {
                test: /\.less$/,
                loader: 'style!css!less'
            }, {
                test: /\.(eot|woff|woff2|ttf|svg|png|jpg)$/,
                loader: 'url-loader?limit=30000&name=[name]-[hash].[ext]'
            }
        ]
    }
};

以下是package.json 脚本:

 "scripts": {
    "build:webpack": "SET NODE_ENV=production webpack --config webpack.config.js"
  },

最佳答案

您的 npm 脚本只是一个命令,您实际运行的是 SET NODE_ENV={everything else}。要让您的脚本在 Windows 上运行,您需要更改一行脚本以运行两个命令,例如SET NODE_ENV=生产&& webpack --config webpack.config.js

引自 documentation复制自"How to run two commands in one line in Windows CMD?" :

Using multiple commands and conditional processing symbols

& [...] command1 & command2
Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

&& [...] command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

关于javascript - 一旦通过 npm 脚本运行,Webpack 就无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41041638/

相关文章:

three.js - 如何使用 WebPack 或 Rollup 摇树 Three.js?

javascript - 如何使用带有 webpack 的 npm install 来使用 normalize.css?

Webpack 没有加载背景图片

javascript - 无法使用 webpack-dev-server 调用 React.Component.setState()

laravel - HMR 不适用于 Docker 中的 Laravel Mix

javascript - 如何在使用 jquery 验证插件验证字段后调用函数

javascript - 我如何解决 : ReferenceError: WebAssembly is not defined

javascript - 使用所选选项对页面上的元素进行排序,而不使用 JQuery

javascript - 使用 Angular 4 将页眉/页脚与内容分开

node.js - React JS : console. 调试不是一个函数