webpack更改路径脚本

时间:2018-09-11 13:41:21

标签: javascript webpack ejs


最近,我设法在{em> Webpack 4 中使用ejs-loader。但是,我开始遇到webpack启动时创建到index.html的脚本标签的问题:<script type="text/javascript" src="main.js"></script>。他的src是不正确的,因为如果我们查看我的构建,我需要src="/dist/main.js"

node_modules/
dist/
  index.html
  main.js
publics/
src/
views/
server.js
package.json
webpack.config.js

我需要添加到webpack.congfig.js中以创建所需的src吗?

var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    devtool: 'source-map',

    module: {
        rules: [{
                test: /\.ejs$/,
                use: ['ejs-loader']
            }]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './views/pages/index.ejs'})
    ]
}

1 个答案:

答案 0 :(得分:1)

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    devtool: 'source-map',
    output: {
        publicPath: '/dist/' // <---- this
    },
    module: {
        rules: [{
                test: /\.ejs$/,
                use: ['ejs-loader']
            }]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './views/pages/index.ejs'})
    ]
}