html-webpack-plugin icon indicating copy to clipboard operation
html-webpack-plugin copied to clipboard

Error "Child compilation failed" when using Typescript

Open MarkusKramer opened this issue 10 years ago • 14 comments

I'm trying to use version 2.1.0 in my typscript project. When I add an error Typescript will report the appropriate error ("error TS2304: Cannot find name 'consol'"). After fixing it I'm getting this:

HtmlWebpackPlugin Error: Child compilation failed:
(2,1): error TS2304: Cannot find name 'consol'.

After restarting webpack-dev-server it works again. This bug is not present in 1.7.0 so I'll simply continue to use this version.

webpack.config.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: {
        mockApp: './main'
    },
    devtool: 'source-map',
    resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.ts', '.js'],
    },
    plugins: [
              new HtmlWebpackPlugin({}),
    ],
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'ts'
            },
        ]
    }
}

main.ts

console.log("hello");

tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "module": "commonjs",
    "target": "ES5"
  },
  "files": [
    "./main.ts"
  ]
}

MarkusKramer avatar Dec 30 '15 13:12 MarkusKramer

@MarkusKramer could you please retest the 2.x branch?

jantimon avatar Jan 19 '16 13:01 jantimon

I've experienced the same issue today after jumping from 1.7.0 to 2.7.1

r-park avatar Jan 21 '16 08:01 r-park

I have the same problem. It seems the last error is retained/cached for 1 recompile. Triggering a second compile (touch file) removes the error report.

I tried the showErrors: false flag, but this causes another problem:

ERROR in   ReferenceError: ERROR is not defined

  - index.template.html:1 
.../index.template.html:1:1

  - index.js:183 HtmlWebpackPlugin.evaluateCompilationResult
    [client]/[html-webpack-plugin]/index.js:183:26

laurentvdl avatar Jan 25 '16 11:01 laurentvdl

I had a short look at the ts loader - it might be an issue there as it hooks in the compilations. In the end ts compiles something although no ts files are touched...

jantimon avatar Jan 25 '16 18:01 jantimon

Same issue here after upgrading from 1.7 to 2.7

gitnik avatar Feb 02 '16 15:02 gitnik

2.x uses a child compilation similar to the extract-text-plugin. This allows to use any template engine and loaders for the template.

Unfortunately the ts loader does a lot of stuff.. maybe the maintainer of the ts-loader can help to fix this issue. So please open a ticket there.

jantimon avatar Feb 03 '16 16:02 jantimon

@jbrantly @basarat cc

andriictkn avatar Feb 04 '16 14:02 andriictkn

Hi, ts-loader maintainer here. I've investigated this issue a bit but I've gotten to the point where I might need a little collaboration @jantimon

A quick overview of how ts-loader works:

  • It creates a TypeScript language service which essentially holds an entire "program" in memory (spanning multiple files)
  • As webpack runs the loader against each TypeScript file, the loader updates the TypeScript language service with the contents of that file
  • An 'after-compile' hook is installed where the language service is queried for all TypeScript errors which are then added to compilation.errors.

Looking at html-webpack-plugin, it seems that the main hook is during the make lifecycle which is run before any loaders are executed. So let's walk through what happens in the OP where you start with "console.log", change to "consol.log" and then back to "console.log".

webpack --watch

console.log

html-webpack-plugin runs during the make lifecycle, ts-loader runs inside the child compilation, no errors, everything is good

consol.log

html-webpack-plugin runs during the make lifecycle, ts-loader does not run at this point for some reason that I don't understand, the after-compile hook is run against the child compilation but as far as the language service knows there are no errors so nothing is added to compilation.errors for the child compilation and html-webpack-plugin completes without errors. However, the primary compilation continues to run and it does run ts-loader, which updates the language service, and the after-compile hook is run against the primary compilation which does add the error (and webpack reports it).

console.log

html-webpack-plugin runs during the make lifecycle, again ts-loader does not run at this point, and the after-compile hook is run against the child compilation. At this point the language service thinks there's an error (from the previous compilation) and so adds it to the child compilation which html-webpack-plugin then sees and throws the error from the OP. The primary compilation continues and ts-loader runs which updates the language service. For the primary compilation the original error is removed.

So it seems that for the first compilation, ts-loader runs within the child compilation, but for each subsequent run it is executed after the child compilation completes. To reiterate, this point is important because ts-loader is what updates the TypeScript language service. Since ts-loader isn't running within the child compilation, the after-compile hook is running for the child compilation and adding errors based on old information.

I wanted to reach out here because I'm not sure which project, if any, is misbehaving and I don't have a lot of experience with child compilations.

jbrantly avatar Feb 10 '16 04:02 jbrantly

FWIW, I have published an interim fix for this by simply returning early in the after-compile hook if inside the child compilation. This seems to have the desired effect. However, I'm still interested in a "better" fix if it's out there.

jbrantly avatar Feb 10 '16 05:02 jbrantly

@jbrantly sorry for the delay.. I am sorry but the child compilation stuff is really hard to understand and there is almost no documentation for it. RIght now I am moving into another country and therefore I can't support this well at the moment.

jantimon avatar Feb 28 '16 08:02 jantimon

@jantimon why you close it?

andriictkn avatar Mar 10 '16 10:03 andriictkn

@AndreyUtka It looks like we can't do anything on our end. Furthermore it was solved by the ts-loader. Why should it stay open?

jantimon avatar Mar 10 '16 12:03 jantimon

👍 @jantimon

bneiluj avatar Apr 26 '16 16:04 bneiluj

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar May 31 '18 23:05 lock[bot]