|
1 | | -import { createWriteStream } from 'node:fs' |
2 | 1 | import { cp, readFile, rm } from 'node:fs/promises'
|
3 | 2 | import { dirname, join, resolve } from 'node:path'
|
4 | 3 | import { fileURLToPath } from 'node:url'
|
5 | | -import { Readable } from 'stream' |
6 | | -import { finished } from 'stream/promises' |
7 | 4 |
|
8 | 5 | import { build, context } from 'esbuild'
|
9 | | -import { execaCommand } from 'execa' |
10 | 6 | import glob from 'fast-glob'
|
11 | 7 |
|
| 8 | +import { vendorDeno } from './build-helpers.js' |
| 9 | + |
12 | 10 | const OUT_DIR = 'dist'
|
13 | 11 | await rm(OUT_DIR, { force: true, recursive: true })
|
14 | 12 |
|
@@ -83,47 +81,22 @@ async function bundle(entryPoints, format, watch) {
|
83 | 81 | })
|
84 | 82 | }
|
85 | 83 |
|
86 | | -async function vendorDeno() { |
| 84 | +async function vendorMiddlewareDenoModules() { |
87 | 85 | const vendorSource = resolve('edge-runtime/vendor.ts')
|
88 | | - const vendorDest = resolve('edge-runtime/vendor') |
89 | | - |
90 | | - try { |
91 | | - await execaCommand('deno --version') |
92 | | - } catch { |
93 | | - throw new Error('Could not check the version of Deno. Is it installed on your system?') |
94 | | - } |
95 | | - |
96 | | - console.log(`🧹 Deleting '${vendorDest}'...`) |
97 | | - |
98 | | - await rm(vendorDest, { force: true, recursive: true }) |
| 86 | + const middlewareDir = resolve('edge-runtime') |
99 | 87 |
|
100 | | - console.log(`📦 Vendoring Deno modules into '${vendorDest}'...`) |
101 | | - |
102 | | - await execaCommand(`deno vendor ${vendorSource} --output=${vendorDest} --force`) |
103 | | - |
104 | | - // htmlrewriter contains wasm files and those don't currently work great with vendoring |
105 | | - // see https://github.com/denoland/deno/issues/14123 |
106 | | - // to workaround this we copy the wasm files manually |
107 | | - const filesToDownload = ['https://deno.land/x/htmlrewriter@v1.0.0/pkg/htmlrewriter_bg.wasm'] |
108 | | - await Promise.all( |
109 | | - filesToDownload.map(async (urlString) => { |
110 | | - const url = new URL(urlString) |
111 | | - |
112 | | - const destination = join(vendorDest, url.hostname, url.pathname) |
113 | | - |
114 | | - const res = await fetch(url) |
115 | | - if (!res.ok) throw new Error('Failed to fetch .wasm file to vendor', { cause: err }) |
116 | | - const fileStream = createWriteStream(destination, { flags: 'wx' }) |
117 | | - await finished(Readable.fromWeb(res.body).pipe(fileStream)) |
118 | | - }), |
119 | | - ) |
| 88 | + await vendorDeno({ |
| 89 | + vendorSource, |
| 90 | + cwd: middlewareDir, |
| 91 | + wasmFilesToDownload: ['https://deno.land/x/htmlrewriter@v1.0.0/pkg/htmlrewriter_bg.wasm'], |
| 92 | + }) |
120 | 93 | }
|
121 | 94 |
|
122 | 95 | const args = new Set(process.argv.slice(2))
|
123 | 96 | const watch = args.has('--watch') || args.has('-w')
|
124 | 97 |
|
125 | 98 | await Promise.all([
|
126 | | - vendorDeno(), |
| 99 | + vendorMiddlewareDenoModules(), |
127 | 100 | bundle(entryPointsESM, 'esm', watch),
|
128 | 101 | ...entryPointsCJS.map((entry) => bundle([entry], 'cjs', watch)),
|
129 | 102 | cp('src/build/templates', join(OUT_DIR, 'build/templates'), { recursive: true, force: true }),
|
|
0 commit comments