javascript - How to bundle JS and CSS file independently with Webpack? - Stack Overflow

I have a few JS and SCSS files. I need Webpack 4 to bundle each JS entry to one JS file and each SCSS e

I have a few JS and SCSS files. I need Webpack 4 to bundle each JS entry to one JS file and each SCSS entry to one CSS file. The JS files don't import the SCSS files. I try to do it with the following webpack.config.js:

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

module.exports = {
  entry: {
    scriptFoo: './src/js/scriptFoo.js',
    scriptBar: './src/js/scriptBar.js',
    // ...
    styleBaz: './src/css/styleBaz.scss',
    styleBaq: './src/css/styleBaq.scss'
    // ...
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: 'babel-loader'
      },
      {
        test: /\.(scss|sass)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'sass-loader'
        ]
      }
    ]
  },
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist')
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].css'
    })
  ]
};

It works fine, Webpack puts the piled files to the dist directory. But it also creates an excess dummy JS file for each SCSS file in the dist directory:

webpack.config.js
src/
  js/
    scriptFoo.js
    scriptBar.js
    ...
  css/
    styleBaz.scss
    styleBaq.scss
    ...
dist/
  scriptFoo.js
  scriptBar.js
  ...
  styleBaz.css
  styleBaz.js // Excess
  styleBaq.css
  styleBaq.js // Excess
  ...

How to make Webpack not to create the excess JS files?

I have a few JS and SCSS files. I need Webpack 4 to bundle each JS entry to one JS file and each SCSS entry to one CSS file. The JS files don't import the SCSS files. I try to do it with the following webpack.config.js:

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

module.exports = {
  entry: {
    scriptFoo: './src/js/scriptFoo.js',
    scriptBar: './src/js/scriptBar.js',
    // ...
    styleBaz: './src/css/styleBaz.scss',
    styleBaq: './src/css/styleBaq.scss'
    // ...
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: 'babel-loader'
      },
      {
        test: /\.(scss|sass)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'sass-loader'
        ]
      }
    ]
  },
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist')
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].css'
    })
  ]
};

It works fine, Webpack puts the piled files to the dist directory. But it also creates an excess dummy JS file for each SCSS file in the dist directory:

webpack.config.js
src/
  js/
    scriptFoo.js
    scriptBar.js
    ...
  css/
    styleBaz.scss
    styleBaq.scss
    ...
dist/
  scriptFoo.js
  scriptBar.js
  ...
  styleBaz.css
  styleBaz.js // Excess
  styleBaq.css
  styleBaq.js // Excess
  ...

How to make Webpack not to create the excess JS files?

Share Improve this question edited Sep 29, 2018 at 12:47 Finesse asked Sep 29, 2018 at 12:33 FinesseFinesse 10.8k7 gold badges71 silver badges95 bronze badges 1
  • in theory each css should be included where it is being used, or if it is global inside your entrypoint. Webpack creating dummy js when css is an entry point is a known bug, which has not been fixed yet. That depends on them. – PlayMa256 Commented Sep 29, 2018 at 13:38
Add a ment  | 

2 Answers 2

Reset to default 5

Use the ignore-emit-webpack-plugin Webpack plugin to not create the excess file. First install it by running in a console:

npm install --save-dev ignore-emit-webpack-plugin

Then add it to your Webpack configuration:

const IgnoreEmitPlugin = require('ignore-emit-webpack-plugin');

module.exports = {
  // ...
  plugins: [
    // ...
    new IgnoreEmitPlugin(['styleBaz.js', 'styleBaq.js']) // Or simply: new IgnoreEmitPlugin(/^style.*\.js$/)
  ]
};

It is because for each property in the entry object ,The js file is created in output destinations.

output: { filename: '[name].js', path: path.resolve(__dirname, 'dist') },

Webpack creating dummy js when css is an entry point is a known bug, which has not been fixed yet.

Also having multiple entry files in the entry configuration will also affect treeshaking capabilties

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745066364a4609277.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信