Skip to content

Commit

Permalink
refactor(optimizer): remove using resolvePackageData API from Vite (#…
Browse files Browse the repository at this point in the history
…5312)

* refactor(optimizer): remove using resolvePackageData API from Vite

* fix: build

* fix: another

* refactor: cleaner
  • Loading branch information
bluwy committed Oct 19, 2023
1 parent cdcf684 commit ec53ef7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/qwik/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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"
Expand Down
20 changes: 11 additions & 9 deletions packages/qwik/src/optimizer/src/plugins/vite.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -683,7 +684,6 @@ const findQwikRoots = async (
): Promise<QwikPackages[]> => {
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' });
Expand All @@ -702,21 +702,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);
}
Expand Down
11 changes: 7 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions scripts/submodule-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { build, type BuildOptions } from 'esbuild';
import {
access,
type BuildConfig,
editDeps,
getBanner,
nodeTarget,
readFile,
Expand Down Expand Up @@ -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! */
Expand All @@ -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');
Expand All @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion scripts/submodule-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)),
Expand Down
46 changes: 45 additions & 1 deletion scripts/util.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -134,6 +134,50 @@ export function importPath(filter: RegExp, newModulePath: string) {
return plugin;
}

const depEdits: Record<string, { src: string; replacement: string }[]> = {
// 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 `
Expand Down

0 comments on commit ec53ef7

Please sign in to comment.