Skip to content

Commit

Permalink
fix(dev): find all qwik packages in monorepos
Browse files Browse the repository at this point in the history
This is a temporary fix, the real fix is to remove the full build during buildStart, and then only rely on `.qwik` in the filename. This avoids overbuilding and allows other vite plugins to transform the code.
  • Loading branch information
wmertens committed Jul 9, 2024
1 parent 8c54f46 commit 574cc3b
Showing 1 changed file with 49 additions and 40 deletions.
89 changes: 49 additions & 40 deletions packages/qwik/src/optimizer/src/plugins/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {

const shouldFindVendors =
!qwikViteOpts.disableVendorScan && (target !== 'lib' || viteCommand === 'serve');
const vendorRoots = shouldFindVendors
? await findQwikRoots(sys, path.join(sys.cwd(), 'package.json'))
: [];
const vendorRoots = shouldFindVendors ? await findQwikRoots(sys, sys.cwd()) : [];
viteAssetsDir = viteConfig.build?.assetsDir;
const useAssetsDir = target === 'client' && !!viteAssetsDir && viteAssetsDir !== '_astro';
const pluginOpts: QwikPluginOptions = {
Expand Down Expand Up @@ -816,53 +814,64 @@ async function findDepPkgJsonPath(sys: OptimizerSystem, dep: string, parent: str

const findQwikRoots = async (
sys: OptimizerSystem,
packageJsonPath: string
packageJsonDir: string
): Promise<QwikPackages[]> => {
const paths = new Map<string, string>();
if (sys.env === 'node') {
const fs: typeof import('fs') = await sys.dynamicImport('node:fs');

try {
const data = await fs.promises.readFile(packageJsonPath, { encoding: 'utf-8' });

let prevPackageJsonDir: string | undefined;
do {
try {
const packageJson = JSON.parse(data);
const dependencies = packageJson['dependencies'];
const devDependencies = packageJson['devDependencies'];
const data = await fs.promises.readFile(sys.path.join(packageJsonDir, 'package.json'), {
encoding: 'utf-8',
});

const packages: string[] = [];
if (typeof dependencies === 'object') {
packages.push(...Object.keys(dependencies));
}
if (typeof devDependencies === 'object') {
packages.push(...Object.keys(devDependencies));
}
try {
const packageJson = JSON.parse(data);
const dependencies = packageJson['dependencies'];
const devDependencies = packageJson['devDependencies'];

const packages: string[] = [];
if (typeof dependencies === 'object') {
packages.push(...Object.keys(dependencies));
}
if (typeof devDependencies === 'object') {
packages.push(...Object.keys(devDependencies));
}

const basedir = sys.cwd();
const qwikDirs = await Promise.all(
packages.map(async (id) => {
const pkgJsonPath = await findDepPkgJsonPath(sys, 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(sys.path.dirname(pkgJsonPath), qwikPath),
};
const basedir = sys.cwd();
await Promise.all(
packages.map(async (id) => {
const pkgJsonPath = await findDepPkgJsonPath(sys, 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;
}
// Support multiple paths
const allPaths = Array.isArray(qwikPath) ? qwikPath : [qwikPath];
for (const p of allPaths) {
paths.set(
await fs.promises.realpath(sys.path.resolve(sys.path.dirname(pkgJsonPath), p)),
id
);
}
}
}
})
);
return qwikDirs.filter(isNotNullable);
})
);
} catch (e) {
console.error(e);
}
} catch (e) {
console.error(e);
// ignore errors if package.json not found
}
} catch (e) {
// ignore errors if root package.json not found
}
prevPackageJsonDir = packageJsonDir;
packageJsonDir = sys.path.dirname(packageJsonDir);
} while (packageJsonDir !== prevPackageJsonDir);
}
return [];
return Array.from(paths).map(([path, id]) => ({ path, id }));
};

export const isNotNullable = <T>(v: T): v is NonNullable<T> => {
Expand Down

0 comments on commit 574cc3b

Please sign in to comment.