Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brown-pillows-write.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

Fix `ERR_UNSUPPORTED_ESM_URL_SCHEME` errors when bundling on Windows
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import semver from 'semver';
import type TerserPlugin from 'terser-webpack-plugin';
import { importDefaultESM } from '../../../helpers/index.js';

// prefer `terser-webpack-plugin` installed in the project root to the one shipped with Re.Pack
async function getTerserPlugin(rootDir: string) {
Expand All @@ -10,8 +12,8 @@ async function getTerserPlugin(rootDir: string) {
} catch {
terserPluginPath = require.resolve('terser-webpack-plugin');
}
const plugin = await import(terserPluginPath);
return 'default' in plugin ? plugin.default : plugin;
const plugin = await importDefaultESM<typeof TerserPlugin>(terserPluginPath);
return plugin;
}

async function getTerserConfig(rootDir: string) {
Expand Down
6 changes: 5 additions & 1 deletion packages/repack/src/commands/common/getDevMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import url from 'node:url';

export async function getDevMiddleware(reactNativePath: string) {
const reactNativeCommunityCliPluginPath = require.resolve(
'@react-native/community-cli-plugin',
Expand All @@ -8,5 +10,7 @@ export async function getDevMiddleware(reactNativePath: string) {
paths: [reactNativeCommunityCliPluginPath],
});

return import(devMiddlewarePath);
// use fileURL to import correctly on both Windows & MacOS
const { href: fileUrl } = url.pathToFileURL(devMiddlewarePath);
return await import(fileUrl);
}
7 changes: 7 additions & 0 deletions packages/repack/src/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os from 'node:os';
import url from 'node:url';
import type { Compiler as RspackCompiler } from '@rspack/core';
import type { Compiler as WebpackCompiler } from 'webpack';

Expand Down Expand Up @@ -60,3 +61,9 @@ export function moveElementBefore<T>(
// Insert source element right before the target element
array.splice(targetIndex, 0, moveElement);
}

export async function importDefaultESM<T>(absolutePath: string): Promise<T> {
const { href: fileUrl } = url.pathToFileURL(absolutePath);
const module = await import(fileUrl);
return 'default' in module ? module.default : module;
}
3 changes: 2 additions & 1 deletion packages/repack/src/loaders/babelLoader/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ParseResult } from '@babel/core';
import { importDefaultESM } from '../../helpers/index.js';

interface HermesParser {
parse: (
Expand Down Expand Up @@ -46,7 +47,7 @@ export async function loadHermesParser(
const hermesParserPath =
providedHermesParserPath ??
resolveHermesParser(projectRoot ?? process.cwd());
const hermesParser = await import(hermesParserPath);
const hermesParser = await importDefaultESM<HermesParser>(hermesParserPath);
return hermesParser;
} catch (e) {
console.error(e);
Expand Down
20 changes: 6 additions & 14 deletions packages/repack/src/loaders/babelSwcLoader/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type {
SwcLoaderParserConfig,
experiments,
} from '@rspack/core';
import type Rspack from '@rspack/core';
import { importDefaultESM } from '../../helpers/index.js';

type Swc = (typeof experiments)['swc'];
type Logger = ReturnType<LoaderContext['getLogger']>;
Expand Down Expand Up @@ -136,26 +138,16 @@ async function getSwcModule(loaderContext: LoaderContext): Promise<Swc | null> {
// use optional chaining to avoid type errors when there is no experiments.swc
const rspackCorePath = safelyResolve('@rspack/core', projectRoot);
if (rspackCorePath && !isWebpack) {
const rspack = await import(rspackCorePath);
if ('default' in rspack) {
return rspack.default?.experiments?.swc ?? null;
}
if (rspack) {
return rspack?.experiments?.swc ?? null;
}
const rspack = await importDefaultESM<typeof Rspack>(rspackCorePath);
return rspack.experiments?.swc ?? null;
}
}
// fallback to checking for `@swc/core` installed in the project
// this can be in both webpack & rspack projects
const swcCorePath = safelyResolve('@swc/core', projectRoot);
if (swcCorePath) {
const swc = await import(swcCorePath);
if ('default' in swc) {
return swc.default as Swc;
}
if (swc) {
return swc as Swc;
}
const swc = await importDefaultESM<Swc>(swcCorePath);
return swc;
}
// at this point, we've tried all possible ways to get swc and failed
return null;
Expand Down