在webpack

时间:2017-09-14 17:19:13

标签: html typescript webpack typescript-typings webpack-html-loader

尝试使用html-loader插件在TypeScript中导入html:

import buttonHtml from './button.html';

出现TypeScript错误:

  

TS2307:找不到模块'./button.html'

Webpack配置:

const path = require('path');

module.exports = {
  entry: {
      'background.js':path.resolve(__dirname, './background.ts'),
      'content.js': path.resolve(__dirname,'./content.ts')
  },
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      },
      {
        test: /\.html$/,
        exclude: /node_modules/,
        use: {loader: 'html-loader'}
    }
    ]
  },
  resolve: {
    extensions: [ ".tsx", ".ts", ".js" ]
  },
  output: {
    filename: '[name]',
    path: path.resolve(__dirname, 'dist')
  }
};

1 个答案:

答案 0 :(得分:11)

如果你想加载这样的html,你需要一个带有这个条目的typings.d.ts文件:

declare module '*.html' {
  const value: any;
  export default value;
}