From 7c3f8201a4ce60c1bbd13366aaf467da045cf797 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 27 Oct 2022 15:49:06 +0800 Subject: [PATCH] Improve Solid libraries config handling --- .changeset/dull-moles-sell.md | 5 ++ packages/integrations/solid/package.json | 3 +- .../integrations/solid/src/dependencies.ts | 61 +++++-------------- packages/integrations/solid/src/index.ts | 17 +++--- pnpm-lock.yaml | 13 ++++ 5 files changed, 45 insertions(+), 54 deletions(-) create mode 100644 .changeset/dull-moles-sell.md diff --git a/.changeset/dull-moles-sell.md b/.changeset/dull-moles-sell.md new file mode 100644 index 000000000000..ba551c12acf6 --- /dev/null +++ b/.changeset/dull-moles-sell.md @@ -0,0 +1,5 @@ +--- +'@astrojs/solid-js': patch +--- + +Improve third-party solid packages config handling diff --git a/packages/integrations/solid/package.json b/packages/integrations/solid/package.json index df1fc3199d33..b867fd069513 100644 --- a/packages/integrations/solid/package.json +++ b/packages/integrations/solid/package.json @@ -32,7 +32,8 @@ "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { - "babel-preset-solid": "^1.4.2" + "babel-preset-solid": "^1.4.2", + "vitefu": "^0.1.0" }, "devDependencies": { "astro": "workspace:*", diff --git a/packages/integrations/solid/src/dependencies.ts b/packages/integrations/solid/src/dependencies.ts index 6a073148f2b6..93e870bc1de0 100644 --- a/packages/integrations/solid/src/dependencies.ts +++ b/packages/integrations/solid/src/dependencies.ts @@ -1,55 +1,26 @@ -// This file is a fork of vite-plugin-solid. -// Original: https://github.com/solidjs/vite-plugin-solid/blob/03130c8a0a2ceaab9a07e16f1e1df832b996e1b8/src/index.ts#L251-L297 -// License: MIT (https://github.com/solidjs/vite-plugin-solid/blob/03130c8a0a2ceaab9a07e16f1e1df832b996e1b8/package.json#L38) - -import fs from 'fs'; -import { createRequire } from 'module'; -import path from 'path'; import { fileURLToPath } from 'url'; +import { crawlFrameworkPkgs } from 'vitefu'; + +export async function getSolidPkgsConfig(root: URL, isBuild: boolean) { + return await crawlFrameworkPkgs({ + root: fileURLToPath(root), + isBuild, + isFrameworkPkgByJson(pkgJson) { + return containsSolidField(pkgJson.exports || {}); + }, + }); +} +// Reference vite-plugin-solid heuristic +// https://github.com/solidjs/vite-plugin-solid/blob/5558486b0c63788e1275244256918f80294a8338/src/index.ts#L251-L259 +// License: MIT (https://github.com/solidjs/vite-plugin-solid/blob/5558486b0c63788e1275244256918f80294a8338/package.json#L38) function containsSolidField(fields: Record) { const keys = Object.keys(fields); for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (key === 'solid') return true; - if (typeof fields[key] === 'object' && containsSolidField(fields[key])) return true; + if (typeof fields[key] === 'object' && fields[key] != null && containsSolidField(fields[key])) + return true; } return false; } - -export function getSolidDeps(root: URL) { - const pkgPath = path.join(fileURLToPath(root), 'package.json'); - if (!fs.existsSync(pkgPath)) { - // eslint-disable-next-line no-console - console.log('No package.json found at project root'); - return []; - } - const require = createRequire(pkgPath); - const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')); - const deps = [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.devDependencies || {})]; - const pkgs = deps.map((dep) => { - try { - return require(`${dep}/package.json`); - } catch { - try { - let dir = path.dirname(require.resolve(dep)); - while (dir) { - const subPkgPath = path.join(dir, 'package.json'); - if (fs.existsSync(subPkgPath)) { - const subPkg = JSON.parse(fs.readFileSync(subPkgPath, 'utf-8')); - if (subPkg && subPkg.name === dep) return subPkg; - } - const parent = path.dirname(dir); - if (parent === dir) { - break; - } - dir = parent; - } - } catch {} - } - }); - return deps.reduce((acc, dep, i) => { - if (pkgs[i] && pkgs[i].exports && containsSolidField(pkgs[i].exports)) acc.push(dep); - return acc; - }, []); -} diff --git a/packages/integrations/solid/src/index.ts b/packages/integrations/solid/src/index.ts index 5241141a92c1..1a7667d59c80 100644 --- a/packages/integrations/solid/src/index.ts +++ b/packages/integrations/solid/src/index.ts @@ -1,5 +1,5 @@ import type { AstroIntegration, AstroRenderer } from 'astro'; -import { getSolidDeps } from './dependencies.js'; +import { getSolidPkgsConfig } from './dependencies.js'; function getRenderer(): AstroRenderer { return { @@ -24,10 +24,11 @@ function getRenderer(): AstroRenderer { }; } -function getViteConfiguration(isDev: boolean, root: URL) { +async function getViteConfiguration(isDev: boolean, root: URL) { // https://github.com/solidjs/vite-plugin-solid // We inject the dev mode only if the user explicitely wants it or if we are in dev (serve) mode const nestedDeps = ['solid-js', 'solid-js/web', 'solid-js/store', 'solid-js/html', 'solid-js/h']; + const solidPkgsConfig = await getSolidPkgsConfig(root, !isDev); return { /** * We only need esbuild on .ts or .js files. @@ -40,13 +41,13 @@ function getViteConfiguration(isDev: boolean, root: URL) { alias: [{ find: /^solid-refresh$/, replacement: '/@solid-refresh' }], }, optimizeDeps: { - include: nestedDeps, - exclude: ['@astrojs/solid-js/server.js'], + include: [...nestedDeps, ...solidPkgsConfig.optimizeDeps.include], + exclude: ['@astrojs/solid-js/server.js', ...solidPkgsConfig.optimizeDeps.exclude], }, ssr: { - external: ['babel-preset-solid'], target: 'node', - noExternal: ['solid-js', ...getSolidDeps(root)], + external: ['babel-preset-solid', ...solidPkgsConfig.ssr.external], + noExternal: ['solid-js', ...solidPkgsConfig.ssr.noExternal], }, }; } @@ -55,9 +56,9 @@ export default function (): AstroIntegration { return { name: '@astrojs/solid-js', hooks: { - 'astro:config:setup': ({ command, addRenderer, updateConfig, config }) => { + 'astro:config:setup': async ({ command, addRenderer, updateConfig, config }) => { addRenderer(getRenderer()); - updateConfig({ vite: getViteConfiguration(command === 'dev', config.root) }); + updateConfig({ vite: await getViteConfiguration(command === 'dev', config.root) }); }, }, }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4ef405d6773e..689786c1e80f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3077,8 +3077,10 @@ importers: astro-scripts: workspace:* babel-preset-solid: ^1.4.2 solid-js: ^1.5.1 + vitefu: ^0.1.0 dependencies: babel-preset-solid: 1.6.0 + vitefu: 0.1.0 devDependencies: astro: link:../../astro astro-scripts: link:../../../scripts @@ -18016,6 +18018,17 @@ packages: fsevents: 2.3.2 dev: false + /vitefu/0.1.0: + resolution: {integrity: sha512-5MQSHP9yr0HIve8q4XNb7QXfO1P4tzZDZP99qH0FM5ClcwYddeGXRDQ4TQYRUeXLjZ+vLecirHtGNpwFFUF7sw==} + peerDependencies: + vite: ^3.0.0 + peerDependenciesMeta: + vite: + optional: true + dependencies: + import-meta-resolve: 2.1.0 + dev: false + /vitefu/0.1.0_vite@3.1.8: resolution: {integrity: sha512-5MQSHP9yr0HIve8q4XNb7QXfO1P4tzZDZP99qH0FM5ClcwYddeGXRDQ4TQYRUeXLjZ+vLecirHtGNpwFFUF7sw==} peerDependencies: