diff --git a/.changeset/spotty-rockets-grow.md b/.changeset/spotty-rockets-grow.md new file mode 100644 index 000000000..68d455432 --- /dev/null +++ b/.changeset/spotty-rockets-grow.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Allow specifying entryFileNames for client JS diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index fd49f9945..25e0b04e4 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -116,10 +116,10 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp input: [], output: { format: 'esm', - entryFileNames: opts.buildConfig.serverEntry, chunkFileNames: 'chunks/[name].[hash].mjs', assetFileNames: 'assets/[name].[hash][extname]', ...viteConfig.build?.rollupOptions?.output, + entryFileNames: opts.buildConfig.serverEntry, }, }, ssr: true, diff --git a/packages/astro/test/entry-file-names.test.js b/packages/astro/test/entry-file-names.test.js new file mode 100644 index 000000000..e9d1117c9 --- /dev/null +++ b/packages/astro/test/entry-file-names.test.js @@ -0,0 +1,26 @@ +import { expect } from 'chai'; +import * as cheerio from 'cheerio'; +import { loadFixture } from './test-utils.js'; + +describe('vite.build.rollupOptions.entryFileNames', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/entry-file-names', + }); + await fixture.build(); + }); + + it('Renders correctly', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + expect($('#hello')).to.have.a.lengthOf(1); + }); + + it('Outputs a client module that was specified by the config', async () => { + const js = await fixture.readFile('/assets/js/Hello.js'); + expect(js).to.be.a('string'); + expect(js.length).to.be.greaterThan(0); + }) +}); diff --git a/packages/astro/test/fixtures/entry-file-names/astro.config.mjs b/packages/astro/test/fixtures/entry-file-names/astro.config.mjs new file mode 100644 index 000000000..fbca4b567 --- /dev/null +++ b/packages/astro/test/fixtures/entry-file-names/astro.config.mjs @@ -0,0 +1,16 @@ +import { defineConfig } from 'astro/config'; +import preact from '@astrojs/preact'; + +// https://astro.build/config +export default defineConfig({ + integrations: [preact()], + vite: { + build: { + rollupOptions: { + output: { + entryFileNames: `assets/js/[name].js`, + }, + }, + }, + }, +}); diff --git a/packages/astro/test/fixtures/entry-file-names/package.json b/packages/astro/test/fixtures/entry-file-names/package.json new file mode 100644 index 000000000..760acaedb --- /dev/null +++ b/packages/astro/test/fixtures/entry-file-names/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/entry-file-names", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/preact": "workspace:", + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/entry-file-names/src/components/Hello.jsx b/packages/astro/test/fixtures/entry-file-names/src/components/Hello.jsx new file mode 100644 index 000000000..833f20f8e --- /dev/null +++ b/packages/astro/test/fixtures/entry-file-names/src/components/Hello.jsx @@ -0,0 +1,6 @@ + +export default function() { + return ( +