Repository navigation
fix(deps): patch nitropack so comark plugins stay external on Nuxt 4.6 #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
larbish
wants to merge
2
commits into
main
Choose a base branch
from
fix/playground
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+373
−19
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| nitropack@2.13.4, two fixes until a Nitro v2 release ships them: | ||
|
|
||
| 1. dist/presets/vercel/utils.mjs: nitrojs/nitro#4651, header-only Vercel routes need `continue: true`. | ||
| 2. dist/rollup/index.mjs: externals map files back to pattern exports (`comark/plugins/*`). | ||
| Without it, layer components importing a comark plugin crash SSR on Nuxt 4.6. | ||
| Not upstream yet. | ||
|
|
||
| Each patched block is tagged with a `[comark-docs patch]` comment. | ||
|
|
||
| diff --git a/dist/presets/vercel/utils.mjs b/dist/presets/vercel/utils.mjs | ||
| index 6b4a25e30eceded9b820a446c3c4519f4f43c0ac..8a807ea3d67b754e2bc409914105cf97fb7ff5d9 100644 | ||
| --- a/dist/presets/vercel/utils.mjs | ||
| +++ b/dist/presets/vercel/utils.mjs | ||
| @@ -129,19 +129,24 @@ function generateBuildConfig(nitro, o11Routes) { | ||
| ) | ||
| }, | ||
| routes: [ | ||
| - // Redirect and header rules | ||
| - ...rules.filter(([_, routeRules]) => routeRules.redirect || routeRules.headers).map(([path, routeRules]) => { | ||
| + // [comark-docs patch] nitrojs/nitro#4651, merged in Nitro v2 but not released yet. | ||
| + // Header-only rules (least specific first, so more specific headers override on `continue`) | ||
| + ...rules.filter(([_, routeRules]) => routeRules.headers && !routeRules.redirect).reverse().map(([path, routeRules]) => ({ | ||
| + src: path.replace("/**", "/(.*)"), | ||
| + headers: routeRules.headers, | ||
| + continue: true | ||
| + })), | ||
| + // Redirect rules | ||
| + ...rules.filter( | ||
| + (entry) => !!entry[1].redirect | ||
| + ).map(([path, routeRules]) => { | ||
| let route = { | ||
| - src: path.replace("/**", "/(.*)") | ||
| + src: path.replace("/**", "/(.*)"), | ||
| + status: routeRules.redirect.statusCode, | ||
| + headers: { | ||
| + Location: routeRules.redirect.to.replace("/**", "/$1") | ||
| + } | ||
| }; | ||
| - if (routeRules.redirect) { | ||
| - route = defu(route, { | ||
| - status: routeRules.redirect.statusCode, | ||
| - headers: { | ||
| - Location: routeRules.redirect.to.replace("/**", "/$1") | ||
| - } | ||
| - }); | ||
| - } | ||
| if (routeRules.headers) { | ||
| route = defu(route, { headers: routeRules.headers }); | ||
| } | ||
| diff --git a/dist/rollup/index.mjs b/dist/rollup/index.mjs | ||
| index 9aadcbc7925e965062670f1d86436f8e0830b200..3d55aefa5647fe6f313aa86abf3ae2e34c3aebab 100644 | ||
| --- a/dist/rollup/index.mjs | ||
| +++ b/dist/rollup/index.mjs | ||
| @@ -413,10 +413,19 @@ function externals$1(opts) { | ||
| } | ||
| const packageEntry = tryResolve(pkgName, importer); | ||
| if (packageEntry !== id) { | ||
| - const guessedSubpath = await lookupNodeModuleSubpath(id).catch(() => null); | ||
| - const resolvedGuess = guessedSubpath && tryResolve(join(pkgName, guessedSubpath), importer); | ||
| - if (resolvedGuess === id) { | ||
| - trackedExternals.add(resolvedGuess); | ||
| + // [comark-docs patch] Map files back to pattern exports (`comark/plugins/*`), not upstream yet. | ||
| + const resolvesToId = (subpath) => !!subpath && tryResolve(join(pkgName, subpath), importer) === id; | ||
| + const exactSubpath = await lookupNodeModuleSubpath(id).catch( | ||
| + () => null | ||
| + ); | ||
| + const guessedSubpath = resolvesToId(exactSubpath) ? exactSubpath : ( | ||
| + // `lookupNodeModuleSubpath` only matches exact export targets | ||
| + (await lookupPatternExportSubpaths(id)).find( | ||
| + (subpath) => resolvesToId(subpath) | ||
| + ) | ||
| + ); | ||
| + if (guessedSubpath) { | ||
| + trackedExternals.add(id); | ||
| return { | ||
| id: join(pkgName, guessedSubpath), | ||
| external: true | ||
| @@ -686,6 +695,55 @@ function applyProductionCondition(exports$1) { | ||
| applyProductionCondition(exports$1[key]); | ||
| } | ||
| } | ||
| +// [comark-docs patch] Pattern-export lookup used by the externals plugin above. | ||
| +async function lookupPatternExportSubpaths(id) { | ||
| + const { dir, name, subpath } = parseNodeModulePath$1(id); | ||
| + if (!dir || !name || !subpath) { | ||
| + return []; | ||
| + } | ||
| + const { exports } = await readPackageJSON(join(dir, name)).catch( | ||
| + () => ({}) | ||
| + ); | ||
| + return findPatternExportSubpaths(exports, subpath); | ||
| +} | ||
| +function findPatternExportSubpaths(exports, subpath) { | ||
| + if (!exports || typeof exports !== "object" || Array.isArray(exports)) { | ||
| + return []; | ||
| + } | ||
| + const subpaths = []; | ||
| + for (const [key, value] of Object.entries(exports)) { | ||
| + if (!key.startsWith("./") || key.split("*").length !== 2) { | ||
| + continue; | ||
| + } | ||
| + for (const target of exportTargets(value)) { | ||
| + const parts = target.split("*"); | ||
| + if (parts.length !== 2) { | ||
| + continue; | ||
| + } | ||
| + const [prefix, suffix] = parts; | ||
| + if (subpath.length > prefix.length + suffix.length && subpath.startsWith(prefix) && subpath.endsWith(suffix)) { | ||
| + const match = subpath.slice( | ||
| + prefix.length, | ||
| + subpath.length - suffix.length | ||
| + ); | ||
| + subpaths.push(key.replace("*", match)); | ||
| + } | ||
| + } | ||
| + } | ||
| + return subpaths; | ||
| +} | ||
| +function exportTargets(value) { | ||
| + if (typeof value === "string") { | ||
| + return [value]; | ||
| + } | ||
| + if (Array.isArray(value)) { | ||
| + return value.flatMap((item) => exportTargets(item)); | ||
| + } | ||
| + if (value && typeof value === "object") { | ||
| + return Object.values(value).flatMap((item) => exportTargets(item)); | ||
| + } | ||
| + return []; | ||
| +} | ||
| async function isFile$1(file) { | ||
| try { | ||
| const stat = await promises.stat(file); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { readdirSync, readFileSync } from 'node:fs' | ||
| import { isBuiltin } from 'node:module' | ||
| import { join, relative } from 'node:path' | ||
| import { fileURLToPath, pathToFileURL } from 'node:url' | ||
| import { init, parse } from 'es-module-lexer' | ||
| import { resolveModuleURL } from 'exsolve' | ||
| import { expect, it } from 'vitest' | ||
|
|
||
| const serverDir = fileURLToPath(new URL('../../playground/.output/server/', import.meta.url)) | ||
|
|
||
| // Optional peers imported behind a try/catch, absent unless a site installs them. | ||
| const OPTIONAL_IMPORTS = new Set([ | ||
| // `@nuxtjs/mcp-toolkit` Code Mode. | ||
| 'secure-exec', | ||
| ]) | ||
|
|
||
| /** The bundled chunks: the traced `node_modules/` are third-party files Nitro copied as they are. */ | ||
| function bundledFiles(): string[] { | ||
| return readdirSync(serverDir, { withFileTypes: true, recursive: true }) | ||
| .filter((entry) => entry.isFile() && entry.name.endsWith('.mjs')) | ||
| .map((entry) => join(entry.parentPath, entry.name)) | ||
| .filter((file) => !relative(serverDir, file).startsWith('node_modules')) | ||
| } | ||
|
|
||
| // A lazy chunk (a component, a route) only fails once requested, so the route crawl alone can miss it. | ||
| it('every import in the server bundle resolves inside the build output', async () => { | ||
| await init() | ||
| const files = bundledFiles() | ||
| const broken: string[] = [] | ||
| let checked = 0 | ||
|
|
||
| for (const file of files) { | ||
| const [imports] = parse(readFileSync(file, 'utf8')) | ||
| for (const entry of imports) { | ||
| // `import.meta`, computed dynamic imports and template globs name no single module. | ||
| const specifier = entry.type === 'dynamic' && entry.glob ? undefined : entry.specifier | ||
| if (!specifier || isBuiltin(specifier) || OPTIONAL_IMPORTS.has(specifier)) continue | ||
| checked++ | ||
| const resolved = resolveModuleURL(specifier, { from: pathToFileURL(file), try: true }) | ||
| // Resolving into the repo's own `node_modules` works locally but not once deployed. | ||
| if (!resolved?.startsWith(pathToFileURL(serverDir).href)) { | ||
| broken.push(`${relative(serverDir, file)} imports ${specifier}`) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Guards against a vacuous pass, e.g. the lexer reporting no specifiers after an API change. | ||
| expect(checked).toBeGreaterThan(files.length) | ||
| expect(broken).toEqual([]) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.