简体   繁体   English

无法在 webpack 3 中设置捆绑文件的自定义文件命名

[英]can't set custom file naming of bundled files in webpack 3

I expect to have my bundled files with the names index.js and styles.css (like the names of source files) after running webpack for production.我希望在运行 webpack 进行生产后,我的捆绑文件的名称为index.jsstyles.css (如源文件的名称) But I get files with names main.js and main.css instead.但是我得到了名称为main.jsmain.css 的文件。 My webpack config seems to be set up correcty, when I set filename prop in both cases.当我在两种情况下都设置filename属性时,我的 webpack 配置似乎设置正确。 And i can't understand why this is not working.我不明白为什么这不起作用。 What can be wrong with my config?我的配置有什么问题?

"webpack": "^3.1.0" "webpack": "^3.1.0"

 const path = require('path'); // Plugins const ExtractTextPlugin = require('extract-text-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); // Webpack Plugins const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); const NamedModulesPlugin = require('webpack/lib/NamedModulesPlugin'); // VARS const NODE_ENV = process.env.NODE_ENV; const DEVELOPMENT = NODE_ENV === 'development'; const PRODUCTION = NODE_ENV === 'production'; module.exports = { context: path.resolve(__dirname, "src"), entry: "./index.js", output: { path: path.resolve(__dirname, "public"), filename: "assets/js/[name].js" }, module: { rules: [ { test: /\\.js$/, exclude: /node_modules/, loader: "babel-loader" }, { test: /\\.css$/, use: ['style-loader', 'css-loader'] }, { test: /\\.scss$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use: ['css-loader', 'sass-loader'], }) } ] }, plugins: [ new NamedModulesPlugin(), new LoaderOptionsPlugin({ debug: !PRODUCTION, cache: !PRODUCTION, minimize: PRODUCTION, options: { sassLoader: { outputStyle: PRODUCTION ? 'compressed' : 'expanded', precision: 10, sourceComments: false, sourceMap: PRODUCTION, } } }), new ExtractTextPlugin({ filename: 'assets/css/[name].css', disable: !PRODUCTION, }), new HtmlWebpackPlugin({ filename: 'index.html', hash: false, inject: 'body', chunksSortMode: 'dependency', template: './index.html', }) ], devtool: DEVELOPMENT ? "cheap-module-eval-source-map" : "hidden-source-map", devServer: { contentBase: path.resolve(__dirname, "public"), historyApiFallback: true, host: '127.0.0.1', port: '3000' } };

When you want to use regex [name] , your should provide a object as entry because webpack will use each key of this object for for the file name of output.当你想使用正则表达式[name] ,你应该提供一个对象作为条目,因为 webpack 将使用这个对象的每个键作为输出的文件名。
If you only have one entry specified as string or array, I suppose that main is the default name for bundles when none is specified .如果您只将一个条目指定为字符串或数组,我想main是当没有指定时包的默认名称

So, to achieve what you want, you should do the following:因此,要实现您想要的,您应该执行以下操作:

module.exports = {
    entry: {
        "index": "./index.js",
        "styles.css": "./path/to/styles.scss" //The .css in the key to avoid .js file
    }
    output: {
        path: path.resolve(__dirname, "public"),
        filename: "assets/js/[name].js"
    },
    plugins: [
        //...
        new ExtractTextPlugin({
           filename: 'assets/css/[name]'
        }),
    ]
    //Rest of your config...
}

BE CAREFUL:当心:

  • As you are using styles as entry, remove the import/require of the styles.scss file from your index.js .当您使用样式作为入口时,请从 index.js 中删除 style.scss 文件的导入/要求 If not it will generate a extra and useless index.css file.如果没有,它将生成一个额外的无用的 index.css 文件。

  • Unfortunately, with this configuration, if you are using automatic inject of HtmlWebpackPlugin, you will get an extra script with src to your .css file in the index.html.不幸的是,使用此配置,如果您使用 HtmlWebpackPlugin 的自动注入,您将在 index.html 中的 .css 文件中获得一个额外的带有 src 的脚本。 So this solution won't work with automatic inject.所以这个解决方案不适用于自动注入。

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

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