@wyw-in-js/transform 1.0.6 → 1.0.8

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 (98) hide show
  1. package/esm/cache.js +160 -12
  2. package/esm/cache.js.map +1 -1
  3. package/esm/debug/fileReporter.js.map +1 -1
  4. package/esm/module.js +59 -5
  5. package/esm/module.js.map +1 -1
  6. package/esm/plugins/shaker.js +152 -13
  7. package/esm/plugins/shaker.js.map +1 -1
  8. package/esm/shaker.js +51 -23
  9. package/esm/shaker.js.map +1 -1
  10. package/esm/transform/BaseEntrypoint.js +3 -1
  11. package/esm/transform/BaseEntrypoint.js.map +1 -1
  12. package/esm/transform/Entrypoint.js +68 -20
  13. package/esm/transform/Entrypoint.js.map +1 -1
  14. package/esm/transform/EvaluatedEntrypoint.js.map +1 -1
  15. package/esm/transform/actions/BaseAction.js +2 -1
  16. package/esm/transform/actions/BaseAction.js.map +1 -1
  17. package/esm/transform/actions/actionRunner.js +2 -2
  18. package/esm/transform/actions/actionRunner.js.map +1 -1
  19. package/esm/transform/barrelManifest.js +291 -0
  20. package/esm/transform/barrelManifest.js.map +1 -0
  21. package/esm/transform/generators/getExports.js +5 -0
  22. package/esm/transform/generators/getExports.js.map +1 -1
  23. package/esm/transform/generators/processEntrypoint.js +31 -1
  24. package/esm/transform/generators/processEntrypoint.js.map +1 -1
  25. package/esm/transform/generators/resolveImports.js +29 -5
  26. package/esm/transform/generators/resolveImports.js.map +1 -1
  27. package/esm/transform/generators/rewriteBarrelImports.js +733 -0
  28. package/esm/transform/generators/rewriteBarrelImports.js.map +1 -0
  29. package/esm/transform/generators/transform.js +154 -21
  30. package/esm/transform/generators/transform.js.map +1 -1
  31. package/esm/transform/types.js.map +1 -1
  32. package/esm/transform.js +45 -23
  33. package/esm/transform.js.map +1 -1
  34. package/esm/utils/collectTemplateDependencies.js +9 -0
  35. package/esm/utils/collectTemplateDependencies.js.map +1 -1
  36. package/lib/cache.js +163 -12
  37. package/lib/cache.js.map +1 -1
  38. package/lib/debug/fileReporter.js.map +1 -1
  39. package/lib/module.js +61 -7
  40. package/lib/module.js.map +1 -1
  41. package/lib/plugins/shaker.js +152 -13
  42. package/lib/plugins/shaker.js.map +1 -1
  43. package/lib/shaker.js +58 -26
  44. package/lib/shaker.js.map +1 -1
  45. package/lib/transform/BaseEntrypoint.js +3 -1
  46. package/lib/transform/BaseEntrypoint.js.map +1 -1
  47. package/lib/transform/Entrypoint.js +69 -20
  48. package/lib/transform/Entrypoint.js.map +1 -1
  49. package/lib/transform/EvaluatedEntrypoint.js.map +1 -1
  50. package/lib/transform/actions/BaseAction.js +2 -1
  51. package/lib/transform/actions/BaseAction.js.map +1 -1
  52. package/lib/transform/actions/actionRunner.js +2 -2
  53. package/lib/transform/actions/actionRunner.js.map +1 -1
  54. package/lib/transform/barrelManifest.js +300 -0
  55. package/lib/transform/barrelManifest.js.map +1 -0
  56. package/lib/transform/generators/getExports.js +5 -0
  57. package/lib/transform/generators/getExports.js.map +1 -1
  58. package/lib/transform/generators/processEntrypoint.js +31 -1
  59. package/lib/transform/generators/processEntrypoint.js.map +1 -1
  60. package/lib/transform/generators/resolveImports.js +29 -5
  61. package/lib/transform/generators/resolveImports.js.map +1 -1
  62. package/lib/transform/generators/rewriteBarrelImports.js +743 -0
  63. package/lib/transform/generators/rewriteBarrelImports.js.map +1 -0
  64. package/lib/transform/generators/transform.js +158 -22
  65. package/lib/transform/generators/transform.js.map +1 -1
  66. package/lib/transform/types.js.map +1 -1
  67. package/lib/transform.js +45 -23
  68. package/lib/transform.js.map +1 -1
  69. package/lib/utils/collectTemplateDependencies.js +9 -0
  70. package/lib/utils/collectTemplateDependencies.js.map +1 -1
  71. package/package.json +8 -4
  72. package/types/cache.d.ts +23 -2
  73. package/types/cache.js +170 -10
  74. package/types/debug/fileReporter.d.ts +1 -0
  75. package/types/module.d.ts +3 -0
  76. package/types/module.js +65 -5
  77. package/types/plugins/shaker.js +161 -16
  78. package/types/shaker.d.ts +10 -1
  79. package/types/shaker.js +56 -28
  80. package/types/transform/BaseEntrypoint.d.ts +3 -1
  81. package/types/transform/BaseEntrypoint.js +5 -1
  82. package/types/transform/Entrypoint.d.ts +10 -1
  83. package/types/transform/Entrypoint.js +81 -23
  84. package/types/transform/EvaluatedEntrypoint.d.ts +2 -0
  85. package/types/transform/actions/BaseAction.d.ts +2 -1
  86. package/types/transform/actions/BaseAction.js +3 -1
  87. package/types/transform/actions/actionRunner.js +2 -2
  88. package/types/transform/barrelManifest.d.ts +42 -0
  89. package/types/transform/barrelManifest.js +300 -0
  90. package/types/transform/generators/getExports.js +5 -0
  91. package/types/transform/generators/processEntrypoint.js +29 -1
  92. package/types/transform/generators/resolveImports.js +29 -5
  93. package/types/transform/generators/rewriteBarrelImports.d.ts +15 -0
  94. package/types/transform/generators/rewriteBarrelImports.js +815 -0
  95. package/types/transform/generators/transform.js +148 -19
  96. package/types/transform/types.d.ts +3 -0
  97. package/types/transform.js +47 -23
  98. package/types/utils/collectTemplateDependencies.js +9 -0
@@ -0,0 +1,743 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.rewriteOptimizedBarrelImports = rewriteOptimizedBarrelImports;
7
+ var _generator = _interopRequireDefault(require("@babel/generator"));
8
+ var t = _interopRequireWildcard(require("@babel/types"));
9
+ var _EventEmitter = require("../../utils/EventEmitter");
10
+ var _shaker = require("../../shaker");
11
+ var _cache = require("../../cache");
12
+ var _Entrypoint = require("../Entrypoint");
13
+ var _barrelManifest = require("../barrelManifest");
14
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
15
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
16
+ /* eslint-disable no-continue, @typescript-eslint/no-use-before-define, @typescript-eslint/no-explicit-any, no-param-reassign, prefer-destructuring */
17
+
18
+ const createAnalysisServices = services => ({
19
+ ...services,
20
+ cache: new _cache.TransformCacheCollection({
21
+ barrelManifests: services.cache.barrelManifests,
22
+ exports: services.cache.exports
23
+ }),
24
+ eventEmitter: _EventEmitter.EventEmitter.dummy
25
+ });
26
+ const addBinding = (manifest, exported, binding, explicitExports) => {
27
+ const existing = manifest[exported];
28
+ if (explicitExports.has(exported)) {
29
+ return;
30
+ }
31
+ if (!existing) {
32
+ manifest[exported] = binding;
33
+ return;
34
+ }
35
+ if (existing.kind === 'blocked') {
36
+ return;
37
+ }
38
+ if (!isSameBinding(existing, binding)) {
39
+ manifest[exported] = {
40
+ kind: 'blocked',
41
+ reason: 'ambiguous'
42
+ };
43
+ }
44
+ };
45
+ const addBlockedExport = (manifest, exported, reason) => {
46
+ manifest[exported] = {
47
+ kind: 'blocked',
48
+ reason
49
+ };
50
+ };
51
+ const buildResolvedDependencyMap = resolvedImports => new Map(resolvedImports.map(dependency => [dependency.source, dependency]));
52
+ const cloneExportedName = name => t.isValidIdentifier(name) ? t.identifier(name) : t.stringLiteral(name);
53
+ const cloneImportedName = name => name === 'default' || t.isValidIdentifier(name) ? t.identifier(name) : t.stringLiteral(name);
54
+ const isSameBinding = (left, right) => {
55
+ if (left.kind !== right.kind) {
56
+ return false;
57
+ }
58
+ if (left.source !== right.source) {
59
+ return false;
60
+ }
61
+ if (left.kind === 'named' && right.kind === 'named') {
62
+ return left.imported === right.imported;
63
+ }
64
+ return true;
65
+ };
66
+ function createAnalysisEntrypoint(services, filename, loadedCode) {
67
+ return _Entrypoint.Entrypoint.createRoot(services, filename, ['*'], loadedCode);
68
+ }
69
+ function getUniqueSources(raw) {
70
+ return Array.from(new Set([...raw.reexports.map(reexport => reexport.source), ...raw.exportAll]));
71
+ }
72
+ function buildImportsForResolve(raw) {
73
+ return new Map(getUniqueSources(raw).map(source => [source, []]));
74
+ }
75
+ function getReexportResolutionMap(resolvedImports) {
76
+ return new Map(resolvedImports.map(dependency => [dependency.source, dependency.resolved]));
77
+ }
78
+ function isBarrelEntry(entry) {
79
+ return entry.kind === 'barrel';
80
+ }
81
+ function isRawBarrelManifest(entry) {
82
+ return 'reexports' in entry;
83
+ }
84
+ function shouldUseDirectExports(reason) {
85
+ return reason === 'impure';
86
+ }
87
+ const getModuleName = node => t.isIdentifier(node) ? node.name : node.value;
88
+ const canEmitNamedReexport = imported => imported === 'default' || t.isValidIdentifier(imported);
89
+ function collectOptimizedImports(ast) {
90
+ const imports = new Map();
91
+ const addImport = (source, imported) => {
92
+ if (!imports.has(source)) {
93
+ imports.set(source, []);
94
+ }
95
+ if (!imports.get(source).includes(imported)) {
96
+ imports.get(source).push(imported);
97
+ }
98
+ };
99
+ const program = ast.program;
100
+ for (const statement of program.body) {
101
+ if (t.isImportDeclaration(statement) && t.isStringLiteral(statement.source)) {
102
+ if (statement.specifiers.length === 0) {
103
+ addImport(statement.source.value, 'side-effect');
104
+ continue;
105
+ }
106
+ for (const specifier of statement.specifiers) {
107
+ if (t.isImportDefaultSpecifier(specifier)) {
108
+ addImport(statement.source.value, 'default');
109
+ continue;
110
+ }
111
+ if (t.isImportNamespaceSpecifier(specifier)) {
112
+ addImport(statement.source.value, '*');
113
+ continue;
114
+ }
115
+ if (t.isImportSpecifier(specifier)) {
116
+ addImport(statement.source.value, t.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value);
117
+ }
118
+ }
119
+ continue;
120
+ }
121
+ if (t.isExportNamedDeclaration(statement) && statement.source && t.isStringLiteral(statement.source)) {
122
+ for (const specifier of statement.specifiers) {
123
+ if (t.isExportNamespaceSpecifier(specifier)) {
124
+ addImport(statement.source.value, '*');
125
+ continue;
126
+ }
127
+ if (t.isExportDefaultSpecifier(specifier)) {
128
+ addImport(statement.source.value, 'default');
129
+ continue;
130
+ }
131
+ if (t.isExportSpecifier(specifier)) {
132
+ addImport(statement.source.value, getModuleName(specifier.local));
133
+ }
134
+ }
135
+ continue;
136
+ }
137
+ if (t.isExportAllDeclaration(statement) && statement.source && t.isStringLiteral(statement.source)) {
138
+ addImport(statement.source.value, '*');
139
+ }
140
+ }
141
+ return imports;
142
+ }
143
+ function groupImportSpecifiers(optimized) {
144
+ const grouped = new Map();
145
+ for (const specifier of optimized) {
146
+ if (!grouped.has(specifier.source)) {
147
+ grouped.set(specifier.source, {
148
+ defaults: [],
149
+ named: []
150
+ });
151
+ }
152
+ const bucket = grouped.get(specifier.source);
153
+ if (specifier.kind === 'default') {
154
+ bucket.defaults.push(t.importDefaultSpecifier(specifier.local));
155
+ continue;
156
+ }
157
+ bucket.named.push(t.importSpecifier(specifier.local, cloneImportedName(specifier.imported)));
158
+ }
159
+ const declarations = [];
160
+ for (const [source, bucket] of grouped) {
161
+ if (bucket.defaults.length === 0) {
162
+ declarations.push(t.importDeclaration(bucket.named, t.stringLiteral(source)));
163
+ continue;
164
+ }
165
+ declarations.push(t.importDeclaration([bucket.defaults[0], ...bucket.named], t.stringLiteral(source)));
166
+ for (const defaultSpecifier of bucket.defaults.slice(1)) {
167
+ declarations.push(t.importDeclaration([defaultSpecifier], t.stringLiteral(source)));
168
+ }
169
+ }
170
+ return declarations;
171
+ }
172
+ function groupExportSpecifiers(optimized) {
173
+ const grouped = new Map();
174
+ for (const specifier of optimized) {
175
+ if (!grouped.has(specifier.source)) {
176
+ grouped.set(specifier.source, []);
177
+ }
178
+ if (specifier.kind === 'namespace') {
179
+ grouped.get(specifier.source).push(t.exportNamespaceSpecifier(t.identifier(specifier.exported)));
180
+ continue;
181
+ }
182
+ grouped.get(specifier.source).push(t.exportSpecifier(t.identifier(specifier.imported), cloneExportedName(specifier.exported)));
183
+ }
184
+ return Array.from(grouped.entries()).map(([source, specifiers]) => t.exportNamedDeclaration(null, specifiers, t.stringLiteral(source)));
185
+ }
186
+ function createImportFallback(statement, fallbackSpecifiers) {
187
+ if (fallbackSpecifiers.length === 0) {
188
+ return null;
189
+ }
190
+ return t.importDeclaration(fallbackSpecifiers, t.stringLiteral(statement.source.value));
191
+ }
192
+ function createExportFallback(statement, fallbackSpecifiers) {
193
+ if (!statement.source || fallbackSpecifiers.length === 0) {
194
+ return null;
195
+ }
196
+ return t.exportNamedDeclaration(null, fallbackSpecifiers, t.stringLiteral(statement.source.value));
197
+ }
198
+ function emitRewriteSkipped(action, source, reason) {
199
+ action.services.eventEmitter.single({
200
+ file: action.entrypoint.name,
201
+ kind: 'barrelRewriteSkipped',
202
+ reason,
203
+ source
204
+ });
205
+ }
206
+ function getManifestExport(manifest, exported) {
207
+ var _manifest$exports$exp;
208
+ return (_manifest$exports$exp = manifest.exports[exported]) !== null && _manifest$exports$exp !== void 0 ? _manifest$exports$exp : null;
209
+ }
210
+ function* getNamedBinding(analysisServices, targetResolved, imported, stack) {
211
+ if (stack.has(targetResolved)) {
212
+ return {
213
+ kind: 'blocked',
214
+ reason: 'cycle'
215
+ };
216
+ }
217
+ const targetManifest = yield* getOrBuildBarrelManifest.call(this, analysisServices, targetResolved, stack);
218
+ if (isBarrelEntry(targetManifest)) {
219
+ const resolved = getManifestExport(targetManifest, imported);
220
+ if (resolved) {
221
+ return resolved;
222
+ }
223
+ return {
224
+ kind: 'blocked',
225
+ reason: targetManifest.complete ? 'unresolved' : 'unknown-star'
226
+ };
227
+ }
228
+ return {
229
+ kind: 'named',
230
+ imported,
231
+ source: targetResolved
232
+ };
233
+ }
234
+ function* getStarCandidates(analysisServices, targetResolved, stack) {
235
+ if (stack.has(targetResolved)) {
236
+ return {
237
+ candidates: [],
238
+ complete: false
239
+ };
240
+ }
241
+ const targetManifest = yield* getOrBuildBarrelManifest.call(this, analysisServices, targetResolved, stack);
242
+ if (isBarrelEntry(targetManifest)) {
243
+ if (!targetManifest.complete) {
244
+ return {
245
+ candidates: [],
246
+ complete: false
247
+ };
248
+ }
249
+ return {
250
+ candidates: Object.entries(targetManifest.exports).filter(([exported, binding]) => exported !== 'default' && binding.kind !== 'blocked').map(([exported, binding]) => ({
251
+ binding: binding,
252
+ exported
253
+ })),
254
+ complete: true
255
+ };
256
+ }
257
+ if (!shouldUseDirectExports(targetManifest.reason)) {
258
+ return {
259
+ candidates: [],
260
+ complete: false
261
+ };
262
+ }
263
+ const exports = yield* getExportsForFile.call(this, analysisServices, targetResolved);
264
+ return {
265
+ candidates: exports.filter(exported => exported !== 'default').map(exported => ({
266
+ binding: {
267
+ kind: 'named',
268
+ imported: exported,
269
+ source: targetResolved
270
+ },
271
+ exported
272
+ })),
273
+ complete: true
274
+ };
275
+ }
276
+ function* getExportsForFile(services, filename) {
277
+ const entrypoint = createAnalysisEntrypoint(services, filename);
278
+ return yield* this.getNext('getExports', entrypoint, undefined, null);
279
+ }
280
+ function* getOrBuildBarrelManifest(analysisServices, filename, stack = new Set()) {
281
+ const cached = this.services.cache.get('barrelManifests', filename);
282
+ if (cached) {
283
+ this.services.eventEmitter.single({
284
+ file: filename,
285
+ kind: 'barrelManifest',
286
+ status: 'hit'
287
+ });
288
+ return cached;
289
+ }
290
+ const loadedAndParsed = analysisServices.loadAndParseFn(analysisServices, filename, undefined, analysisServices.log);
291
+ if (loadedAndParsed.evaluator === 'ignored') {
292
+ const ignoredEntry = {
293
+ kind: 'ineligible',
294
+ reason: 'ignored'
295
+ };
296
+ this.services.cache.add('barrelManifests', filename, ignoredEntry);
297
+ return ignoredEntry;
298
+ }
299
+ if (loadedAndParsed.evaluator !== _shaker.shaker) {
300
+ const customEntry = {
301
+ kind: 'ineligible',
302
+ reason: 'custom-evaluator'
303
+ };
304
+ this.services.cache.add('barrelManifests', filename, customEntry);
305
+ return customEntry;
306
+ }
307
+ const analyzed = (0, _barrelManifest.analyzeBarrelFile)(loadedAndParsed.ast);
308
+ if (!isRawBarrelManifest(analyzed)) {
309
+ this.services.cache.add('barrelManifests', filename, analyzed);
310
+ return analyzed;
311
+ }
312
+ const resolveEntrypoint = createAnalysisEntrypoint(analysisServices, filename, loadedAndParsed.code);
313
+ const resolvedImports = yield* this.getNext('resolveImports', resolveEntrypoint, {
314
+ imports: buildImportsForResolve(analyzed)
315
+ }, null);
316
+ const manifestDependencies = resolvedImports.flatMap(dependency => dependency.resolved ? [dependency.resolved] : []);
317
+ const resolutionMap = getReexportResolutionMap(resolvedImports);
318
+ const manifest = {
319
+ complete: analyzed.complete,
320
+ exports: {},
321
+ kind: 'barrel'
322
+ };
323
+ const explicitExports = new Set(analyzed.explicitExports);
324
+ const nextStack = new Set(stack);
325
+ nextStack.add(filename);
326
+ for (const reexport of analyzed.reexports) {
327
+ explicitExports.add(reexport.exported);
328
+ const targetResolved = resolutionMap.get(reexport.source);
329
+ if (!targetResolved) {
330
+ manifest.complete = false;
331
+ addBlockedExport(manifest.exports, reexport.exported, 'unresolved');
332
+ continue;
333
+ }
334
+ if (reexport.kind === 'namespace') {
335
+ const namespaceBinding = yield* getNamespaceBinding.call(this, analysisServices, targetResolved, nextStack);
336
+ manifest.exports[reexport.exported] = namespaceBinding;
337
+ if (namespaceBinding.kind === 'blocked') {
338
+ manifest.complete = false;
339
+ }
340
+ continue;
341
+ }
342
+ const binding = yield* getNamedBinding.call(this, analysisServices, targetResolved, reexport.imported, nextStack);
343
+ manifest.exports[reexport.exported] = binding;
344
+ if (binding.kind === 'blocked') {
345
+ manifest.complete = false;
346
+ }
347
+ }
348
+ for (const exportAllSource of analyzed.exportAll) {
349
+ const targetResolved = resolutionMap.get(exportAllSource);
350
+ if (!targetResolved) {
351
+ manifest.complete = false;
352
+ continue;
353
+ }
354
+ const {
355
+ candidates,
356
+ complete
357
+ } = yield* getStarCandidates.call(this, analysisServices, targetResolved, nextStack);
358
+ if (!complete) {
359
+ manifest.complete = false;
360
+ continue;
361
+ }
362
+ for (const candidate of candidates) {
363
+ addBinding(manifest.exports, candidate.exported, candidate.binding, explicitExports);
364
+ }
365
+ }
366
+ this.services.cache.add('barrelManifests', filename, manifest);
367
+ this.services.cache.setCacheDependencies('barrelManifests', filename, manifestDependencies);
368
+ this.services.eventEmitter.single({
369
+ complete: manifest.complete,
370
+ file: filename,
371
+ kind: 'barrelManifest',
372
+ status: 'built'
373
+ });
374
+ return manifest;
375
+ }
376
+ function* getNamespaceBinding(analysisServices, targetResolved, stack) {
377
+ if (stack.has(targetResolved)) {
378
+ return {
379
+ kind: 'blocked',
380
+ reason: 'cycle'
381
+ };
382
+ }
383
+ const targetManifest = yield* getOrBuildBarrelManifest.call(this, analysisServices, targetResolved, stack);
384
+ if (isBarrelEntry(targetManifest)) {
385
+ return {
386
+ kind: 'blocked',
387
+ reason: 'namespace-barrel'
388
+ };
389
+ }
390
+ return {
391
+ kind: 'namespace',
392
+ source: targetResolved
393
+ };
394
+ }
395
+ function getResolvedDependency(dependencies, source) {
396
+ return dependencies.get(source);
397
+ }
398
+ function* rewriteImportDeclaration(analysisServices, statement, dependencies) {
399
+ const dependency = getResolvedDependency(dependencies, statement.source.value);
400
+ if (!(dependency !== null && dependency !== void 0 && dependency.resolved)) {
401
+ return {
402
+ generatedSources: [],
403
+ mode: 'unchanged',
404
+ statements: [statement]
405
+ };
406
+ }
407
+ const manifest = yield* getOrBuildBarrelManifest.call(this, analysisServices, dependency.resolved);
408
+ if (!isBarrelEntry(manifest)) {
409
+ return {
410
+ generatedSources: [],
411
+ mode: 'unchanged',
412
+ statements: [statement]
413
+ };
414
+ }
415
+ const optimized = [];
416
+ const fallback = [];
417
+ for (const specifier of statement.specifiers) {
418
+ if (t.isImportNamespaceSpecifier(specifier)) {
419
+ fallback.push(specifier);
420
+ continue;
421
+ }
422
+ if (t.isImportDefaultSpecifier(specifier)) {
423
+ const binding = getManifestExport(manifest, 'default');
424
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === 'named' && binding.imported === 'default') {
425
+ optimized.push({
426
+ kind: 'default',
427
+ local: specifier.local,
428
+ source: binding.source
429
+ });
430
+ continue;
431
+ }
432
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === 'blocked') {
433
+ emitRewriteSkipped(this, statement.source.value, binding.reason);
434
+ }
435
+ fallback.push(specifier);
436
+ continue;
437
+ }
438
+ const imported = t.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value;
439
+ const binding = getManifestExport(manifest, imported);
440
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === 'named') {
441
+ optimized.push({
442
+ imported: binding.imported,
443
+ kind: 'named',
444
+ local: specifier.local,
445
+ source: binding.source
446
+ });
447
+ continue;
448
+ }
449
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === 'blocked') {
450
+ emitRewriteSkipped(this, statement.source.value, binding.reason);
451
+ }
452
+ fallback.push(specifier);
453
+ }
454
+ if (optimized.length === 0) {
455
+ return {
456
+ generatedSources: [],
457
+ mode: 'unchanged',
458
+ statements: [statement]
459
+ };
460
+ }
461
+ const rewritten = groupImportSpecifiers(optimized);
462
+ const fallbackDeclaration = createImportFallback(statement, fallback);
463
+ const mode = fallbackDeclaration ? 'partial' : 'full';
464
+ this.services.eventEmitter.single({
465
+ file: this.entrypoint.name,
466
+ kind: 'barrelRewrite',
467
+ mode,
468
+ optimized: optimized.length,
469
+ source: statement.source.value
470
+ });
471
+ return {
472
+ generatedSources: Array.from(new Set(optimized.map(specifier => specifier.source))),
473
+ mode,
474
+ statements: fallbackDeclaration ? [fallbackDeclaration, ...rewritten] : rewritten
475
+ };
476
+ }
477
+ function* rewriteExportNamedDeclaration(analysisServices, statement, dependencies) {
478
+ if (!statement.source || !t.isStringLiteral(statement.source)) {
479
+ return {
480
+ generatedSources: [],
481
+ mode: 'unchanged',
482
+ statements: [statement]
483
+ };
484
+ }
485
+ const dependency = getResolvedDependency(dependencies, statement.source.value);
486
+ if (!(dependency !== null && dependency !== void 0 && dependency.resolved)) {
487
+ return {
488
+ generatedSources: [],
489
+ mode: 'unchanged',
490
+ statements: [statement]
491
+ };
492
+ }
493
+ const manifest = yield* getOrBuildBarrelManifest.call(this, analysisServices, dependency.resolved);
494
+ if (!isBarrelEntry(manifest)) {
495
+ return {
496
+ generatedSources: [],
497
+ mode: 'unchanged',
498
+ statements: [statement]
499
+ };
500
+ }
501
+ const optimized = [];
502
+ const fallback = [];
503
+ for (const specifier of statement.specifiers) {
504
+ if (t.isExportNamespaceSpecifier(specifier)) {
505
+ fallback.push(specifier);
506
+ continue;
507
+ }
508
+ if (t.isExportDefaultSpecifier(specifier)) {
509
+ const binding = getManifestExport(manifest, 'default');
510
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === 'named' && binding.imported === 'default') {
511
+ optimized.push({
512
+ exported: getModuleName(specifier.exported),
513
+ imported: 'default',
514
+ kind: 'named',
515
+ source: binding.source
516
+ });
517
+ continue;
518
+ }
519
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === 'blocked') {
520
+ emitRewriteSkipped(this, statement.source.value, binding.reason);
521
+ }
522
+ fallback.push(specifier);
523
+ continue;
524
+ }
525
+ const imported = getModuleName(specifier.local);
526
+ const exported = getModuleName(specifier.exported);
527
+ const binding = getManifestExport(manifest, imported);
528
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === 'named' && canEmitNamedReexport(binding.imported)) {
529
+ optimized.push({
530
+ exported,
531
+ imported: binding.imported,
532
+ kind: 'named',
533
+ source: binding.source
534
+ });
535
+ continue;
536
+ }
537
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === 'namespace' && t.isValidIdentifier(exported)) {
538
+ optimized.push({
539
+ exported,
540
+ kind: 'namespace',
541
+ source: binding.source
542
+ });
543
+ continue;
544
+ }
545
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === 'blocked') {
546
+ emitRewriteSkipped(this, statement.source.value, binding.reason);
547
+ }
548
+ fallback.push(specifier);
549
+ }
550
+ if (optimized.length === 0) {
551
+ return {
552
+ generatedSources: [],
553
+ mode: 'unchanged',
554
+ statements: [statement]
555
+ };
556
+ }
557
+ const rewritten = groupExportSpecifiers(optimized);
558
+ const fallbackDeclaration = createExportFallback(statement, fallback);
559
+ const mode = fallbackDeclaration ? 'partial' : 'full';
560
+ this.services.eventEmitter.single({
561
+ file: this.entrypoint.name,
562
+ kind: 'barrelRewrite',
563
+ mode,
564
+ optimized: optimized.length,
565
+ source: statement.source.value
566
+ });
567
+ return {
568
+ generatedSources: Array.from(new Set(optimized.map(specifier => specifier.source))),
569
+ mode,
570
+ statements: fallbackDeclaration ? [fallbackDeclaration, ...rewritten] : rewritten
571
+ };
572
+ }
573
+ function* rewriteExportAllDeclaration(analysisServices, statement, dependencies) {
574
+ const dependency = getResolvedDependency(dependencies, statement.source.value);
575
+ if (!(dependency !== null && dependency !== void 0 && dependency.resolved)) {
576
+ return {
577
+ generatedSources: [],
578
+ mode: 'unchanged',
579
+ statements: [statement]
580
+ };
581
+ }
582
+ const manifest = yield* getOrBuildBarrelManifest.call(this, analysisServices, dependency.resolved);
583
+ if (!isBarrelEntry(manifest) || !manifest.complete) {
584
+ return {
585
+ generatedSources: [],
586
+ mode: 'unchanged',
587
+ statements: [statement]
588
+ };
589
+ }
590
+ const optimized = [];
591
+ let hasUnrewritableExport = false;
592
+ for (const [exported, binding] of Object.entries(manifest.exports)) {
593
+ if (exported === 'default' || binding.kind === 'blocked') {
594
+ continue;
595
+ }
596
+ if (binding.kind === 'namespace') {
597
+ if (!t.isValidIdentifier(exported)) {
598
+ emitRewriteSkipped(this, statement.source.value, 'namespace-barrel');
599
+ hasUnrewritableExport = true;
600
+ continue;
601
+ }
602
+ optimized.push({
603
+ exported,
604
+ kind: 'namespace',
605
+ source: binding.source
606
+ });
607
+ continue;
608
+ }
609
+ if (!canEmitNamedReexport(binding.imported)) {
610
+ emitRewriteSkipped(this, statement.source.value, 'unknown-star');
611
+ hasUnrewritableExport = true;
612
+ continue;
613
+ }
614
+ optimized.push({
615
+ exported,
616
+ imported: binding.imported,
617
+ kind: 'named',
618
+ source: binding.source
619
+ });
620
+ }
621
+ if (hasUnrewritableExport) {
622
+ return {
623
+ generatedSources: [],
624
+ mode: 'unchanged',
625
+ statements: [statement]
626
+ };
627
+ }
628
+ if (optimized.length === 0) {
629
+ this.services.eventEmitter.single({
630
+ file: this.entrypoint.name,
631
+ kind: 'barrelRewrite',
632
+ mode: 'full',
633
+ optimized: 0,
634
+ source: statement.source.value
635
+ });
636
+ return {
637
+ generatedSources: [],
638
+ mode: 'full',
639
+ statements: []
640
+ };
641
+ }
642
+ this.services.eventEmitter.single({
643
+ complete: true,
644
+ file: this.entrypoint.name,
645
+ kind: 'barrelRewrite',
646
+ mode: 'full',
647
+ optimized: optimized.length,
648
+ source: statement.source.value
649
+ });
650
+ return {
651
+ generatedSources: Array.from(new Set(optimized.map(specifier => specifier.source))),
652
+ mode: 'full',
653
+ statements: groupExportSpecifiers(optimized)
654
+ };
655
+ }
656
+ function* rewriteOptimizedBarrelImports(ast, code, resolvedImports) {
657
+ const dependencies = buildResolvedDependencyMap(resolvedImports);
658
+ const analysisServices = createAnalysisServices(this.services);
659
+ const nextBody = [];
660
+ const generatedSources = new Set();
661
+ let optimizedCount = 0;
662
+ const sourceModes = new Map();
663
+ let skippedCount = 0;
664
+ const recordSourceMode = (source, mode, statementChanged) => {
665
+ if (mode === 'unchanged') {
666
+ return;
667
+ }
668
+ if (statementChanged) {
669
+ optimizedCount += 1;
670
+ }
671
+ if (mode === 'partial') {
672
+ sourceModes.set(source, 'partial');
673
+ return;
674
+ }
675
+ if (!sourceModes.has(source)) {
676
+ sourceModes.set(source, 'full');
677
+ }
678
+ };
679
+ for (const statement of ast.program.body) {
680
+ if (t.isImportDeclaration(statement) && t.isStringLiteral(statement.source)) {
681
+ const rewritten = yield* rewriteImportDeclaration.call(this, analysisServices, statement, dependencies);
682
+ recordSourceMode(statement.source.value, rewritten.mode, !(rewritten.statements.length === 1 && rewritten.statements[0] === statement));
683
+ for (const source of rewritten.generatedSources) {
684
+ generatedSources.add(source);
685
+ }
686
+ nextBody.push(...rewritten.statements);
687
+ continue;
688
+ }
689
+ if (t.isExportNamedDeclaration(statement) && statement.source && t.isStringLiteral(statement.source)) {
690
+ const rewritten = yield* rewriteExportNamedDeclaration.call(this, analysisServices, statement, dependencies);
691
+ recordSourceMode(statement.source.value, rewritten.mode, !(rewritten.statements.length === 1 && rewritten.statements[0] === statement));
692
+ for (const source of rewritten.generatedSources) {
693
+ generatedSources.add(source);
694
+ }
695
+ nextBody.push(...rewritten.statements);
696
+ continue;
697
+ }
698
+ if (t.isExportAllDeclaration(statement) && statement.source && t.isStringLiteral(statement.source)) {
699
+ const rewritten = yield* rewriteExportAllDeclaration.call(this, analysisServices, statement, dependencies);
700
+ if (rewritten.mode === 'unchanged' && rewritten.statements.length === 1 && rewritten.statements[0] === statement) {
701
+ skippedCount += 1;
702
+ }
703
+ recordSourceMode(statement.source.value, rewritten.mode, !(rewritten.statements.length === 1 && rewritten.statements[0] === statement));
704
+ for (const source of rewritten.generatedSources) {
705
+ generatedSources.add(source);
706
+ }
707
+ nextBody.push(...rewritten.statements);
708
+ continue;
709
+ }
710
+ nextBody.push(statement);
711
+ }
712
+ ast.program.body = nextBody;
713
+ const rewrittenCode = (0, _generator.default)(ast).code;
714
+ const imports = collectOptimizedImports(ast);
715
+ const preResolvedImports = Array.from(imports.entries()).flatMap(([source, only]) => {
716
+ const dependency = dependencies.get(source);
717
+ if (dependency) {
718
+ return [{
719
+ ...dependency,
720
+ only
721
+ }];
722
+ }
723
+ if (generatedSources.has(source)) {
724
+ return [{
725
+ only,
726
+ resolved: source,
727
+ source
728
+ }];
729
+ }
730
+ return [];
731
+ });
732
+ return {
733
+ ast,
734
+ code: rewrittenCode,
735
+ fullyRewrittenSources: Array.from(sourceModes.entries()).filter(([, mode]) => mode === 'full').map(([source]) => source),
736
+ imports,
737
+ optimizedCount,
738
+ partialFallbackSources: Array.from(sourceModes.entries()).filter(([, mode]) => mode === 'partial').map(([source]) => source),
739
+ preResolvedImports,
740
+ skippedCount
741
+ };
742
+ }
743
+ //# sourceMappingURL=rewriteBarrelImports.js.map