27

I'm adding TypeScript to the build process of a currently all-JavaScript web app whose build process is structured around Webpack. I've been following TypeScript's Migrating from JavaScript article, and have a question about the suggested Webpack configuration¹, copied below for your convenience:

module.exports = {
    entry: "./src/index.ts",
    output: {
        filename: "./dist/bundle.js",
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },

    module: {
        loaders: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            { test: /\.tsx?$/, loader: "awesome-typescript-loader" }
        ],

        preLoaders: [
            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { test: /\.js$/, loader: "source-map-loader" }
        ]
    },

    // Other options...
};

The presence of awesome-typescript-loader is of course required — we want to compile TypeScript, after all. My question is about source-map-loader, used here as a pre-loader. The article is pretty terse about its purpose, saying only:

You can use awesome-typescript-loader, a TypeScript loader, combined with source-map-loader for easier debugging.

Another article adds only a bit of extra information:

Both of these dependencies will let TypeScript and Webpack play well together. awesome-typescript-loader helps Webpack compile your TypeScript code using the TypeScript’s standard configuration file named tsconfig.json. source-map-loader uses any sourcemap outputs from TypeScript to inform webpack when generating its own sourcemaps. This will allow you to debug your final output file as if you were debugging your original TypeScript source code.

Finally, source-map-loader's own README says:

source-map-loader extracts existing source maps from all JavaScript entries. This includes both inline source maps as well as those linked via URL. All source map data is passed to webpack for processing as per a chosen source map style specified by the devtool option in webpack.config.js.

Ok. So source-map-loader ingests source maps and provides them to Webpack. My question, though, is why should it be running as a pre-loader? Based on the second quote, above, it sounds like source-map-loader is supposed to pick up sourcemap outputs from TypeScript. If it runs as a pre-loader, though, it runs before normal loaders, and I don't understand how it can use output from TypeScript.

Further, I was under the impression that Webpack doesn't deal with intermediate files — in the configuration, source-map-loader only loads .jsx? files. Doesn't that mean source-map-loader won't be invoked on the results of TypeScript compilation at all?


1: Note that this Webpack configuration uses outdated syntax, listing normal loaders in the module.loaders section, and pre-loaders in the module.preLoaders section. This doesn't affect the content of this question, and an example with current syntax is available at a similar article from the same site.

5
  • 1
    Waiting for the answers. I have the same confusion but instead of typescript I'm using ES6 Javascript. Commented Dec 3, 2018 at 13:02
  • 1
    Same, can't find anywhere online that explain this. I don't understand why it should be a pre-loader, make more sense if it is a post loader: since it say it is processing source-map outputs from typescript. Commented Dec 13, 2018 at 3:07
  • 2
    I think the answer is that source-map-loader is not needed, at least not in a generic Typescript project.. I use it in node.js to get sensible stack traces etc, but not in webpack. When running the code in the browser, it resolves the source maps and does the mapping. Commented Jan 25, 2019 at 6:41
  • Interesting. If you can find evidence for that supposition, post an answer! Commented Jan 25, 2019 at 6:42
  • 2
    @jørgen-tvedt, I also have similar suspicion that "source-map-loader" is not needed. I did play around with ts+webpack setup not using it (github.com/ApolloTang/webpack4-babel7-ts3-w-babel-loader). it seem to work just fine; however, it is a setup with babel@7 using @babel/typescript thou. Commented Jan 28, 2019 at 5:08

1 Answer 1

6

It doesn't have anything to do with TypeScript compilation per se, and you don't need it to get source maps to work for your own sources. However, you probably want it for the following reason, as mentioned in the paragraph right after the one you quoted from source-map-loader's README:

This loader is especially useful when using 3rd-party libraries having their own source maps. If not extracted and processed into the source map of the webpack bundle, browsers may misinterpret source map data. source-map-loader allows webpack to maintain source map data continuity across libraries so ease of debugging is preserved.

The reason it's a preloader is because it has to be run on .js files before they are minified, since that tends to remove inline source maps. Since it only reacts on .js files and not .ts files it isn't run before your TypeScript files are transpiled, but right after they have been outputted to .js files.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.