diff --git a/scripts/build.ts b/scripts/build.ts index db168c5..4f412c9 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -42,8 +42,9 @@ export default async function build(dev = Dev, log = true) { input: 'src/index.ts', platform: 'neutral', optimization: { - // ! BROKEN, DO NOT USE! - // inlineConst: true, + inlineConst: { + mode: 'smart', + }, // @ts-expect-error: Option not documented, but added in 1.0.0-beta.30 pifeForModuleWrappers: true, }, @@ -91,8 +92,8 @@ export default async function build(dev = Dev, log = true) { asRequire(), shimAliases(ShimsDir), importGlobPlugin(), - importDefer(), hermesSwcPlugin(), + importDefer(), hermesCPlugin({ flags: [ dev ? '-Og' : '-O', diff --git a/scripts/plugins/import-defer.ts b/scripts/plugins/import-defer.ts index 92ce710..5a296cf 100644 --- a/scripts/plugins/import-defer.ts +++ b/scripts/plugins/import-defer.ts @@ -8,6 +8,8 @@ const DEFER_NAMESPACE_IMPORT_REGEX = /** * Really wacky support for `import defer` statements by transforming them to `require()`. * + * Only supports JavaScript files. TypeScript files with conflicting type names may cause issues. + * * `import defer` statements are transformed to provide on-demand/lazy loading. * * This is achieved by creating a scoped closure to cache the module. @@ -41,7 +43,7 @@ export default function importDefer() { hasReplacements = true // Find all usages of the imported identifier - const usageRegex = new RegExp(`\\b${localName}\\b`, 'g'); + const usageRegex = new RegExp(`\\b${localName}\\b`, 'g') for (const usageMatch of code.matchAll(usageRegex)) { // Original import declaration @@ -123,5 +125,5 @@ function generateDeferReplacement(bindings: Bindings, modulePath: string) { const [{ local: localName }] = bindings const cacheVar = `_cache_${modulePath.replace(/[^a-zA-Z0-9]/g, '_')}` - return `var ${cacheVar};const ${localName}=()=>${cacheVar}??=require('${modulePath}')` + return `var ${cacheVar};const ${localName}=()=>${cacheVar}??=require('${modulePath}');` }