1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var readPkg = _interopDefault(require('read-pkg'));
|
8 | var camelcase = _interopDefault(require('camelcase'));
|
9 | var path = require('path');
|
10 | var builtinModules = _interopDefault(require('builtin-modules'));
|
11 | var fs = require('fs');
|
12 | var resolveFrom = _interopDefault(require('resolve-from'));
|
13 | var slash = _interopDefault(require('slash'));
|
14 |
|
15 | function error(msg) {
|
16 | return new TypeError(msg);
|
17 | }
|
18 | function invalidPkgField(field, type) {
|
19 | return error(`Invalid package.json "${field}" field. It has to be of type ${type}`);
|
20 | }
|
21 | function invalidOption(option, type) {
|
22 | return error(`Invalid "${option}" option. It has to be ${type}`);
|
23 | }
|
24 |
|
25 | const keys = Object.keys;
|
26 | function keysOrNull(deps) {
|
27 | return deps ? keys(deps) : null;
|
28 | }
|
29 | function setProp(name, value, target) {
|
30 | target[name] = value;
|
31 | return target;
|
32 | }
|
33 | function createObject(names, value) {
|
34 | return names.reduce((result, key) => setProp(key, value, result), {});
|
35 | }
|
36 |
|
37 | function isNull(value) {
|
38 | return value == null;
|
39 | }
|
40 | function isObject(value) {
|
41 | return !!value && typeof value === 'object';
|
42 | }
|
43 | function isString(value) {
|
44 | return typeof value === 'string';
|
45 | }
|
46 | function isBool(value) {
|
47 | return value === !!value;
|
48 | }
|
49 | const isArray = Array.isArray;
|
50 | function isDictionary(value) {
|
51 | return isObject(value) && !isArray(value);
|
52 | }
|
53 | function isStringOrNull(value) {
|
54 | return isNull(value) || isString(value);
|
55 | }
|
56 | function isDictionaryOrNull(value) {
|
57 | return isNull(value) || isDictionary(value);
|
58 | }
|
59 |
|
60 | function createInList(...model) {
|
61 | return str => model.some(inList => typeof inList === 'function' ? inList(str) : str === inList);
|
62 | }
|
63 |
|
64 | const isModuleOptionKey = createInList('sourcemap', 'min');
|
65 |
|
66 | const isCJSOptionKey = createInList(isModuleOptionKey, 'esModule', 'interop');
|
67 |
|
68 | const isBrowserOption = createInList(isCJSOptionKey, 'format', 'name', 'id', 'extend', 'globals');
|
69 |
|
70 | const isModuleString = createInList('main', 'browser', 'bin');
|
71 | function isModuleOption(value) {
|
72 | return isNull(value) || isBool(value) || isModuleString(value) || isArray(value) && value.every(isModuleString);
|
73 | }
|
74 | function normalizeModuleOption(option) {
|
75 | const keys = ['main', 'browser', 'bin'];
|
76 | return !option ? createObject(keys, false) : option === true ? createObject(keys, true) : isArray(option) ? option.reduce((result, field) => setProp(field, true, result), createObject(keys, false)) : setProp(option, true, createObject(keys, false));
|
77 | }
|
78 | function normalizeBuildModule(build, key, field, def) {
|
79 | return !build || isNull(build[key]) ? def[field] : build[key];
|
80 | }
|
81 |
|
82 | function normalizeBuildFlag(build, key, def) {
|
83 | return !build || isNull(build[key]) ? def : !!build[key];
|
84 | }
|
85 |
|
86 | const isBrowserFormat = createInList(isNull, 'iife', 'amd', 'umd');
|
87 |
|
88 | function isValidGlobals(value) {
|
89 | return isNull(value) || isObject(value) && (isArray(value) ? value.every(item => isString(item)) : keys(value).every(key => isString(key) && isString(value[key])));
|
90 | }
|
91 | function normalizeGlobals(globals) {
|
92 | return !globals ? null : isArray(globals) ? globals.reduce((result, value) => setProp(value, value, result), {}) : globals;
|
93 | }
|
94 | function normalizeBuildGlobals(build, def) {
|
95 | return !build || isNull(build.globals) ? def : normalizeGlobals(build.globals);
|
96 | }
|
97 |
|
98 | const isInOpKey = createInList('api', 'bin');
|
99 |
|
100 | const isMinString = createInList(isModuleString, 'module');
|
101 | function isValidMinOption(value) {
|
102 | return isNull(value) || isBool(value) || isMinString(value) || isArray(value) && value.every(item => isMinString(item));
|
103 | }
|
104 | function normalizeMinOption(min) {
|
105 | const keys = ['main', 'module', 'browser', 'bin'];
|
106 | return !min ? createObject(keys, false) : min === true ? createObject(keys, true) : isArray(min) ? min.reduce((result, field) => setProp(field, true, result), createObject(keys, false)) : setProp(min, true, createObject(keys, false));
|
107 | }
|
108 | function normalizeBuildMin(build, field, def) {
|
109 | return !build || isNull(build.min) ? def[field] : build.min;
|
110 | }
|
111 |
|
112 | function normalizeBuildName(cwd, browserName, nameOption, pkgName) {
|
113 | return browserName || nameOption || pkgName && camelcase(path.basename(pkgName)) || camelcase(path.basename(cwd)) || null;
|
114 | }
|
115 |
|
116 | function normalizeSourcemap(sourcemap) {
|
117 | return sourcemap === 'inline' ? 'inline' : sourcemap !== false;
|
118 | }
|
119 | function normalizeBuildSourcemap(build, def) {
|
120 | return !build || isNull(build.sourcemap) ? def : normalizeSourcemap(build.sourcemap);
|
121 | }
|
122 |
|
123 | const isTypesOptionKey = createInList('equals');
|
124 |
|
125 | function invalidKeys(object, list) {
|
126 | const invalid = keys(object).filter(key => !list.includes(key));
|
127 | return invalid.length ? invalid : null;
|
128 | }
|
129 | function keysInList(obj, inList) {
|
130 | return keys(obj).every(inList);
|
131 | }
|
132 |
|
133 | async function analizePkg(cwd, inputPkg) {
|
134 | const pkg = inputPkg || (await readPkg({
|
135 | cwd,
|
136 | normalize: false
|
137 | }));
|
138 |
|
139 | if (!isDictionary(pkg)) {
|
140 | throw error('Invalid package.json content');
|
141 | }
|
142 |
|
143 | const {
|
144 | name: pkgName,
|
145 | main: pkgMain,
|
146 | module: pkgModule,
|
147 | 'jsnext:main': pkgJsNextMain,
|
148 | browser: pkgBrowser,
|
149 | bin: pkgBin,
|
150 | types: pkgTypes,
|
151 | typings,
|
152 | dependencies: runtimeDependencies,
|
153 | devDependencies,
|
154 | peerDependencies,
|
155 | bundlib: bundlibOptions = {}
|
156 | } = pkg;
|
157 |
|
158 | if (!isDictionaryOrNull(bundlibOptions)) {
|
159 | throw invalidPkgField('bundlib', 'Object');
|
160 | }
|
161 |
|
162 | const invalidOptions = invalidKeys(bundlibOptions, ['input', 'extend', 'esModule', 'interop', 'equals', 'sourcemap', 'format', 'name', 'id', 'globals', 'min', 'cache', 'project', 'main', 'module', 'browser', 'bin', 'types']);
|
163 |
|
164 | if (invalidOptions) {
|
165 | const optionNames = invalidOptions.map(name => `"${name}"`).join(', ');
|
166 | throw error(`Unknown options found: (${optionNames})`);
|
167 | }
|
168 |
|
169 | const {
|
170 | input: inputOption,
|
171 | sourcemap: sourcemapOption,
|
172 | esModule,
|
173 | interop,
|
174 | extend,
|
175 | format: browserFormat,
|
176 | name: browserName,
|
177 | id: amdId,
|
178 | globals: browserGlobals,
|
179 | min,
|
180 | cache: cacheOption,
|
181 | project: projectOption,
|
182 | main: mainOptions,
|
183 | module: moduleOptions,
|
184 | browser: browserOptions,
|
185 | bin: binaryOptions,
|
186 | types: typesOptions
|
187 | } = bundlibOptions;
|
188 |
|
189 | if (!isStringOrNull(inputOption) && !(isDictionary(inputOption) && keysInList(inputOption, isInOpKey) && keys(inputOption).every(key => isString(inputOption[key])))) {
|
190 | throw invalidOption('input', 'string | { api?: string, bin?: string }');
|
191 | }
|
192 |
|
193 | if (!isNull(sourcemapOption) && !isBool(sourcemapOption) && sourcemapOption !== 'inline') {
|
194 | throw invalidOption('sourcemap', 'boolean | "inline"');
|
195 | }
|
196 |
|
197 | if (!isModuleOption(esModule)) {
|
198 | throw invalidOption('esModule', 'boolean | "main" | "browser" | "bin" | Array<"main" | "browser" | "bin">');
|
199 | }
|
200 |
|
201 | if (!isModuleOption(interop)) {
|
202 | throw invalidOption('interop', 'boolean | "main" | "browser" | "bin" | Array<"main" | "browser" | "bin">');
|
203 | }
|
204 |
|
205 | if (!isBrowserFormat(browserFormat)) {
|
206 | throw invalidOption('format', '"amd" | "iife" | "amd"');
|
207 | }
|
208 |
|
209 | if (!isStringOrNull(browserName)) {
|
210 | throw invalidOption('name', 'string');
|
211 | }
|
212 |
|
213 | if (!isStringOrNull(amdId)) {
|
214 | throw invalidOption('id', 'string');
|
215 | }
|
216 |
|
217 | if (!isValidGlobals(browserGlobals)) {
|
218 | throw invalidOption('globals', 'Object<string, string> | string[]');
|
219 | }
|
220 |
|
221 | if (!isValidMinOption(min)) {
|
222 | throw invalidOption('min', 'boolean | "main" | "module" | "browser" | "bin" | Array<"main" | "module" | "browser" | "bin">');
|
223 | }
|
224 |
|
225 | if (!isStringOrNull(cacheOption)) {
|
226 | throw invalidOption('cache', 'string');
|
227 | }
|
228 |
|
229 | if (!isStringOrNull(projectOption)) {
|
230 | throw invalidOption('project', 'string');
|
231 | }
|
232 |
|
233 | if (!isNull(mainOptions) && mainOptions !== false && !(isDictionary(mainOptions) && keysInList(mainOptions, isCJSOptionKey))) {
|
234 | throw invalidOption('main', 'false | { sourcemap?: boolean | "inline", esModule?: boolean, interop?: boolean, min?: boolean }');
|
235 | }
|
236 |
|
237 | if (!isNull(moduleOptions) && moduleOptions !== false && !(isDictionary(moduleOptions) && keysInList(moduleOptions, isModuleOptionKey))) {
|
238 | throw invalidOption('module', 'false | { sourcemap?: boolean | "inline", min?: boolean }');
|
239 | }
|
240 |
|
241 | if (!isNull(browserOptions) && browserOptions !== false && !(isDictionary(browserOptions) && keysInList(browserOptions, isBrowserOption) && isBrowserFormat(browserOptions.format) && ['name', 'id'].every(key => isStringOrNull(browserOptions[key])) && isValidGlobals(browserOptions.globals))) {
|
242 | throw invalidOption('browser', 'false | { sourcemap?: boolean | "inline", esModule?: boolean, interop?: boolean, min?: boolean, ... }');
|
243 | }
|
244 |
|
245 | if (!isNull(binaryOptions) && binaryOptions !== false && !(isDictionary(binaryOptions) && keysInList(binaryOptions, isCJSOptionKey))) {
|
246 | throw invalidOption('bin', 'false | { sourcemap?: boolean | "inline", esModule?: boolean, interop?: boolean, min?: boolean }');
|
247 | }
|
248 |
|
249 | if (!isNull(typesOptions) && typesOptions !== false && !(isDictionary(typesOptions) && keysInList(typesOptions, isTypesOptionKey))) {
|
250 | throw invalidOption('types', 'false | { equals?: boolean }');
|
251 | }
|
252 |
|
253 | if (mainOptions !== false && !isStringOrNull(pkgMain)) {
|
254 | throw invalidPkgField('main', 'string');
|
255 | }
|
256 |
|
257 | if (moduleOptions !== false && !isStringOrNull(pkgModule)) {
|
258 | throw invalidPkgField('module', 'string');
|
259 | }
|
260 |
|
261 | if (!pkgModule && moduleOptions !== false && !isStringOrNull(pkgJsNextMain)) {
|
262 | throw invalidPkgField('jsnext:main', 'string');
|
263 | }
|
264 |
|
265 | if (browserOptions !== false && !isStringOrNull(pkgBrowser)) {
|
266 | throw invalidPkgField('browser', 'string');
|
267 | }
|
268 |
|
269 | if (binaryOptions !== false && !isStringOrNull(pkgBin)) {
|
270 | throw invalidPkgField('bin', 'string');
|
271 | }
|
272 |
|
273 | if (!isDictionaryOrNull(runtimeDependencies)) {
|
274 | throw invalidPkgField('dependencies', 'Object');
|
275 | }
|
276 |
|
277 | if (!isDictionaryOrNull(peerDependencies)) {
|
278 | throw invalidPkgField('peerDependencies', 'Object');
|
279 | }
|
280 |
|
281 | const esModuleFile = pkgModule || pkgJsNextMain;
|
282 | const typesPath = pkgTypes || typings;
|
283 | const {
|
284 | api: apiInput,
|
285 | bin: binInput
|
286 | } = isStringOrNull(inputOption) ? {
|
287 | api: inputOption
|
288 | } : inputOption;
|
289 | const input = {
|
290 | api: apiInput || null,
|
291 | bin: binInput || null
|
292 | };
|
293 | const globalSourcemap = normalizeSourcemap(sourcemapOption);
|
294 | const globalESModule = normalizeModuleOption(esModule);
|
295 | const globalInterop = normalizeModuleOption(interop);
|
296 | const globalMin = normalizeMinOption(min);
|
297 | const mainOutput = mainOptions === false || !pkgMain ? null : {
|
298 | path: pkgMain,
|
299 | sourcemap: normalizeBuildSourcemap(mainOptions, globalSourcemap),
|
300 | esModule: normalizeBuildModule(mainOptions, 'esModule', 'main', globalESModule),
|
301 | interop: normalizeBuildModule(mainOptions, 'interop', 'main', globalInterop),
|
302 | min: normalizeBuildMin(mainOptions, 'main', globalMin)
|
303 | };
|
304 | const moduleOutput = moduleOptions === false || !esModuleFile ? null : {
|
305 | path: esModuleFile,
|
306 | sourcemap: normalizeBuildSourcemap(moduleOptions, globalSourcemap),
|
307 | min: normalizeBuildMin(moduleOptions, 'module', globalMin)
|
308 | };
|
309 | const browserOutput = browserOptions === false || !pkgBrowser ? null : {
|
310 | path: pkgBrowser,
|
311 | sourcemap: normalizeBuildSourcemap(browserOptions, globalSourcemap),
|
312 | esModule: normalizeBuildModule(browserOptions, 'esModule', 'browser', globalESModule),
|
313 | interop: normalizeBuildModule(browserOptions, 'interop', 'browser', globalInterop),
|
314 | min: normalizeBuildMin(browserOptions, 'browser', globalMin),
|
315 | format: browserOptions && !isNull(browserOptions.format) ? browserOptions.format : browserFormat || 'umd',
|
316 | name: normalizeBuildName(cwd, browserOptions ? browserOptions.name : null, browserName, pkgName),
|
317 | id: browserOptions && browserOptions.id || amdId || null,
|
318 | globals: normalizeBuildGlobals(browserOptions, normalizeGlobals(browserGlobals)),
|
319 | extend: normalizeBuildFlag(browserOptions, 'extend', !!extend)
|
320 | };
|
321 | const binaryOutput = binaryOptions === false || !pkgBin ? null : {
|
322 | path: pkgBin,
|
323 | sourcemap: normalizeBuildSourcemap(binaryOptions, globalSourcemap),
|
324 | esModule: normalizeBuildModule(binaryOptions, 'esModule', 'bin', globalESModule),
|
325 | interop: normalizeBuildModule(binaryOptions, 'interop', 'bin', globalInterop),
|
326 | min: normalizeBuildMin(binaryOptions, 'bin', globalMin)
|
327 | };
|
328 | const typesOutput = typesOptions === false || !typesPath ? null : {
|
329 | path: typesPath
|
330 | };
|
331 | const output = {
|
332 | main: mainOutput,
|
333 | module: moduleOutput,
|
334 | browser: browserOutput,
|
335 | bin: binaryOutput,
|
336 | types: typesOutput
|
337 | };
|
338 | const dependencies = {
|
339 | runtime: runtimeDependencies || null,
|
340 | dev: devDependencies || null,
|
341 | peer: peerDependencies || null
|
342 | };
|
343 | const cache = cacheOption || null;
|
344 | const project = projectOption || null;
|
345 | return {
|
346 | cwd,
|
347 | pkg,
|
348 | input,
|
349 | output,
|
350 | dependencies,
|
351 | cache,
|
352 | project
|
353 | };
|
354 | }
|
355 |
|
356 | const hasOwn = Object.prototype.hasOwnProperty;
|
357 |
|
358 | function arrayToExternal(...modules) {
|
359 | const filtered = modules.reduce((result, list) => list ? [...result, ...list] : result, []);
|
360 |
|
361 | if (!filtered.length) {
|
362 | return () => false;
|
363 | }
|
364 |
|
365 | const cache = {};
|
366 | return (source, importer, isResolved) => {
|
367 | if (isResolved || source.startsWith('.')) {
|
368 | return;
|
369 | }
|
370 |
|
371 | if (hasOwn.call(cache, source)) {
|
372 | return cache[source];
|
373 | }
|
374 |
|
375 | return cache[source] = filtered.some(moduleName => {
|
376 | if (source === moduleName) {
|
377 | return true;
|
378 | }
|
379 |
|
380 | const len = moduleName.length;
|
381 | return source.substr(0, len) === moduleName && /^[/\\]$/.test(source[len]);
|
382 | });
|
383 | };
|
384 | }
|
385 |
|
386 | function createConfig(input, output, external, plugins, chokidar) {
|
387 | const watch = {
|
388 | exclude: ['node_modules/**']
|
389 | };
|
390 |
|
391 | if (chokidar) {
|
392 | watch.chokidar = chokidar === true ? {} : chokidar;
|
393 | }
|
394 |
|
395 | return {
|
396 | input,
|
397 | output,
|
398 | external,
|
399 | plugins,
|
400 | watch
|
401 | };
|
402 | }
|
403 | function createModuleConfig(input, format, file, sourcemap, esModule, interop, external, plugins, chokidar) {
|
404 | return createConfig(input, {
|
405 | file,
|
406 | format,
|
407 | sourcemap,
|
408 | esModule,
|
409 | interop
|
410 | }, external, plugins, chokidar);
|
411 | }
|
412 | const requiresId = createInList('amd', 'umd');
|
413 | function createBrowserConfig(input, format, file, sourcemap, esModule, interop, isExternal, plugins, chokidar, name, extend, globals, id) {
|
414 | const output = {
|
415 | file,
|
416 | format,
|
417 | sourcemap,
|
418 | esModule,
|
419 | interop,
|
420 | extend,
|
421 | name,
|
422 | globals: globals || {}
|
423 | };
|
424 |
|
425 | if (id && requiresId(format)) {
|
426 | output.amd = {
|
427 | id
|
428 | };
|
429 | }
|
430 |
|
431 | return createConfig(input, output, isExternal, plugins, chokidar);
|
432 | }
|
433 |
|
434 | const TS_ONLY_EXTENSIONS = ['.ts', '.tsx'];
|
435 | const JS_EXTENSIONS = ['.js', '.jsx', '.mjs', '.es6', '.es', '.node'];
|
436 | const TS_EXTENSIONS = [...TS_ONLY_EXTENSIONS, ...JS_EXTENSIONS];
|
437 |
|
438 | function findFirst(...filenames) {
|
439 | for (const filename of filenames) {
|
440 | if (fs.existsSync(filename)) {
|
441 | return filename;
|
442 | }
|
443 | }
|
444 |
|
445 | return null;
|
446 | }
|
447 |
|
448 | function createIsInstalled(...dependencies) {
|
449 | return id => !!dependencies.reduce((r, o) => ({ ...r,
|
450 | ...o
|
451 | }), {})[id];
|
452 | }
|
453 |
|
454 | function createPluginLoader(cwd, isInstalled) {
|
455 | return (id, named) => {
|
456 | if (!isInstalled(id)) {
|
457 | return null;
|
458 | }
|
459 |
|
460 | const content = require(resolveFrom(cwd, id));
|
461 |
|
462 | return named ? content[named] : 'default' in content ? content.default : content;
|
463 | };
|
464 | }
|
465 |
|
466 | function mapIdExternal(cwd, outputDir, map) {
|
467 | const normalizedMap = keys(map).reduce((result, source) => setProp(slash(path.resolve(cwd, source)), path.resolve(cwd, map[source]), result), {});
|
468 | return {
|
469 | name: 'api',
|
470 |
|
471 | resolveId(moduleId, from) {
|
472 | const resolved = !from ? moduleId : path.join(path.dirname(from), moduleId);
|
473 | const target = normalizedMap[slash(resolved)] || normalizedMap[slash(resolved + '.ts')] || normalizedMap[slash(path.join(resolved, 'index.ts'))];
|
474 |
|
475 | if (!target) {
|
476 | return null;
|
477 | }
|
478 |
|
479 | const relativeTarget = path.relative(outputDir, target);
|
480 | return {
|
481 | id: !relativeTarget ? '.' : relativeTarget.startsWith('.') ? relativeTarget : path.join('.', relativeTarget),
|
482 | external: true,
|
483 | moduleSideEffects: false
|
484 | };
|
485 | }
|
486 |
|
487 | };
|
488 | }
|
489 |
|
490 | function renamePre(filename, pre) {
|
491 | const {
|
492 | dir,
|
493 | name,
|
494 | ext
|
495 | } = path.parse(filename);
|
496 | return path.join(dir, `${name}.${pre}${ext}`);
|
497 | }
|
498 | function renameMin(filename) {
|
499 | return renamePre(filename, 'min');
|
500 | }
|
501 |
|
502 | function extensionMatch(filename, extensions) {
|
503 | return extensions.includes(path.extname(filename).toLowerCase());
|
504 | }
|
505 |
|
506 | function pkgToConfigs({
|
507 | cwd,
|
508 | input,
|
509 | output,
|
510 | dependencies,
|
511 | cache,
|
512 | project
|
513 | }, {
|
514 | dev,
|
515 | watch
|
516 | }) {
|
517 | const {
|
518 | api: apiInput1,
|
519 | bin: binInput1
|
520 | } = input;
|
521 | const {
|
522 | main: cjsOutput,
|
523 | module: esOutput,
|
524 | browser: browserOutput,
|
525 | bin: binaryOutput,
|
526 | types: typesOutput
|
527 | } = output;
|
528 | const {
|
529 | runtime: runtimeDeps,
|
530 | dev: devDeps,
|
531 | peer: peerDeps
|
532 | } = dependencies;
|
533 | const bundlibCache = path.resolve(cwd, cache || 'node_modules/.cache/bundlib');
|
534 |
|
535 | if (browserOutput) {
|
536 | const {
|
537 | format,
|
538 | name
|
539 | } = browserOutput;
|
540 |
|
541 | if ((format === 'iife' || format === 'umd') && !name) {
|
542 | throw error('option \'name\' is required for IIFE and UMD builds');
|
543 | }
|
544 | }
|
545 |
|
546 | const isInstalled = createIsInstalled(runtimeDeps, devDeps);
|
547 | const pluginLoader = createPluginLoader(cwd, isInstalled);
|
548 | const loadPluginTypescript2 = pluginLoader('rollup-plugin-typescript2');
|
549 | const loadPluginTypescript = pluginLoader('@rollup/plugin-typescript');
|
550 | const loadPluginESLint = pluginLoader('rollup-plugin-eslint', 'eslint');
|
551 | const loadPluginNodeResolve = pluginLoader('@rollup/plugin-node-resolve');
|
552 | const loadPluginCommonJS = pluginLoader('@rollup/plugin-commonjs');
|
553 | const loadPluginJSON = pluginLoader('@rollup/plugin-json');
|
554 | const loadPluginBabel = pluginLoader('rollup-plugin-babel');
|
555 | const loadPluginBuble = pluginLoader('@rollup/plugin-buble');
|
556 | const loadPluginTerser = pluginLoader('rollup-plugin-terser', 'terser');
|
557 | const loadPluginStripShebang = pluginLoader('rollup-plugin-strip-shebang');
|
558 | const loadPluginAddShebang = pluginLoader('rollup-plugin-add-shebang');
|
559 | const loadPluginExportEquals = pluginLoader('rollup-plugin-export-equals');
|
560 | const useTypescriptPlugin = !!loadPluginTypescript2 || !!loadPluginTypescript;
|
561 | const isInstalledChokidar = isInstalled('chokidar');
|
562 | const apiInput = apiInput1 ? path.resolve(cwd, apiInput1) : (useTypescriptPlugin ? findFirst(...['index.ts', 'index.tsx'].map(filename => path.resolve(cwd, 'src', filename))) : null) || path.resolve(cwd, 'src', 'index.js');
|
563 | const binInput = binInput1 ? path.resolve(cwd, binInput1) : (useTypescriptPlugin ? findFirst(...['index.ts'].map(filename => path.resolve(cwd, 'src-bin', filename))) : null) || path.resolve(cwd, 'src-bin', 'index.js');
|
564 | const isTypescriptAPIInput = extensionMatch(apiInput, TS_ONLY_EXTENSIONS);
|
565 | const isTypescriptBinaryInput = extensionMatch(binInput, TS_ONLY_EXTENSIONS);
|
566 |
|
567 | if (typesOutput && !isTypescriptAPIInput) {
|
568 | throw error('can\'t generate types from javascript source');
|
569 | }
|
570 |
|
571 | const apiExtensions = isTypescriptAPIInput ? TS_EXTENSIONS : JS_EXTENSIONS;
|
572 | const binaryExtensions = isTypescriptBinaryInput ? TS_EXTENSIONS : JS_EXTENSIONS;
|
573 | const production = !dev;
|
574 | const apiFolder = path.dirname(apiInput);
|
575 | const apiFolderContent = apiExtensions.map(ext => path.resolve(apiFolder, `**/*${ext}`));
|
576 | const cwdFolderContent = binaryExtensions.map(ext => path.resolve(cwd, `**/*${ext}`));
|
577 | const typesFilename = renamePre(path.basename(apiInput), 'd');
|
578 | let typesOutputDir = typesOutput ? path.resolve(cwd, typesOutput.path) : null;
|
579 |
|
580 | if (typesOutputDir && extensionMatch(typesOutputDir, ['.ts'])) {
|
581 | typesOutputDir = path.dirname(typesOutputDir);
|
582 | }
|
583 |
|
584 | const isExternal = arrayToExternal(keysOrNull(runtimeDeps), keysOrNull(peerDeps), builtinModules);
|
585 | const useChokidar = isInstalledChokidar && !!watch;
|
586 | const exclude = /node_modules/;
|
587 | const configs = [];
|
588 |
|
589 | function createPlugins(inputIsTypescript, extensions, outputFile, sourcemap, mini, browser, bin) {
|
590 | const sourcemapBool = !!sourcemap;
|
591 | const declarationDir = inputIsTypescript && configs.length === 0 && !bin && typesOutputDir;
|
592 | const include = bin ? cwdFolderContent : apiFolderContent;
|
593 | const cacheRoot = path.join(bundlibCache, 'rpt2');
|
594 | let shebang;
|
595 | const plugins = [loadPluginESLint && loadPluginESLint({
|
596 | include,
|
597 | exclude,
|
598 | throwOnWarning: false,
|
599 | throwOnError: false
|
600 | }), bin && loadPluginStripShebang && loadPluginStripShebang({
|
601 | capture: shebangFromFile => shebang = shebangFromFile,
|
602 | sourcemap: sourcemapBool
|
603 | }), bin && cjsOutput && outputFile && mapIdExternal(cwd, path.dirname(outputFile), setProp(apiInput, cwd, {})), loadPluginNodeResolve && loadPluginNodeResolve({
|
604 | preferBuiltins: !browser,
|
605 | extensions,
|
606 | rootDir: cwd
|
607 | }), browser && loadPluginCommonJS && loadPluginCommonJS({
|
608 | sourceMap: sourcemapBool
|
609 | }), inputIsTypescript && loadPluginTypescript2 && loadPluginTypescript2({
|
610 | cacheRoot,
|
611 | useTsconfigDeclarationDir: true,
|
612 | tsconfigDefaults: {
|
613 | exclude: []
|
614 | },
|
615 | tsconfigOverride: {
|
616 | compilerOptions: {
|
617 | sourceMap: sourcemapBool,
|
618 | declaration: !!declarationDir,
|
619 | ...(declarationDir && {
|
620 | declarationDir
|
621 | })
|
622 | }
|
623 | },
|
624 | ...(project && {
|
625 | tsconfig: path.resolve(cwd, project)
|
626 | })
|
627 | }), inputIsTypescript && loadPluginTypescript && loadPluginTypescript(), loadPluginJSON && loadPluginJSON({
|
628 | preferConst: true
|
629 | }), declarationDir && typesOutput && loadPluginExportEquals && loadPluginExportEquals({
|
630 | file: path.resolve(cwd, path.join(declarationDir, typesFilename))
|
631 | }), loadPluginBabel && loadPluginBabel({
|
632 | include,
|
633 | extensions,
|
634 | exclude
|
635 | }), loadPluginBuble && loadPluginBuble(), bin && outputFile && loadPluginAddShebang && loadPluginAddShebang({
|
636 | include: outputFile,
|
637 | shebang: () => shebang || '#!/usr/bin/env node'
|
638 | }), mini && loadPluginTerser && loadPluginTerser({
|
639 | sourcemap: sourcemapBool,
|
640 | toplevel: true,
|
641 | module: true,
|
642 | compress: {
|
643 | passes: 2
|
644 | }
|
645 | })];
|
646 | return plugins.filter(Boolean);
|
647 | }
|
648 |
|
649 | if (esOutput) {
|
650 | const {
|
651 | path: path$1,
|
652 | sourcemap,
|
653 | min
|
654 | } = esOutput;
|
655 | const resolvedPath = path.resolve(cwd, path$1);
|
656 | configs.push(createModuleConfig(apiInput, 'es', resolvedPath, sourcemap, true, false, isExternal, createPlugins(isTypescriptAPIInput, apiExtensions, resolvedPath, sourcemap, production && !min, false, false), useChokidar));
|
657 |
|
658 | if (min) {
|
659 | configs.push(createModuleConfig(apiInput, 'es', renameMin(resolvedPath), sourcemap, true, false, isExternal, createPlugins(isTypescriptAPIInput, apiExtensions, resolvedPath, sourcemap, true, false, false), useChokidar));
|
660 | }
|
661 | }
|
662 |
|
663 | if (cjsOutput) {
|
664 | const {
|
665 | path: path$1,
|
666 | sourcemap,
|
667 | esModule,
|
668 | interop,
|
669 | min
|
670 | } = cjsOutput;
|
671 | const resolvedPath = path.resolve(cwd, path$1);
|
672 | configs.push(createModuleConfig(apiInput, 'cjs', resolvedPath, sourcemap, esModule, interop, isExternal, createPlugins(isTypescriptAPIInput, apiExtensions, resolvedPath, sourcemap, production && !min, false, false), useChokidar));
|
673 |
|
674 | if (min) {
|
675 | configs.push(createModuleConfig(apiInput, 'cjs', renameMin(resolvedPath), sourcemap, esModule, interop, isExternal, createPlugins(isTypescriptAPIInput, apiExtensions, resolvedPath, sourcemap, true, false, false), useChokidar));
|
676 | }
|
677 | }
|
678 |
|
679 | if (browserOutput) {
|
680 | const {
|
681 | path: path$1,
|
682 | sourcemap,
|
683 | esModule,
|
684 | interop,
|
685 | format,
|
686 | name,
|
687 | extend,
|
688 | id,
|
689 | globals,
|
690 | min
|
691 | } = browserOutput;
|
692 | const resolvedPath = path.resolve(cwd, path$1);
|
693 | const isBrowserExternal = arrayToExternal(keysOrNull(globals));
|
694 | configs.push(createBrowserConfig(apiInput, format, resolvedPath, sourcemap, esModule, interop, isBrowserExternal, createPlugins(isTypescriptAPIInput, apiExtensions, null, sourcemap, production && !min, true, false), useChokidar, name, extend, globals, id));
|
695 |
|
696 | if (min) {
|
697 | configs.push(createBrowserConfig(apiInput, format, renameMin(resolvedPath), sourcemap, esModule, interop, isBrowserExternal, createPlugins(isTypescriptAPIInput, apiExtensions, null, sourcemap, true, true, false), useChokidar, name, extend, globals, id));
|
698 | }
|
699 | }
|
700 |
|
701 | if (binaryOutput) {
|
702 | const {
|
703 | path: path$1,
|
704 | sourcemap,
|
705 | esModule,
|
706 | interop,
|
707 | min
|
708 | } = binaryOutput;
|
709 | const resolvedPath = path.resolve(cwd, path$1);
|
710 | configs.push(createModuleConfig(binInput, 'cjs', resolvedPath, sourcemap, esModule, interop, isExternal, createPlugins(isTypescriptBinaryInput, binaryExtensions, resolvedPath, sourcemap, production && !min, false, true), useChokidar));
|
711 |
|
712 | if (min) {
|
713 | configs.push(createModuleConfig(binInput, 'cjs', renameMin(resolvedPath), sourcemap, esModule, interop, isExternal, createPlugins(isTypescriptBinaryInput, binaryExtensions, resolvedPath, sourcemap, true, false, true), useChokidar));
|
714 | }
|
715 | }
|
716 |
|
717 | return configs;
|
718 | }
|
719 |
|
720 | async function configsFromPkg(cwd, options, pkgJson) {
|
721 | return pkgToConfigs((await analizePkg(cwd, pkgJson)), options || {});
|
722 | }
|
723 |
|
724 | exports.analizePkg = analizePkg;
|
725 | exports.configsFromPkg = configsFromPkg;
|
726 |
|