简体   繁体   中英

Webpack, multiple entry points Sass and JS

Below is my webpack config. At the moment the file loads this main.js entry point

import './resources/assets/js/app.js';
import './resources/assets/sass/main.scss';

I can get both files in the public/js files but I would like to get the css and js in their own folder. Is this possible?

var webpack = require('webpack');
var path = require('path');
let ExtractTextPlugin = require("extract-text-webpack-plugin");
var WebpackNotifierPlugin = require('webpack-notifier');

module.exports = {

    resolve: {
    alias: {
      'masonry': 'masonry-layout',
      'isotope': 'isotope-layout'
    }
  },

    entry: './main.js',
    devtool: 'source-map',
    output: {
        path: path.resolve(__dirname, './public'),
        filename: 'bundle.js'
    },

    module: {
        rules: [

         {  test: /\.js$/, 
                exclude: /node_modules/, 
                loader: "babel-loader?presets[]=es2015",

             },

            {
                test:/\.scss$/,
                use: ExtractTextPlugin.extract({
                    use: [{loader:'css-loader?sourceMap'}, {loader:'sass-loader', options: {
                    sourceMap: true
                }}],

                })
            },

            /*{
        test    : /\.(png|jpg|svg)$/,
        include : path.join(__dirname, '/dist/'),
        loader  : 'url-loader?limit=30000&name=images/[name].[ext]'
    }, */

            {
                 test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },



        ]
    },

    plugins: [

        //new webpack.optimize.UglifyJsPlugin(),
        new ExtractTextPlugin('app.css'),
        new WebpackNotifierPlugin(),

    ]

};

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")
],

ExtractTextPlugin has been deprecated for CSS and the recommended approach is using MiniCssExtractPlugin .

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

module.exports = {
  entry: ['index.js', 'index.css'],
  module: {
    rules: [{
      test: /\.(scss|css)$/,
      use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
    }]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name]-[hash].css'
    })
  ]
};

You will also need to install npm i node-sass separately.

I have an admin panel(panel.js and panel.scss) and non-admin content pages(index.js and main.scss), I want to compile to 2 css files and 2 js files This is what I did:

module.exports = {
    entry: {
        'pages': ['./src/js/index.js', './src/css/main.scss'],
        'admin': ['./src/js/admin-index.js', './src/css/admin.scss']
    },

 output: {
            filename: "./js/[name]-bundle.js",
            path: path.resolve(__dirname, "dist"), 
        },

plugins: [
 new MiniCssExtractPlugin(
            {
                filename: "./css/[name].css"
            }
        ),
   ]

}

The result came as:

dist
    css
        admin.css
        pages.css
    js
        admin-bundle.js
        pages-bundle.js

Worked pretty well, hope this helps

The mini-css-extract-plugin can fix it with webpack 5

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

module.exports = {
  entry: ['./src/js/index.js', './src/scss/index.scss'],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js'
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }, {
        test: /\.(sa|sc|c)ss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'sass-loader',
        ],
      },
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name]-[hash].css'
    }),
  ]
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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