|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const glob = require('glob'); |
| 6 | +const camelCase = require('camelcase'); |
| 7 | +const ngc = require('@angular/compiler-cli/src/main').main; |
| 8 | +const rollup = require('rollup'); |
| 9 | +const uglify = require('rollup-plugin-uglify'); |
| 10 | +const sourcemaps = require('rollup-plugin-sourcemaps'); |
| 11 | +const nodeResolve = require('rollup-plugin-node-resolve'); |
| 12 | +const commonjs = require('rollup-plugin-commonjs'); |
| 13 | + |
| 14 | +const inlineResources = require('./inline-resources'); |
| 15 | + |
| 16 | +const libName = require('./package.json').name.replace('@thisissoon/', ''); |
| 17 | +const rootFolder = path.join(__dirname); |
| 18 | +const compilationFolder = path.join(rootFolder, 'out-tsc'); |
| 19 | +const srcFolder = path.join(rootFolder, 'src/lib'); |
| 20 | +const distFolder = path.join(rootFolder, 'dist'); |
| 21 | +const tempLibFolder = path.join(compilationFolder, 'lib'); |
| 22 | +const es5OutputFolder = path.join(compilationFolder, 'lib-es5'); |
| 23 | +const es2015OutputFolder = path.join(compilationFolder, 'lib-es2015'); |
| 24 | + |
| 25 | +return Promise.resolve() |
| 26 | + // Copy library to temporary folder and inline html/css. |
| 27 | + .then(() => _relativeCopy(`**/*`, srcFolder, tempLibFolder) |
| 28 | + .then(() => inlineResources(tempLibFolder)) |
| 29 | + .then(() => console.log('Inlining succeeded.')) |
| 30 | + ) |
| 31 | + // Compile to ES2015. |
| 32 | + .then(() => ngc({ project: `${tempLibFolder}/tsconfig.lib.json` }) |
| 33 | + .then(exitCode => exitCode === 0 ? Promise.resolve() : Promise.reject()) |
| 34 | + .then(() => console.log('ES2015 compilation succeeded.')) |
| 35 | + ) |
| 36 | + // Compile to ES5. |
| 37 | + .then(() => ngc({ project: `${tempLibFolder}/tsconfig.es5.json` }) |
| 38 | + .then(exitCode => exitCode === 0 ? Promise.resolve() : Promise.reject()) |
| 39 | + .then(() => console.log('ES5 compilation succeeded.')) |
| 40 | + ) |
| 41 | + // Copy typings and metadata to `dist/` folder. |
| 42 | + .then(() => Promise.resolve() |
| 43 | + .then(() => _relativeCopy('**/*.d.ts', es2015OutputFolder, distFolder)) |
| 44 | + .then(() => _relativeCopy('**/*.metadata.json', es2015OutputFolder, distFolder)) |
| 45 | + .then(() => console.log('Typings and metadata copy succeeded.')) |
| 46 | + ) |
| 47 | + // Bundle lib. |
| 48 | + .then(() => { |
| 49 | + // Base configuration. |
| 50 | + const es5Entry = path.join(es5OutputFolder, `${libName}.js`); |
| 51 | + const es2015Entry = path.join(es2015OutputFolder, `${libName}.js`); |
| 52 | + const rollupBaseConfig = { |
| 53 | + moduleName: camelCase(libName), |
| 54 | + sourceMap: true, |
| 55 | + // ATTENTION: |
| 56 | + // Add any dependency or peer dependency your library to `globals` and `external`. |
| 57 | + // This is required for UMD bundle users. |
| 58 | + globals: { |
| 59 | + // The key here is library name, and the value is the the name of the global variable name |
| 60 | + // the window object. |
| 61 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#globals for more. |
| 62 | + '@angular/core': 'ng.core' |
| 63 | + }, |
| 64 | + external: [ |
| 65 | + // List of dependencies |
| 66 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#external for more. |
| 67 | + '@angular/core' |
| 68 | + ], |
| 69 | + plugins: [ |
| 70 | + commonjs({ |
| 71 | + include: ['node_modules/rxjs/**'] |
| 72 | + }), |
| 73 | + sourcemaps(), |
| 74 | + nodeResolve({ jsnext: true, module: true }) |
| 75 | + ] |
| 76 | + }; |
| 77 | + |
| 78 | + // UMD bundle. |
| 79 | + const umdConfig = Object.assign({}, rollupBaseConfig, { |
| 80 | + entry: es5Entry, |
| 81 | + dest: path.join(distFolder, `bundles`, `${libName}.umd.js`), |
| 82 | + format: 'umd', |
| 83 | + }); |
| 84 | + |
| 85 | + // Minified UMD bundle. |
| 86 | + const minifiedUmdConfig = Object.assign({}, rollupBaseConfig, { |
| 87 | + entry: es5Entry, |
| 88 | + dest: path.join(distFolder, `bundles`, `${libName}.umd.min.js`), |
| 89 | + format: 'umd', |
| 90 | + plugins: rollupBaseConfig.plugins.concat([uglify({})]) |
| 91 | + }); |
| 92 | + |
| 93 | + // ESM+ES5 flat module bundle. |
| 94 | + const fesm5config = Object.assign({}, rollupBaseConfig, { |
| 95 | + entry: es5Entry, |
| 96 | + dest: path.join(distFolder, `${libName}.es5.js`), |
| 97 | + format: 'es' |
| 98 | + }); |
| 99 | + |
| 100 | + // ESM+ES2015 flat module bundle. |
| 101 | + const fesm2015config = Object.assign({}, rollupBaseConfig, { |
| 102 | + entry: es2015Entry, |
| 103 | + dest: path.join(distFolder, `${libName}.js`), |
| 104 | + format: 'es' |
| 105 | + }); |
| 106 | + |
| 107 | + const allBundles = [ |
| 108 | + umdConfig, |
| 109 | + minifiedUmdConfig, |
| 110 | + fesm5config, |
| 111 | + fesm2015config |
| 112 | + ].map(cfg => rollup.rollup(cfg).then(bundle => bundle.write(cfg))); |
| 113 | + |
| 114 | + return Promise.all(allBundles) |
| 115 | + .then(() => console.log('All bundles generated successfully.')) |
| 116 | + }) |
| 117 | + // Copy package files |
| 118 | + .then(() => Promise.resolve() |
| 119 | + .then(() => _relativeCopy('LICENSE', rootFolder, distFolder)) |
| 120 | + .then(() => _relativeCopy('package.json', rootFolder, distFolder)) |
| 121 | + .then(() => _relativeCopy('README.md', rootFolder, distFolder)) |
| 122 | + .then(() => console.log('Package files copy succeeded.')) |
| 123 | + ) |
| 124 | + .catch(e => { |
| 125 | + console.error('\Build failed. See below for errors.\n'); |
| 126 | + console.error(e); |
| 127 | + process.exit(1); |
| 128 | + }); |
| 129 | + |
| 130 | + |
| 131 | +// Copy files maintaining relative paths. |
| 132 | +function _relativeCopy(fileGlob, from, to) { |
| 133 | + return new Promise((resolve, reject) => { |
| 134 | + glob(fileGlob, { cwd: from, nodir: true }, (err, files) => { |
| 135 | + if (err) reject(err); |
| 136 | + files.forEach(file => { |
| 137 | + const origin = path.join(from, file); |
| 138 | + const dest = path.join(to, file); |
| 139 | + const data = fs.readFileSync(origin, 'utf-8'); |
| 140 | + _recursiveMkDir(path.dirname(dest)); |
| 141 | + fs.writeFileSync(dest, data); |
| 142 | + resolve(); |
| 143 | + }) |
| 144 | + }) |
| 145 | + }); |
| 146 | +} |
| 147 | + |
| 148 | +// Recursively create a dir. |
| 149 | +function _recursiveMkDir(dir) { |
| 150 | + if (!fs.existsSync(dir)) { |
| 151 | + _recursiveMkDir(path.dirname(dir)); |
| 152 | + fs.mkdirSync(dir); |
| 153 | + } |
| 154 | +} |
0 commit comments