summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Bjorn Lu <bjornlu.dev@gmail.com> 2022-08-08 21:55:18 +0800
committerGravatar GitHub <noreply@github.com> 2022-08-08 21:55:18 +0800
commit28ab273a3d0a1ecea84bd83591d5b5f3b212de4f (patch)
tree4cccf421cb3a5e3eb1003a92ef3ce4786d00e450
parent6086562a934ac31a186d7e72b5c4137116cc384f (diff)
downloadastro-28ab273a3d0a1ecea84bd83591d5b5f3b212de4f.tar.gz
astro-28ab273a3d0a1ecea84bd83591d5b5f3b212de4f.tar.zst
astro-28ab273a3d0a1ecea84bd83591d5b5f3b212de4f.zip
Fix client build sourcemap generation (#4195)
-rw-r--r--.changeset/strong-plants-tell.md5
-rw-r--r--packages/astro/src/core/build/static-build.ts1
-rw-r--r--packages/astro/src/vite-plugin-astro/index.ts4
-rw-r--r--packages/astro/src/vite-plugin-env/index.ts13
-rw-r--r--packages/astro/test/fixtures/sourcemap/astro.config.mjs11
-rw-r--r--packages/astro/test/fixtures/sourcemap/package.json11
-rw-r--r--packages/astro/test/fixtures/sourcemap/src/components/Counter.jsx11
-rw-r--r--packages/astro/test/fixtures/sourcemap/src/pages/index.astro17
-rw-r--r--packages/astro/test/sourcemap.test.js22
-rw-r--r--pnpm-lock.yaml12
10 files changed, 100 insertions, 7 deletions
diff --git a/.changeset/strong-plants-tell.md b/.changeset/strong-plants-tell.md
new file mode 100644
index 000000000..0fb705640
--- /dev/null
+++ b/.changeset/strong-plants-tell.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fix client build sourcemap generation
diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts
index 84574ace9..eb8f8fb5b 100644
--- a/packages/astro/src/core/build/static-build.ts
+++ b/packages/astro/src/core/build/static-build.ts
@@ -214,6 +214,7 @@ async function clientBuild(
exclude: [...(viteConfig.optimizeDeps?.exclude ?? [])],
},
build: {
+ ...viteConfig.build,
emptyOutDir: false,
minify: 'esbuild',
outDir: fileURLToPath(out),
diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts
index 63eae758e..8d59d6fa7 100644
--- a/packages/astro/src/vite-plugin-astro/index.ts
+++ b/packages/astro/src/vite-plugin-astro/index.ts
@@ -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);
diff --git a/packages/astro/src/vite-plugin-env/index.ts b/packages/astro/src/vite-plugin-env/index.ts
index af9d2d557..f513771db 100644
--- a/packages/astro/src/vite-plugin-env/index.ts
+++ b/packages/astro/src/vite-plugin-env/index.ts
@@ -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') {
@@ -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);
@@ -133,7 +133,10 @@ export default function envVitePlugin({
s.overwrite(start, end, replacement);
}
- return s.toString();
+ return {
+ code: s.toString(),
+ map: s.generateMap(),
+ };
},
};
}
diff --git a/packages/astro/test/fixtures/sourcemap/astro.config.mjs b/packages/astro/test/fixtures/sourcemap/astro.config.mjs
new file mode 100644
index 000000000..b9c082273
--- /dev/null
+++ b/packages/astro/test/fixtures/sourcemap/astro.config.mjs
@@ -0,0 +1,11 @@
+import { defineConfig } from 'astro/config';
+import react from '@astrojs/react';
+
+export default defineConfig({
+ integrations: [react()],
+ vite: {
+ build: {
+ sourcemap: true,
+ }
+ }
+})
diff --git a/packages/astro/test/fixtures/sourcemap/package.json b/packages/astro/test/fixtures/sourcemap/package.json
new file mode 100644
index 000000000..34a6086ba
--- /dev/null
+++ b/packages/astro/test/fixtures/sourcemap/package.json
@@ -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"
+ }
+}
diff --git a/packages/astro/test/fixtures/sourcemap/src/components/Counter.jsx b/packages/astro/test/fixtures/sourcemap/src/components/Counter.jsx
new file mode 100644
index 000000000..56c220522
--- /dev/null
+++ b/packages/astro/test/fixtures/sourcemap/src/components/Counter.jsx
@@ -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>
+ );
+}
diff --git a/packages/astro/test/fixtures/sourcemap/src/pages/index.astro b/packages/astro/test/fixtures/sourcemap/src/pages/index.astro
new file mode 100644
index 000000000..0556f0d62
--- /dev/null
+++ b/packages/astro/test/fixtures/sourcemap/src/pages/index.astro
@@ -0,0 +1,17 @@
+
+---
+import Counter from '../components/Counter';
+---
+
+<html>
+
+<head>
+ <title>Testing</title>
+</head>
+
+<body>
+ <h1>Testing</h1>
+ <Counter client:load />
+</body>
+
+</html> \ No newline at end of file
diff --git a/packages/astro/test/sourcemap.test.js b/packages/astro/test/sourcemap.test.js
new file mode 100644
index 000000000..c24d7f3f0
--- /dev/null
+++ b/packages/astro/test/sourcemap.test.js
@@ -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":[]');
+ });
+});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b10ebcdc9..1c5ae5244 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1783,6 +1783,18 @@ importers:
'@astrojs/solid-js': link:../../../../integrations/solid
astro: link:../../..
+ packages/astro/test/fixtures/sourcemap:
+ specifiers:
+ '@astrojs/react': workspace:*
+ astro: workspace:*
+ react: ^18.1.0
+ react-dom: ^18.1.0
+ dependencies:
+ '@astrojs/react': link:../../../../integrations/react
+ astro: link:../../..
+ react: 18.2.0
+ react-dom: 18.2.0_react@18.2.0
+
packages/astro/test/fixtures/ssr-api-route:
specifiers:
astro: workspace:*