admin管理员组

文章数量:1594978

在通过html-webpack-plugin解析html文件的时候,出现了“You may need an appropriate loader to handle this file type.”问题。

意思是我需要添加一些loader来解析html,网上基本上的解决方案是用babel或者html-loader

babel我嫌麻烦放弃了;html-loader会和html-webpack-plugin有冲突,导致html中的类似<%= htmlWebpackPlugin.options.title %>的变量不能够正确解析了。

最后,我决定还是回归本源问题,最好减少引入的插件。

首先,我们参考一下官方文档:


这里的描述可以看出,template属性是默认使用ejs文件的。

什么是EJS呢?大家可以参考官网描述。

于是,我将index.html更改为index.ejs,配置的地方和文件都需要修改。


配置:

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

module.exports = {
  entry: './index.ts',
  ......,
  plugins: [
    new HtmlWebpackPlugin({
      title: 'typescript examination',
      template: './index.ejs',
      chunks: ['app']
    }),
  ]
}

重新运行一下,问题已经解决了。

本文标签: PluginwebpackhtmlLoadertype