Ash*_*man 5 typescript webpack
webpack.config.js:
resolveLoader: {
alias: {
'copy': 'file-loader?name=[path][name].[ext]&context=./src',
}
},
Run Code Online (Sandbox Code Playgroud)
当我使用javascript时,这工作:
entry.js:
var index = require("copy!../src/index.html");
Run Code Online (Sandbox Code Playgroud)
但我已经使用(ts-loader)移动到打字稿了,所以我略微修改了我在做的事情entry.ts:
import * as index from 'copy!index.html';
Run Code Online (Sandbox Code Playgroud)
但现在这给了我一个错误:
ERROR in ./src/entry.ts
(3,24): error TS2307: Cannot find module 'copy!../src/index.html'.
Run Code Online (Sandbox Code Playgroud)
使用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;
}
Run Code Online (Sandbox Code Playgroud)
现在您可以导入与"*!text"或"json!*"匹配的内容.
import fileContent from "./xyz.txt!text";
import data from "json!http://example.com/data.json";
console.log(data, fileContent);
Run Code Online (Sandbox Code Playgroud)
小智 5
我遇到了一个更特定于文件类型的替代方法,因此它的灵活性较差,但不需要前缀/后缀。
declare module '*.png'import * as logo from "./ss-logo-transparent.png";找不到模块“copy!../src/index.html”。
必须声明不是用 TypeScript 编写的外部模块。
只需使用require此处定义的函数:https://github.com/TypeStrong/ts-loader#code-splitting-and-loading-other-resources
代码:
declare var require: {
<T>(path: string): T;
(paths: string[], callback: (...modules: any[]) => void): void;
ensure: (
paths: string[],
callback: (require: <T>(path: string) => T) => void
) => void;
};
Run Code Online (Sandbox Code Playgroud)