2
我試圖導入HTML到使用的WebPack在打字稿的變量。導入HTML用的WebPack
這裏是我的設置:
的package.json:
{
"devDependencies": {
"awesome-typescript-loader": "^3.2.3",
"html-loader": "^0.5.1",
"ts-loader": "^2.3.7",
"typescript": "^2.5.3",
"webpack": "^3.6.0"
}
}
webpack.config.js:
const path = require('path');
const webpack = require('webpack');
module.exports = {
context: path.join(__dirname),
entry: './main',
output: {
path: path.join(__dirname, 'dist'),
filename: 'app.js'
},
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".js"],
modules: [
"node_modules",
path.join(__dirname, 'app'),
],
},
module: {
rules: [
{
enforce: 'pre',
test: /\.html$/,
loader: 'html-loader',
},
// Faster alternative to ts-loader
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
options: {
configFileName: 'tsconfig.json',
},
exclude: /(node_modules)/,
},
],
},
};
main.ts:
import template from './template.html';
console.log(template);
模板。 HTML:
<p>Hello World !</p>
當我試圖用的WebPack編譯:
$ ./node_modules/.bin/webpack
[at-loader] Using [email protected] from typescript and "tsconfig.json" from /tmp/test/tsconfig.json.
[at-loader] Checking started in a separate process...
[at-loader] Checking finished with 1 errors
Hash: d06d08edc313f90c0533
Version: webpack 3.6.0
Time: 2194ms
Asset Size Chunks Chunk Names
app.js 2.72 kB 0 [emitted] main
[0] ./main.ts 136 bytes {0} [built]
[1] ./template.html 40 bytes {0} [built]
ERROR in [at-loader] ./main.ts:1:22
TS2307: Cannot find module './template.html'.
我一直在這半天,所以你可以肯定template.html
是它應該是。
從我從webpack配置中瞭解到,html-loader應該首先處理文件,所以它應該將html文件的內容加載到變量中。至少,這個使用ES6工作...
任何人都可以電話我如何加載HTML與的WebPack /打字稿變量?或者至少我的方法出了什麼問題。
如果你曾經能夠解決這個問題? –
請參閱下面的mamacdon的答案。我缺乏的是html擴展的模塊定義。 –