1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const fs = require("fs");
|
5 | const lodash_1 = require("lodash");
|
6 | const os_1 = require("os");
|
7 | const path_1 = require("path");
|
8 | const rollup_1 = require("rollup");
|
9 | const rollupTsPlugin$ = require("rollup-plugin-typescript2");
|
10 | const uglify_es_1 = require("uglify-es");
|
11 | const yargs = require("yargs");
|
12 | const mkTsconfig_1 = require("../../fns/mkTsconfig");
|
13 | const BuildTarget_1 = require("../../interfaces/BuildTarget");
|
14 | const cli_serialiser_1 = require("../cli-serialiser");
|
15 | const Log_1 = require("../Log");
|
16 | const tmp_1 = require("../tmp");
|
17 | function coerce(v) {
|
18 | return cli_serialiser_1.CLISerialiser.unserialise(v);
|
19 | }
|
20 | const argv = yargs
|
21 | .option('opts', {
|
22 | coerce,
|
23 | demandOption: true,
|
24 | type: 'string'
|
25 | })
|
26 | .option('dist', {
|
27 | demandOption: true,
|
28 | type: 'string'
|
29 | })
|
30 | .option('ignored-externals', {
|
31 | coerce,
|
32 | demandOption: true,
|
33 | type: 'string'
|
34 | })
|
35 | .option('tsconfig', {
|
36 | coerce,
|
37 | demandOption: true,
|
38 | type: 'string'
|
39 | })
|
40 | .option('formats', {
|
41 | demandOption: true,
|
42 | type: 'array'
|
43 | })
|
44 | .argv;
|
45 | delete argv.opts.output.format;
|
46 | delete argv.opts.output.dir;
|
47 | delete argv.opts.output.file;
|
48 | const rollupTsPlugin = rollupTsPlugin$;
|
49 | const incUmd = argv.formats.includes(BuildTarget_1.BuildTarget.UMD);
|
50 | const inc5 = argv.formats.includes(BuildTarget_1.BuildTarget.FESM5);
|
51 | function mkRollupTsConfig(overrides) {
|
52 | return {
|
53 | cacheRoot: tmp_1.tmp.dirSync({ unsafeCleanup: true }).name,
|
54 | tsconfigOverride: lodash_1.merge(lodash_1.cloneDeep(argv.tsconfig), mkTsconfig_1.mkTsconfig(overrides), {
|
55 | compilerOptions: {
|
56 | module: 'es2015'
|
57 | }
|
58 | })
|
59 | };
|
60 | }
|
61 | const bundleDir = path_1.join(process.cwd(), argv.dist, '_bundle');
|
62 | (() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
63 | if (incUmd || inc5) {
|
64 | const baseOpts = lodash_1.cloneDeep(argv.opts);
|
65 | baseOpts.plugins.push(rollupTsPlugin(mkRollupTsConfig({
|
66 | compilerOptions: {
|
67 | target: 'es5'
|
68 | }
|
69 | })));
|
70 | if (incUmd) {
|
71 | const opts = lodash_1.cloneDeep(baseOpts);
|
72 |
|
73 | const nodeResolve = require('rollup-plugin-node-resolve');
|
74 | opts.plugins.push(nodeResolve());
|
75 | const unminifiedFile = path_1.join(bundleDir, 'umd.js');
|
76 | if (Array.isArray(opts.external) && opts.external.includes('tslib')) {
|
77 | Log_1.Log.info(`Removing ${argv.ignoredExternals.join(', ')} from UMD bundle's externals`);
|
78 | lodash_1.pull(opts.external, ...argv.ignoredExternals);
|
79 | }
|
80 | Log_1.Log.info('Compiling UMD');
|
81 | const rolled = yield rollup_1.rollup(opts);
|
82 | Log_1.Log.info('Writing UMD');
|
83 | yield rolled.write(lodash_1.merge(lodash_1.cloneDeep(opts.output), {
|
84 | file: unminifiedFile,
|
85 | format: 'umd'
|
86 | }));
|
87 | Log_1.Log.info('Minifying UMD');
|
88 | const uglified = uglify_es_1.minify(fs.readFileSync(unminifiedFile, 'utf8'));
|
89 | if (uglified.error) {
|
90 | throw uglified.error;
|
91 | }
|
92 | else {
|
93 | let output = uglified.code;
|
94 | if (opts.output.banner) {
|
95 | output = opts.output.banner + output;
|
96 | }
|
97 | fs.writeFileSync(path_1.join(bundleDir, 'umd.min.js'), output);
|
98 | }
|
99 | Log_1.Log.success('Finished UMD');
|
100 | }
|
101 | if (inc5) {
|
102 | Log_1.Log.info('Compiling FESM5');
|
103 | const rolled = yield rollup_1.rollup(baseOpts);
|
104 | Log_1.Log.info('Writing FESM5');
|
105 | yield rolled.write(lodash_1.merge(lodash_1.cloneDeep(baseOpts.output), {
|
106 | file: path_1.join(bundleDir, 'fesm5.js'),
|
107 | format: 'esm'
|
108 | }));
|
109 | Log_1.Log.success('Finished FESM5');
|
110 | }
|
111 | }
|
112 | else if (argv.formats.includes(BuildTarget_1.BuildTarget.FESM2015)) {
|
113 | Log_1.Log.info('Compiling FESM2015');
|
114 | const opts = lodash_1.cloneDeep(argv.opts);
|
115 | opts.plugins.push(rollupTsPlugin(mkRollupTsConfig({
|
116 | compilerOptions: {
|
117 | target: 'esnext'
|
118 | }
|
119 | })));
|
120 | const rolled = yield rollup_1.rollup(opts);
|
121 | Log_1.Log.info('Writing FESM2015');
|
122 | yield rolled.write(lodash_1.merge(lodash_1.cloneDeep(opts.output), {
|
123 | file: path_1.join(bundleDir, 'fesm2015.js'),
|
124 | format: 'esm'
|
125 | }));
|
126 | Log_1.Log.success('Finished FESM2015');
|
127 | }
|
128 | }))().catch((e) => {
|
129 | process.stderr.write((e.stack || e.toString() || e.message) + os_1.EOL, () => {
|
130 | process.exit(1);
|
131 | });
|
132 | });
|