こんにちは、node.js/react/webpackアプリケーションを使って作業しています。アプリケーションがビルドされたら、ファイルは静的public
フォルダディレクトリにコンパイルする必要があります。ただし、これは発生しませんし、すべてがルートレベル(index.htmlをとbundle.js)のままで、私は次のようでserver.jsを持つルートでWebpackは私のファイルを正しい公開ディレクトリにバンドルしません
:
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"
}
更新されました。 publicディレクトリに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コードをレンダリングではなく、スクリプト/ index.jsから
render(
<App />,
document.getElementById('root')
);
。次のようにindex.htmlには
Target container is not a DOM element
:
<!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、インデックスhtmlのjsをリンクするために使用される仮想パスです – Ravi
はい、私はこれについて検索していました。 webpack wtih copy-webpack-pluginを更新しましたが、公開ディレクトリには何もありません。 – HGB
copy-webpack-pluginは必要ありません。私が言っていることは、__dirnameから新しい出力フォルダに出力パスを変更することだけです。 – Ravi