[英]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.我有一个 Web 应用程序,需要一个包含端点和其他所需启动参数的配置 JSON 文件。
If I use the json-loader, the file is not "real json", it'll look something like this:如果我使用 json-loader,文件不是“真正的 json”,它看起来像这样:
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.我想要的是普通的旧 JSON,这意味着它可以作为部署过程的一部分进行解析和更改,然后再发送到 web 服务器,从那里提供服务。
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.我想让 webpack 处理这个问题并且有空。
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?有没有一种方法可以require
一个 JSON 文件,它被写成一个普通的 JSON 文件并在运行时导入?
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? 有没有办法我可以require
一个JSON文件,它被编写为一个普通的JSON文件并在运行时导入?
Webpack acts on compile-time. Webpack在编译时起作用。 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. 这意味着当您需要一个文件时,它会加载该文件,执行loader-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. 所以你要求的声音就像一个加载器,它就像一个文件加载器,但是导出一个IIFE,返回所需文件的承诺。
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. 从理论上讲,有可能有一个webpack加载器,假设一个async-file-loader从文件加载器获取输入将为你处理异步请求。 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: async-file-loader看起来像这样:
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: 当你想在运行时加载你的json文件时,我会看到2个选项:
As ShabbY said above, Webpack is a module bundler and compiles your files ahead of time. 正如ShabbY上面所说,Webpack是一个模块捆绑器,可以提前编译文件。
It sounds like you need to add a configuration file (json) after build time to be accessed at runtime. 听起来您需要在构建时间之后添加配置文件(json)以便在运行时访问。 For this, a solution would be to load the file using a normal ajax call rather than attempting to bundle it with Webpack. 为此,解决方案是使用普通的ajax调用加载文件,而不是尝试将其与Webpack捆绑在一起。
I am looking to solve same problem and avoid building react app for each environment.我希望解决同样的问题并避免为每个环境构建 React 应用程序。
Since this answer is 4 years old, Just want to check if there is a better way to handle this with webpack 5 (latest version).由于此答案已有 4 年历史,因此只想检查是否有更好的方法使用 webpack 5(最新版本)来处理此问题。
instead of using file-loader and request the json file asynchronously on your own.而不是使用文件加载器并自己异步请求 json 文件。
how about webpack dynamic import with file-loader webpack 使用文件加载器 动态导入怎么样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.