如何在dist文件夹中导入HTML文件?

时间:2017-07-10 19:55:02

标签: html node.js express webpack

在我的开发环境中,我给出了

res.sendFile(path.resolve(__dirname,'../Views/setpwd.html');

它在开发环境中运行良好。

但是在生产中,即使我在webpack中添加了html加载器,视图也没有转移到dist文件夹。你能帮帮我吗?

提前致谢

## Webpack config ##

var path = require('path');
 var webpack=require('webpack');
 var nodeExternals = require('webpack-node-externals');
module.exports = {
     entry: './src/app.js',
     output: {
         path: path.resolve(__dirname,'dist'),
         filename: 'src/server.js',
     },
      target: 'node',
      externals: [nodeExternals()],
      module: {
         loaders: [{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'}],
         loaders: [
            { test: /\.(html)$/, loader: "file" }
        ]
     },
     plugins: [
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
            },
            output: {
                comments: false,
            },
        }),
    ]
 }

deVdependencies

"devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.5.1",
    "babel-preset-es2015": "^6.24.1",
    "file-loader": "^0.11.2",
    "webpack": "^2.6.1",
    "webpack-node-externals": "^1.6.0"
  },

1 个答案:

答案 0 :(得分:2)

如果我正确理解了这一点,您希望使用webpack将../Views/*.html中的HTML文件复制到dist/目录。如果是,那么您需要使用copy-webpack-plugin

new CopyWebpackPlugin([
  {
    from: path.resolve(__dirname, '../path/to/views/directory'),
    to: path.resolve(__dirname, '../path/to/dist')
    ignore: ['.*']
  }
])