gpt4 book ai didi

javascript - Webpack ES6-使用动态导入加载 Json(保留 json 文件)

转载 作者:搜寻专家 更新时间:2023-11-01 04:12:49 24 4
gpt4 key购买 nike

我正在尝试将我的 json 文件(2 个文件)拆分成单独的 block 。我能够做到,但有一个“骗局”。

那些 json 被转换成 .js通过 webpack,这就是我添加 file-loader 的原因至 .json文件,但是 await import现在返回一个字符串而不是 json。

Webpack 规则

module: {
rules: [
{ test: /\.scss$/, use: [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader", "sass-loader",] },
{ test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff&name=fonts/[name].[ext]" },
{ test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff2&name=fonts/[name].[ext]" },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url-loader?limit=10000&mimetype=application/octet-stream&name=fonts/[name].[ext]" },
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "url-loader?limit=10000&mimetype=application/vnd.ms-fontobject&name=fonts/[name].[ext]" },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url-loader?limit=10000&mimetype=image/svg+xml&name=fonts/[name].[ext]" },
{
test: /\.(js|jsx)$/,
// Skip any files outside of project's `src` directory
include: [
path.resolve(__dirname, "../src"),
],
loaders: ["babel-loader"],
},
{
test: /\.json$/,
type: "javascript/auto",
loader: "file-loader",
options: { name: "i18/[name].[ext]" },
include: [
path.resolve(__dirname, "../src"),
],
},
{ test: /\.(jpg|png)$/, loader: "url-loader" }
]
}

注意 test: /\.json$/规则。

这里我是说我希望它被 file-loader 加载而不是默认的 webpack 捆绑 js。

注意:没有这个规则,应用程序可以正常工作,但我需要那些 .json 的

这是最终结果:

S1

两个 json 都可以。问题出在 i18n.0<...>i18n.1代表en-USpt-PT .

现在...我使用动态导入调用这些文件(无需转译):

/* This loading must be async, it will load the file from the server on-demand */

export const getLanguageFile = async (lang = "en-US")
=> await import(/* webpackChunkName: "i18n." */ `../i18n/${lang}.json`);

然后在这里调用:

async componentDidMount() {
/* Get user info (properties) */
var properties = await fetch.get("/account/GetUserProperties");

/* Get language file based in the language code provided in the properties */
var file = await getLanguageFile(properties.data.LanguageCode);
this.props.setUserProperties(properties.data, file);
}

最后一个变量 file分配给:

S2

此变量应包含文件中存在的完整 json。

我知道为什么会这样,因为这会调用 i18n.0<...>而不是 .json,并且该文件具有以下内容:

(window.webpackJsonp = window.webpackJsonp || [])
.push(
[[2], {
"./src/translations/i18n/en-US.json": function (n, o, s) {
n.exports = s.p + "i18/en-US.json"
}
}]
);

我如何使用动态导入调用那些 json 但保留完整的 json 文件?

我需要 json,因为它可能在服务器端被编辑。

最佳答案

好的,我是怎么解决这个问题的?

我没有使用 import(),而是对文件发出了一个 xhr 请求。

但是有一个问题,我仍然需要将文件放在 dist 文件夹中。

为此,我使用了 copy-webpack-plugin ,这样您就可以在捆绑过程中将文件和文件夹转储到任何地方。

我删除了我的 json 规则,并添加了这样的插件:

new CopyWebpackPlugin([{ from: "src/translations/i18n", to: "i18n" }]),

好的,它正在将 i18n 文件夹转储到 dist 中:

enter image description here

在此之后,我将函数 getLanguageFile 更改为:

export const getLanguageFile = async (lang = "en-US") 
=> (await axios.get(`/i18n/${lang}.json`)).data;

这将以 json 格式返回文件,一切正常。

注意

开发构建:webpack-dev-server(服务器)+ ASP.Net Core(API)

生产构建:ASP.Net Core(服务器和 API)

关于javascript - Webpack ES6-使用动态导入加载 Json(保留 json 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50686102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com