Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract multiple css files but created a unnecessary js file. #279

Closed
stephenliu1944 opened this issue Sep 19, 2018 · 4 comments
Closed

Comments

@stephenliu1944
Copy link

stephenliu1944 commented Sep 19, 2018

I want to extract multiple css files base on "src" and "node_modules" paths with specific output path.
I try to use splitChunks.cacheGroups but it create two files whitin js and css folders.
I don't need the xxx.styles.js file, it's unnecessary. how could I fix it? thanks.
image

entry: {
        main: ['./src/index.js']
},  
output: {  
        publicPath: '/',
        path: path.resolve(__dirname, 'build'),
        filename: `${ASSETS_PATH}/js/[chunkhash].[name].js`
},
optimization: {
        splitChunks: {
            cacheGroups: {
                styles: {
                    name: 'styles',
                    test: /[\\/]node_modules[\\/]{1}.+\.css$/,
                    chunks: 'all',
                    enforce: true     // and what does it mean?
                }
            }
        }
    },
    module: {
        rules: [{           
            /**
             * project's css.
             */
            test: /\.(css|scss)$/,
            include: path.resolve(__dirname, 'src'),
            use: [
                MiniCssExtractPlugin.loader, 
                {
                    loader: 'css-loader'                 
                }, 
                {
                    loader: 'postcss-loader',
                    options: {
                        autoprefixer: {
                            browsers: ['last 2 versions']
                        },
                        plugins: () => [autoprefixer]
                    }
                }, 
                'sass-loader'
            ]
        }, {
            /**
             * third party css.
             */
            test: /\.(css|scss)$/,
            include: path.resolve(__dirname, 'node_modules'),
            use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
        }]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: `${ASSETS_PATH}/css/[name]_[contenthash].css`,
            chunkFilename: `${ASSETS_PATH}/css/[name]_[id].css`
        })
    ]

xxx.styles.js:

(window.webpackJsonp=window.webpackJsonp||[]).push([[1],{2:function(n,w,o){}}]);
@stephenliu1944 stephenliu1944 changed the title How to extract multiple css files? extract multiple css files but created a unnecessary js file. Sep 19, 2018
@michael-ciniawsky
Copy link
Member

It's a known bug in webpack itself webpack/webpack#1967 && https://github.com/webpack/webpack/projects/5 (Yes Column 2nd Card)

@vyushin
Copy link

vyushin commented Aug 25, 2019

@michael-ciniawsky It isn't webpack bug. I think MiniCssExtractPlugin should remove assets.
After almost 1 year the problem exists... :) Guys, you can resolve it by adding simple plugin, like this:

plugins: [
    new HtmlWebpackPlugin({
      template: resolve(SRC_DIR, './index.html'),
      inject: 'head',
      minify: IS_PRODUCTION,
      hash: true,
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css',
      hmr: !IS_PRODUCTION,
    }),
    {
      apply(compiler) {
        compiler.hooks.shouldEmit.tap('Remove styles from output', (compilation) => {
          delete compilation.assets['styles.js'];  // Remove asset. Name of file depends of your entries and 
          return true;
        })
      }
    }
  ],

@Devinora
Copy link

@michael-ciniawsky It isn't webpack bug. I think MiniCssExtractPlugin should remove assets.
After almost 1 year the problem exists... :) Guys, you can resolve it by adding simple plugin, like this:

plugins: [
    new HtmlWebpackPlugin({
      template: resolve(SRC_DIR, './index.html'),
      inject: 'head',
      minify: IS_PRODUCTION,
      hash: true,
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css',
      hmr: !IS_PRODUCTION,
    }),
    {
      apply(compiler) {
        compiler.hooks.shouldEmit.tap('Remove styles from output', (compilation) => {
          delete compilation.assets['styles.js'];  // Remove asset. Name of file depends of your entries and 
          return true;
        })
      }
    }
  ],

the file is no longer created, but a tag with the path to the file is generated.

@zanderwar
Copy link

@michael-ciniawsky It isn't webpack bug. I think MiniCssExtractPlugin should remove assets.
After almost 1 year the problem exists... :) Guys, you can resolve it by adding simple plugin, like this:

plugins: [
    new HtmlWebpackPlugin({
      template: resolve(SRC_DIR, './index.html'),
      inject: 'head',
      minify: IS_PRODUCTION,
      hash: true,
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css',
      hmr: !IS_PRODUCTION,
    }),
    {
      apply(compiler) {
        compiler.hooks.shouldEmit.tap('Remove styles from output', (compilation) => {
          delete compilation.assets['styles.js'];  // Remove asset. Name of file depends of your entries and 
          return true;
        })
      }
    }
  ],

causes

(node:17584) [DEP_WEBPACK_COMPILATION_ASSETS] DeprecationWarning: Compilation.assets will be frozen in future, all modifications are deprecated.
BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
        Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
        Make sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants