Skip to content

Commit

Permalink
fix: use full filename for svelte.compile (#700)
Browse files Browse the repository at this point in the history
* fix: use full filename for svelte.compile to ensure errors work as expected

* chore: add changeset

* fix: normalize snapshots for difference in windows compiler comment output

* fix: try harder to normalize comment

* test: add testcase for hermetic build output

* chore: log files that are leaking

* fix: yeah, that was dumb

* fix: set NODE_ENV to prodution in build tests
  • Loading branch information
dominikg authored Aug 6, 2023
1 parent 20c214f commit 703c377
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-trains-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

fix links in error handling (console and vite overlay)
5 changes: 4 additions & 1 deletion packages/e2e-tests/e2e-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export async function serve(root, isBuild, port) {
try {
const buildProcess = execa('pnpm', ['build'], {
cwd: root,
stdio: 'pipe'
stdio: 'pipe',
env: {
NODE_ENV: 'production'
}
});
logs.build = { out, err };
collectLogs(buildProcess, logs.build);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { VERSION } from 'svelte/compiler';
function normalizeSnapshot(result: string) {
// during dev, the import is rewritten but can vary on the v= hash. replace with stable short import
return result
.replace(
/\/\* .* generated by Svelte v\d\.\d+\.\d+(?:-[a-z]+\.\d+)? \*\//g,
'/* src/Dummy.svelte generated by Svelte vXXX */'
) // ensure generated svelte compiler comment is stable
.replace(/\.js\?v=[0-9a-f]{8}/g, '.js?v=XXX') // vite import analysis import rewrite version query
.replace(/generated by Svelte v\d\.\d+\.\d+(?:-[a-z]+\.\d+)?/g, 'generated by Svelte vXXX') // compiler version comment
.replace(/"total": *\d+\.\d+/g, '"total":0.123456789'); // svelte compile stats
}

Expand Down
21 changes: 18 additions & 3 deletions packages/e2e-tests/kit-node/__tests__/kit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
editFile,
editFileAndWaitForHmrComplete,
getColor,
getEl,
Expand All @@ -8,14 +7,15 @@ import {
testDir,
sleep,
untilMatches,
waitForNavigation,
page,
browserLogs,
fetchPageText,
reloadPage
reloadPage,
readFileContent
} from '~utils';

import glob from 'tiny-glob';
import path from 'path';

describe('kit-node', () => {
describe('index route', () => {
Expand Down Expand Up @@ -105,6 +105,21 @@ describe('kit-node', () => {
);
expect(includesClientOnlyModule).toBe(true);
});

it('should produce hermetic build', async () => {
const outputFiles = await glob('./build/**/*', { cwd: testDir, filesOnly: true });
expect(outputFiles.length).toBeGreaterThan(10);
const dir = path.basename(testDir);
const leakingFiles = outputFiles.filter(
(f) => !f.endsWith('.png') && readFileContent(f).includes(dir)
);
if (leakingFiles.length > 0) {
console.error(
`These build output files leak parent dir: "${dir}"\n\t${leakingFiles.join('\n\t')}`
);
}
expect(leakingFiles).toEqual([]);
});
}

if (!isBuild) {
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e-tests/kit-node/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const config = {
}
},
build: {
minify: false
minify: false,
sourcemap: true // must be true for hermetic build test!
},
plugins: [transformValidation(), sveltekit()],
optimizeDeps: {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/src/utils/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const _createCompileSvelte = (makeHot) => {
/** @type {import('../index.d.ts').CompileOptions} */
const compileOptions = {
...options.compilerOptions,
filename: normalizedFilename, // use normalized here to avoid bleeding absolute fs path
filename,
generate: ssr ? 'ssr' : 'dom'
};
if (isSvelte3) {
Expand Down

0 comments on commit 703c377

Please sign in to comment.