您好我正在使用node.js/react/webpack应用程序。一旦构建应用程序,文件应该被编译到一个静态的public
文件夹目录中。但是,这种情况不会发生,一切都保持在根级别(index.html,然后bundle.js)Webpack将不会将我的文件捆绑在正确的公共目录中
在根我有如下一个server.js:
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true
}).listen(5000, 'localhost', (err) => {
if (err) {
console.log(err);
}
console.log('Listening at localhost:5000');
});
然后我有一个scripts
文件夹,我的App.js和所有密钥导入的脚本和CSS都存在。我的package.json包含启动/构建脚本:
"scripts": {
"start": "node server.js",
"build": "cross-env BABEL_ENV=production webpack --config webpack.config.production.js",
"lint": "eslint --cache --ignore-path .gitignore --format=node_modules/eslint-formatter-pretty . *.js",
"test": "npm run lint"
}
已更新。没有错误,bundle.js和index.html加载在公共目录中,但页面是空白的。
const webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
module.exports = {
entry: [
'webpack-dev-server/client?http://localhost:3000',
'webpack/hot/dev-server',
'./scripts/index'
],
output: {
path:path.join(__dirname, 'public'),
filename: 'bundle.js',
publicPath: '/public/'
},
resolve: {
modulesDirectories: ['node_modules', 'scripts'],
extensions: ['', '.js', '.jsx']
},
devtool: 'eval-source-map',
module: {
loaders: [
{
exclude: /node_modules/,
test: /\.jsx?$/,
loaders: ['babel'],
include: path.join(__dirname, 'scripts')
},
{
test: /\.css/,
loaders: ['style', 'css'],
include: __dirname + '/scripts'
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new HtmlWebpackPlugin()
]
};
它使得通过脚本/ index.js外围HTML代码,但不是
render(
<App />,
document.getElementById('root')
);
。我已经包括import { render } from 'react-dom';
在控制台中我得到了以下问题:
Target container is not a DOM element
中的index.html如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack App</title>
</head>
<script type="text/javascript" src="/public/bundle.js"></script></body>
<body>
</html>
路径:__dirname ,,这里追加公用文件夹输出你的文件.. PublicPath只是一个虚拟路径,用于链接捆绑CSS,索引中的js html – Ravi
是的,我正在寻找这个。我使用copy-webpack-plugin更新了webpack,但公共目录中仍然没有任何内容。 – HGB
你不需要copy-webpack-plugin。我只是说,只是改变你的输出路径:从__dirname到新的输出文件夹。 – Ravi