diff --git a/.changeset/chatty-flies-report.md b/.changeset/chatty-flies-report.md new file mode 100644 index 0000000000..53b1a8ea17 --- /dev/null +++ b/.changeset/chatty-flies-report.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/web-platform-rsbuild-plugin": patch +--- + +Fix build on Windows diff --git a/packages/web-platform/web-rsbuild-plugin/src/loaders/native-modules.ts b/packages/web-platform/web-rsbuild-plugin/src/loaders/native-modules.ts index f9930e9d4f..5d3721879d 100644 --- a/packages/web-platform/web-rsbuild-plugin/src/loaders/native-modules.ts +++ b/packages/web-platform/web-rsbuild-plugin/src/loaders/native-modules.ts @@ -1,3 +1,9 @@ +// Copyright 2025 The Lynx Authors. All rights reserved. +// Licensed under the Apache License Version 2.0 that can be found in the +// LICENSE file in the root directory of this source tree. + +import { pathToFileURL } from 'node:url'; + import type { LoaderContext } from 'webpack'; interface NativeModulesLoaderOptions { @@ -12,7 +18,9 @@ export default function( const { nativeModulesPath } = options; const modifiedSource = source.replace( /\/\* LYNX_NATIVE_MODULES_IMPORT \*\//g, - `import CUSTOM_NATIVE_MODULES from '${nativeModulesPath}';`, + `import CUSTOM_NATIVE_MODULES from ${ + JSON.stringify(pathToFileURL(nativeModulesPath)) + };`, ).replace( /\/\* LYNX_NATIVE_MODULES_ADD \*\//g, `Object.entries(CUSTOM_NATIVE_MODULES).map(([moduleName, moduleFunc]) => { diff --git a/packages/web-platform/web-rsbuild-plugin/src/pluginWebPlatform.ts b/packages/web-platform/web-rsbuild-plugin/src/pluginWebPlatform.ts index b58b41db34..9d27f6cc68 100644 --- a/packages/web-platform/web-rsbuild-plugin/src/pluginWebPlatform.ts +++ b/packages/web-platform/web-rsbuild-plugin/src/pluginWebPlatform.ts @@ -2,11 +2,12 @@ // Licensed under the Apache License Version 2.0 that can be found in the // LICENSE file in the root directory of this source tree. +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + import type { RsbuildPlugin } from '@rsbuild/core'; -import path from 'path'; -const __filename = new URL('', import.meta.url).pathname; -const __dirname = path.dirname(__filename); +const __dirname = path.dirname(fileURLToPath(import.meta.url)); /** * The options for {@link pluginWebPlatform}. @@ -88,17 +89,13 @@ export function pluginWebPlatform( }); api.modifyRspackConfig(rspackConfig => { - console.log(path.resolve( - __dirname, - './loaders/native-modules.js', - )); rspackConfig.module = { ...rspackConfig.module, rules: [ ...(rspackConfig.module?.rules ?? []), { test: - /backgroundThread\/background-apis\/createNativeModules\.js$/, + /backgroundThread[\\/]background-apis[\\/]createNativeModules\.js$/, loader: path.resolve( __dirname, './loaders/native-modules.js', diff --git a/packages/web-platform/web-rsbuild-plugin/tests/bundle.test.ts b/packages/web-platform/web-rsbuild-plugin/tests/bundle.test.ts index 25fd61d07e..867d0b479a 100644 --- a/packages/web-platform/web-rsbuild-plugin/tests/bundle.test.ts +++ b/packages/web-platform/web-rsbuild-plugin/tests/bundle.test.ts @@ -47,7 +47,9 @@ describe('Bundle Build', () => { for (const m of modules) { if ( m.type === 'javascript/auto' - && m.userRequest.includes('tests/fixtures/index.native-modules.ts') + && m.userRequest.includes( + 'tests/fixtures/index.native-modules.ts'.replaceAll('/', path.sep), + ) ) { if (!i.isOnlyInitial()) { asyncChunkImportCount++;