1 | import sade from 'sade';
|
2 | let { version } = require('../package.json');
|
3 |
|
4 | const toArray = val => (Array.isArray(val) ? val : val == null ? [] : [val]);
|
5 |
|
6 | export default handler => {
|
7 | const ENABLE_MODERN = process.env.MICROBUNDLE_MODERN !== 'false';
|
8 |
|
9 | const DEFAULT_FORMATS = ENABLE_MODERN ? 'modern,es,cjs,umd' : 'es,cjs,umd';
|
10 |
|
11 | const cmd = type => (str, opts) => {
|
12 | opts.watch = opts.watch || type === 'watch';
|
13 |
|
14 | opts.entries = toArray(str || opts.entry).concat(opts._);
|
15 |
|
16 | if (opts.compress != null) {
|
17 |
|
18 | if (typeof opts.compress !== 'boolean') {
|
19 | opts.compress = opts.compress !== 'false' && opts.compress !== '0';
|
20 | }
|
21 | } else {
|
22 |
|
23 | opts.compress = opts.target !== 'node';
|
24 | }
|
25 |
|
26 | handler(opts);
|
27 | };
|
28 |
|
29 | let prog = sade('microbundle');
|
30 |
|
31 | prog
|
32 | .version(version)
|
33 | .option('--entry, -i', 'Entry module(s)')
|
34 | .option('--output, -o', 'Directory to place build files into')
|
35 | .option(
|
36 | '--format, -f',
|
37 | `Only build specified formats (any of ${DEFAULT_FORMATS} or iife)`,
|
38 | DEFAULT_FORMATS,
|
39 | )
|
40 | .option('--watch, -w', 'Rebuilds on any change', false)
|
41 | .option(
|
42 | '--pkg-main',
|
43 | 'Outputs files analog to package.json main entries',
|
44 | true,
|
45 | )
|
46 | .option('--target', 'Specify your target environment (node or web)', 'web')
|
47 | .option('--external', `Specify external dependencies, or 'none'`)
|
48 | .option('--globals', `Specify globals dependencies, or 'none'`)
|
49 | .example('microbundle --globals react=React,jquery=$')
|
50 | .option('--define', 'Replace constants with hard-coded values')
|
51 | .example('microbundle --define API_KEY=1234')
|
52 | .option('--alias', `Map imports to different modules`)
|
53 | .example('microbundle --alias react=preact')
|
54 | .option('--compress', 'Compress output using Terser', null)
|
55 | .option('--strict', 'Enforce undefined global context and add "use strict"')
|
56 | .option('--name', 'Specify name exposed in UMD builds')
|
57 | .option('--cwd', 'Use an alternative working directory', '.')
|
58 | .option('--sourcemap', 'Generate source map', true)
|
59 | .option(
|
60 | '--css-modules',
|
61 | 'Turns on css-modules for all .css imports. Passing a string will override the scopeName. eg --css-modules="_[hash]"',
|
62 | null,
|
63 | )
|
64 | .example("microbundle --no-sourcemap # don't generate sourcemaps")
|
65 | .option('--raw', 'Show raw byte size', false)
|
66 | .option(
|
67 | '--jsx',
|
68 | 'A custom JSX pragma like React.createElement (default: h)',
|
69 | )
|
70 | .option('--tsconfig', 'Specify the path to a custom tsconfig.json')
|
71 | .example('microbundle build --tsconfig tsconfig.build.json');
|
72 |
|
73 | prog
|
74 | .command('build [...entries]', '', { default: true })
|
75 | .describe('Build once and exit')
|
76 | .action(cmd('build'));
|
77 |
|
78 | prog
|
79 | .command('watch [...entries]')
|
80 | .describe('Rebuilds on any change')
|
81 | .action(cmd('watch'));
|
82 |
|
83 |
|
84 | return argv =>
|
85 | prog.parse(argv, {
|
86 | alias: {
|
87 | o: ['output', 'd'],
|
88 | i: ['entry', 'entries', 'e'],
|
89 | w: ['watch'],
|
90 | },
|
91 | });
|
92 | };
|