webpack-dev-server 在语法错误后停止编译,需要重新启动

6

我的 webpack-dev-server 通常可以正常工作,实时更新也可以正常运行,一切都很好。

然而,如果我保存了一个有语法错误的文件,服务器就停止编译并要求我重新启动它才能使其再次正常工作。

例如,假设我在 .scss 文件中意外添加了一个逗号,webpack-dev-server 将输出以下错误:

Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Invalid CSS after '...oto Condensed",': expected expression (e.g. 1px, bold), was ", sans-serif;"
        on line 18 of /Users/r/Documents/sol/src/styles/app.scss
>>   font-family: "Roboto Condensed",, sans-serif;

   ----------------------------------^

 @ ./src/styles/app.scss 2:26-181
 @ ./src/index.jsx
ℹ 「wdm」: Failed to compile.

但是当我通过删除逗号并保存文件来修复错误时,服务器不会像通常一样重新编译(似乎根本没有输出任何东西)。我必须杀死webpack-dev-server并重新启动它。

我在网上找到的与我的问题最接近的解决方案是这个https://github.com/webpack/webpack-dev-server/issues/463,但那里的解决方案并没有解决我的问题。

这是我的webpack.config.js:

const path = require('path');

module.exports = {
    entry: path.resolve(__dirname, 'src', 'index.jsx'),

    output: {
        path: path.resolve(__dirname, 'output'),
        filename: 'bundle.js'
    },

    devServer: {
        publicPath: '/output/',
        contentBase: path.resolve(__dirname, 'src'),
    },
    resolve: {
        extensions: ['.js', '.jsx']
    },
    module: {
        rules: [
            {
                test: /\.jsx/,
                use: {
                    loader: 'babel-loader',
                    options: { presets: ['@babel/preset-react', '@babel/preset-env'] }
                }
            },
            {
                test: /\.scss/,
                use: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader']
            }
        ]
    }
};

服务器在出现错误后停止编译是否是正常行为?还是有一个标志可以更改,以便我无需在每个语法错误之后重新启动服务器?


你好,你能修复这个问题吗? - HauPham
2个回答

0

我不确定这是否与您的webpack配置相关,但在我删除了prettierprettier-webpack-plugin插件之前,我遇到了同样的问题。 我注意到这是我的日志中崩溃发生的地方:

✖ 「wdm」: SyntaxError: Unexpected token (100:3)
   98 | // cull whenever the viewport moves
   99 | PIXI.Ticker.shared.add(() =>
> 100 |   if (viewport.dirty) {
      |   ^
  101 |     if (viewport.top < chunk.top) loadChunk('top');
  102 |     if (viewport.left < chunk.left) loadChunk('left');
  103 |
    at t (./node_modules/prettier/parser-babel.js:1:379)
    at Object.parse (./node_modules/prettier/parser-babel.js:1:346226)
    at Object.parse (./node_modules/prettier/index.js:13625:19)
    at coreFormat (./node_modules/prettier/index.js:14899:14)
    at format (./node_modules/prettier/index.js:15131:14)
    at ./node_modules/prettier/index.js:57542:12
    at Object.format (./node_modules/prettier/index.js:57562:12)
    at ./node_modules/prettier-webpack-plugin/index.js:70:47
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:69:3)
ℹ 「wdm」: wait until bundle finished: /
ℹ 「wdm」: wait until bundle finished: /```

0

我遇到了同样的问题,并通过在webpack-hmr.config.js中更改"autoRestart: true"来解决了它。

const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');

module.exports = function (options, webpack) {
  return {
    ...options,
    entry: ['webpack/hot/poll?100', options.entry],
    externals: [
      nodeExternals({
        allowlist: ['webpack/hot/poll?100'],
      }),
    ],
    plugins: [
      ...options.plugins,
      new webpack.HotModuleReplacementPlugin(),
      new webpack.WatchIgnorePlugin({
        paths: [/\.js$/, /\.d\.ts$/],
      }),
      new RunScriptWebpackPlugin({
        name: options.output.filename,
        autoRestart: true,
      }),
    ],
  };
};```

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接