|
1 | | -import { globSync } from 'node:fs'; |
2 | | -import { builtinModules } from 'node:module'; |
3 | | -import { extname, resolve } from 'node:path'; |
| 1 | +import { resolve } from 'node:path'; |
4 | 2 | import { defineConfig } from 'vite'; |
5 | 3 |
|
6 | 4 | export const baseConfig = defineConfig(() => { |
@@ -38,69 +36,3 @@ export const baseConfig = defineConfig(() => { |
38 | 36 | }, |
39 | 37 | }; |
40 | 38 | }); |
41 | | - |
42 | | -// ESM doesn't define __dirname/__filename. This shim ensures bundled CJS dependencies |
43 | | -// that reference them won't throw a ReferenceError at runtime. |
44 | | -// TODO: [MI-251] Support shims only when needed instead consider looking at tsup, tsdown and rollup shims plugins |
45 | | -const shimBanner = `import { fileURLToPath as __shim_fileURLToPath } from 'node:url'; |
46 | | -import { dirname as __shim_dirname } from 'node:path'; |
47 | | -const __filename = __shim_fileURLToPath(import.meta.url); |
48 | | -const __dirname = __shim_dirname(__filename);`; |
49 | | - |
50 | | -/** |
51 | | - * Creates Vite environments for bundling Lambda handlers using the Vite Environment API. |
52 | | - * Each handler file gets its own environment that outputs to dist/<entryName>/index.mjs. |
53 | | - * |
54 | | - * This uses Vite's built-in `mode` to control build behaviour. |
55 | | - * See https://vite.dev/guide/env-and-mode |
56 | | - */ |
57 | | -export function defineLambdaEnvironments(subPath, options = {}, envPrefix = 'lambda') { |
58 | | - // When running vitest, skip the Lambda environments entirely |
59 | | - if (process.env.VITEST === 'true') return {}; |
60 | | - |
61 | | - if (subPath.includes('..')) throw new Error('Invalid path provided'); |
62 | | - |
63 | | - const handlersPath = resolve(process.cwd(), subPath); |
64 | | - const handlers = globSync(`${handlersPath}/**/*.ts`); |
65 | | - |
66 | | - const environments = {}; |
67 | | - for (const handler of handlers) { |
68 | | - const bundledPath = handler.replace(`${handlersPath}/`, ''); |
69 | | - const entryName = bundledPath.replace(extname(bundledPath), ''); |
70 | | - const envName = `${envPrefix}_${entryName.replace(/[\\/]/g, '_')}`; |
71 | | - |
72 | | - environments[envName] = { |
73 | | - resolve: { noExternal: true }, |
74 | | - build: { |
75 | | - outDir: `dist/${entryName}`, |
76 | | - minify: options.mode === 'development' ? 'oxc' : false, |
77 | | - sourcemap: options.mode !== 'production', |
78 | | - platform: 'node', |
79 | | - rollupOptions: { |
80 | | - input: { index: handler }, |
81 | | - moduleTypes: { ...options.moduleTypes }, |
82 | | - external: [...builtinModules, ...builtinModules.map(m => `node:${m}`)], |
83 | | - output: { |
84 | | - entryFileNames: 'index.mjs', |
85 | | - format: 'esm', |
86 | | - comments: { legal: false }, |
87 | | - codeSplitting: false, |
88 | | - banner: shimBanner, |
89 | | - }, |
90 | | - }, |
91 | | - }, |
92 | | - }; |
93 | | - } |
94 | | - |
95 | | - return { |
96 | | - environments, |
97 | | - builder: { |
98 | | - buildApp: async builder => { |
99 | | - const lambdaEnvs = Object.entries(builder.environments) |
100 | | - .filter(([name]) => name.startsWith(`${envPrefix}_`)) |
101 | | - .map(([, env]) => env); |
102 | | - await Promise.all(lambdaEnvs.map(env => builder.build(env))); |
103 | | - }, |
104 | | - }, |
105 | | - }; |
106 | | -} |
0 commit comments