rollup-plugin-iife-split 0.0.2 → 0.0.3
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/dist/index.d.ts +4 -2
- package/dist/index.js +72 -41
- package/package.json +8 -3
package/dist/index.d.ts
CHANGED
|
@@ -39,9 +39,11 @@ interface IifeSplitOptions {
|
|
|
39
39
|
*/
|
|
40
40
|
unshared?: (id: string) => boolean;
|
|
41
41
|
/**
|
|
42
|
-
*
|
|
42
|
+
* Directory to write intermediate files for debugging.
|
|
43
|
+
* If set, writes ESM files before IIFE conversion to help diagnose issues.
|
|
44
|
+
* Example: './debug-output'
|
|
43
45
|
*/
|
|
44
|
-
|
|
46
|
+
debugDir?: string;
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
declare function iifeSplit(options: IifeSplitOptions): Plugin;
|
package/dist/index.js
CHANGED
|
@@ -158,18 +158,8 @@ function destructureSharedParameter(code, mappings, parse) {
|
|
|
158
158
|
return ms.toString();
|
|
159
159
|
}
|
|
160
160
|
async function convertToIife(options) {
|
|
161
|
-
const { code, globalName, globals, sharedGlobalPath, sharedChunkFileName, parse
|
|
161
|
+
const { code, globalName, globals, sharedGlobalPath, sharedChunkFileName, parse } = options;
|
|
162
162
|
const importMappings = sharedGlobalPath ? extractSharedImportMappings(code, parse) : [];
|
|
163
|
-
if (debug && sharedGlobalPath) {
|
|
164
|
-
console.log("\n=== DEBUG convertToIife ===");
|
|
165
|
-
console.log("globalName:", globalName);
|
|
166
|
-
console.log("sharedGlobalPath:", sharedGlobalPath);
|
|
167
|
-
console.log("sharedChunkFileName:", sharedChunkFileName);
|
|
168
|
-
console.log("--- ESM code (first 500 chars) ---");
|
|
169
|
-
console.log(code.slice(0, 500));
|
|
170
|
-
console.log("--- Import mappings ---");
|
|
171
|
-
console.log(importMappings);
|
|
172
|
-
}
|
|
173
163
|
const rollupGlobals = (id) => {
|
|
174
164
|
if (sharedGlobalPath) {
|
|
175
165
|
if (id.includes(SHARED_CHUNK_NAME)) {
|
|
@@ -199,18 +189,9 @@ async function convertToIife(options) {
|
|
|
199
189
|
});
|
|
200
190
|
await bundle.close();
|
|
201
191
|
let result = output[0].code;
|
|
202
|
-
if (debug && sharedGlobalPath) {
|
|
203
|
-
console.log("--- IIFE before destructuring (first 800 chars) ---");
|
|
204
|
-
console.log(result.slice(0, 800));
|
|
205
|
-
}
|
|
206
192
|
if (sharedGlobalPath && importMappings.length > 0) {
|
|
207
193
|
result = stripNamespaceGuards(result);
|
|
208
194
|
result = destructureSharedParameter(result, importMappings, parse);
|
|
209
|
-
if (debug) {
|
|
210
|
-
console.log("--- IIFE after destructuring (first 800 chars) ---");
|
|
211
|
-
console.log(result.slice(0, 800));
|
|
212
|
-
console.log("=== END DEBUG ===\n");
|
|
213
|
-
}
|
|
214
195
|
} else if (globalName && !globalName.includes(".")) {
|
|
215
196
|
} else if (globalName) {
|
|
216
197
|
result = stripNamespaceGuards(result);
|
|
@@ -347,6 +328,7 @@ function removeSharedImportsAndRewriteRefs(code, sharedChunkFileName, sharedExpo
|
|
|
347
328
|
const ast = parse(code);
|
|
348
329
|
const s = new MagicString2(code);
|
|
349
330
|
const namespaceNames = /* @__PURE__ */ new Set();
|
|
331
|
+
const namedImportRenames = /* @__PURE__ */ new Map();
|
|
350
332
|
walk2(ast, {
|
|
351
333
|
enter(node) {
|
|
352
334
|
if (node.type === "ImportDeclaration") {
|
|
@@ -356,6 +338,19 @@ function removeSharedImportsAndRewriteRefs(code, sharedChunkFileName, sharedExpo
|
|
|
356
338
|
for (const spec of importNode.specifiers) {
|
|
357
339
|
if (spec.type === "ImportNamespaceSpecifier") {
|
|
358
340
|
namespaceNames.add(spec.local.name);
|
|
341
|
+
} else if (spec.type === "ImportSpecifier" && spec.imported) {
|
|
342
|
+
const exportedName = spec.imported.name;
|
|
343
|
+
const localAlias = spec.local.name;
|
|
344
|
+
const actualLocal = sharedExportToLocal.get(exportedName) ?? exportedName;
|
|
345
|
+
if (localAlias !== actualLocal) {
|
|
346
|
+
namedImportRenames.set(localAlias, actualLocal);
|
|
347
|
+
}
|
|
348
|
+
} else if (spec.type === "ImportDefaultSpecifier") {
|
|
349
|
+
const localAlias = spec.local.name;
|
|
350
|
+
const actualLocal = sharedExportToLocal.get("default") ?? "__shared_default__";
|
|
351
|
+
if (localAlias !== actualLocal) {
|
|
352
|
+
namedImportRenames.set(localAlias, actualLocal);
|
|
353
|
+
}
|
|
359
354
|
}
|
|
360
355
|
}
|
|
361
356
|
}
|
|
@@ -369,6 +364,7 @@ function removeSharedImportsAndRewriteRefs(code, sharedChunkFileName, sharedExpo
|
|
|
369
364
|
const source = importNode.source.value;
|
|
370
365
|
if (typeof source === "string" && isSharedChunkSource(source, sharedChunkFileName)) {
|
|
371
366
|
s.remove(importNode.start, importNode.end);
|
|
367
|
+
this.skip();
|
|
372
368
|
}
|
|
373
369
|
}
|
|
374
370
|
if (node.type === "ExportNamedDeclaration") {
|
|
@@ -388,6 +384,7 @@ function removeSharedImportsAndRewriteRefs(code, sharedChunkFileName, sharedExpo
|
|
|
388
384
|
}
|
|
389
385
|
}
|
|
390
386
|
s.overwrite(exportNode.start, exportNode.end, `export { ${exportParts.join(", ")} };`);
|
|
387
|
+
this.skip();
|
|
391
388
|
}
|
|
392
389
|
}
|
|
393
390
|
}
|
|
@@ -397,6 +394,14 @@ function removeSharedImportsAndRewriteRefs(code, sharedChunkFileName, sharedExpo
|
|
|
397
394
|
const propertyName = memberNode.property.name;
|
|
398
395
|
const localName = sharedExportToLocal.get(propertyName) ?? propertyName;
|
|
399
396
|
s.overwrite(memberNode.start, memberNode.end, localName);
|
|
397
|
+
this.skip();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (node.type === "Identifier" && namedImportRenames.size > 0) {
|
|
401
|
+
const id = node;
|
|
402
|
+
const newName = namedImportRenames.get(id.name);
|
|
403
|
+
if (newName) {
|
|
404
|
+
s.overwrite(id.start, id.end, newName);
|
|
400
405
|
}
|
|
401
406
|
}
|
|
402
407
|
}
|
|
@@ -494,7 +499,7 @@ function removeChunkImportsAndRewriteRefs(code, chunkFileName, exportToLocal, pa
|
|
|
494
499
|
function chunkImportsFrom(chunk, sourceChunkFileName) {
|
|
495
500
|
return chunk.imports.some((imp) => isChunkSource(imp, sourceChunkFileName));
|
|
496
501
|
}
|
|
497
|
-
function mergeUnsharedIntoImporters(unsharedChunk, entryChunks, parse
|
|
502
|
+
function mergeUnsharedIntoImporters(unsharedChunk, entryChunks, parse) {
|
|
498
503
|
const { exports: unsharedExports, hasDefault } = extractExports(unsharedChunk.code, parse);
|
|
499
504
|
const exportToLocal = /* @__PURE__ */ new Map();
|
|
500
505
|
for (const exp of unsharedExports) {
|
|
@@ -534,16 +539,12 @@ function mergeUnsharedIntoImporters(unsharedChunk, entryChunks, parse, debug) {
|
|
|
534
539
|
localExportToLocal,
|
|
535
540
|
parse
|
|
536
541
|
);
|
|
537
|
-
entry.code =
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
"",
|
|
541
|
-
debug && "// === Entry code ===",
|
|
542
|
-
entryWithoutImports.trim()
|
|
543
|
-
].filter((line) => line !== false).join("\n");
|
|
542
|
+
entry.code = `${codeToInline.trim()}
|
|
543
|
+
|
|
544
|
+
${entryWithoutImports.trim()}`;
|
|
544
545
|
}
|
|
545
546
|
}
|
|
546
|
-
function mergeSharedIntoPrimary(primaryChunk, sharedChunk, sharedProperty, neededExports, parse
|
|
547
|
+
function mergeSharedIntoPrimary(primaryChunk, sharedChunk, sharedProperty, neededExports, parse) {
|
|
547
548
|
const { exports: sharedExports, hasDefault } = extractExports(sharedChunk.code, parse);
|
|
548
549
|
const sharedDeclarations = extractTopLevelDeclarations(sharedChunk.code, parse);
|
|
549
550
|
const primaryDeclarations = extractTopLevelDeclarations(primaryChunk.code, parse);
|
|
@@ -581,21 +582,30 @@ function mergeSharedIntoPrimary(primaryChunk, sharedChunk, sharedProperty, neede
|
|
|
581
582
|
];
|
|
582
583
|
const sharedExportObject = `const ${sharedProperty} = { ${sharedExportEntries.join(", ")} };`;
|
|
583
584
|
primaryChunk.code = [
|
|
584
|
-
debug && "// === Shared code (merged by rollup-plugin-iife-split) ===",
|
|
585
585
|
strippedSharedCode.trim(),
|
|
586
586
|
"",
|
|
587
|
-
debug && "// === Primary entry code ===",
|
|
588
587
|
primaryWithoutSharedImports.trim(),
|
|
589
588
|
"",
|
|
590
|
-
debug && "// === Shared exports object ===",
|
|
591
589
|
sharedExportObject,
|
|
592
590
|
`export { ${sharedProperty} };`
|
|
593
|
-
].
|
|
591
|
+
].join("\n");
|
|
594
592
|
}
|
|
595
593
|
|
|
596
594
|
// src/index.ts
|
|
595
|
+
import { writeFileSync, mkdirSync } from "fs";
|
|
596
|
+
import { join } from "path";
|
|
597
597
|
function iifeSplit(options) {
|
|
598
|
-
const { primary, primaryGlobal, secondaryProps, sharedProp, unshared,
|
|
598
|
+
const { primary, primaryGlobal, secondaryProps, sharedProp, unshared, debugDir } = options;
|
|
599
|
+
const sanitizeName = (name) => name.replace(/[/\\]/g, "-");
|
|
600
|
+
const writeDebugFile = (filename, content) => {
|
|
601
|
+
if (!debugDir) return;
|
|
602
|
+
try {
|
|
603
|
+
mkdirSync(debugDir, { recursive: true });
|
|
604
|
+
writeFileSync(join(debugDir, filename), content);
|
|
605
|
+
} catch (e) {
|
|
606
|
+
console.warn(`[iife-split] Failed to write debug file ${filename}:`, e);
|
|
607
|
+
}
|
|
608
|
+
};
|
|
599
609
|
let outputGlobals = {};
|
|
600
610
|
const manualChunks = (id, { getModuleInfo }) => {
|
|
601
611
|
const moduleInfo = getModuleInfo(id);
|
|
@@ -625,6 +635,18 @@ function iifeSplit(options) {
|
|
|
625
635
|
async generateBundle(outputOptions, bundle) {
|
|
626
636
|
const parse = this.parse.bind(this);
|
|
627
637
|
const analysis = analyzeChunks(bundle, primary);
|
|
638
|
+
if (debugDir) {
|
|
639
|
+
writeDebugFile("1-primary-original.js", analysis.primaryChunk.code);
|
|
640
|
+
if (analysis.sharedChunk) {
|
|
641
|
+
writeDebugFile("1-shared-original.js", analysis.sharedChunk.code);
|
|
642
|
+
}
|
|
643
|
+
for (const satellite of analysis.satelliteChunks) {
|
|
644
|
+
writeDebugFile(`1-satellite-${sanitizeName(satellite.name)}-original.js`, satellite.code);
|
|
645
|
+
}
|
|
646
|
+
for (const unshared2 of analysis.unsharedChunks) {
|
|
647
|
+
writeDebugFile(`1-unshared-${sanitizeName(unshared2.name)}-original.js`, unshared2.code);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
628
650
|
const sharedChunkFileName = analysis.sharedChunk?.fileName ?? null;
|
|
629
651
|
if (analysis.sharedChunk) {
|
|
630
652
|
const neededExports = /* @__PURE__ */ new Set();
|
|
@@ -634,21 +656,32 @@ function iifeSplit(options) {
|
|
|
634
656
|
neededExports.add(imp);
|
|
635
657
|
}
|
|
636
658
|
}
|
|
659
|
+
if (debugDir) {
|
|
660
|
+
writeDebugFile("2-needed-exports.json", JSON.stringify(Array.from(neededExports), null, 2));
|
|
661
|
+
}
|
|
637
662
|
mergeSharedIntoPrimary(
|
|
638
663
|
analysis.primaryChunk,
|
|
639
664
|
analysis.sharedChunk,
|
|
640
665
|
sharedProp,
|
|
641
666
|
neededExports,
|
|
642
|
-
parse
|
|
643
|
-
debug
|
|
667
|
+
parse
|
|
644
668
|
);
|
|
645
669
|
delete bundle[analysis.sharedChunk.fileName];
|
|
646
670
|
}
|
|
671
|
+
if (debugDir) {
|
|
672
|
+
writeDebugFile("2-primary-after-shared-merge.js", analysis.primaryChunk.code);
|
|
673
|
+
}
|
|
647
674
|
const allEntries = [analysis.primaryChunk, ...analysis.satelliteChunks];
|
|
648
675
|
for (const unsharedChunk of analysis.unsharedChunks) {
|
|
649
|
-
mergeUnsharedIntoImporters(unsharedChunk, allEntries, parse
|
|
676
|
+
mergeUnsharedIntoImporters(unsharedChunk, allEntries, parse);
|
|
650
677
|
delete bundle[unsharedChunk.fileName];
|
|
651
678
|
}
|
|
679
|
+
if (debugDir) {
|
|
680
|
+
writeDebugFile("3-primary-before-iife.js", analysis.primaryChunk.code);
|
|
681
|
+
for (const satellite of analysis.satelliteChunks) {
|
|
682
|
+
writeDebugFile(`3-satellite-${sanitizeName(satellite.name)}-before-iife.js`, satellite.code);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
652
685
|
const conversions = [];
|
|
653
686
|
conversions.push(
|
|
654
687
|
convertToIife({
|
|
@@ -658,8 +691,7 @@ function iifeSplit(options) {
|
|
|
658
691
|
sharedGlobalPath: null,
|
|
659
692
|
// Primary doesn't need to import shared
|
|
660
693
|
sharedChunkFileName: null,
|
|
661
|
-
parse
|
|
662
|
-
debug
|
|
694
|
+
parse
|
|
663
695
|
}).then((code) => {
|
|
664
696
|
analysis.primaryChunk.code = code;
|
|
665
697
|
})
|
|
@@ -680,8 +712,7 @@ function iifeSplit(options) {
|
|
|
680
712
|
globals: outputGlobals,
|
|
681
713
|
sharedGlobalPath: `${primaryGlobal}.${sharedProp}`,
|
|
682
714
|
sharedChunkFileName,
|
|
683
|
-
parse
|
|
684
|
-
debug
|
|
715
|
+
parse
|
|
685
716
|
}).then((code) => {
|
|
686
717
|
satellite.code = code;
|
|
687
718
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rollup-plugin-iife-split",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Rollup plugin for intelligent IIFE code-splitting",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -30,11 +30,16 @@
|
|
|
30
30
|
"bundle"
|
|
31
31
|
],
|
|
32
32
|
"license": "MIT",
|
|
33
|
+
"homepage": "https://github.com/arshaw/rollup-plugin-iife-split",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/arshaw/rollup-plugin-iife-split.git"
|
|
37
|
+
},
|
|
33
38
|
"scripts": {
|
|
34
39
|
"clean": "rm -rf dist",
|
|
35
40
|
"build": "tsup src/index.ts --format esm --dts",
|
|
36
41
|
"typecheck": "tsc --noEmit",
|
|
37
|
-
"test": "vitest",
|
|
38
|
-
"test:
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"test:dev": "vitest"
|
|
39
44
|
}
|
|
40
45
|
}
|