Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundle JSON as a plain JSON file with Webpack

I have a web-app that requires a configuration JSON file which contains end-points and other required start-up parameters.

If I use the json-loader, the file is not "real json", it'll look something like this:

module.exports = {
    "name": "foo",
    "key": true,
};

What I would like is plain old JSON which means it can be parsed and changed as part of a deployment process before being sent to the web server where it will be served from.

An alternative is to use the file-loader. However, that means that (even though it's a trivial task) I have to write the code to download the file myself. I would like to have webpack handle this and be available.

Is there a way that I can require a JSON file, which is written as a plain-old JSON file and imported at run time?

like image 349
Ray Booysen Avatar asked Aug 20 '16 12:08

Ray Booysen


People also ask

How do I add a JSON file to webpack?

How do I import JSON into webpack? The basic webpack config rule with the json5-loader is configured as defined below. If your React app uses a webpack config version > 2.0. In the webpack config file, the loader rule accepts the NPM package to load any of the JSON files in your app.

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 a bundle in webpack?

Bundle: Produced from a number of distinct modules, bundles contain the final versions of source files that have already undergone the loading and compilation process. Bundle Splitting: This process offers one way of optimizing a build, allowing webpack to generate multiple bundles for a single application.

How do I open a json5 file?

You can use any text/code editor such as Notepad, Sublime Text, and others to open and edit JSON files. Alternatively, use an online editor to edit your JSON files.


2 Answers

Is there a way that I can require a JSON file, which is written as a plain-old JSON file and imported at run time?

Webpack acts on compile-time. That means when you require a file, it loads that file, performs modifications as specified by the loader-chain and exports you the final result from that chain. So what you're asking for sounds like a loader, that acts like a file-loader, but exports an IIFE that returns a promise for the required file.

In theory it's possible to have a webpack loader, lets say an async-file-loader that gets the input from the file-loader will handle the async requst for you. Let's assume something like:

const promisedFile = require('async-file-loader!file-loader!./myJson.json');
promisedFile.then(file => { console.log(JSON.parse(file)); });

But then the whole logic for handling the request would be boundled within that call. The async-file-loader would look something like this:

module.exports = function(source) {
    return `module.exports = eval("(function(url){
      /* define async request func, preform request and return promise in here */
   })(${url})")`;
}

Pretty nasty...

When you want to load your json-file in runtime, I'll see 2 options:

  1. use the file-loader and request the json file asyncronously on your own.
  2. use a server-sided scripting language and inject the config into the .html file. If your're using nodejs and express for example on your backend side, you could easily inject the config using Cheerio before serving the request.
like image 97
ShabbY Avatar answered Sep 22 '22 14:09

ShabbY


As ShabbY said above, Webpack is a module bundler and compiles your files ahead of time.

It sounds like you need to add a configuration file (json) after build time to be accessed at runtime. For this, a solution would be to load the file using a normal ajax call rather than attempting to bundle it with Webpack.

like image 41
JusMalcolm Avatar answered Sep 20 '22 14:09

JusMalcolm