[英]Can't resolve declaration with ts-loader and webpack
為了導入.html
文件,我在html-loader.ts
文件中使用以下聲明:
declare module "html-loader!*" {
const content: string;
export default content;
}
此聲明在其他文件中使用如下:
import htmlcode from 'html-loader!./template.html';
...
該項目是使用webpack
和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
}
}
這是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' ],
},
};
捆綁包已構建,但構建的命令webpack
退出並出現錯誤代碼130
和以下消息:
找不到模塊:錯誤:無法解析“/path/to/file_that_use_declaration”中的“html-loader”
任何想法?
使用raw-loader
webpack 插件就可以了。
首先,使用npm install raw-loader --save-dev
。
這是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' ],
},
};
這是包含聲明的html-loader.ts
:
declare module '*.html' {
const content: any;
export default content;
}
使用import HTMLVariable from './template.html';
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.