Skip to content

Allow source maps to be merged in loader pipeline #1367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"chalk": "^4.1.0",
"enhanced-resolve": "^5.0.0",
"micromatch": "^4.0.0",
"semver": "^7.3.4"
"semver": "^7.3.4",
"source-map": "^0.7.3"
},
"devDependencies": {
"@types/micromatch": "^4.0.0",
Expand Down
45 changes: 38 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as crypto from 'crypto';
import * as path from 'path';
import {
RawSourceMap,
SourceMapConsumer,
SourceMapGenerator,
} from 'source-map';
import type * as typescript from 'typescript';
import type * as webpack from 'webpack';

Expand Down Expand Up @@ -31,7 +36,11 @@ const loaderOptionsCache: LoaderOptionsCache = {};
/**
* The entry point for ts-loader
*/
function loader(this: webpack.LoaderContext<LoaderOptions>, contents: string) {
function loader(
this: webpack.LoaderContext<LoaderOptions>,
contents: string,
map?: Record<string, any>
) {
this.cacheable && this.cacheable();
const callback = this.async();
const options = getLoaderOptions(this);
Expand All @@ -43,14 +52,15 @@ function loader(this: webpack.LoaderContext<LoaderOptions>, contents: string) {
}
const instance = instanceOrError.instance!;
buildSolutionReferences(instance, this);
successLoader(this, contents, callback, instance);
successLoader(this, contents, callback, instance, map);
}

function successLoader(
loaderContext: webpack.LoaderContext<LoaderOptions>,
contents: string,
callback: ReturnType<webpack.LoaderContext<LoaderOptions>['async']>,
instance: TSInstance
instance: TSInstance,
map?: Record<string, any>
) {
initializeInstance(loaderContext, instance);
reportTranspileErrors(instance, loaderContext);
Expand Down Expand Up @@ -86,19 +96,21 @@ function successLoader(
loaderContext,
fileVersion,
callback,
instance
instance,
map
);
}

function makeSourceMapAndFinish(
async function makeSourceMapAndFinish(
sourceMapText: string | undefined,
outputText: string | undefined,
filePath: string,
contents: string,
loaderContext: webpack.LoaderContext<LoaderOptions>,
fileVersion: number,
callback: ReturnType<webpack.LoaderContext<LoaderOptions>['async']>,
instance: TSInstance
instance: TSInstance,
map?: Record<string, any>
) {
if (outputText === null || outputText === undefined) {
setModuleMeta(loaderContext, instance, fileVersion);
Expand Down Expand Up @@ -129,8 +141,10 @@ function makeSourceMapAndFinish(
loaderContext
);

const mappedSourceMap = await mapSourceMap(sourceMap, loaderContext, map);

setModuleMeta(loaderContext, instance, fileVersion);
callback(null, output, sourceMap);
callback(null, output, mappedSourceMap);
}

function setModuleMeta(
Expand Down Expand Up @@ -649,6 +663,23 @@ function makeSourceMap(
};
}

async function mapSourceMap(
sourceMap: RawSourceMap,
loaderContext: webpack.LoaderContext<LoaderOptions>,
map?: Record<string, any>
) {
if (sourceMap !== undefined && map !== undefined) {
const inMap = map as RawSourceMap;
inMap.file = loaderContext.remainingRequest;
const sm1 = await new SourceMapConsumer(inMap);
const sm2 = await new SourceMapConsumer(sourceMap);
const generator = SourceMapGenerator.fromSourceMap(sm2);
generator.applySourceMap(sm1);
return generator.toJSON();
}
return sourceMap;
}

export = loader;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export function initializeInstance(
} else if (typeof customerTransformers === 'string') {
try {
customerTransformers = require(customerTransformers);
} catch (err) {
} catch (err: any) {
throw new Error(
`Failed to load customTransformers from "${instance.loaderOptions.getCustomTransformers}": ${err.message}`
);
Expand Down