-
Notifications
You must be signed in to change notification settings - Fork 794
/
cli.ts
89 lines (80 loc) · 2.87 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import rollupCommonjs from '@rollup/plugin-commonjs';
import rollupJson from '@rollup/plugin-json';
import rollupResolve from '@rollup/plugin-node-resolve';
import fs from 'fs-extra';
import { join } from 'path';
import { OutputOptions, RollupOptions } from 'rollup';
import sourcemaps from 'rollup-plugin-sourcemaps';
import { getBanner } from '../utils/banner';
import { BuildOptions } from '../utils/options';
import { writePkgJson } from '../utils/write-pkg-json';
import { aliasPlugin } from './plugins/alias-plugin';
import { relativePathPlugin } from './plugins/relative-path-plugin';
import { replacePlugin } from './plugins/replace-plugin';
/**
* Generates a rollup configuration for the `cli` submodule
* @param opts build options needed to generate the rollup configuration
* @returns an array containing the generated rollup options
*/
export async function cli(opts: BuildOptions): Promise<ReadonlyArray<RollupOptions>> {
const inputDir = join(opts.buildDir, 'cli');
const outputDir = opts.output.cliDir;
const esmFilename = 'index.js';
const cjsFilename = 'index.cjs';
const dtsFilename = 'index.d.ts';
const esOutput: OutputOptions = {
format: 'es',
file: join(outputDir, esmFilename),
preferConst: true,
sourcemap: true,
banner: getBanner(opts, `Stencil CLI`, true),
};
const cjsOutput: OutputOptions = {
format: 'cjs',
file: join(outputDir, cjsFilename),
preferConst: true,
sourcemap: true,
banner: getBanner(opts, `Stencil CLI (CommonJS)`, true),
};
// create public d.ts
let dts = await fs.readFile(join(inputDir, 'public.d.ts'), 'utf8');
dts = dts.replace('@stencil/core/internal', '../internal/index');
await fs.writeFile(join(opts.output.cliDir, dtsFilename), dts);
// copy config-flags.d.ts
let configDts = await fs.readFile(join(inputDir, 'config-flags.d.ts'), 'utf8');
configDts = configDts.replace('@stencil/core/declarations', '../internal/index');
await fs.writeFile(join(opts.output.cliDir, 'config-flags.d.ts'), configDts);
// write @stencil/core/compiler/package.json
writePkgJson(opts, opts.output.cliDir, {
name: '@stencil/core/cli',
description: 'Stencil CLI.',
main: cjsFilename,
module: esmFilename,
types: dtsFilename,
});
const cliBundle: RollupOptions = {
input: join(inputDir, 'index.js'),
output: [esOutput, cjsOutput],
external: ['path'],
plugins: [
relativePathPlugin('@stencil/core/testing', '../testing/index.js'),
relativePathPlugin('prompts', '../sys/node/prompts.js'),
aliasPlugin(opts),
rollupResolve({
preferBuiltins: true,
}),
rollupCommonjs(),
rollupJson({
preferConst: true,
}),
replacePlugin(opts),
sourcemaps(),
],
treeshake: {
moduleSideEffects: false,
propertyReadSideEffects: false,
unknownGlobalSideEffects: false,
},
};
return [cliBundle];
}