1 | import Fs from 'fs';
|
2 | import Util from 'util';
|
3 | import Rollup from 'rollup';
|
4 | import Babel from '@babel/core';
|
5 | import Package from './package.json';
|
6 |
|
7 |
|
8 | const WriteFile = Util.promisify(Fs.writeFile);
|
9 |
|
10 | const header = `/*
|
11 | Name: ${Package.name}
|
12 | Version: ${Package.version}
|
13 | License: ${Package.license}
|
14 | Author: ${Package.author}
|
15 | Email: ${Package.email}
|
16 | This Source Code Form is subject to the terms of the Mozilla Public
|
17 | License, v. 2.0. If a copy of the MPL was not distributed with this
|
18 | file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
19 | */
|
20 | `;
|
21 |
|
22 | const main = async function (input, output, name, option) {
|
23 |
|
24 | if (!name) return console.error('name required');
|
25 | if (!input) return console.error('input path required');
|
26 | if (!output) return console.error('output path required');
|
27 |
|
28 | const bundled = await Rollup.rollup({ input });
|
29 |
|
30 | const generated = await bundled.generate({
|
31 | name: `${name[0].toUpperCase()}${name.slice(1).toLowerCase()}`,
|
32 | indent: '\t',
|
33 |
|
34 | format: 'umd',
|
35 | treeshake: true
|
36 | });
|
37 |
|
38 | const code = generated.output[0].code;
|
39 |
|
40 | const options = {
|
41 | moduleId: `${name[0].toUpperCase()}${name.slice(1).toLowerCase()}`,
|
42 | comments: false,
|
43 | sourceMaps: false,
|
44 | plugins: [
|
45 | [ 'module:fast-async', {
|
46 | spec: true
|
47 | } ]
|
48 | ],
|
49 | presets: [
|
50 | [ '@babel/preset-env', {
|
51 | modules: false,
|
52 | targets: { ie: '11' },
|
53 | exclude: [
|
54 | 'transform-regenerator',
|
55 | 'transform-async-to-generator',
|
56 | 'proposal-async-generator-functions'
|
57 | ]
|
58 | } ]
|
59 | ]
|
60 | };
|
61 |
|
62 | if (option.includes('m')) {
|
63 | options.minified = true;
|
64 | }
|
65 |
|
66 | const result = Babel.transform(code, options);
|
67 |
|
68 | await WriteFile(`./${output}`, header + result.code);
|
69 |
|
70 | };
|
71 |
|
72 | if (process.argv.length > 2) {
|
73 | const args = [];
|
74 | const opt = [];
|
75 |
|
76 | process.argv.slice(2).forEach(function (arg) {
|
77 | if (arg[0] === '-') {
|
78 | opt.push.apply(opt, arg.slice(1).split());
|
79 | } else {
|
80 | args.push(arg);
|
81 | }
|
82 | });
|
83 |
|
84 | args.push(opt);
|
85 |
|
86 | main.apply(null, args, opt).catch(console.error);
|
87 | }
|
88 |
|
89 | export default main;
|