[英]Can't resolve declaration with ts-loader and webpack
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
.该项目是使用
webpack
和ts-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.