Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I output multiple folders with webpack?

I have a folder structure for my JS modules. I want one module per page. This is not a single page app.

How can I output files in a folder structure?

From what I can see, the only possibility is to output [name].js. This could work if I make the names very unique, or I could make the names have a - for a folder separator. That would mean a/b/c.js would translate to name a-b-c. I really don't like this. I would like to be able to require("a/b/c").

From what I can tell, I can't use a single bundled file either because require is not available outside of the module. If it was, I could just build a single bundle and require("a/b/c") on every page.

If there is a good way to do this that I'm not finding on the internet, please let me know.

It looks like I'm able to easily do this with require.js using r.js, but I don't want to use require.js and would like CommonJS modules.

like image 849
Josh Close Avatar asked Jan 10 '15 17:01

Josh Close


People also ask

Can webpack have multiple entry points?

webpack.config.js We 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 output in webpack config?

The top-level output key contains a set of options instructing webpack on how and where it should output your bundles, assets, and anything else you bundle or load with webpack.

Where does webpack output go?

Webpack will generate the files and put them in the /dist folder for you, but it doesn't keep track of which files are actually in use by your project. In general it's good practice to clean the /dist folder before each build, so that only used files will be generated. Let's take care of that with output.


1 Answers

You can define an entry point using slash, like this:

entry: {
    "main-plugin/js/background":"./src/main-plugin/background"
}

And output like this:

output: {
    path: path.join(__dirname, 'public'),
    filename: '[name].js'
},

This setup will create a public/main-plugin/js folder and will place the background.js into it. It works at least on Win7x64.

like image 98
Anatoliy Arkhipov Avatar answered Sep 21 '22 06:09

Anatoliy Arkhipov