简体   繁体   English

webpack-dev-server 不重新编译(JS 文件和 SCSS 文件)

[英]webpack-dev-server not recompiling (JS files AND SCSS files)

Good morning,早上好,

Rise and shine, the sun is already high in the sky and webpack is ruining my day!升起和闪耀,太阳已经高高挂起,webpack 毁了我的一天!

I'm using webpack-dev-server (through a script in packages.json):我正在使用 webpack-dev-server(通过packages.json 中的脚本):

"scripts": {
    "dev-server": "webpack-dev-server",
 }

That I run with yarn run dev-server我用yarn run dev-server 运行

What I want is the code to recompile and the browser to refresh whenever I save a file.我想要的是每次保存文件时重新编译的代码和刷新浏览器的代码。 I can live with the fact that it doesn't work with SCSS files, but recompiling "manually" on each change in my components is just physically painful.我可以接受它不适用于 SCSS 文件的事实,但是在我的组件中的每个更改上“手动”重新编译只是身体上的痛苦。 I tried a lot of solution found online (non-exhaustive list coming) before asking here, but the result is always the same:在问这里之前,我尝试了很多在线找到的解决方案(非详尽列表即将推出),但结果总是一样的:

ℹ 「wdm」: Compiled successfully ℹ「wdm」:编译成功

And nothing happens when I modify a file (JS or SCSS).当我修改文件(JS 或 SCSS)时没有任何反应。

This is a simple React app, with SCSS for styling.这是一个简单的 React 应用程序,使用 SCSS 进行样式设置。

Here is my webpack config:这是我的 webpack 配置:

const path = require('path');
const MiniCSSExtractPlugin = require('mini-css-extract-plugin');

const mode = process.env.NODE_ENV || 'development';

module.exports = env => {  
  return {
    entry: ['babel-polyfill', './src/app.js'],
    output: {
      path: path.join(__dirname, 'public', 'dist'),
      filename: 'bundle.js'
    },
    module: {
      rules: [{
        loader: 'babel-loader',
        test: /\.js$/,
        exclude: /node_modules/
      }, {
        test: /\.s?css$/,
        loader: [
          mode === 'development' ? 'style-loader' : MiniCSSExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              sourceMap: true
            }
          },
          {
            loader: 'sass-loader',
            options: {
              sourceMap: true
            }
          }
        ]
       }
      ]
    },
    plugins: [
      new MiniCSSExtractPlugin({ filename: 'styles.css' })
    ],
    devtool: env === 'production' ? 'source-map' : 'cheap-module-eval-source-map',
    devServer: {
      contentBase: path.join(__dirname, 'public'),
      publicPath: '/dist/'
    }
  };
};

Here a list of things I tried:这是我尝试过的事情的清单:

  • Add --output-public-path=/dist/ to the script--output-public-path=/dist/到脚本中
  • Use the following content to the devServer config in webpack.config.js:将以下内容用于 webpack.config.js 中的 devServer 配置:
host: '0.0.0.0',
contentBase: path.join(__dirname, 'public'),
publicPath: '/dist/',
historyApiFallback: true,
compress: true,
port: 8080,
watchContentBase: true,
inline: true,
hot: true
  • Use HtmlWebpackConfig with the following config:使用带有以下配置的 HtmlWebpackConfig:
new HtmlWebpackPlugin({
   title: 'Prognostic',
   filename: './public/dist/index.html',
   template: './public/index.html'
})
  • Remove / add webpack and webpack-dev-server删除/添加 webpack 和 webpack-dev-server
  • Use a global webpack-dev-server instead of the project one ( npm i -g webpack-dev-server )使用全局 webpack-dev-server 而不是项目 ( npm i -g webpack-dev-server )
  • Certainly more things but I don't remember... Whoops当然还有更多的东西,但我不记得了......哎呀

For information, here are the version I use:有关信息,这是我使用的版本:

  • Babel-loader@7巴别装载机@7
  • react@^16.8.6,反应@^16.8.6,
  • webpack-dev-server@^3.9.0 webpack-dev-server@^3.9.0
  • webpack@^4.41.2 webpack@^4.41.2

So, I'd like two things to happen:所以,我希望发生两件事:

  1. Automatic recompile when JS file changed JS文件改变时自动重新编译
  2. Automatic recompile when SCSS file changed (if possible) SCSS 文件更改时自动重新编译(如果可能)

If you can help me do that, I'll nominate you my Santa Dev of the year (yes, you can add that to your CV)如果你能帮我做到这一点,我会提名你成为我的年度圣诞老人(是的,你可以将它添加到你的简历中)

Thank you!谢谢!

PS: great laugh when Grammarly told me that my text sounds "friendly" PS:当 Grammarly 告诉我我的文字听起来“友好”时,大笑

Webpack dev server adds a watcher on your files to trigger the compilation when they have been modified. Webpack 开发服务器在您的文件上添加了一个观察器,以在文件被修改时触发编译。 Sometimes though, depending on the text editor you are using, this won't trigger at all.但有时,根据您使用的文本编辑器,这根本不会触发。

I had the same problem, using sublimetext : when I saved my code the webpack dev server wouldn't rebuild.我有同样的问题,使用 sublimetext :当我保存我的代码时,webpack 开发服务器不会重建。

So instead of using the default triggering mechanism, I'm using another option of webpack :因此,我没有使用默认的触发机制,而是使用 webpack 的另一个选项:

  devServer: {
    hot: true,
    watchOptions: { 
      aggregateTimeout: 300, 
      poll: true 
    },
  }

Every 300ms the server will check if files have changed and if so, rebuild.每 300 毫秒,服务器将检查文件是否已更改,如果已更改,则重建。

I hope I am your Santa Dev of the year :]我希望我是你的年度圣诞老人 :]

我认为你不能通过 webpack 做到这一点,你需要使用像react-hot-loader这样的库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM