@pi-r/rollup 0.11.5 → 0.12.1

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.
Files changed (2) hide show
  1. package/index.js +50 -29
  2. package/package.json +3 -7
package/index.js CHANGED
@@ -1,21 +1,21 @@
1
1
  "use strict";
2
- const path = require("node:path");
3
- const fs = require("node:fs");
4
- const commonjs = require("@rollup/plugin-commonjs");
5
- const nodeResolve = require("@rollup/plugin-node-resolve");
6
- const node_crypto_1 = require("node:crypto");
7
- const Document = require("@e-mc/document");
8
- const types_1 = require("@e-mc/types");
9
- const util_1 = require("@e-mc/document/util");
2
+
3
+ const path = require('node:path');
4
+ const fs = require('node:fs');
5
+ const commonjs = require('@rollup/plugin-commonjs');
6
+ const { randomUUID } = require('node:crypto');
7
+ const { nodeResolve } = require('@rollup/plugin-node-resolve');
8
+ const Document = require('@e-mc/document');
9
+ const { isPlainObject, loadPlugins } = require('@e-mc/document/util');
10
10
  const PLUGIN_MAP = Object.freeze({
11
11
  'commonjs': commonjs,
12
12
  'node-resolve': nodeResolve,
13
13
  '@rollup/plugin-commonjs': commonjs,
14
14
  '@rollup/plugin-node-resolve': nodeResolve
15
15
  });
16
- function getPlugins(plugin) {
17
- if (plugin) {
18
- return !Array.isArray(plugin) ? [plugin] : plugin;
16
+ function asArray(value) {
17
+ if (value !== undefined) {
18
+ return !Array.isArray(value) ? [value] : value;
19
19
  }
20
20
  return [];
21
21
  }
@@ -30,13 +30,16 @@ async function transform(context, value, options) {
30
30
  if (Object.keys(outputConfig).length === 0) {
31
31
  outputConfig = baseConfig.output || { format };
32
32
  }
33
- else if ((0, types_1.isPlainObject)(baseConfig.output)) {
33
+ else if (isPlainObject(baseConfig.output)) {
34
34
  outputConfig = Object.assign(baseConfig.output, outputConfig);
35
35
  }
36
36
  outputConfig.format ||= format;
37
37
  const single = outputConfig.format === 'iife' || outputConfig.format === 'umd';
38
- if ((0, types_1.isArray)(baseConfig.plugins)) {
39
- baseConfig.plugins = (0, util_1.loadPlugins)(baseConfig.plugins, PLUGIN_MAP);
38
+ if (Array.isArray(baseConfig.plugins)) {
39
+ baseConfig.plugins = loadPlugins(baseConfig.plugins, PLUGIN_MAP);
40
+ }
41
+ if (Array.isArray(outputConfig.plugins)) {
42
+ outputConfig.plugins = loadPlugins(outputConfig.plugins, PLUGIN_MAP);
40
43
  }
41
44
  if (esmData?.sourceFile) {
42
45
  const files = [];
@@ -46,7 +49,7 @@ async function transform(context, value, options) {
46
49
  if (!content) {
47
50
  continue;
48
51
  }
49
- fs.writeFileSync(targetFile = path.join(localFile && path.dirname(localFile) || createDir(), (0, node_crypto_1.randomUUID)()), content);
52
+ fs.writeFileSync(targetFile = path.join(localFile && path.dirname(localFile) || createDir(), randomUUID()), content);
50
53
  tmp.push(targetFile);
51
54
  }
52
55
  files.push(targetFile || inputFile);
@@ -56,7 +59,7 @@ async function transform(context, value, options) {
56
59
  }
57
60
  else {
58
61
  if (!sourceFile || esmData?.code) {
59
- fs.writeFileSync(sourceFile = path.join((sourcesRelativeTo = esmData?.sourcesRelativeTo) || createDir(), (0, node_crypto_1.randomUUID)()), esmData?.code || value);
62
+ fs.writeFileSync(sourceFile = path.join((sourcesRelativeTo = esmData?.sourcesRelativeTo) || createDir(), randomUUID()), esmData?.code || value);
60
63
  tmp.push(sourceFile);
61
64
  }
62
65
  baseConfig.input = sourceFile;
@@ -73,7 +76,7 @@ async function transform(context, value, options) {
73
76
  }
74
77
  };
75
78
  };
76
- const plugins = getPlugins(baseConfig.plugins);
79
+ const plugins = asArray(baseConfig.plugins);
77
80
  baseConfig.plugins = plugins;
78
81
  plugins.unshift(dynamicImportPlugin());
79
82
  }
@@ -106,21 +109,38 @@ async function transform(context, value, options) {
106
109
  url = path.basename(outputConfig.sourcemapFile);
107
110
  }
108
111
  }
109
- const bundle = await context.rollup(baseConfig);
110
- if ((0, types_1.isArray)(outputConfig.plugins)) {
111
- outputConfig.plugins = (0, util_1.loadPlugins)(outputConfig.plugins, PLUGIN_MAP);
112
- }
113
- if (external) {
112
+ if (isPlainObject(external)) {
113
+ if ('external' in external) {
114
+ baseConfig.external = asArray(baseConfig.external).concat(asArray(external.external));
115
+ delete external.external;
116
+ }
114
117
  if ('plugins' in external) {
115
- if ((0, types_1.isArray)(external.plugins)) {
116
- const plugins = getPlugins(outputConfig.plugins);
117
- outputConfig.plugins = plugins;
118
- plugins.push(...(0, util_1.loadPlugins)(external.plugins, PLUGIN_MAP));
119
- }
118
+ const plugins = asArray(outputConfig.plugins);
119
+ plugins.push(...loadPlugins(asArray(external.plugins), PLUGIN_MAP));
120
+ outputConfig.plugins = plugins;
120
121
  delete external.plugins;
121
122
  }
122
123
  Object.assign(outputConfig, external);
123
124
  }
125
+ if (baseConfig.external) {
126
+ baseConfig.external = asArray(baseConfig.external).map(specifier => {
127
+ if (typeof specifier === 'string') {
128
+ const match = /^\/(\^)?(?:[^/]|(?<=\\)\/)+?(\$)?\/[dgimsuvy]*$/.exec(specifier);
129
+ if (match && (match[1] || match[2]) && !specifier.includes('**')) {
130
+ try {
131
+ const pattern = eval(specifier);
132
+ if (pattern instanceof RegExp) {
133
+ return pattern;
134
+ }
135
+ }
136
+ catch {
137
+ }
138
+ }
139
+ }
140
+ return specifier;
141
+ });
142
+ }
143
+ const bundle = await context.rollup(baseConfig);
124
144
  if (sourcesRelativeTo) {
125
145
  outputConfig.sourcemapPathTransform = (relativeSourcePath, sourcemapPath) => {
126
146
  return path.resolve(path.dirname(sourcemapPath), relativeSourcePath);
@@ -130,7 +150,7 @@ async function transform(context, value, options) {
130
150
  outputConfig.sourcemapExcludeSources = false;
131
151
  }
132
152
  if (single) {
133
- const filename = (0, node_crypto_1.randomUUID)();
153
+ const filename = randomUUID();
134
154
  outputConfig.manualChunks = () => filename + '.js';
135
155
  }
136
156
  const data = await bundle.generate(outputConfig);
@@ -177,7 +197,7 @@ async function transform(context, value, options) {
177
197
  }
178
198
  }
179
199
  const length = bundle.watchFiles.length;
180
- if (length) {
200
+ if (length > 0) {
181
201
  if (length > 1) {
182
202
  options.out.ignoreCache = true;
183
203
  }
@@ -205,4 +225,5 @@ async function transform(context, value, options) {
205
225
  return result;
206
226
  }
207
227
  }
228
+
208
229
  module.exports = transform;
package/package.json CHANGED
@@ -1,11 +1,8 @@
1
1
  {
2
2
  "name": "@pi-r/rollup",
3
- "version": "0.11.5",
3
+ "version": "0.12.1",
4
4
  "description": "Rollup transform function for E-mc.",
5
5
  "main": "index.js",
6
- "publishConfig": {
7
- "access": "public"
8
- },
9
6
  "repository": {
10
7
  "type": "git",
11
8
  "url": "git+https://github.com/anpham6/pi-r.git",
@@ -19,10 +16,9 @@
19
16
  "license": "MIT",
20
17
  "homepage": "https://github.com/anpham6/pi-r#readme",
21
18
  "dependencies": {
22
- "@e-mc/document": "^0.13.10",
23
- "@e-mc/types": "^0.13.10",
19
+ "@e-mc/document": "^0.14.1",
24
20
  "@rollup/plugin-commonjs": "*",
25
21
  "@rollup/plugin-node-resolve": "*",
26
- "rollup": "^4.60.2"
22
+ "rollup": "^4.60.3"
27
23
  }
28
24
  }