我是webpack的新手。我正在制作一个React应用程序,它将从外部config.js文件读取文本并将其显示在UI上。我正在尝试拥有一个外部config.js文件,该文件我不希望被捆绑,但在运行时需要。这是我的webpack.config.js:
const path = require('path');
const html_plugin = require("html-webpack-plugin");
module.exports = {
mode: 'development',
entry: './src/app.js',
output: {
path: path.resolve(__dirname,'build'),
filename: 'build.js'
},
externals: {
config: './config.js',
},
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: ['style-loader','css-loader']
}
]
},
plugins: [new html_plugin({ template: './src/index.html' })]
}
config.js与webpack.config.js位于同一目录中。这是我的config.js:
module.exports = {
title: 'ssup'
}
这是我在组件中使用config.js的方式:
import config from 'config';
class ... {
return <h1>{config.title}</h1>
}
这是我的项目结构:
- src/
- webpack.config.js
- config.js
[当我尝试运行此应用程序时,它在Chrome控制台中显示以下错误:
build.js中的第681行包含以下内容:
eval("module.exports = ./config.js;\n\n//# sourceURL=webpack:///external_%22./config.js%22?");
这是我的package.json:
{
"name": "react-from-scratch",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --open",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react": "^16.10.2",
"react-dom": "^16.10.2"
},
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@babel/preset-react": "^7.6.3",
"babel-loader": "^8.0.6",
"css-loader": "^3.2.0",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^1.0.0",
"webpack": "^4.41.1",
"webpack-cli": "^3.3.9",
"webpack-dev-server": "^3.8.2"
}
}
我在做什么错?
您不需要任何Webpack配置即可。将您的配置另存为JSON,并在运行时(而不是在构建时)将其保存为fetch
。
而且,您正在尝试使用此技术来实现什么?您的用例是什么?