From b766592561fcea41736e668467786e8cfffd43f6 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 20 Oct 2023 02:53:31 +0800 Subject: [PATCH] chore(core): remove resolvePackageData (#5312) --- packages/qwik/package.json | 3 +- .../qwik/src/optimizer/src/plugins/vite.ts | 20 ++++---- pnpm-lock.yaml | 14 ++++++ scripts/submodule-optimizer.ts | 7 +-- scripts/submodule-server.ts | 2 +- scripts/util.ts | 46 ++++++++++++++++++- 6 files changed, 77 insertions(+), 15 deletions(-) diff --git a/packages/qwik/package.json b/packages/qwik/package.json index f9900fc52c52..e7a7d4023eaa 100644 --- a/packages/qwik/package.json +++ b/packages/qwik/package.json @@ -28,7 +28,8 @@ "devDependencies": { "@builder.io/qwik-dom": "workspace:*", "image-size": "^1.0.2", - "kleur": "4.1.5" + "kleur": "4.1.5", + "vitefu": "^0.2.5" }, "engines": { "node": ">=16.8.0 <18.0.0 || >=18.11" diff --git a/packages/qwik/src/optimizer/src/plugins/vite.ts b/packages/qwik/src/optimizer/src/plugins/vite.ts index 81030f9bbcc3..8ff262b81b25 100644 --- a/packages/qwik/src/optimizer/src/plugins/vite.ts +++ b/packages/qwik/src/optimizer/src/plugins/vite.ts @@ -1,4 +1,5 @@ import type { UserConfig, ViteDevServer, Plugin as VitePlugin } from 'vite'; +import { findDepPkgJsonPath } from 'vitefu'; import { QWIK_LOADER_DEFAULT_DEBUG, QWIK_LOADER_DEFAULT_MINIFIED } from '../scripts'; import type { EntryStrategy, @@ -747,7 +748,6 @@ const findQwikRoots = async ( ): Promise => { if (sys.env === 'node') { const fs: typeof import('fs') = await sys.dynamicImport('node:fs'); - const { resolvePackageData }: typeof import('vite') = await sys.strictDynamicImport('vite'); try { const data = await fs.promises.readFile(packageJsonPath, { encoding: 'utf-8' }); @@ -766,21 +766,23 @@ const findQwikRoots = async ( } const basedir = sys.cwd(); - const qwikDirs = packages - .map((id) => { - const pkgData = resolvePackageData(id, basedir); - if (pkgData) { - const qwikPath = pkgData.data['qwik']; + const qwikDirs = await Promise.all( + packages.map(async (id) => { + const pkgJsonPath = await findDepPkgJsonPath(id, basedir); + if (pkgJsonPath) { + const pkgJsonContent = await fs.promises.readFile(pkgJsonPath, 'utf-8'); + const pkgJson = JSON.parse(pkgJsonContent); + const qwikPath = pkgJson['qwik']; if (qwikPath) { return { id, - path: sys.path.resolve(pkgData.dir, qwikPath), + path: sys.path.resolve(sys.path.dirname(pkgJsonPath), qwikPath), }; } } }) - .filter(isNotNullable); - return qwikDirs; + ); + return qwikDirs.filter(isNotNullable); } catch (e) { console.error(e); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e5cb61136818..0d38d6f83ec9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -518,6 +518,9 @@ importers: kleur: specifier: 4.1.5 version: 4.1.5 + vitefu: + specifier: ^0.2.5 + version: 0.2.5(vite@5.0.0) packages/qwik-auth: devDependencies: @@ -10112,6 +10115,17 @@ packages: optionalDependencies: fsevents: 2.3.3 + /vitefu@0.2.5(vite@5.0.0): + resolution: {integrity: sha512-SgHtMLoqaeeGnd2evZ849ZbACbnwQCIwRH57t18FxcXoZop0uQu0uzlIhJBlF/eWVzuce0sHeqPcDo+evVcg8Q==} + peerDependencies: + vite: ^3.0.0 || ^4.0.0 || ^5.0.0 + peerDependenciesMeta: + vite: + optional: true + dependencies: + vite: 5.0.0(@types/node@20.8.4)(terser@5.21.0) + dev: true + /vitest@0.34.6(terser@5.21.0): resolution: {integrity: sha512-+5CALsOvbNKnS+ZHMXtuUC7nL8/7F1F2DnHGjSsszX8zCjWSSviphCb/NuS9Nzf4Q03KyyDRBAXhF/8lffME4Q==} engines: {node: '>=v14.18.0'} diff --git a/scripts/submodule-optimizer.ts b/scripts/submodule-optimizer.ts index cc1344172cca..2dc97ae57c33 100644 --- a/scripts/submodule-optimizer.ts +++ b/scripts/submodule-optimizer.ts @@ -2,6 +2,7 @@ import { build, type BuildOptions } from 'esbuild'; import { access, type BuildConfig, + editDeps, getBanner, nodeTarget, readFile, @@ -29,6 +30,7 @@ export async function submoduleOptimizer(config: BuildConfig) { outdir: config.distQwikPkgDir, bundle: true, sourcemap: false, + platform: 'node', target, external: [ /* no Node.js built-in externals allowed! */ @@ -49,7 +51,7 @@ export async function submoduleOptimizer(config: BuildConfig) { 'globalThis.QWIK_VERSION': JSON.stringify(config.distVersion), ...qwikloaderScripts, }, - plugins: [RawPlugin()], + plugins: [RawPlugin(), editDeps()], }); const cjsBanner = [`globalThis.qwikOptimizer = (function (module) {`].join('\n'); @@ -68,9 +70,8 @@ export async function submoduleOptimizer(config: BuildConfig) { 'globalThis.QWIK_VERSION': JSON.stringify(config.distVersion), ...qwikloaderScripts, }, - platform: 'node', target: nodeTarget, - plugins: [RawPlugin()], + plugins: [RawPlugin(), editDeps()], }); await Promise.all([esmBuild, cjsBuild]); diff --git a/scripts/submodule-server.ts b/scripts/submodule-server.ts index b924a900f4b1..e0b1828822f1 100644 --- a/scripts/submodule-server.ts +++ b/scripts/submodule-server.ts @@ -22,6 +22,7 @@ export async function submoduleServer(config: BuildConfig) { outdir: config.distQwikPkgDir, sourcemap: config.dev, bundle: true, + platform: 'node', target, external: [ /* no Node.js built-in externals allowed! */ '@builder.io/qwik-dom', @@ -61,7 +62,6 @@ export async function submoduleServer(config: BuildConfig) { }, outExtension: { '.js': '.cjs' }, plugins: [importPath(/^@builder\.io\/qwik$/, '@builder.io/qwik'), qwikDomPlugin], - platform: 'node', target: nodeTarget, define: { ...(await inlineQwikScriptsEsBuild(config)), diff --git a/scripts/util.ts b/scripts/util.ts index 134d334fe405..bf140a587a81 100644 --- a/scripts/util.ts +++ b/scripts/util.ts @@ -1,5 +1,5 @@ import type { Plugin } from 'esbuild'; -import { join } from 'node:path'; +import { dirname, join } from 'node:path'; import mri from 'mri'; import { access as fsAccess, @@ -134,6 +134,50 @@ export function importPath(filter: RegExp, newModulePath: string) { return plugin; } +const depEdits: Record = { + // Replace top-level await with a top-level import + 'vitefu/src/index.js': [ + { + src: `import path from 'node:path'`, + replacement: `import path from 'node:path'\nimport _module from 'node:module'`, + }, + { + src: `(await import('module')).default`, + replacement: `_module`, + }, + ], +}; + +/** Esbuild plugin to edit dependency code so it builds successfully */ +export function editDeps() { + const plugin: Plugin = { + name: 'editDepsPlugin', + setup(build) { + const filter = new RegExp( + `^.*(${Object.keys(depEdits) + .map((mod) => { + return mod + .replace('.', '\\.') + .replace('/', process.platform === 'win32' ? '\\\\' : '\\/'); + }) + .join('|')})$` + ); + build.onLoad({ filter }, async (args) => { + let contents = await readFile(args.path, 'utf-8'); + for (const modPath in depEdits) { + if (args.path.endsWith(modPath)) { + for (const edit of depEdits[modPath]) { + contents = contents.replace(edit.src, edit.replacement); + } + } + } + return { contents, resolveDir: dirname(args.path) }; + }); + }, + }; + return plugin; +} + /** Standard license banner to place at the top of the generated files. */ export const getBanner = (moduleName: string, version: string) => { return `