html-webpack-plugin只在根路由中注入js?

时间:2017-08-04 06:55:18

标签: javascript reactjs html-webpack-plugin webpack-3

使用Html-webpack-plugin 如果我去根目录localhost:8080它会按预期注入javascript 如果我去localhost:8080 /测试html呈现但没有注入js

server.js

app.use('/', express.static(__dirname + '/dist'));

app.get('*', (req, res)=> {
    res.sendFile(__dirname + '/dist/index.html');
});

webpack.config.js

entry: [
        "webpack-hot-middleware/client?reload=true",
        "babel-polyfill",
        path.resolve(__dirname, './config/app/app.js')
    ],
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: 'app.min.js',
        publicPath: '/'
    },
    plugins: isDeveloping ? [
        new HtmlWebpackPlugin({
            hash: true,
            filename: 'index.html',
            template: __dirname + '/dist/index.html',
            inject: 'body'
        })

我发现了一个与此类似的问题,他们的解决方案是配置output.publicPath ='/'我做了但仍然没有运气。我只是得到没有JS注入的模板

1 个答案:

答案 0 :(得分:0)

原来我运行的是webpack-dev-server,并没有注入文件系统。我将此片段添加到我的webpack并解决了问题

"scripts": {
    "start": "npm run build",
    "build": "webpack -p && webpack-dev-server"
  },