简体   繁体   English

"如何防止 webpack 为 css 创建 js 文件?"

[英]How to prevent webpack to create js file for css?

const path = require('path');

var webpack = require('webpack'),
    ExtractTextPlugin = require("extract-text-webpack-plugin");
var nodeExternals = require('webpack-node-externals');
const CssEntryPlugin = require("css-entry-webpack-plugin");

module.exports = {
    entry: {
        index: './js/index.js',
        product_details: './js/index.js',
        signup: ['./js/signup.js','./js/index.js'],
        signup_style: ['./css/signup.css']
   },
    output: {
        path: path.resolve(__dirname, 'dist/js'),
        filename: "[name].min.js"
    },
    externals: [nodeExternals()],
    module: {
        rules: [
          {
            test: /\.js/,
            use: 'babel-loader',
            exclude: /node_modules/,
          },
          { 
            test: /\.css$/,
            use: ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use: [{
                        loader: 'css-loader',
                        options: {
                            minimize: true,
                        }
                    }]
            })
          }
        ]
      },
      plugins: [
        new ExtractTextPlugin('../css/[name].css')
      ]
};

Above is my webpack.config.js file.以上是我的 webpack.config.js 文件。 It creates signup_style.min.js and signup_style.css files.它创建signup_style.min.jssignup_style.css文件。 I want only signup_style.css file to create and not signup_style.min.js .我只想创建signup_style.css文件而不是signup_style.min.js

How can we do that?我们怎么能做到这一点?

You can use https://www.npmjs.com/package/webpack-fix-style-only-entries您可以使用https://www.npmjs.com/package/webpack-fix-style-only-entries

This is a small plugin developed to solve the problem of having a style only entry (css/sass/less) generating an extra js file.这是一个小插件,旨在解决仅样式条目(css/sass/less)生成额外js文件的问题。

By default webpack will generate that .min.js file.默认情况下,webpack 会生成那个 .min.js 文件。 As in your configuration output all the entry files must generate its js files.在您的配置output所有入口文件都must generate其 js 文件。

Note: extract-text-webpack-plugin will extract all css/sass located in your js entry files.注意: extract-text-webpack-plugin将提取位于 js 入口文件中的所有 css/sass。

It moves all the required *.css modules in entry chunks into a separate CSS file.它将入口块中所有必需的 *.css 模块移动到一个单独的 CSS 文件中。 So your styles are no longer inlined into the JS bundle, but in a separate CSS file (styles.css).所以你的样式不再内联到 JS 包中,而是在一个单独的 CSS 文件 (styles.css) 中。

All you need to do is to remove that file if you don't want it.如果您不想要它,您需要做的就是删除该文件。 I am using https://github.com/gregnb/filemanager-webpack-plugin/ .我正在使用https://github.com/gregnb/filemanager-webpack-plugin/ Where you can define what files you will remove before or after webpack bundling.您可以在哪里定义在 webpack 捆绑之前或之后要删除的文件。

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

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