Skip to content

Commit

Permalink
Separate makeSourcemapsRelative into a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Apr 10, 2020
1 parent c08cc3c commit 576f9d3
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions build-system/compile/closure-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ function logError(message) {
log(`${message}\n` + formatClosureCompilerError(compilerErrors));
}

/**
* Normalize the sourcemap file paths before pushing into Closure.
* Closure don't follow Gulp's normal sourcemap "root" pattern. Gulp considers
* all files to be relative to the CWD by default, meaning a file `src/foo.js`
* with a sourcemap alongside points to `src/foo.js`. Closure considers each
* file relative to the sourcemap. Since the sourcemap for `src/foo.js` "lives"
* in `src/`, it ends up resolving to `src/src/foo.js`.
*
* @param {!Stream} closureStream
* @return {!Stream}
*/
function makeSourcemapsRelative(closureStream) {
const relativeSourceMap = sourcemaps.mapSources((source, file) => {
const dir = path.dirname(file.sourceMap.file);
return path.relative(dir, source);
});

return pumpify.obj(relativeSourceMap, closureStream);
}

/**
* @param {Array<string>} compilerOptions
* @param {?string} nailgunPort
Expand Down Expand Up @@ -147,19 +167,7 @@ function gulpClosureCompile(compilerOptions, nailgunPort) {
}
}

// Normalize the file path before pushing into Closure.
// Closure don't follow Gulp's normal sourcemap "root" pattern. Gulp
// considers all files to be relative to the CWD by default, meaning a file
// `src/foo.js` with a sourcemap alongside points to `src/foo.js`. Closure
// considers each file relative to the sourcemap. Since the sourcemap for
// `src/foo.js` "lives" in `src/`, it ends up resolving to `src/src/foo.js`.
const relativeSourceMap = sourcemaps.mapSources((source, file) => {
const dir = path.dirname(file.sourceMap.file);
return path.relative(dir, source);
});

return pumpify.obj(
relativeSourceMap,
return makeSourcemapsRelative(
closureCompiler.gulp(initOptions)(compilerOptions, pluginOptions)
);
}
Expand Down

0 comments on commit 576f9d3

Please sign in to comment.