Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(),      ]  }; 
like image 925
LeBlaireau Avatar asked Mar 15 '17 14:03

LeBlaireau


People also ask

Can webpack have multiple entry points?

Configuring the output configuration options tells webpack how to write the compiled files to disk. Note that, while there can be multiple entry points, only one output configuration is specified.

What are four core concepts of webpack?

There are four basic concepts in webpack: entry , output , modules and plug-ins . These configurations are added in webpack.

What is bundling webpack?

Webpack is an aggressive and powerful module bundler for JavaScript applications. It packages all the modules in your application into one or more bundles (often, just one) and serves it to the browser.

What is single entry syntax in Webpack?

Single Entry Syntax is a great choice when you are looking to quickly setup a webpack configuration for an application or tool with one entry point (i.e. a library). However, there is not much flexibility in extending or scaling your configuration with this syntax.

How to define an entry-point in Webpack?

By default, Webpack makes you define an entry-point, basically the root JavaScript file for you app: module.exports = { entry: { app: "./static/src/app.js"}, output: { path: "./static/dist", filename: "app-bundle.js"} }; Our site structure doesn't match up with that.

What is Webpack in Webpack?

Webpack: Creating multiple bundles using entry points Webpackis module bundler that generates static assets for almost all of your front-end dependencies, and their dependencies.

What is the use of merge in Webpack?

This is a popular technique used to separate concerns by environment, build target, and runtime. They are then merged using specialized tools like webpack-merge. You can pass empty object {} to entry when you have only entry points generated by plugins.


2 Answers

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.

like image 92
Ivan Avatar answered Sep 23 '22 07:09

Ivan


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"

like image 35
olore Avatar answered Sep 20 '22 07:09

olore