Skip to content

Commit 0acd4d9

Browse files
devversionjelbourn
authored andcommitted
build: create sourcemap files (#4022)
* Creates sourceMap files for all bundles inside of the release. Fixes #852
1 parent b5f15ad commit 0acd4d9

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"sass": "^0.5.0",
107107
"scss-bundle": "^1.0.1",
108108
"selenium-webdriver": "^3.1.0",
109+
"sorcery": "^0.10.0",
109110
"stylelint": "^7.8.0",
110111
"travis-after-modes": "0.0.7",
111112
"ts-node": "^3.0.0",

tools/gulp/util/package-build.ts

+32-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313

1414
// There are no type definitions available for these imports.
1515
const uglify = require('uglify-js');
16+
const sorcery = require('sorcery');
1617

1718
/**
1819
* Copies different output files into a folder structure that follows the `angular/angular`
@@ -28,8 +29,8 @@ export function composeRelease(packageName: string) {
2829
inlinePackageMetadataFiles(packagePath);
2930

3031
copyFiles(packagePath, '**/*.+(d.ts|metadata.json)', join(releasePath, 'typings'));
31-
copyFiles(DIST_BUNDLES, `${packageName}.umd?(.min).js`, join(releasePath, 'bundles'));
32-
copyFiles(DIST_BUNDLES, `${packageName}?(.es5).js`, join(releasePath, '@angular'));
32+
copyFiles(DIST_BUNDLES, `${packageName}.umd?(.min).js?(.map)`, join(releasePath, 'bundles'));
33+
copyFiles(DIST_BUNDLES, `${packageName}?(.es5).js?(.map)`, join(releasePath, '@angular'));
3334
copyFiles(PROJECT_ROOT, 'LICENSE', releasePath);
3435
copyFiles(SOURCE_ROOT, 'README.md', releasePath);
3536
copyFiles(sourcePath, 'package.json', releasePath);
@@ -57,13 +58,17 @@ export async function buildModuleEntry(entryFile: string, entryName = 'material'
5758
format: 'es',
5859
});
5960

61+
await remapSourcemap(fesm2015File);
62+
6063
// Downlevel FESM-2015 file to ES5.
6164
transpileFile(fesm2015File, fesm2014File, {
6265
target: ScriptTarget.ES5,
6366
module: ModuleKind.ES2015,
6467
allowJs: true
6568
});
6669

70+
await remapSourcemap(fesm2014File);
71+
6772
// Create UMD bundle of FESM-2014 output.
6873
await createRollupBundle({
6974
moduleName: moduleName,
@@ -72,8 +77,31 @@ export async function buildModuleEntry(entryFile: string, entryName = 'material'
7277
format: 'umd'
7378
});
7479

75-
// Output a minified version of the UMD bundle
76-
writeFileSync(umdMinFile, uglify.minify(umdFile, { preserveComments: 'license' }).code);
80+
await remapSourcemap(umdFile);
81+
82+
uglifyFile(umdFile, umdMinFile);
83+
84+
await remapSourcemap(umdMinFile);
85+
}
86+
87+
/**
88+
* Finds the original sourcemap of the file and maps it to the current file.
89+
* This is useful when multiple transformation happen (e.g TSC -> Rollup -> Uglify)
90+
**/
91+
async function remapSourcemap(sourceFile: string) {
92+
(await sorcery.load(sourceFile)).write();
93+
}
94+
95+
/** Minifies a JavaScript file using UglifyJS2. Also writes sourcemaps to the output. */
96+
function uglifyFile(inputPath: string, outputPath: string) {
97+
let sourcemapOut = `${outputPath}.map`;
98+
let result = uglify.minify(inputPath, {
99+
preserveComments: 'license',
100+
outSourceMap: sourcemapOut
101+
});
102+
103+
writeFileSync(outputPath, result.code);
104+
writeFileSync(sourcemapOut, result.map);
77105
}
78106

79107
function copyFiles(fromPath: string, fileGlob: string, outDir: string) {

tools/gulp/util/rollup-helper.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export function createRollupBundle(config: BundleConfig): Promise<any> {
6060
format: config.format,
6161
dest: config.dest,
6262
globals: ROLLUP_GLOBALS,
63+
sourceMap: true
6364
};
6465

6566
return rollup.rollup(bundleOptions).then((bundle: any) => bundle.write(writeOptions));

tools/gulp/util/ts-compiler.ts

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export function transpileFile(inputPath: string, outputPath: string, options: ts
2525
reportDiagnostics(transpiled.diagnostics);
2626

2727
fs.writeFileSync(outputPath, transpiled.outputText);
28+
29+
if (transpiled.sourceMapText) {
30+
fs.writeFileSync(`${outputPath}.map`, transpiled.sourceMapText);
31+
}
2832
}
2933

3034
/** Parses a TypeScript project configuration. */

0 commit comments

Comments
 (0)