@@ -11,6 +11,7 @@ import {writeFileSync} from 'fs';
1111// There are no type definitions available for these imports.
1212const inlineResources = require ( '../../../scripts/release/inline-resources' ) ;
1313const uglify = require ( 'uglify-js' ) ;
14+ const sorcery = require ( 'sorcery' ) ;
1415
1516const libraryRoot = join ( SOURCE_ROOT , 'lib' ) ;
1617const 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']);
92101task ( 'library:assets:scss' , sassBuildTask ( materialDir , libraryRoot , true ) ) ;
93102task ( 'library:assets:html' , copyTask ( join ( libraryRoot , '**/*.+(html|scss)' ) , materialDir ) ) ;
94103task ( '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+ }
0 commit comments