Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiple file entry and output in project with webpack?

How to set multiple file entry/output in project with webpack?

I follow http://webpack.github.io/docs/tutorials/getting-started/ success compile if only one file in one entry/output...

directory

app webpack.config.js ./assets ././javascripts/Administrator/Article/Create/Base.js ././javascripts/Administrator/Article/Edit/Base.js ././javascripts/Account/Index/Base.js ././javascripts/Contact/Index/Base.js ... 

how to output like this?

././javascripts/Administrator/Article/Create/bundle.js ././javascripts/Administrator/Article/Edit/bundle.js ././javascripts/Account/Index/bundle.js ././javascripts/Contact/Index/bundle.js 

webpack.config.js

module.exports = {   entry: {     'AdministratorArticleCreate':['./assets/javascripts/Administrator/Article/Create/Base.js']   },   output: {     path:    }      // if only one file     // entry: "./assets/javascripts/Administrator/Article/Create/Base.js",     // output: {     //     // path: __dirname,     //     path: "./assets/javascripts/Administrator/Article/Create/",     //     filename: "bundle.js"     // } }; 
like image 272
user1775888 Avatar asked Aug 09 '15 18:08

user1775888


People also ask

Can webpack have multiple entry points?

webpack.config.jsWe can also pass an array of file paths to the entry property which creates what is known as a "multi-main entry". This is useful when you would like to inject multiple dependent files together and graph their dependencies into one "chunk".

How do I bundle files with webpack?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script.

What is __ Dirname in webpack?

The __dirname is set to / by webpack, that's why you end up with /views/index. html which is the root of your file system, that happens to be D:\ in your case. You can set node. dirname to false in your webpack config to not inject it and defer it to runtime.

How can we generate webpack config file automatically?

Use webpack-cli's init command to rapidly generate webpack configuration files for your project requirements, it will ask you a couple of questions before creating a configuration file. npx might prompt you to install @webpack-cli/init if it is not yet installed in the project or globally.


1 Answers

For many entry points use arrays as a value of entry property:

entry: {   app: ['./app/main.js', '.lib/index.js'],   vendors: ['react'] } 

app and vendors are arrays, so you can put there as many file paths as you need.

For output case:

output: {   path: staticPath,   filename: '[name].js' } 

The [name] is taken from entry properties, so if we have app and vendors as properties, we got 2 output files - app.js and vendors.js.

Documentation link

like image 117
Kania Avatar answered Oct 03 '22 05:10

Kania