Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript 2.0 and Webpack importing of HTML as string

I'm trying to import a HTML file as string with the help of webpack (Currently using webpack because TypeScript 2.0 doesn't support async/await on non ES6 targets).

The problem I have is, even if the html-loader version from github supports a config flag 'exportAsEs6Default' i don't get it to set correctly. Is there any way to set a loader options globally? Because if I add the html-loader to the loaders section in the config file the loader is called twice causing the content to be nested.


I have the following definition file to support importing of HTML (like in the example on the modules documentation)

declare module "html!*" {
    const content: string;
    export default content;
}

The corresponsing import statement:

import templateString from "html!./Hello.html";

The versions of the packages I use:

"babel-core": "^6.17.0",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.16.0",
"html-loader": "git://github.com/webpack/html-loader.git#4633a1c00c86b78d119b7862c71b17dbf68d49de",
"ts-loader": "^0.9.5",
"typescript": "2.0.3",
"webpack": "^1.13.2"

And the webpack config file

"use strict";

module.exports = {
    entry: "./WebApp/Hello.ts",
    output: {
        path: "./wwwroot/compiled",
        filename: "app.bundle.js"
    },
    resolve: {
        extensions: ["", ".webpack.js", ".web.js", ".js", ".ts"]
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                exclude: /node_modules/,
                loader: "babel-loader!ts-loader"
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            }
        ]
    }
};
like image 595
Fionn Avatar asked Oct 28 '16 01:10

Fionn


People also ask

How to import CSS file in typescript?

I think now typescript can import css file by simply doing import 'fileTobeImportedPath.css' This case is related to Typescript. You can add typings.d.ts in your project with this content:

How to load a file as a string in typescript?

If you'd prefer to keep using import, it's possible to configure the TypeScript compiler to load the file as a string. The method below worked for me under TypeScript 2.4.2. Add this block to your project's type declarations file (mine is called typings.d.ts ): Full disclosure: my project used different loaders from yours (see below).

What is typescript in Webpack?

This guide stems from the Getting Started guide. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. In this guide we will learn how to integrate TypeScript with webpack.

Why can't I find a module in typescript?

First, you’ll need to enable some module system by setting TypeScript’s module option. Valid options are commonjs, amd, system, and umd. If you started converting over to TypeScript imports, you’ll probably run into errors like Cannot find module 'foo'. . The issue here is that you likely don’t have declaration files to describe your library.


1 Answers

So after some tinkering I found a way to get this done. As i didn't want to add a 'exportAsEs6Default' query parameter to every import statement, I changed from an explicit loader for html to a configured loader.

I'll leave this question open in case someone knows a better way, as currently I'm not sure if I'm all to happy with this way (something native to typescript would be find without the need of a loader), but maybe it will help others facing the same problem.


So the import statement from the example above changed to

import templateString from "./Hello.html";

Together with an updated definition file

declare module "*.html" {
    const content: string;
    export default content;
}

And the webpack config file changed to this:

"use strict";

module.exports = {
    entry: "./WebApp/Hello.ts",
    output: {
        path: "./wwwroot/compiled",
        filename: "app.bundle.js"
    },
    resolve: {
        extensions: ["", ".webpack.js", ".web.js", ".js", ".ts", ".html"]
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                exclude: /node_modules/,
                loader: "babel-loader!ts-loader"
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                test: /\.html$/,
                exclude: /node_modules/,
                loader: "html-loader?exportAsEs6Default"
            }
        ]
    }
};

No change to the used packages.

like image 149
Fionn Avatar answered Sep 28 '22 23:09

Fionn