1 | import { dirname, extname, join as joinPath } from "path";
|
2 | import { Plugin, RollupOptions } from "rollup";
|
3 |
|
4 | import { createBrowserConfig, createModuleConfig } from "./create-config";
|
5 | import { AnalizedPkg } from "./pkg";
|
6 | import renameMin from "./rename-min";
|
7 | import resolvePath from "./resolve";
|
8 |
|
9 | import babel from "rollup-plugin-babel";
|
10 | import buble from "rollup-plugin-buble";
|
11 | import commonjs from "rollup-plugin-commonjs";
|
12 | import exportEquals from "rollup-plugin-export-equals";
|
13 | import json from "rollup-plugin-json";
|
14 | import nodeResolve from "rollup-plugin-node-resolve";
|
15 | import { terser } from "rollup-plugin-terser";
|
16 | import ts2 from "rollup-plugin-typescript2";
|
17 |
|
18 | const pkgToConfigs = (
|
19 | {
|
20 | cwd,
|
21 | input: apiInput,
|
22 | output,
|
23 | minify,
|
24 | dependencies,
|
25 | browser: browserOptions,
|
26 | options,
|
27 | }: AnalizedPkg,
|
28 | dev?: boolean,
|
29 | ): RollupOptions[] => {
|
30 |
|
31 | const apiFolder = dirname(apiInput);
|
32 |
|
33 | const {
|
34 | main: cjsOutputFile,
|
35 | module: esOutputFile,
|
36 | browser: browserOutputFile,
|
37 | types: typesOutputFile,
|
38 | } = output;
|
39 |
|
40 | const {
|
41 | builtin: builtinDependencies,
|
42 | runtime: runtimeDependencies,
|
43 | peer: peerDependencies,
|
44 | } = dependencies;
|
45 |
|
46 | const {
|
47 | sourcemap,
|
48 | esModule,
|
49 | interop,
|
50 | extend,
|
51 | equals,
|
52 | } = options;
|
53 |
|
54 | const {
|
55 | format: browserFormat,
|
56 | name: pkgName,
|
57 | id,
|
58 | globals,
|
59 | } = browserOptions;
|
60 |
|
61 | const prod = !dev;
|
62 |
|
63 | const configs: RollupOptions[] = [];
|
64 |
|
65 | let typesOutputDir = typesOutputFile;
|
66 | if (typesOutputDir && extname(typesOutputDir) === ".ts") {
|
67 | typesOutputDir = dirname(typesOutputDir);
|
68 | }
|
69 |
|
70 | const modulePlugins = (mini: boolean): Array<Plugin | null | false> => {
|
71 |
|
72 | const declarationDir = !configs.length && typesOutputDir;
|
73 | const srcFolderContent = resolvePath("**/*.ts", apiFolder);
|
74 |
|
75 | return [
|
76 |
|
77 | ts2({
|
78 | include: srcFolderContent,
|
79 | cacheRoot: resolvePath(".cache/rpt2", cwd),
|
80 | useTsconfigDeclarationDir: true,
|
81 | tsconfigDefaults: {
|
82 | include: [
|
83 | srcFolderContent,
|
84 | ],
|
85 | exclude: [],
|
86 | },
|
87 | tsconfigOverride: {
|
88 | compilerOptions: {
|
89 | target: "esnext",
|
90 | module: "esnext",
|
91 | moduleResolution: "node",
|
92 | sourceMap: sourcemap,
|
93 | declaration: !!declarationDir,
|
94 | declarationDir: declarationDir || "",
|
95 | emitDeclarationOnly: false,
|
96 | },
|
97 | },
|
98 | }),
|
99 |
|
100 | json() as Plugin,
|
101 |
|
102 | declarationDir && equals && exportEquals({
|
103 | file: resolvePath(joinPath(declarationDir, "index.d.ts"), cwd),
|
104 | }) || null,
|
105 |
|
106 | babel({
|
107 | extensions: [".ts", ".js"],
|
108 | exclude: /node_modules/,
|
109 | babelrc: false,
|
110 | plugins: [require.resolve("babel-plugin-transform-async-to-promises")],
|
111 | }),
|
112 |
|
113 | buble({
|
114 | exclude: /node_modules/,
|
115 | target: {
|
116 | node: 0.12,
|
117 | },
|
118 | objectAssign: true,
|
119 | }) as Plugin,
|
120 |
|
121 | mini && terser({
|
122 | sourcemap,
|
123 | toplevel: true,
|
124 | module: true,
|
125 | }),
|
126 |
|
127 | ];
|
128 |
|
129 | };
|
130 |
|
131 | const browserPlugins = (mini: boolean) => [
|
132 |
|
133 | nodeResolve(),
|
134 | commonjs(),
|
135 |
|
136 | ...modulePlugins(mini),
|
137 |
|
138 | ];
|
139 |
|
140 | const external = [
|
141 | ...builtinDependencies,
|
142 | ...runtimeDependencies,
|
143 | ...peerDependencies,
|
144 | ];
|
145 |
|
146 | if (esOutputFile) {
|
147 |
|
148 | const config: RollupOptions = createModuleConfig(
|
149 | apiInput,
|
150 | "es",
|
151 | esOutputFile,
|
152 | sourcemap,
|
153 | true,
|
154 | false,
|
155 | external,
|
156 | modulePlugins(prod && !minify.module),
|
157 | );
|
158 |
|
159 | configs.push(config);
|
160 |
|
161 | if (minify.module) {
|
162 |
|
163 | const configMin: RollupOptions = createModuleConfig(
|
164 | apiInput,
|
165 | "es",
|
166 | renameMin(esOutputFile),
|
167 | sourcemap,
|
168 | true,
|
169 | false,
|
170 | external,
|
171 | modulePlugins(true),
|
172 | );
|
173 |
|
174 | configs.push(configMin);
|
175 |
|
176 | }
|
177 |
|
178 | }
|
179 |
|
180 | if (cjsOutputFile) {
|
181 |
|
182 | const config: RollupOptions = createModuleConfig(
|
183 | apiInput,
|
184 | "cjs",
|
185 | cjsOutputFile,
|
186 | sourcemap,
|
187 | esModule,
|
188 | interop,
|
189 | external,
|
190 | modulePlugins(prod && !minify.main),
|
191 | );
|
192 |
|
193 | configs.push(config);
|
194 |
|
195 | if (minify.main) {
|
196 |
|
197 | const configMin: RollupOptions = createModuleConfig(
|
198 | apiInput,
|
199 | "cjs",
|
200 | renameMin(cjsOutputFile),
|
201 | sourcemap,
|
202 | esModule,
|
203 | interop,
|
204 | external,
|
205 | modulePlugins(true),
|
206 | );
|
207 |
|
208 | configs.push(configMin);
|
209 |
|
210 | }
|
211 |
|
212 | }
|
213 |
|
214 | if (!pkgName && browserOutputFile) {
|
215 | throw new Error("name option is required for IIFE and UMD builds");
|
216 | }
|
217 |
|
218 | if (browserOutputFile) {
|
219 |
|
220 | const config = createBrowserConfig(
|
221 | apiInput,
|
222 | browserFormat,
|
223 | browserOutputFile,
|
224 | sourcemap,
|
225 | esModule,
|
226 | interop,
|
227 | browserPlugins(prod && !minify.browser),
|
228 | pkgName as string,
|
229 | extend,
|
230 | globals || {},
|
231 | id,
|
232 | );
|
233 |
|
234 | configs.push(config);
|
235 |
|
236 | if (minify.browser) {
|
237 |
|
238 | const configMin = createBrowserConfig(
|
239 | apiInput,
|
240 | browserFormat,
|
241 | renameMin(browserOutputFile),
|
242 | sourcemap,
|
243 | esModule,
|
244 | interop,
|
245 | browserPlugins(true),
|
246 | pkgName as string,
|
247 | extend,
|
248 | globals || {},
|
249 | id,
|
250 | );
|
251 |
|
252 | configs.push(configMin);
|
253 |
|
254 | }
|
255 |
|
256 | }
|
257 |
|
258 | return configs;
|
259 |
|
260 | };
|
261 |
|
262 | export default pkgToConfigs;
|