1+ 'use strict' ;
2+
3+ // Node Dependencies
4+ const fs = require ( 'fs' ) ;
5+ const path = require ( 'path' ) ;
6+ const glob = require ( 'glob' ) ;
7+ const camelCase = require ( 'camelcase' ) ;
8+
9+ // Compilation Dependencies
10+ const ngc = require ( '@angular/compiler-cli/src/main' ) . main ;
11+ const webpack = require ( 'webpack' ) ;
12+ const UglifyJsPlugin = require ( 'uglifyjs-webpack-plugin' ) ;
13+
14+ // Utils
15+ const inlineResources = require ( './inline-resources' ) ;
16+ const relativeCopy = require ( './utils' ) . relativeCopy ;
17+
18+ // Scaffolding Paths
19+ const libName = require ( '../package.json' ) . name ;
20+ const rootFolder = path . join ( __dirname ) ;
21+ const compilationFolder = path . join ( rootFolder , 'out-tsc' ) ;
22+ const srcFolder = path . join ( rootFolder , '../lib' ) ;
23+ const distFolder = path . join ( rootFolder , '../dist' ) ;
24+ const tempLibFolder = path . join ( compilationFolder , 'lib' ) ;
25+ const es5OutputFolder = path . join ( compilationFolder , 'lib-es5' ) ;
26+ const es2015OutputFolder = path . join ( compilationFolder , 'lib-es2015' ) ;
27+
28+ return Promise . resolve ( )
29+ . then ( ( ) =>
30+ // Copy library to temporary folder and inline html/css.
31+ relativeCopy ( `**/*` , srcFolder , tempLibFolder )
32+ . then ( ( ) => inlineResources ( tempLibFolder ) )
33+ . then ( ( ) => console . log ( 'Inlining succeeded.' ) )
34+ )
35+ . then ( ( ) =>
36+ // TypeScript Compilation
37+ Promise . all ( [
38+ ngc ( { project : `${ tempLibFolder } /tsconfig.lib.json` } ) , // Compile to ES2015.
39+ ngc ( { project : `${ tempLibFolder } /tsconfig.es5.json` } ) // Compile to ES5.
40+ ] )
41+ . then ( ( [ es2015ExitCode , es5ExitCode ] ) =>
42+ es2015ExitCode === 0 && es5ExitCode === 0 ?
43+ Promise . resolve ( ) : Promise . reject ( )
44+ )
45+ . then ( ( ) => console . log ( 'TypeScript compilation succeeded.' ) )
46+ )
47+ . then ( ( ) =>
48+ // Copy typings and metadata to `dist/` folder.
49+ relativeCopy ( '**/*.d.ts' , es2015OutputFolder , distFolder )
50+ . then ( ( ) => relativeCopy ( '**/*.metadata.json' , es2015OutputFolder , distFolder ) )
51+ . then ( ( ) => console . log ( 'Typings and metadata copy succeeded.' ) )
52+ )
53+ . then ( ( ) => {
54+ // Bundle lib.
55+
56+ // Base configuration.
57+ const es5Entry = path . join ( es5OutputFolder , `${ libName } .js` ) ;
58+ const es2015Entry = path . join ( es2015OutputFolder , `${ libName } .js` ) ;
59+
60+ const webpackBaseConfig = {
61+ output : {
62+ library : camelCase ( libName )
63+ } ,
64+ devtool : 'source-map' ,
65+ externals : [ / ^ @ a n g u l a r \/ / ]
66+ } ;
67+
68+ // UMD bundle.
69+ const umdConfig = Object . assign ( { } , webpackBaseConfig , {
70+ entry : es5Entry ,
71+ output : {
72+ path : path . join ( distFolder , 'bundles' ) ,
73+ filename : `${ libName } .umd.js` ,
74+ libraryTarget : 'umd'
75+ }
76+ } ) ;
77+
78+ // Minified UMD bundle.
79+ const minifiedUmdConfig = Object . assign ( { } , webpackBaseConfig , {
80+ entry : es5Entry ,
81+ output : {
82+ path : path . join ( distFolder , 'bundles' ) ,
83+ filename : `${ libName } .umd.min.js` ,
84+ libraryTarget : 'umd'
85+ } ,
86+ plugins : [
87+ new UglifyJsPlugin ( { sourceMap : true } )
88+ ]
89+ } ) ;
90+
91+ // ESM+ES5 flat module bundle.
92+ const fesm5config = Object . assign ( { } , webpackBaseConfig , {
93+ entry : es5Entry ,
94+ output : {
95+ path : path . join ( distFolder ) ,
96+ filename : `${ libName } .es5.js` ,
97+ }
98+ } ) ;
99+
100+ // ESM+ES2015 flat module bundle.
101+ const fesm2015config = Object . assign ( { } , webpackBaseConfig , {
102+ entry : es2015Entry ,
103+ output : {
104+ path : path . join ( distFolder ) ,
105+ filename : `${ libName } .js` ,
106+ }
107+ } ) ;
108+
109+ const allBundles = [
110+ umdConfig ,
111+ minifiedUmdConfig ,
112+ fesm5config ,
113+ fesm2015config
114+ ]
115+
116+ return new Promise ( ( resolve , reject ) => {
117+ webpack ( allBundles , ( err , stats ) => {
118+ if ( err ) {
119+ reject ( err ) ;
120+ }
121+
122+ resolve ( stats ) ;
123+ } ) ;
124+ } )
125+ . then ( ( ) => console . log ( 'All bundles generated successfully.' ) )
126+ } )
127+ . then ( ( ) =>
128+ // Copy package files
129+ relativeCopy ( 'LICENSE' , rootFolder , distFolder )
130+ . then ( ( ) => relativeCopy ( 'package.json' , rootFolder , distFolder ) )
131+ . then ( ( ) => relativeCopy ( 'README.md' , rootFolder , distFolder ) )
132+ . then ( ( ) => console . log ( 'Package files copy succeeded.' ) )
133+ )
134+ . catch ( err => {
135+ console . error ( '\Build failed. See below for errors.\n' ) ;
136+ console . error ( err ) ;
137+ process . exit ( 1 ) ;
138+ } ) ;
0 commit comments