pkgbld 1.17.1 → 1.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -118,7 +118,13 @@ File(s) to make executable. The first entry will be added to package.json
118
118
  pkgbld --include-externals
119
119
  ```
120
120
 
121
- Bundles all externals into a package.
121
+ or
122
+
123
+ ```
124
+ pkgbld --include-externals=lodash
125
+ ```
126
+
127
+ Bundles all or specified externals into a package.
122
128
 
123
129
  ### eject
124
130
 
@@ -140,6 +146,30 @@ pkgbld --no-update-package-json
140
146
 
141
147
  Do not write package.json.
142
148
 
149
+ ### commonjs-pattern
150
+
151
+ ```
152
+ pkgbld --commonjs-pattern=[name].js
153
+ ```
154
+
155
+ Defines the pattern for commonjs output files. Default is `[name].cjs`.
156
+
157
+ ### esm-pattern
158
+
159
+ ```
160
+ pkgbld --esm-pattern=[name].js
161
+ ```
162
+
163
+ Defines the pattern for esm output files. Default is `[name].mjs`.
164
+
165
+ ### umd-pattern
166
+
167
+ ```
168
+ pkgbld --umd-pattern=[name].js
169
+ ```
170
+
171
+ Defines the pattern for umd output files. Default is `[name].umd.js`.
172
+
143
173
  ## Plugin API
144
174
 
145
175
  `pkgbld` reads all installed packages named `pkgbld-plugin-*` and assumes they are plugins
package/dist/index.js CHANGED
@@ -41,11 +41,23 @@ const defaults = {
41
41
  includeExternals: false,
42
42
  eject: false,
43
43
  noTsConfig: false,
44
- noUpdatePackageJson: false
44
+ noUpdatePackageJson: false,
45
+ commonjsPattern: '[name].cjs',
46
+ esPattern: '[name].mjs',
47
+ umdPattern: '[name].umd.js'
45
48
  };
46
49
  function CommaSeparatedString(value) {
47
50
  return value.split(',').map((arg) => arg.trim());
48
51
  }
52
+ function CommaSeparatedStringOrBoolean(value) {
53
+ if (typeof value === 'boolean') {
54
+ return value;
55
+ }
56
+ if (Array.isArray(value) && value.length === 0) {
57
+ return true;
58
+ }
59
+ return CommaSeparatedString(value);
60
+ }
49
61
  function getCliOptions(plugins, pkg) {
50
62
  const cliOptions = cleye.cli({
51
63
  name: 'pkgbld',
@@ -91,14 +103,14 @@ function getCliOptions(plugins, pkg) {
91
103
  description: 'Executable files'
92
104
  },
93
105
  includeExternals: {
94
- type: Boolean,
95
- description: 'Include all externals into result bundle(s)',
106
+ type: CommaSeparatedStringOrBoolean,
107
+ description: 'Include all/specified externals into result bundle(s)',
96
108
  default: defaults.includeExternals
97
109
  },
98
110
  eject: {
99
111
  type: Boolean,
100
112
  description: 'Eject config',
101
- default: defaults.includeExternals
113
+ default: defaults.eject
102
114
  },
103
115
  noTsConfig: {
104
116
  type: Boolean,
@@ -109,6 +121,21 @@ function getCliOptions(plugins, pkg) {
109
121
  type: Boolean,
110
122
  description: 'Do not create / update package.json',
111
123
  default: defaults.noUpdatePackageJson
124
+ },
125
+ commonjsPattern: {
126
+ type: String,
127
+ description: 'CommonJS output file name pattern',
128
+ default: defaults.commonjsPattern
129
+ },
130
+ esmPattern: {
131
+ type: String,
132
+ description: 'ES output file name pattern',
133
+ default: defaults.esPattern
134
+ },
135
+ umdPattern: {
136
+ type: String,
137
+ description: 'UMD output file name pattern',
138
+ default: defaults.umdPattern
112
139
  }
113
140
  }
114
141
  });
@@ -126,7 +153,10 @@ function getCliOptions(plugins, pkg) {
126
153
  includeExternals: flags.includeExternals,
127
154
  eject: flags.eject,
128
155
  noTsConfig: flags.noTsConfig,
129
- noUpdatePackageJson: flags.noUpdatePackageJson
156
+ noUpdatePackageJson: flags.noUpdatePackageJson,
157
+ commonjsPattern: flags.commonjsPattern,
158
+ esPattern: flags.esmPattern,
159
+ umdPattern: flags.umdPattern
130
160
  };
131
161
  for (const plugin of plugins) {
132
162
  plugin.options?.(flags, options);
@@ -158,28 +188,54 @@ async function commonjs (provider) {
158
188
  }
159
189
 
160
190
  async function externals (provider, config, inputs) {
161
- if (config.includeExternals) {
191
+ if (config.includeExternals === true) {
162
192
  return;
163
193
  }
164
194
  const pluginExternals = await provider.import('@rollup-extras/plugin-externals');
165
195
  const allowGenericUmd = config.umdInputs.length === 1 && inputs.length === 1;
166
196
  if (config.formats.length > 0) {
167
197
  const format = (allowGenericUmd ? undefined : config.formats.filter(format => format !== 'umd'));
168
- provider.provide(() => pluginExternals(), 2000 /* Priority.externals */, { format });
198
+ provider.provide(() => pluginExternals({
199
+ external: (id, external, importer) => includeExternals(importer, external, id, config)
200
+ }), 2000 /* Priority.externals */, { format });
201
+ // for eject config
202
+ provider.globalImport('path', 'path');
203
+ provider.globalSetup(includeExternals);
169
204
  }
170
205
  if (!allowGenericUmd && config.umdInputs.length > 0) {
171
206
  const curry = (await provider.import('lodash/curry.js'));
172
207
  for (const currentInput of config.umdInputs) {
173
- const isExternal = curry((currentInput, id, external) => external || isExternalInput(currentInput, inputs, id, config))(currentInput);
208
+ const isExternal = curry((currentInput, id, external, importer) => includeExternals(importer, external, id, config) || isExternalInput(currentInput, inputs, id, config))(currentInput);
174
209
  provider.provide(() => pluginExternals({
175
210
  external: isExternal
176
211
  }), 2000 /* Priority.externals */, { format: 'umd', inputs: [`./${config.sourceDir}/${currentInput}.ts`] });
177
212
  }
178
213
  // for eject config
179
- provider.globalImport('path', 'path');
214
+ if (config.formats.length === 0) {
215
+ provider.globalImport('path', 'path');
216
+ provider.globalSetup(includeExternals);
217
+ }
180
218
  provider.globalSetup(isExternalInput);
181
219
  }
182
220
  }
221
+ function includeExternals(importer, external, id, config) {
222
+ if (config.includeExternals === false)
223
+ return external;
224
+ if (!external)
225
+ return false;
226
+ if (path.isAbsolute(id)) {
227
+ id = './' + path.relative(process.cwd(), id);
228
+ }
229
+ const internals = config.includeExternals;
230
+ if (internals.includes(id) || internals.some(internal => id.startsWith(internal))) {
231
+ return false;
232
+ }
233
+ const relative = path.relative('.', path.resolve(importer ?? '.', id));
234
+ if (internals.some(internal => relative.includes(`node_modules/${internal}/`))) {
235
+ return false;
236
+ }
237
+ return true;
238
+ }
183
239
  function isExternalInput(currentInput, inputs, id, config) {
184
240
  let normalizedPath;
185
241
  if (path.isAbsolute(currentInput)) {
@@ -329,13 +385,13 @@ function getTimeDiff(starting) {
329
385
  }
330
386
  const areSetsEqual = (a, b) => a.size === b.size ? [...a].every(value => b.has(value)) : false;
331
387
 
332
- const fileNamePatterns = {
333
- 'es': '[name].mjs',
334
- 'cjs': '[name].cjs',
335
- 'umd': '[name].umd.js',
336
- };
337
388
  async function getRollupConfigs([provider, plugins$1], inputs, config, helpers, externalPlugins) {
338
389
  const factoryInProgress = [];
390
+ const fileNamePatterns = {
391
+ 'es': config.esPattern,
392
+ 'cjs': config.commonjsPattern,
393
+ 'umd': config.umdPattern
394
+ };
339
395
  for (const factory of plugins) {
340
396
  factoryInProgress.push(factory(provider, config, inputs));
341
397
  }
@@ -537,7 +593,7 @@ function processPackage(pkg, config, plugins) {
537
593
  }
538
594
  pkg.types = `./${config.dir}/index.d.ts`;
539
595
  if (allowUmd && typeof pkg.umd === 'string') {
540
- pkg.umd = `./${config.dir}/index.umd.js`;
596
+ pkg.umd = `./${config.dir}/${patternToName(config.umdPattern, 'index')}`;
541
597
  if (!config.umdInputs.includes('index')) {
542
598
  config.umdInputs.push('index');
543
599
  }
@@ -562,23 +618,23 @@ function processPackage(pkg, config, plugins) {
562
618
  else {
563
619
  delete pkg.bin;
564
620
  }
565
- if (allowCjs && typeof pkg.main !== 'string') {
566
- pkg.main = `./${config.dir}/index.cjs`;
621
+ if (allowCjs) {
622
+ pkg.main = `./${config.dir}/${patternToName(config.commonjsPattern, 'index')}`;
567
623
  }
568
- if (allowEsm && !allowCjs && typeof pkg.main !== 'string') {
569
- pkg.main = `./${config.dir}/index.mjs`;
624
+ if (allowEsm && !allowCjs) {
625
+ pkg.main = `./${config.dir}/${patternToName(config.esPattern, 'index')}`;
570
626
  }
571
627
  if (allowCjs && pkg.main !== pkg.exports['.'].require) {
572
628
  pkg.exports['.'].require = pkg.main;
573
629
  }
574
630
  if (allowCjs && allowEsm && typeof pkg.module !== 'string') {
575
- pkg.module = `./${config.dir}/index.mjs`;
631
+ pkg.module = `./${config.dir}/${patternToName(config.esPattern, 'index')}`;
576
632
  }
577
633
  if (pkg.module !== pkg.exports['.']?.default) {
578
634
  pkg.exports['.'].default = pkg.module;
579
635
  }
580
636
  if (allowUmd && config.umdInputs.includes('index')) {
581
- pkg.unpkg = `./${config.dir}/index.umd.js`;
637
+ pkg.unpkg = `./${config.dir}/${patternToName(config.umdPattern, 'index')}`;
582
638
  }
583
639
  for (const id in pkg.exports) {
584
640
  if (id === './package.json')
@@ -588,10 +644,10 @@ function processPackage(pkg, config, plugins) {
588
644
  pkg.exports[id] = {};
589
645
  }
590
646
  if (allowCjs) {
591
- pkg.exports[id].require = `./${config.dir}/${basename}.cjs`;
647
+ pkg.exports[id].require = `./${config.dir}/${patternToName(config.commonjsPattern, basename)}`;
592
648
  }
593
649
  if (allowEsm) {
594
- pkg.exports[id].default = `./${config.dir}/${basename}.mjs`;
650
+ pkg.exports[id].default = `./${config.dir}/${patternToName(config.esPattern, basename)}`;
595
651
  }
596
652
  if (basename !== 'index') {
597
653
  if (!pkg.files.includes(basename)) {
@@ -608,12 +664,15 @@ function processPackage(pkg, config, plugins) {
608
664
  }
609
665
  return inputs;
610
666
  }
667
+ function patternToName(pattern, input) {
668
+ return pattern.replace('[name]', input);
669
+ }
611
670
 
612
671
  async function writeJson(path, json) {
613
672
  await fs.writeFile(path, JSON.stringify(json, null, 2) + '\n');
614
673
  }
615
674
 
616
- var version = "1.17.1";
675
+ var version = "1.19.0";
617
676
  var name = "pkgbld";
618
677
  var license = "MIT";
619
678
  var author = "Konstantin Shutkin";
@@ -9,8 +9,11 @@ export declare function getCliOptions(plugins: Partial<PkgbldPlugin>[], pkg: Pac
9
9
  dir: string;
10
10
  sourceDir: string;
11
11
  bin?: string[] | undefined;
12
- includeExternals: boolean;
12
+ includeExternals: boolean | string[];
13
13
  eject: boolean;
14
14
  noTsConfig: boolean;
15
15
  noUpdatePackageJson: boolean;
16
+ commonjsPattern: string;
17
+ esPattern: string;
18
+ umdPattern: string;
16
19
  };
@@ -21,7 +21,7 @@ export declare function getRollupConfigs([provider, plugins]: [Provider, PkgbldR
21
21
  externalLiveBindings?: boolean | undefined;
22
22
  file?: string | undefined;
23
23
  footer?: string | import("rollup").AddonFunction | undefined;
24
- format: "amd" | "cjs" | "es" | "iife" | "system" | "umd" | "commonjs" | "esm" | "module" | "systemjs";
24
+ format: "umd" | "es" | "cjs" | "amd" | "iife" | "system" | "commonjs" | "esm" | "module" | "systemjs";
25
25
  freeze?: boolean | undefined;
26
26
  generatedCode?: import("rollup").GeneratedCodePreset | import("rollup").GeneratedCodeOptions | undefined;
27
27
  globals?: import("rollup").GlobalsOption | undefined;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.17.1",
2
+ "version": "1.19.0",
3
3
  "name": "pkgbld",
4
4
  "license": "MIT",
5
5
  "author": "Konstantin Shutkin",