【发布时间】:2019-04-05 04:44:18
【问题描述】:
我正在构建一个小型 Web 应用程序,我从多个 JSON 文件加载数据。
import config from './config.json';
import data from './data.json';
console.log(config, data)
对于我的 webpack 构建,我想从包中排除 JSON 文件,因为它们非常大(并且可以异步加载)。目前我正在尝试使用file-loader 来实现这一目标。
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
devtool: '#cheap-source-map',
resolve: {
modules: ['node_modules']
},
entry: {
main: path.resolve('./index.js')
},
output: {
filename: '[name].bundle.js',
path: path.resolve('./dist')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
type: 'javascript/auto',
test: /\.json$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]'
}
}
]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: './index.html',
filename: 'index.html',
inject: 'body'
})
]
};
通过这种配置,我得到了单独的文件,但它们不会被导入。在这种特殊情况下,console.log() 仅将文件名返回为字符串data.json 和config.json。好像没有加载实际的 JSON 文件。
我做错了什么? file-loader 是去这里的路吗?
【问题讨论】:
标签: javascript json webpack