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.
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".
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With