Skip to content

Commit d133165

Browse files
committed
build: create sourcemap files
* Creates sourceMap files for all bundles inside of the release. Fixes #852
1 parent c38c9c3 commit d133165

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
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/tasks/library.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {writeFileSync} from 'fs';
1111
// There are no type definitions available for these imports.
1212
const inlineResources = require('../../../scripts/release/inline-resources');
1313
const uglify = require('uglify-js');
14+
const sorcery = require('sorcery');
1415

1516
const libraryRoot = join(SOURCE_ROOT, 'lib');
1617
const tsconfigPath = join(libraryRoot, 'tsconfig-build.json');
@@ -64,13 +65,17 @@ async function buildModuleEntry(entryFile: string, entryName = '') {
6465
format: 'es',
6566
});
6667

68+
await remapSourcemap(fesm2015File);
69+
6770
// Downlevel FESM-2015 file to ES5.
6871
transpileFile(fesm2015File, fesm2014File, {
6972
target: ScriptTarget.ES5,
7073
module: ModuleKind.ES2015,
71-
allowJs: true
74+
allowJs: true,
7275
});
7376

77+
await remapSourcemap(fesm2014File);
78+
7479
// Create UMD bundle of FESM-2014 output.
7580
await createRollupBundle({
7681
moduleName: moduleName,
@@ -79,8 +84,12 @@ async function buildModuleEntry(entryFile: string, entryName = '') {
7984
format: 'umd'
8085
});
8186

87+
await remapSourcemap(umdFile);
88+
8289
// Output a minified version of the UMD bundle
83-
writeFileSync(umdMinFile, uglify.minify(umdFile, { preserveComments: 'license' }).code);
90+
uglifyFile(umdFile, umdMinFile);
91+
92+
await remapSourcemap(umdMinFile);
8493
}
8594

8695
/**
@@ -92,3 +101,23 @@ task('library:assets', ['library:assets:scss', 'library:assets:html']);
92101
task('library:assets:scss', sassBuildTask(materialDir, libraryRoot, true));
93102
task('library:assets:html', copyTask(join(libraryRoot, '**/*.+(html|scss)'), materialDir));
94103
task('library:assets:inline', () => inlineResources(materialDir));
104+
105+
/**
106+
* Finds the original sourcemap of the file and maps it to the current file.
107+
* This is useful when multiple transformation happen (e.g TSC -> Rollup -> Uglify)
108+
**/
109+
async function remapSourcemap(sourceFile: string) {
110+
(await sorcery.load(sourceFile)).write();
111+
}
112+
113+
/** Minifies a JavaScript file using UglifyJS2. Also writes sourcemaps to the output. */
114+
function uglifyFile(inputPath: string, outputPath: string) {
115+
let sourcemapOut = `${outputPath}.map`;
116+
let result = uglify.minify(inputPath, {
117+
preserveComments: 'license',
118+
outSourceMap: sourcemapOut
119+
});
120+
121+
writeFileSync(outputPath, result.code);
122+
writeFileSync(sourcemapOut, result.map);
123+
}

tools/gulp/tasks/release.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const typingsGlob = join(DIST_MATERIAL, '**/*.+(d.ts|metadata.json)');
2626
// Matches the "package.json" and "README.md" file that needs to be shipped.
2727
const assetsGlob = join(COMPONENTS_DIR, '+(package.json|README.md)');
2828
// Matches all UMD bundles inside of the bundles distribution.
29-
const umdGlob = join(DIST_BUNDLES, '*.umd.*');
29+
const umdGlob = join(DIST_BUNDLES, '*.umd?(.min).js?(.map)');
3030
// Matches all flat ESM bundles (e.g material.js and material.es5.js)
31-
const fesmGlob = [join(DIST_BUNDLES, '*.js'), `!${umdGlob}`];
31+
const fesmGlob = [join(DIST_BUNDLES, '*.js?(.map)'), `!${umdGlob}`];
3232

3333
// The entry-point for the scss theming bundle.
3434
const themingEntryPointPath = join(COMPONENTS_DIR, 'core', 'theming', '_all-theme.scss');

tools/gulp/util/rollup-helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function createRollupBundle(config: BundleConfig): Promise<any> {
5858
format: config.format,
5959
dest: config.dest,
6060
globals: ROLLUP_GLOBALS,
61+
sourceMap: true
6162
};
6263

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

tools/gulp/util/ts-compiler.ts

Lines changed: 4 additions & 0 deletions
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)