【发布时间】:2022-01-16 12:30:47
【问题描述】:
我正在尝试在 TypeScript 中导入 JSON 文件的内容,但我得到的不是 JSON 的内容,而是它的 URL。
App.tsx
import * as videosInfo from './videos-info.json'
console.log("Print");
console.log(videosInfo);
控制台输出:
Print
http://localhost:3000/src/videos-info.json
在tsconfig.json 中,我使用"resolveJsonModule": true。
如何使用 JSON 的 contents 而不是它的路径?
编辑:这是我的webpack.config.js 供参考:
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.tsx',
module: {
rules: [
{
test: /\.json/,
type: 'asset/resource',
generator: {
filename: '[name][ext][query]'
}
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
},
output: {
filename: 'client.bundle.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new HtmlWebPackPlugin({
template: './public/index.html',
filename: 'index.html'
})
],
devServer: {
static: {
directory: path.join(__dirname, 'dist')
},
compress: true,
port: 3000
}
};
【问题讨论】:
-
您是否在使用某种捆绑程序或可能会产生干扰的东西?看起来它应该可以正常工作......
-
@T.J.Crowder 我添加了我的
webpack.config.js以供参考。我不知道是什么干扰...
标签: typescript webpack