screw-up 1.22.0 → 1.23.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/dist/index.cjs CHANGED
@@ -1,11 +1,11 @@
1
1
  /*!
2
2
  * name: screw-up
3
- * version: 1.22.0
3
+ * version: 1.23.0
4
4
  * description: Simply package metadata inserter on Vite plugin
5
5
  * author: Kouji Matsui (@kekyo@mi.kekyo.net)
6
6
  * license: MIT
7
7
  * repository.url: https://github.com/kekyo/screw-up.git
8
- * git.commit.hash: fe583d081fa3f9c395edece7548ec4535bc5e646
8
+ * git.commit.hash: 40f2edb1e98e9724413f5a23b820fc41e8f0b4d6
9
9
  */
10
10
 
11
11
  "use strict";
@@ -34,7 +34,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
34
34
  const fs$1 = require("fs/promises");
35
35
  const fs = require("fs");
36
36
  const path = require("path");
37
- const packageMetadata = require("./metadata-file-vLRNRjsl.cjs");
37
+ const packageMetadata = require("./metadata-file-o2vGfdZO.cjs");
38
+ const crypto = require("crypto");
38
39
  const __NOOP_HANDLER = () => {
39
40
  };
40
41
  const __NOOP_RELEASABLE = {
@@ -403,9 +404,46 @@ const scanHasDefaultImport = (ts, code) => {
403
404
  }
404
405
  return false;
405
406
  };
406
- const helperFunctionSource = `function __resolveDefaultExport<T>(module: T | { default?: T }): T {
407
- return (module as { default?: T }).default ?? (module as T);
407
+ const cjsInteropFlagPrefix = "__isInCJS_";
408
+ const cjsInteropIdLength = 12;
409
+ const cjsInteropFlagAssignmentPattern = new RegExp(
410
+ `\\bvar\\s+${cjsInteropFlagPrefix}[0-9a-f]+(?:\\$\\d+)?\\s*=\\s*false\\b`,
411
+ "g"
412
+ );
413
+ const createCjsInteropHelperId = (seed) => {
414
+ const hash = crypto.createHash("sha256").update(seed).digest("hex");
415
+ return hash.slice(0, cjsInteropIdLength);
416
+ };
417
+ const buildHelperFunctionSource = (helperId) => `
418
+ function __resolveDefaultExport<T>(module: T | { default?: T }, isESM: boolean): T {
419
+ var ${cjsInteropFlagPrefix}${helperId}: boolean = false;
420
+ const maybe = module as { default?: T };
421
+
422
+ if (${cjsInteropFlagPrefix}${helperId}) {
423
+ return maybe && typeof maybe === 'object' && 'default' in maybe
424
+ ? (maybe.default ?? (module as T))
425
+ : (module as T);
426
+ }
427
+
428
+ if (isESM) {
429
+ if (maybe && typeof maybe === 'object' && 'default' in maybe) {
430
+ return maybe.default as T;
431
+ }
432
+ throw new Error('Default export not found.');
433
+ }
434
+
435
+ return maybe && typeof maybe === 'object' && 'default' in maybe
436
+ ? (maybe.default ?? (module as T))
437
+ : (module as T);
408
438
  }`;
439
+ const replaceCjsInteropFlag = (code) => {
440
+ let changed = false;
441
+ const nextCode = code.replace(cjsInteropFlagAssignmentPattern, (match) => {
442
+ changed = true;
443
+ return match.replace(/\bfalse\b/, "true ");
444
+ });
445
+ return { code: nextCode, changed };
446
+ };
409
447
  const hasResolveDefaultExport = (ts, sourceFile) => {
410
448
  return sourceFile.statements.some((statement) => {
411
449
  var _a, _b;
@@ -524,9 +562,10 @@ const transformDefaultImports = async (ts, code, id, resolveModuleKind) => {
524
562
  }
525
563
  const moduleName = statement.moduleSpecifier.text;
526
564
  const moduleKind = await resolveModuleKind(moduleName, normalizedId);
527
- if (moduleKind !== "cjs") {
565
+ if (moduleKind === "unknown" || moduleKind === "unresolvable") {
528
566
  continue;
529
567
  }
568
+ const isESM = moduleKind === "esm";
530
569
  const defaultName = importClause.name.text;
531
570
  const replacementImports = [];
532
571
  let namespaceName;
@@ -547,7 +586,7 @@ const transformDefaultImports = async (ts, code, id, resolveModuleKind) => {
547
586
  }
548
587
  }
549
588
  const replacement = `${replacementImports.join("\n")}
550
- const ${defaultName} = __resolveDefaultExport(${namespaceName});`;
589
+ const ${defaultName} = __resolveDefaultExport(${namespaceName}, ${isESM});`;
551
590
  edits.push({
552
591
  start: statement.getStart(sourceFile),
553
592
  end: statement.getEnd(),
@@ -565,10 +604,11 @@ const ${defaultName} = __resolveDefaultExport(${namespaceName});`;
565
604
  const lastImport = importStatements[importStatements.length - 1];
566
605
  if (lastImport) {
567
606
  const newline = code.includes("\r\n") ? "\r\n" : "\n";
607
+ const helperId = createCjsInteropHelperId(normalizedId);
568
608
  edits.push({
569
609
  start: lastImport.getEnd(),
570
610
  end: lastImport.getEnd(),
571
- text: `${newline}${helperFunctionSource}${newline}`
611
+ text: `${newline}${buildHelperFunctionSource(helperId)}${newline}`
572
612
  });
573
613
  }
574
614
  }
@@ -953,6 +993,16 @@ const screwUp = (options = {}) => {
953
993
  l.release();
954
994
  }
955
995
  },
996
+ renderChunk: (code, chunk, outputOptions) => {
997
+ if (!fixDefaultImport || outputOptions.format !== "cjs") {
998
+ return null;
999
+ }
1000
+ const result = replaceCjsInteropFlag(code);
1001
+ if (!result.changed) {
1002
+ return null;
1003
+ }
1004
+ return { code: result.code, map: null };
1005
+ },
956
1006
  // Generate bundle phase
957
1007
  generateBundle: {
958
1008
  order: "post",