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: Reflect the configuration for esbuild #12676

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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/seven-feet-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Modified compileAstro to reflect the settings in viteConfig.esbuild
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export async function compileAstro({
// Compile all TypeScript to JavaScript.
// Also, catches invalid JS/TS in the compiled output before returning.
esbuildResult = await transformWithEsbuild(transformResult.code, compileProps.filename, {
...compileProps.viteConfig.esbuild,
loader: 'ts',
target: 'esnext',
Comment on lines +40 to -41
Copy link
Contributor Author

@koyopro koyopro Dec 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the default target in esbuild is esnext, the explicit specification has been removed.
(If the user does not specify vite.esbuild in the astro config, compileProps.viteConfig.esbuild will be undefined, and the default settings of esbuild will be applied)
https://esbuild.github.io/api/#target:~:text=The%20default%20target%20is%20esnext

sourcemap: 'external',
tsconfigRaw: {
compilerOptions: {
Expand Down
31 changes: 28 additions & 3 deletions packages/astro/test/units/vite-plugin-astro/compile.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { describe, it, before } from 'node:test';
import { pathToFileURL } from 'node:url';
import { init, parse } from 'es-module-lexer';
import { resolveConfig } from 'vite';
import { compileAstro } from '../../../dist/vite-plugin-astro/compile.js';

const viteConfig = await resolveConfig({ configFile: false }, 'serve');

let inlineConfig;
/**
* @param {string} source
* @param {string} id
*/
async function compile(source, id) {
const viteConfig = await resolveConfig({ configFile: false, ...inlineConfig }, 'serve');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pass the inlineConfig as a parameter of compile instead? That way we avoid passing the global around.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bluwy Thank you for the review!

I have made the corrections in the following commit. Indeed, it seems better not to have inlineConfig as a global variable. Does this change reflect your intentions?
6a966c0

return await compileAstro({
compileProps: {
astroConfig: { root: pathToFileURL('/'), base: '/', experimental: {} },
Expand All @@ -24,6 +24,10 @@ async function compile(source, id) {
}

describe('astro full compile', () => {
before(() => {
inlineConfig = {};
})

it('should compile a single file', async () => {
const result = await compile(`<h1>Hello World</h1>`, '/src/components/index.astro');
assert.ok(result.code);
Expand Down Expand Up @@ -69,4 +73,25 @@ const name = 'world
assert.equal(names.includes('file'), true);
assert.equal(names.includes('url'), true);
});

describe('when the code contains syntax that is transformed by esbuild', () => {
let code = `\
---
using x = {}
---`;

it('should not transform the syntax by default', async () => {
const result = await compile(code, '/src/components/index.astro');
assert.equal(result.code.includes('using x = {}'), true);
});

it('should transform the syntax by esbuild.target', async () => {
inlineConfig = {
esbuild: { target: 'es2018' },
}
const result = await compile(code, '/src/components/index.astro');
assert.equal(result.code.includes('using x = {}'), false);
});
});

});
Loading