简体   繁体   English

使用webpack从typescript导入html

[英]Import html from typescript with webpack

I am trying to import html into a variable in typescript using webpack. 我正在尝试使用webpack将html导入到typescript中的变量中。

Here is my setup: 这是我的设置:

package.json: 的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: 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: main.ts:

import template from './template.html';
console.log(template);

template.html: template.html:

<p>Hello World !</p>

When I try to compile with with webpack: 当我尝试使用webpack进行编译时:

$ ./node_modules/.bin/webpack 

[at-loader] Using typescript@2.5.3 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'.

I've been on this for half a day so you can be sure template.html is where it is supposed to be. 我已经在这半天了,所以你可以确定template.html是应该的位置。

From what I understand from webpack config is that the html-loader is supposed to process the file first, so it should load the content of the html file into the variable. 根据我对webpack配置的理解,html-loader应该首先处理文件,因此它应该将html文件的内容加载到变量中。 At least, this used to work with es6... 至少,这曾经与es6一起工作......

Can anyone tel me how to load html into a variable with webpack / typescript? 谁能告诉我如何使用webpack / typescript将html加载到变量中? Or at least what is wrong with my approach. 或者至少我的方法有什么问题。

An easy workaround is to stick with CommonJS "require" when loading non-TypeScript assets: 一个简单的解决方法是在加载非TypeScript资源时坚持使用CommonJS“require”:

const template = require('./template.html');

If you'd prefer to keep using import , it's possible to configure the TypeScript compiler to load the file as a string. 如果您更喜欢继续使用import ,则可以配置TypeScript编译器以将文件作为字符串加载。 The method below worked for me under TypeScript 2.4.2. 下面的方法适用于TypeScript 2.4.2。

Add this block to your project's type declarations file (mine is called typings.d.ts ): 将此块添加到项目的类型声明文件中(我的名称为typings.d.ts ):

// typings.d.ts
declare module '*.html' {
  const content: string;
  export default content;
}

You should now be able to import html files like this: 您现在应该可以导入这样的html文件:

// main.ts
import template from './template.html';
console.log(template); // <p>Hello world !</p>

Full disclosure: my project used different loaders from yours (see below). 完全披露:我的项目使用了不同的装载机(见下文)。

// webpack.config.js
config.module = {
  rules: [
    {
      test: /\.ts$/,
      loader: '@ngtools/webpack'
    },
    {
      test: /\.html$/,
      use: 'raw-loader'
    }
  ]
}

There's a Github thread here where other solutions are discussed. 这里有一个Github线程其中讨论了其他解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM