screw-up 1.23.0 → 1.25.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.23.0
3
+ * version: 1.25.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: 40f2edb1e98e9724413f5a23b820fc41e8f0b4d6
8
+ * git.commit.hash: 8275b6db6558235b0fe73f99368acf3f9a6e1b29
9
9
  */
10
10
 
11
11
  "use strict";
@@ -34,7 +34,7 @@ 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-o2vGfdZO.cjs");
37
+ const packageMetadata = require("./metadata-file-3UqfivKm.cjs");
38
38
  const crypto = require("crypto");
39
39
  const __NOOP_HANDLER = () => {
40
40
  };
@@ -404,10 +404,10 @@ const scanHasDefaultImport = (ts, code) => {
404
404
  }
405
405
  return false;
406
406
  };
407
- const cjsInteropFlagPrefix = "__isInCJS_";
407
+ const cjsInteropGlobalFlagPrefix = "__screwUpIsInCJS_";
408
408
  const cjsInteropIdLength = 12;
409
- const cjsInteropFlagAssignmentPattern = new RegExp(
410
- `\\bvar\\s+${cjsInteropFlagPrefix}[0-9a-f]+(?:\\$\\d+)?\\s*=\\s*false\\b`,
409
+ const cjsInteropGlobalFlagAssignmentPattern = new RegExp(
410
+ `\\bglobalThis\\.${cjsInteropGlobalFlagPrefix}[0-9a-f]+\\s*=\\s*false\\b`,
411
411
  "g"
412
412
  );
413
413
  const createCjsInteropHelperId = (seed) => {
@@ -415,33 +415,53 @@ const createCjsInteropHelperId = (seed) => {
415
415
  return hash.slice(0, cjsInteropIdLength);
416
416
  };
417
417
  const buildHelperFunctionSource = (helperId) => `
418
+ globalThis.${cjsInteropGlobalFlagPrefix}${helperId} = false;
418
419
  function __resolveDefaultExport<T>(module: T | { default?: T }, isESM: boolean): T {
419
- var ${cjsInteropFlagPrefix}${helperId}: boolean = false;
420
+ const __isInCJS =
421
+ typeof globalThis !== 'undefined' &&
422
+ (globalThis as any).${cjsInteropGlobalFlagPrefix}${helperId} === true;
420
423
  const maybe = module as { default?: T };
424
+ const hasDefault = !!(maybe && typeof maybe === 'object' && 'default' in maybe);
425
+ const unwrapNamespaceDefault = (value: unknown): unknown => {
426
+ if (!value || typeof value !== 'object') {
427
+ return value;
428
+ }
429
+ const inner = value as any;
430
+ const isModule =
431
+ inner.__esModule === true ||
432
+ (typeof Symbol !== 'undefined' &&
433
+ (inner as any)[Symbol.toStringTag] === 'Module');
434
+ if (isModule && 'default' in inner) {
435
+ return inner.default;
436
+ }
437
+ return value;
438
+ };
439
+ const resolvedDefault = hasDefault
440
+ ? unwrapNamespaceDefault((maybe as any).default)
441
+ : undefined;
421
442
 
422
- if (${cjsInteropFlagPrefix}${helperId}) {
423
- return maybe && typeof maybe === 'object' && 'default' in maybe
424
- ? (maybe.default ?? (module as T))
425
- : (module as T);
443
+ if (__isInCJS) {
444
+ return hasDefault ? ((resolvedDefault as T) ?? (module as T)) : (module as T);
426
445
  }
427
446
 
428
447
  if (isESM) {
429
- if (maybe && typeof maybe === 'object' && 'default' in maybe) {
430
- return maybe.default as T;
448
+ if (hasDefault) {
449
+ return resolvedDefault as T;
431
450
  }
432
451
  throw new Error('Default export not found.');
433
452
  }
434
453
 
435
- return maybe && typeof maybe === 'object' && 'default' in maybe
436
- ? (maybe.default ?? (module as T))
437
- : (module as T);
454
+ return hasDefault ? ((resolvedDefault as T) ?? (module as T)) : (module as T);
438
455
  }`;
439
- const replaceCjsInteropFlag = (code) => {
456
+ const injectCjsInteropFlag = (code) => {
440
457
  let changed = false;
441
- const nextCode = code.replace(cjsInteropFlagAssignmentPattern, (match) => {
442
- changed = true;
443
- return match.replace(/\bfalse\b/, "true ");
444
- });
458
+ const nextCode = code.replace(
459
+ cjsInteropGlobalFlagAssignmentPattern,
460
+ (match) => {
461
+ changed = true;
462
+ return match.replace(/\bfalse\b/, "true ");
463
+ }
464
+ );
445
465
  return { code: nextCode, changed };
446
466
  };
447
467
  const hasResolveDefaultExport = (ts, sourceFile) => {
@@ -997,7 +1017,7 @@ const screwUp = (options = {}) => {
997
1017
  if (!fixDefaultImport || outputOptions.format !== "cjs") {
998
1018
  return null;
999
1019
  }
1000
- const result = replaceCjsInteropFlag(code);
1020
+ const result = injectCjsInteropFlag(code);
1001
1021
  if (!result.changed) {
1002
1022
  return null;
1003
1023
  }