Repository navigation
fix(nuxt): Delete public source maps before Nitro lists its public assets #25267
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
Merged
Merged
Changes from all commits
Commits
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,55 @@ | ||
| import { spawn } from 'node:child_process'; | ||
|
|
||
| export interface NodeServerOptions { | ||
| /** Path to the server entry, e.g. `.output/server/index.mjs`. */ | ||
| entry: string; | ||
| port: number; | ||
| /** How long to wait for the first response. Defaults to 30 seconds. */ | ||
| timeoutMs?: number; | ||
| } | ||
|
|
||
| export interface NodeServer { | ||
| baseUrl: string; | ||
| /** Stops the server and resolves once its process has exited. */ | ||
| close: () => Promise<void>; | ||
| } | ||
|
|
||
| /** | ||
| * Starts a built Node server and resolves once it answers HTTP requests. The server gets the port via `PORT`. | ||
| * | ||
| * Rejects if the server exits or does not answer before the timeout. Its output is printed only then. | ||
| */ | ||
| export async function startNodeServer({ entry, port, timeoutMs = 30_000 }: NodeServerOptions): Promise<NodeServer> { | ||
| const baseUrl = `http://localhost:${port}`; | ||
| const child = spawn(process.execPath, [entry], { env: { ...process.env, PORT: String(port) } }); | ||
|
|
||
| let output = ''; | ||
| child.stdout.on('data', chunk => (output += chunk)); | ||
| child.stderr.on('data', chunk => (output += chunk)); | ||
|
|
||
| const close = async (): Promise<void> => { | ||
| if (child.exitCode !== null || child.signalCode !== null) { | ||
| return; | ||
| } | ||
| const exited = new Promise(resolve => child.once('exit', resolve)); | ||
| child.kill(); | ||
| await exited; | ||
| }; | ||
|
|
||
| const deadline = Date.now() + timeoutMs; | ||
| while (Date.now() < deadline) { | ||
| if (child.exitCode !== null) { | ||
| throw new Error(`Server ${entry} exited with code ${child.exitCode} before it answered:\n${output}`); | ||
| } | ||
|
|
||
| try { | ||
| await fetch(baseUrl); | ||
| return { baseUrl, close }; | ||
| } catch { | ||
| await new Promise(resolve => setTimeout(resolve, 250)); | ||
| } | ||
| } | ||
|
|
||
| await close(); | ||
| throw new Error(`Server ${entry} did not answer on ${baseUrl} within ${timeoutMs}ms:\n${output}`); | ||
| } |
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,95 @@ | ||
| import * as fs from 'fs'; | ||
| import * as os from 'os'; | ||
| import * as path from 'path'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import { deletePublicSourceMaps } from '../../src/vite/sourceMapDeletion'; | ||
|
|
||
| const BUILD_FILES = [ | ||
| '.output/public/_nuxt/entry.DxB3kLmA.js', | ||
| '.output/public/_nuxt/entry.DxB3kLmA.js.map', | ||
| '.output/public/_nuxt/pages/products.Cq81zR4t.js.map', | ||
| '.output/public-assets/_nuxt/entry.DxB3kLmA.js.map', | ||
| '.output/server/chunks/nitro/nitro.mjs.map', | ||
| '.nuxt/dist/server/_nuxt/index-B2kR7mXp.mjs.map', | ||
| ]; | ||
|
|
||
| let rootDir: string; | ||
| let publicDir: string; | ||
|
|
||
| /** Glob patterns need forward slashes, also on Windows. */ | ||
| function toGlob(relativePattern: string): string { | ||
| return path.join(rootDir, relativePattern).split(path.sep).join('/'); | ||
| } | ||
|
|
||
| function remainingFiles(): string[] { | ||
| return BUILD_FILES.filter(file => fs.existsSync(path.join(rootDir, file))); | ||
| } | ||
|
|
||
| describe('deletePublicSourceMaps', () => { | ||
| beforeEach(() => { | ||
| rootDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-nuxt-source-map-deletion-')); | ||
| publicDir = path.join(rootDir, '.output', 'public'); | ||
|
|
||
| for (const file of BUILD_FILES) { | ||
| fs.mkdirSync(path.dirname(path.join(rootDir, file)), { recursive: true }); | ||
| fs.writeFileSync(path.join(rootDir, file), ''); | ||
| } | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fs.rmSync(rootDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('deletes the matched files in the public dir and keeps the matched files outside of it', async () => { | ||
| await deletePublicSourceMaps( | ||
| { | ||
| sourcemaps: { | ||
| filesToDeleteAfterUpload: [toGlob('.*/**/public*/**/*.map'), toGlob('.*/**/server/**/*.map')], | ||
| }, | ||
| }, | ||
| publicDir, | ||
| ); | ||
|
|
||
| expect(remainingFiles()).toEqual([ | ||
| '.output/public/_nuxt/entry.DxB3kLmA.js', | ||
| '.output/public-assets/_nuxt/entry.DxB3kLmA.js.map', | ||
| '.output/server/chunks/nitro/nitro.mjs.map', | ||
| '.nuxt/dist/server/_nuxt/index-B2kR7mXp.mjs.map', | ||
| ]); | ||
| }); | ||
|
|
||
| it('keeps the files in the public dir that no pattern matches', async () => { | ||
| await deletePublicSourceMaps( | ||
| { sourcemaps: { filesToDeleteAfterUpload: toGlob('.output/public/_nuxt/pages/*.map') } }, | ||
| publicDir, | ||
| ); | ||
|
|
||
| expect(remainingFiles()).toEqual([ | ||
| '.output/public/_nuxt/entry.DxB3kLmA.js', | ||
| '.output/public/_nuxt/entry.DxB3kLmA.js.map', | ||
| '.output/public-assets/_nuxt/entry.DxB3kLmA.js.map', | ||
| '.output/server/chunks/nitro/nitro.mjs.map', | ||
| '.nuxt/dist/server/_nuxt/index-B2kR7mXp.mjs.map', | ||
| ]); | ||
| }); | ||
|
|
||
| it('resolves a promised list of patterns', async () => { | ||
| await deletePublicSourceMaps( | ||
| { sourcemaps: { filesToDeleteAfterUpload: Promise.resolve([toGlob('.output/public/**/*.map')]) } }, | ||
| publicDir, | ||
| ); | ||
|
|
||
| expect(remainingFiles()).toEqual([ | ||
| '.output/public/_nuxt/entry.DxB3kLmA.js', | ||
| '.output/public-assets/_nuxt/entry.DxB3kLmA.js.map', | ||
| '.output/server/chunks/nitro/nitro.mjs.map', | ||
| '.nuxt/dist/server/_nuxt/index-B2kR7mXp.mjs.map', | ||
| ]); | ||
| }); | ||
|
|
||
| it('deletes nothing when no deletion patterns are set', async () => { | ||
| await deletePublicSourceMaps({ sourcemaps: {} }, publicDir); | ||
|
|
||
| expect(remainingFiles()).toEqual(BUILD_FILES); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: don't we have to spread it? Or is it automatically flattened?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's not needed as rollup and rolldown flatten nested plugin arrays