简体   繁体   English

无法使用 ts-loader 和 webpack 解析声明

[英]Can't resolve declaration with ts-loader and webpack

Context语境

  • "ts-loader": "^6.2.1", "ts-loader": "^6.2.1",
  • "typescript": "^3.7.2", “打字稿”:“^3.7.2”,
  • "webpack": "^4.41.2", "webpack": "^4.41.2",
  • "webpack-cli": "^3.3.9", "webpack-cli": "^3.3.9",
  • "webpack-dev-middleware": "^3.7.2", "webpack-dev-middleware": "^3.7.2",
  • "webpack-dev-server": "^3.9.0" “webpack-dev-server”:“^3.9.0”

Issue问题

In order to import .html file, i am using the following declaration in a file html-loader.ts :为了导入.html文件,我在html-loader.ts文件中使用以下声明:

declare module "html-loader!*" {
    const content: string;
    export default content;
}

This declaration is used in an other file as follow:此声明在其他文件中使用如下:

import htmlcode from 'html-loader!./template.html';

...

The project is built using webpack and ts-loader .该项目是使用webpackts-loader构建的。

Here is the content of tsconfig.json used by ts-loader .这里是ts-loader使用的tsconfig.json的内容。

{
  "compilerOptions": {
    "outDir": "dist",
    "module": "commonjs",
    "target": "es5",
    "lib": [ "es2015", "dom" ],
    "sourceMap": true,
    "esModuleInterop": true,
    "removeComments": true,
    "noEmitOnError": true,
    "declaration": true
  }
}

Here is the webpack.config.js :这是webpack.config.js

const path = require('path');

module.exports = {
  entry: {
    'lib': './src/index.ts',
  },
  devtool: 'source-map',
  mode: 'development',
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    library: '[name]',
    libraryTarget: 'umd',
    umdNamedDefine: true,
  },
  module: {
      rules: [
        {
            test: /\.ts$/,
            use: 'ts-loader',
            exclude: /node_modules/,
          },
      ]
  },
  resolve: {
    extensions: [ '.ts', '.js' ],
  },
};

The bundles are built but the built command webpack exit with error code 130 and the following message:捆绑包已构建,但构建的命令webpack退出并出现错误代码130和以下消息:

Module not found: Error: Can't resolve 'html-loader' in '/path/to/file_that_use_declaration'找不到模块:错误:无法解析“/path/to/file_that_use_declaration”中的“html-loader”

Any idea?任何想法?

Using raw-loader webpack plugin did the trick.使用raw-loader webpack 插件就可以了。

First, install using npm install raw-loader --save-dev .首先,使用npm install raw-loader --save-dev

Here is the webpack.config.js :这是webpack.config.js

const path = require('path');

module.exports = {
  entry: {
    'lib': './src/index.ts',
  },
  devtool: 'source-map',
  mode: 'development',
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    library: '[name]',
    libraryTarget: 'umd',
    umdNamedDefine: true,
  },
  module: {
      rules: [
        {
            test: /\.ts$/,
            use: 'ts-loader',
            exclude: /node_modules/,
        },
        {
            test: /\.html$/,
            use: 'raw-loader',
        }
      ]
  },
  resolve: {
    extensions: [ '.ts', '.js' ],
  },
};

Here is the html-loader.ts containing the declaration:这是包含声明的html-loader.ts

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

The file is imported as following with import HTMLVariable from './template.html';使用import HTMLVariable from './template.html'; . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM