I have a webpack app that I'd like to read a json file in at runtime.
After webpack packages the application, I'd like the json file to be excluded from the bundle.js but still in the package folder. How can I do this?
I have a webpack app that I'd like to read a json file in at runtime.
After webpack packages the application, I'd like the json file to be excluded from the bundle.js but still in the package folder. How can I do this?
I'd use copy-webpack-plugin
Pretty simply moves files from one location to another
You moved the json file into the destination package by passing it an object:
{ from: 'source', to: 'dest' }
Checkout the repo for usage examples
Use the file-loader
which will let you specify an output file. Not the cleanest, since webpack really really wants to bundle everything, but it works.
require('file?name=../newfile.json!/somefolder/original.json');
The above will create newfile.json a directory above your webpack output folder. (Folder change for illustration purposes; not required.)
If you want to re-require the json*, mark it as an external dependency in the webpack config and use `require('../newfile.json').
*I'd suggest using a regular ajax call to pull the json in though. That way it's clear that the json is an external and you won't have to go through webpack's ajax system.