Skip to content
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

Fix client build sourcemap generation #4195

Merged
merged 7 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/strong-plants-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix client build sourcemap generation
1 change: 1 addition & 0 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ async function clientBuild(
exclude: [...(viteConfig.optimizeDeps?.exclude ?? [])],
},
build: {
...viteConfig.build,
emptyOutDir: false,
minify: 'esbuild',
outDir: fileURLToPath(out),
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/vite-plugin-astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
const parsedId = parseAstroRequest(id);
const query = parsedId.query;
if (!id.endsWith('.astro') || query.astro) {
return source;
return;
}
// if we still get a relative path here, vite couldn't resolve the import
if (isRelativePath(parsedId.filename)) {
return source;
return;
}

const filename = normalizeFilename(parsedId.filename);
Expand Down
13 changes: 8 additions & 5 deletions packages/astro/src/vite-plugin-env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ export default function envVitePlugin({
const ssr = options?.ssr === true;

if (!ssr) {
return source;
return;
}

if (!source.includes('import.meta') || !/\benv\b/.test(source)) {
return source;
return;
}

if (typeof privateEnv === 'undefined') {
Expand Down Expand Up @@ -110,9 +110,9 @@ export default function envVitePlugin({
}
}

if (!privateEnv || !pattern) return source;
if (!privateEnv || !pattern) return;
const references = getReferencedPrivateKeys(source, privateEnv);
if (references.size === 0) return source;
if (references.size === 0) return;

// Find matches for *private* env and do our own replacement.
const s = new MagicString(source);
Expand All @@ -133,7 +133,10 @@ export default function envVitePlugin({
s.overwrite(start, end, replacement);
}

return s.toString();
return {
code: s.toString(),
map: s.generateMap(),
};
},
};
}
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/sourcemap/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'astro/config';
import react from '@astrojs/react';

export default defineConfig({
integrations: [react()],
vite: {
build: {
sourcemap: true,
}
}
})
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/sourcemap/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@test/sourcemap",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/react": "workspace:*",
"astro": "workspace:*",
"react": "^18.1.0",
"react-dom": "^18.1.0"
}
}
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/sourcemap/src/components/Counter.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { useState } from 'react';

export default function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<div>Count: {count}</div>
<button type="button" onClick={() => setCount(count+1)}>Increment</button>
</div>
);
}
17 changes: 17 additions & 0 deletions packages/astro/test/fixtures/sourcemap/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

---
import Counter from '../components/Counter';
---

<html>

<head>
<title>Testing</title>
</head>

<body>
<h1>Testing</h1>
<Counter client:load />
</body>

</html>
22 changes: 22 additions & 0 deletions packages/astro/test/sourcemap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';

describe('Sourcemap', async () => {
let fixture;

before(async () => {
fixture = await loadFixture({ root: './fixtures/sourcemap/' });
await fixture.build();
});

it('Builds sourcemap', async () => {
const dir = await fixture.readdir('.');
const counterMap = dir.find((file) => file.match(/^Counter\.\w+\.js\.map$/));
expect(counterMap).to.be.ok;
});

it('Builds non-empty sourcemap', async () => {
const map = await fixture.readFile('entry.mjs.map');
expect(map).to.not.include('"sources":[]');
});
});
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.