您好我正在使用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