Webpack不会将我的文件捆绑到正确的公共目录中
您好,我正在使用node.js/react/webpack应用程序。构建应用程序后,应将文件编译到静态Webpack不会将我的文件捆绑到正确的公共目录中,webpack,bundle,Webpack,Bundle,您好,我正在使用node.js/react/webpack应用程序。构建应用程序后,应将文件编译到静态公共文件夹目录中。但是,这不会发生,所有内容都保留在根级别(index.html和bundle.js) 在根目录中,我有一个server.js,其中包含以下内容: new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: tr
公共文件夹目录中。但是,这不会发生,所有内容都保留在根级别(index.html和bundle.js)
在根目录中,我有一个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');
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack App</title>
</head>
<body>
<script src="index_bundle.js"></script>
</body>
</html>
然后我有一个scripts
文件夹,我的App.js和所有关键导入的脚本和css都在其中。My 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()
]
};
它呈现外围的HTML代码,但不呈现
render(
<App />,
document.getElementById('root')
);
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>
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
.....
.....
.....
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new HtmlWebpackPlugin()
]
};
网页包应用程序
CopyWebpackPlugin不是您需要的正确选择,即将index.html复制到构建文件夹中
您可以使用HtmlWebpackPlugin实现此功能,并提供更多灵活的选项:
这是一个网页包插件,可以简化HTML文件的创建,从而为您的网页包包提供服务。您可以让插件为您生成HTML文件,使用lodash模板提供您自己的模板,或者使用您自己的加载程序
该插件将为您生成一个HTML5文件,其中包含使用脚本标记在正文中的所有网页包包。只需将插件添加到您的网页包配置中,如下所示:
<!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>
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
.....
.....
.....
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new HtmlWebpackPlugin()
]
};
这将生成一个文件dist/index.html,其中包含以下内容:
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');
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack App</title>
</head>
<body>
<script src="index_bundle.js"></script>
</body>
</html>
网页包应用程序
path:\uu dirname,,将公用文件夹追加到此处以输出文件。。PublicPath只是一个虚拟路径,用于链接索引htmlYes中的捆绑CSS、js,我正在搜索这个。我用复制网页包插件更新了网页包,但公共目录中仍然没有任何内容。你不需要复制网页包插件。我想说的是,只需更改您的输出路径:从_dirname到新的输出文件夹。谢谢,但这只输出bundle.js文件夹。如何将bundle和index.html都移动到其中进行部署。。所以基本上你想在输出文件夹中输出bundle.js和index.js?您的要求有点困惑。模块导出到哪里?我如何确定html文件中的内容?@HGB:现在检查一下,我已经更新了解决方案。另外,您可以将内容放在索引文件中。您好,我也更新了上面的代码,但仍然出现错误:var webpackConfig={^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^语法错误:意外标识符
再次更新。在公用文件夹中不需要使用var-declaratin,但是,即使正确请求bundle.js,也不会加载任何内容。控制台告诉我,目标容器不是DOM元素。与此公用目录中的文件一起工作。