Webpack, multiple entry points Sass and JS

﹥>﹥吖頭↗ 提交于 2019-12-02 18:08:50

Yes you can do this, here's an example that does not require you to import sass files in your js files:

const config = {
    entry: {
        main: ['./assets/js/main.js', './assets/css/main.scss'],
    },
    module: {
        rules: [
            {test: /\.(css|scss)/, use: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])}
            // ...  
        ],
    },
    output: {
        path: './assets/bundles/',
        filename: "[name].min.js",
    },
    plugins: [
        new ExtractTextPlugin({
            filename: '[name].min.css',
        }),
        // ...
    ]
    // ...
}

You should end up with ./assets/bundles/main.min.js and ./assets/bundles/main.min.css. You will have to add js rules obviously.

We have multiple entry points and outputs and define them like this:

entry: {
    'js/main.min.js': './resources/assets/js/app.js', 
    'css/main.min.scss': './resources/assets/sass/main.scss'
},
output: {  
    filename: path.resolve(__dirname, './public/assets/[name]')
},

webpack reads the keys of the entry and replaces them into the [name] tag in the output filename. See documentation for "output filename"

I had a similar need, not sure if this is the correct way of doing it but it works for me.

output: {
        path: path.join(__dirname, './public/js'),
        filename:'bundle.js'
    },

Then for the css:

plugins: [
    new ExtractTextPlugin("../css/[name].css")
],
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!