diff --git a/packages/integrations/deno/package.json b/packages/integrations/deno/package.json index f1b65d62bf..6a94327e8e 100644 --- a/packages/integrations/deno/package.json +++ b/packages/integrations/deno/package.json @@ -22,7 +22,12 @@ "build": "astro-scripts build \"src/**/*.ts\" && tsc", "build:ci": "astro-scripts build \"src/**/*.ts\"", "dev": "astro-scripts dev \"src/**/*.ts\"", - "test": "deno test --allow-run --allow-read --allow-env --allow-net ./test/" + "test:import": "deno test --allow-run --allow-env --allow-read --allow-net --ignore=test/dynamic-import.test.js ./test/", + "test:subprocess": "deno test --allow-run --allow-env --allow-net ./test/dynamic-import.test.js", + "test": "npm run test:import && npm run test:subprocess" + }, + "dependencies": { + "esbuild": "^0.14.42" }, "devDependencies": { "astro": "workspace:*", diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index aef1f83836..d1fc5cc581 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -1,4 +1,8 @@ import type { AstroAdapter, AstroIntegration } from 'astro'; +import { fileURLToPath } from 'url'; +import esbuild from 'esbuild'; +import * as npath from 'path'; +import * as fs from 'fs'; interface Options { port?: number; @@ -15,14 +19,20 @@ export function getAdapter(args?: Options): AstroAdapter { } export default function createIntegration(args?: Options): AstroIntegration { + let _buildConfig: any; + let _vite: any; return { name: '@astrojs/deno', hooks: { 'astro:config:done': ({ setAdapter }) => { setAdapter(getAdapter(args)); }, + 'astro:build:start': ({ buildConfig }) => { + _buildConfig = buildConfig; + }, 'astro:build:setup': ({ vite, target }) => { if (target === 'server') { + _vite = vite; vite.resolve = vite.resolve || {}; vite.resolve.alias = vite.resolve.alias || {}; @@ -41,6 +51,27 @@ export default function createIntegration(args?: Options): AstroIntegration { }; } }, + 'astro:build:done': async () => { + const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server); + const pth = fileURLToPath(entryUrl); + await esbuild.build({ + target: 'es2020', + platform: 'browser', + entryPoints: [pth], + outfile: pth, + allowOverwrite: true, + format: 'esm', + bundle: true + }); + + // Remove chunks, if they exist. Since we have bundled via esbuild these chunks are trash. + try { + const chunkFileNames = _vite?.build?.rollupOptions?.output?.chunkFileNames ?? 'chunks/chunk.[hash].mjs'; + const chunkPath = npath.dirname(chunkFileNames); + const chunksDirUrl = new URL(chunkPath + '/', _buildConfig.server); + await fs.promises.rm(chunksDirUrl, { recursive: true, force: true }); + } catch {} + } }, }; } diff --git a/packages/integrations/deno/src/server.ts b/packages/integrations/deno/src/server.ts index 157d06751c..d1bed9aec9 100644 --- a/packages/integrations/deno/src/server.ts +++ b/packages/integrations/deno/src/server.ts @@ -32,13 +32,15 @@ export function start(manifest: SSRManifest, options: Options) { return fetch(localPath.toString()); }; + const port = options.port ?? 8085; _server = new Server({ - port: options.port ?? 8085, + port, hostname: options.hostname ?? '0.0.0.0', handler, }); - _startPromise = _server.listenAndServe(); + _startPromise = Promise.resolve(_server.listenAndServe()); + console.error(`Server running on port ${port}`); } export function createExports(manifest: SSRManifest, options: Options) { diff --git a/packages/integrations/deno/test/deps.js b/packages/integrations/deno/test/deps.js index 80410a338b..0fd0a66735 100644 --- a/packages/integrations/deno/test/deps.js +++ b/packages/integrations/deno/test/deps.js @@ -1,3 +1,4 @@ export * from 'https://deno.land/std@0.110.0/path/mod.ts'; export * from 'https://deno.land/std@0.132.0/testing/asserts.ts'; export * from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts'; +export * from 'https://deno.land/std@0.142.0/streams/conversion.ts'; diff --git a/packages/integrations/deno/test/dynamic-import.test.js b/packages/integrations/deno/test/dynamic-import.test.js new file mode 100644 index 0000000000..2f1a100515 --- /dev/null +++ b/packages/integrations/deno/test/dynamic-import.test.js @@ -0,0 +1,22 @@ +import { runBuildAndStartAppFromSubprocess } from './helpers.js'; +import { assertEquals, assert, DOMParser } from './deps.js'; + +async function startApp(cb) { + await runBuildAndStartAppFromSubprocess('./fixtures/dynimport/', cb); +} + +Deno.test({ + name: 'Dynamic import', + async fn() { + await startApp(async () => { + const resp = await fetch('http://127.0.0.1:8085/'); + assertEquals(resp.status, 200); + const html = await resp.text(); + console.log(html); + assert(html); + const doc = new DOMParser().parseFromString(html, `text/html`); + const div = doc.querySelector('#thing'); + assert(div, 'div exists'); + }); + }, +}); diff --git a/packages/integrations/deno/test/fixtures/dynimport/astro.config.mjs b/packages/integrations/deno/test/fixtures/dynimport/astro.config.mjs new file mode 100644 index 0000000000..e56fe2e985 --- /dev/null +++ b/packages/integrations/deno/test/fixtures/dynimport/astro.config.mjs @@ -0,0 +1,9 @@ +import { defineConfig } from 'astro/config'; +import deno from '@astrojs/deno'; + +export default defineConfig({ + adapter: deno(), + experimental: { + ssr: true + } +}) diff --git a/packages/integrations/deno/test/fixtures/dynimport/package.json b/packages/integrations/deno/test/fixtures/dynimport/package.json new file mode 100644 index 0000000000..5dcf7da6e8 --- /dev/null +++ b/packages/integrations/deno/test/fixtures/dynimport/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/deno-astro-dynimport", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/deno": "workspace:*" + } +} diff --git a/packages/integrations/deno/test/fixtures/dynimport/src/components/Thing.astro b/packages/integrations/deno/test/fixtures/dynimport/src/components/Thing.astro new file mode 100644 index 0000000000..8d8ef929a0 --- /dev/null +++ b/packages/integrations/deno/test/fixtures/dynimport/src/components/Thing.astro @@ -0,0 +1,4 @@ +--- + +--- +