gpt4 book ai didi

typescript - 在 webpack 中使用带有 es6 模块和 typescript 的文件加载器

转载 作者:搜寻专家 更新时间:2023-10-30 20:41:33 24 4
gpt4 key购买 nike

webpack.config.js:

resolveLoader: { 
alias: {
'copy': 'file-loader?name=[path][name].[ext]&context=./src',
}
},

当我使用 javascript 时,这有效:

entry.js:

 var index = require("copy!../src/index.html");

但是我已经使用 (ts-loader) 转向 typescript ,所以我稍微修改了我在 entry.ts 中所做的事情:

import * as index from 'copy!index.html';

但这现在给了我一个错误:

ERROR in ./src/entry.ts
(3,24): error TS2307: Cannot find module 'copy!../src/index.html'.

最佳答案

使用 TypeScript 2,您可以使用通配符模块声明:

declare module "*!text" {
const content: string;
export default content;
}
// Some do it the other way around.
declare module "json!*" {
const value: any;
export default value;
}

现在您可以导入匹配“*!text”或“json!*”的内容。

import fileContent from "./xyz.txt!text";
import data from "json!http://example.com/data.json";
console.log(data, fileContent);

关于typescript - 在 webpack 中使用带有 es6 模块和 typescript 的文件加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33915930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com