Skip to content

Commit

Permalink
fix(sourcemaps): fix mappings for source maps so they will work
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonaden committed Apr 5, 2018
1 parent 3116275 commit 0a656b4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 18 deletions.
24 changes: 9 additions & 15 deletions .make-compat-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ let mkdirp = require('mkdirp');
let path = require('path');
let klawSync = require('klaw-sync');
let licenseTool = require('./tools/add-license-to-file');
let makePackages = require('./.make-packages');
let copySources = makePackages.copySources;
let createImportTargets = makePackages.createImportTargets;
let cleanSourceMapRoot = makePackages.cleanSourceMapRoot;

let addLicenseToFile = licenseTool.addLicenseToFile;
let addLicenseTextToFile = licenseTool.addLicenseTextToFile;

Expand All @@ -19,6 +24,7 @@ const CJS_PKG = PKG_ROOT + '';
const ESM5_PKG = PKG_ROOT + '_esm5/';
const ESM2015_PKG = PKG_ROOT + '_esm2015/';
const UMD_PKG = PKG_ROOT + 'bundles/';
const SRC_ROOT_PKG = PKG_ROOT + 'src/';
const TYPE_PKG = PKG_ROOT;

// License info for minified files
Expand All @@ -32,22 +38,10 @@ mkdirp.sync(PKG_ROOT);
// Copy over the sources
fs.copySync(TYPE_ROOT, TYPE_PKG);
copySources(CJS_ROOT, CJS_PKG);
cleanSourceMapRoot(CJS_PKG, SRC_ROOT_PKG);
copySources(ESM5_ROOT, ESM5_PKG, true);
cleanSourceMapRoot(ESM5_PKG, SRC_ROOT_PKG);
copySources(ESM2015_ROOT, ESM2015_PKG, true);
cleanSourceMapRoot(ESM2015_PKG, SRC_ROOT_PKG);

fs.copySync('compat/package.json', PKG_ROOT + '/package.json');

function copySources(rootDir, packageDir, ignoreMissing) {
// If we are ignoring missing directories, early return when source doesn't exist
if (!fs.existsSync(rootDir)) {
if (ignoreMissing) {
return;
} else {
throw "Source root dir does not exist!";
}
}
// Copy over the CommonJS files
fs.copySync(rootDir, packageDir);
fs.copySync('./LICENSE.txt', packageDir + 'LICENSE.txt');
fs.copySync('./compat/README.md', packageDir + 'README.md');
}
58 changes: 55 additions & 3 deletions .make-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ const CJS_ROOT = ROOT + 'cjs/';
const ESM5_ROOT = ROOT + 'esm5/';
const ESM2015_ROOT = ROOT + 'esm2015/';
const UMD_ROOT = ROOT + 'global/';
const LEGACY_REEXPORT_ROOT = ROOT + "legacy-reexport/"
const ESM5_FOR_ROLLUP_ROOT = ROOT + 'esm5_for_rollup/';
const LEGACY_REEXPORT_ROOT = ROOT + 'legacy-reexport/';
const TYPE_ROOT = ROOT + 'typings/';
const PKG_ROOT = ROOT + 'package/';
const CJS_PKG = PKG_ROOT + '';
const ESM5_PKG = PKG_ROOT + '_esm5/';
const ESM2015_PKG = PKG_ROOT + '_esm2015/';
const UMD_PKG = PKG_ROOT + 'bundles/';
const SRC_ROOT_PKG = PKG_ROOT + 'src/';
const TYPE_PKG = PKG_ROOT;

// License info for minified files
Expand Down Expand Up @@ -106,13 +108,21 @@ createImportTargets(importTargets, "_esm2015/", ESM2015_PKG);
mkdirp.sync(PKG_ROOT);

// Copy over the sources
copySources('src/', PKG_ROOT + 'src/');
copySources('src/', SRC_ROOT_PKG);
// Copy legacy-reexport sources
copySources('legacy-reexport/', SRC_ROOT_PKG);

copySources(CJS_ROOT, CJS_PKG);
fs.copySync(TYPE_ROOT, TYPE_PKG);
fs.copySync(LEGACY_REEXPORT_ROOT, CJS_PKG, {overwrite: false, errorOnExist: true});

// Clean up the source maps for CJS sources
cleanSourceMapRoot(PKG_ROOT, SRC_ROOT_PKG);
fs.copySync(TYPE_ROOT, TYPE_PKG);

copySources(ESM5_ROOT, ESM5_PKG, true);
cleanSourceMapRoot(ESM5_PKG, SRC_ROOT_PKG);
copySources(ESM2015_ROOT, ESM2015_PKG, true);
cleanSourceMapRoot(ESM2015_PKG, SRC_ROOT_PKG);

// Copy over tsconfig.json for bazel build support
fs.copySync('./tsconfig.base.json', PKG_ROOT + 'src/tsconfig.json');
Expand All @@ -127,11 +137,30 @@ fs.copySync('src/internal-compatibility/package.json', PKG_ROOT + '/internal-com

if (fs.existsSync(UMD_ROOT)) {
fs.copySync(UMD_ROOT, UMD_PKG);
// Clean up source map paths so they can be re-mapped
klawSync(UMD_PKG, {filter: (item) => item.path.endsWith('.js.map')})
.map(f => f.path)
.forEach(fName => {
const sourceMap = fs.readJsonSync(fName);
sourceMap.sources = sourceMap.sources.map(s => {
const nm = 'node_modules/';
const rr = path.resolve(ESM5_FOR_ROLLUP_ROOT);
if (s.includes(nm)) {
return s.substring(s.indexOf(nm) + nm.length);
} else if (s.includes(rr)) {
return s.substring(s.indexOf(rr) + rr.length);
}
return s;
});
fs.writeJsonSync(fName, sourceMap);
});

// Add licenses to tops of bundles
addLicenseToFile('LICENSE.txt', UMD_PKG + 'rxjs.umd.js');
addLicenseTextToFile(license, UMD_PKG + 'rxjs.umd.min.js');
addLicenseToFile('LICENSE.txt', UMD_PKG + 'rxjs.umd.js');
addLicenseTextToFile(license, UMD_PKG + 'rxjs.umd.min.js');
cleanSourceMapRoot(UMD_PKG, PKG_ROOT);
}

// remove umd.js/umd.d.ts files that are only needed for creation of the umd bundle
Expand All @@ -143,6 +172,23 @@ fs.removeSync(ESM2015_PKG + '/internal/umd.js');
fs.removeSync(ESM2015_PKG + '/internal/umd.js.map');
fs.removeSync(TYPE_PKG + '/internal/umd.d.ts');

function cleanSourceMapRoot(mapRoot, sourcesRoot) {
klawSync(mapRoot, {filter: (item) => item.path.endsWith('.js.map')})
.map(f => f.path)
.forEach(fName => {
const sourceMap = fs.readJsonSync(fName);

// Get relative path from map file to source file
sourceMap.sources = sourceMap.sources.map(s => {
const sRel = path.relative(path.parse(fName).dir, path.resolve(path.join(sourcesRoot, s)));
return sRel;
});
delete sourceMap.sourceRoot;

fs.writeJsonSync(fName, sourceMap);
});
}

function copySources(rootDir, packageDir, ignoreMissing) {
// If we are ignoring missing directories, early return when source doesn't exist
if (!fs.existsSync(rootDir)) {
Expand Down Expand Up @@ -179,3 +225,9 @@ module.exports = function() {

fs.outputFileSync(targetDirectory + 'path-mapping.js', outputData);
}

module.exports = {
copySources,
createImportTargets,
cleanSourceMapRoot
}
1 change: 1 addition & 0 deletions tsconfig/compat/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"noEmit": false,
"baseUrl": "../../compat",
"sourceRoot": "../../compat",
"paths": {
"rxjs": ["../src/index"],
"rxjs/*": ["../src/*"],
Expand Down
1 change: 1 addition & 0 deletions tsconfig/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"noEmit": false,
"baseUrl": "../src",
"sourceRoot": "../src",
"paths": {
"rxjs": ["./"],
"rxjs/*": ["./*"]
Expand Down
1 change: 1 addition & 0 deletions tsconfig/tsconfig.legacy-reexport.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"outDir": "../dist/legacy-reexport",
"rootDir": "../legacy-reexport",
"baseUrl": "../legacy-reexport",
"sourceRoot": "../legacy-reexport",
"paths": {
"rxjs": ["../dist/typings/index"],
"rxjs/*": ["../dist/typings/*"],
Expand Down

0 comments on commit 0a656b4

Please sign in to comment.