edge-functions 1.0.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/.eslintrc.json +60 -15
  2. package/.github/workflows/major.yml +1 -1
  3. package/.github/workflows/minor.yml +1 -2
  4. package/README.md +36 -0
  5. package/aliases.js +1 -1
  6. package/docs/overview.md +4 -3
  7. package/docs/presets.md +18 -6
  8. package/examples/angular-static/package.json +0 -1
  9. package/examples/simple-js-esm/main.js +13 -8
  10. package/examples/simple-js-esm-node/index.js +45 -0
  11. package/examples/simple-ts-esm/main.ts +24 -0
  12. package/examples/simple-ts-esm/messages.ts +11 -0
  13. package/examples/simple-ts-esm/package.json +9 -0
  14. package/examples/simple-ts-esm/tsconfig.json +109 -0
  15. package/examples/simple-ts-esm/yarn.lock +8 -0
  16. package/examples/vue-vite-static/README.md +29 -0
  17. package/examples/vue-vite-static/index.html +13 -0
  18. package/examples/vue-vite-static/package.json +18 -0
  19. package/examples/vue-vite-static/public/favicon.ico +0 -0
  20. package/examples/vue-vite-static/src/App.vue +85 -0
  21. package/examples/vue-vite-static/src/assets/base.css +73 -0
  22. package/examples/vue-vite-static/src/assets/logo.svg +1 -0
  23. package/examples/vue-vite-static/src/assets/main.css +35 -0
  24. package/examples/vue-vite-static/src/components/HelloWorld.vue +44 -0
  25. package/examples/vue-vite-static/src/components/TheWelcome.vue +86 -0
  26. package/examples/vue-vite-static/src/components/WelcomeItem.vue +86 -0
  27. package/examples/vue-vite-static/src/components/icons/IconCommunity.vue +7 -0
  28. package/examples/vue-vite-static/src/components/icons/IconDocumentation.vue +7 -0
  29. package/examples/vue-vite-static/src/components/icons/IconEcosystem.vue +7 -0
  30. package/examples/vue-vite-static/src/components/icons/IconSupport.vue +7 -0
  31. package/examples/vue-vite-static/src/components/icons/IconTooling.vue +19 -0
  32. package/examples/vue-vite-static/src/main.js +11 -0
  33. package/examples/vue-vite-static/src/router/index.js +23 -0
  34. package/examples/vue-vite-static/src/views/AboutView.vue +15 -0
  35. package/examples/vue-vite-static/src/views/HomeView.vue +9 -0
  36. package/examples/vue-vite-static/vite.config.js +16 -0
  37. package/jsconfig.json +1 -1
  38. package/lib/build/bundlers/esbuild/esbuild.config.js +12 -0
  39. package/lib/build/bundlers/esbuild/index.js +46 -0
  40. package/lib/build/bundlers/esbuild/plugins/node-polyfills/index.js +147 -0
  41. package/lib/build/bundlers/esbuild/plugins/node-polyfills/node-polyfills-paths.js +124 -0
  42. package/lib/build/bundlers/index.js +2 -2
  43. package/lib/build/bundlers/webpack/index.js +16 -7
  44. package/lib/build/bundlers/webpack/webpack.config.js +5 -25
  45. package/lib/build/dispatcher/dispatcher.js +116 -66
  46. package/lib/constants/framework-initializer.constants.js +51 -0
  47. package/lib/constants/index.js +4 -1
  48. package/lib/constants/messages/build.messages.js +2 -0
  49. package/lib/constants/messages/env.messages.js +5 -3
  50. package/lib/constants/messages/global.messages.js +4 -1
  51. package/lib/env/polyfills/FetchEvent.polyfills.js +13 -0
  52. package/lib/env/polyfills/fetch.polyfills.js +39 -0
  53. package/lib/env/polyfills/index.js +4 -0
  54. package/lib/env/runtime.env.js +57 -63
  55. package/lib/env/server.env.js +32 -76
  56. package/lib/main.js +234 -90
  57. package/lib/notations/namespaces.js +5 -0
  58. package/lib/platform/actions/core/auth.actions.js +1 -1
  59. package/lib/platform/actions/core/propagation.actions.js +8 -5
  60. package/lib/platform/actions/core/storage.actions.js +1 -1
  61. package/lib/platform/actions/function/showFunctionLogs.actions.js +71 -92
  62. package/lib/platform/edgehooks/debugRequest/debugRequest.hooks.js +28 -0
  63. package/lib/platform/edgehooks/debugRequest/index.js +3 -0
  64. package/lib/platform/edgehooks/index.js +4 -1
  65. package/lib/polyfills/FetchEvent.polyfills.js +13 -0
  66. package/lib/polyfills/fetch.polyfills.js +39 -0
  67. package/lib/polyfills/index.js +4 -0
  68. package/lib/presets/custom/angular/deliver/config.js +2 -4
  69. package/lib/presets/custom/angular/deliver/handler.js +12 -5
  70. package/lib/presets/custom/angular/deliver/prebuild.js +8 -11
  71. package/lib/presets/custom/astro/deliver/config.js +2 -4
  72. package/lib/presets/custom/astro/deliver/handler.js +14 -7
  73. package/lib/presets/custom/astro/deliver/prebuild.js +18 -21
  74. package/lib/presets/custom/hexo/deliver/config.js +2 -4
  75. package/lib/presets/custom/hexo/deliver/handler.js +13 -6
  76. package/lib/presets/custom/hexo/deliver/prebuild.js +18 -21
  77. package/lib/presets/custom/next/deliver/config.js +2 -6
  78. package/lib/presets/custom/next/deliver/handler.js +14 -7
  79. package/lib/presets/custom/next/deliver/prebuild.js +37 -40
  80. package/lib/presets/custom/react/deliver/config.js +2 -4
  81. package/lib/presets/custom/react/deliver/handler.js +10 -3
  82. package/lib/presets/custom/react/deliver/prebuild.js +4 -7
  83. package/lib/presets/custom/vue/deliver/config.js +2 -4
  84. package/lib/presets/custom/vue/deliver/handler.js +10 -3
  85. package/lib/presets/custom/vue/deliver/prebuild.js +66 -10
  86. package/lib/presets/default/html/deliver/config.js +1 -4
  87. package/lib/presets/default/html/deliver/handler.js +14 -7
  88. package/lib/presets/default/html/deliver/prebuild.js +3 -2
  89. package/lib/presets/default/javascript/compute/config.js +2 -5
  90. package/lib/presets/default/javascript/compute/handler.js +15 -4
  91. package/lib/presets/default/javascript/compute/prebuild.js +2 -1
  92. package/lib/presets/default/typescript/compute/config.js +10 -0
  93. package/lib/presets/default/typescript/compute/handler.js +16 -0
  94. package/lib/presets/default/typescript/compute/prebuild.js +7 -0
  95. package/lib/providers/azion/worker.js +6 -2
  96. package/lib/utils/exec/exec.utils.js +34 -23
  97. package/lib/utils/feedback/feedback.utils.js +6 -2
  98. package/lib/utils/getAbsoluteLibDirPath/getAbsoluteLibDirPath.utils.js +11 -2
  99. package/lib/utils/getVulcanBuildId/getVulcanBuildId.utils.js +1 -1
  100. package/lib/utils/index.js +4 -2
  101. package/lib/utils/overrideStaticOutputPath/overrideStaticOutputPath.utils.js +2 -2
  102. package/lib/utils/presets/index.js +3 -0
  103. package/lib/utils/presets/presets.utils.js +169 -0
  104. package/lib/utils/spinner/index.js +3 -0
  105. package/lib/utils/spinner/spinner.utils.js +32 -0
  106. package/package.json +17 -4
  107. package/examples/hexo-static/yarn.lock +0 -1625
  108. package/lib/build/polyfills/index.js +0 -0
  109. package/lib/utils/getPresetsList/getPresetsList.utils.js +0 -50
  110. package/lib/utils/getPresetsList/index.js +0 -3
  111. /package/lib/utils/{getPresetsList/getPresetsList.utils.test.js → presets/presets.utils.test.js} +0 -0
@@ -1,5 +1,8 @@
1
+ import debugRequest from './debugRequest/debugRequest.hooks.js';
1
2
  import mountSPA from './mountSPA/mountSPA.hooks.js';
2
3
  import mountSSG from './mountSSG/mountSSG.hooks.js';
3
4
  import ErrorHTML from './ErrorHTML/ErrorHTML.hooks.js';
4
5
 
5
- export { mountSPA, mountSSG, ErrorHTML };
6
+ export {
7
+ debugRequest, mountSPA, mountSSG, ErrorHTML,
8
+ };
@@ -0,0 +1,13 @@
1
+ import primitives from '@edge-runtime/primitives';
2
+ import { feedback } from '#utils';
3
+
4
+ class FetchEventPolyfill extends primitives.FetchEvent {
5
+ constructor(request) {
6
+ super(request);
7
+ this.console = {
8
+ log: (log) => feedback.server.log(log),
9
+ };
10
+ }
11
+ }
12
+
13
+ export default FetchEventPolyfill;
@@ -0,0 +1,39 @@
1
+ import { join } from 'path';
2
+ import { readFileSync } from 'fs';
3
+ import mime from 'mime-types';
4
+ import { EdgeRuntime } from 'edge-runtime';
5
+
6
+ /**
7
+ * A custom fetch implementation that adds an additional path to the URL if it starts with 'file://'.
8
+ * This function is used to simulate the local edge environment. When a 'file://' request is made,
9
+ * it behaves as if the request is made from within the edge itself. In this case, an additional
10
+ * '.edge/storage' folder is appended to the URL to represent the edge environment.
11
+ * @param {EdgeRuntime} context - VMContext
12
+ * @param {URL} url - The URL to fetch.
13
+ * @param {object} [options] - The fetch options.
14
+ * @returns {Promise<Response>} A Promise that resolves to the Response object.
15
+ */
16
+ async function fetchPolyfill(context, url, options) {
17
+ const {
18
+ URL, Headers, Response,
19
+ } = context;
20
+
21
+ const urlOBJ = new URL(url);
22
+ if (urlOBJ.href.startsWith('file://')) {
23
+ // url pathname = /VERSION_ID/filePath
24
+ const file = url.pathname.slice(15);
25
+ const filePath = join(process.cwd(), '.edge', 'storage', file);
26
+ const fileContent = readFileSync(filePath);
27
+ const contentType = mime.lookup(filePath) || 'application/octet-stream';
28
+
29
+ const headers = new Headers();
30
+ headers.append('Content-Type', contentType);
31
+
32
+ const response = new Response(fileContent, { headers, ...options });
33
+ return response;
34
+ }
35
+
36
+ return fetch(url, options);
37
+ }
38
+
39
+ export default fetchPolyfill;
@@ -0,0 +1,4 @@
1
+ import fetchPolyfill from './fetch.polyfills.js';
2
+ import FetchEventPolyfill from './FetchEvent.polyfills.js';
3
+
4
+ export { fetchPolyfill, FetchEventPolyfill };
@@ -3,10 +3,8 @@
3
3
  */
4
4
  const config = {
5
5
  builder: 'webpack',
6
- webpack: {
7
- config: {},
8
- plugins: [],
9
- },
6
+ useNodePolyfills: false,
7
+ custom: {},
10
8
  };
11
9
 
12
10
  export default config;
@@ -1,8 +1,15 @@
1
1
  import { mountSPA, ErrorHTML } from '#edge';
2
2
 
3
- try {
4
- const myApp = await mountSPA(event.request.url, AZION.VERSION_ID);
5
- return myApp;
6
- } catch (e) {
7
- return ErrorHTML('404');
3
+ /**
4
+ * Handles the 'fetch' event.
5
+ * @param {FetchEvent} event - The fetch event.
6
+ * @returns {Promise<Response>} The response for the request.
7
+ */
8
+ async function handle(event) {
9
+ try {
10
+ const myApp = await mountSPA(event.request.url, AZION_VERSION_ID);
11
+ return myApp;
12
+ } catch (e) {
13
+ return ErrorHTML('404');
14
+ }
8
15
  }
@@ -1,20 +1,17 @@
1
- import { exec, feedback, getPackageManager } from '#utils';
1
+ import { exec, getPackageManager } from '#utils';
2
2
 
3
3
  const packageManager = await getPackageManager();
4
4
 
5
5
  /**
6
6
  * Runs custom prebuild actions
7
+ * @param {object} buildContext - info about the build
7
8
  */
8
- async function prebuild() {
9
- try {
10
- // This is because npm interprets arguments passed directly
11
- // after the script as options for npm itself, not the script itself.
12
- const npmArgsForward = packageManager === 'npm' ? '--' : '';
13
- // support npm, yarn, pnpm
14
- await exec(`${packageManager} run build ${npmArgsForward} --output-path=.edge/statics`, 'Angular', true);
15
- } catch (error) {
16
- feedback.prebuild.error(error);
17
- }
9
+ async function prebuild(buildContext) {
10
+ // This is because npm interprets arguments passed directly
11
+ // after the script as options for npm itself, not the script itself.
12
+ const npmArgsForward = packageManager === 'npm' ? '--' : '';
13
+ // support npm, yarn, pnpm
14
+ await exec(`${packageManager} run build ${npmArgsForward} --output-path=.edge/storage`, 'Angular', true);
18
15
  }
19
16
 
20
17
  export default prebuild;
@@ -3,10 +3,8 @@
3
3
  */
4
4
  const config = {
5
5
  builder: 'webpack',
6
- webpack: {
7
- config: {},
8
- plugins: [],
9
- },
6
+ useNodePolyfills: false,
7
+ custom: {},
10
8
  };
11
9
 
12
10
  export default config;
@@ -1,8 +1,15 @@
1
- import { mountSSG, ErrorHTML } from "#edge";
1
+ import { mountSSG, ErrorHTML } from '#edge';
2
2
 
3
- try {
4
- const myApp = await mountSSG(event.request.url, AZION.VERSION_ID);
5
- return myApp;
6
- } catch (error) {
7
- return ErrorHTML("404");
8
- }
3
+ /**
4
+ * Handles the 'fetch' event.
5
+ * @param {FetchEvent} event - The fetch event.
6
+ * @returns {Promise<Response>} The response for the request.
7
+ */
8
+ async function handle(event) {
9
+ try {
10
+ const myApp = await mountSSG(event.request.url, AZION_VERSION_ID);
11
+ return myApp;
12
+ } catch (error) {
13
+ return ErrorHTML('404');
14
+ }
15
+ }
@@ -7,31 +7,28 @@ const packageManager = await getPackageManager();
7
7
 
8
8
  /**
9
9
  * Runs custom prebuild actions
10
+ * @param {object} buildContext - info about the build
10
11
  */
11
- async function prebuild() {
12
- try {
13
- const newOutDir = '.edge/statics';
14
- let outDir = 'dist';
12
+ async function prebuild(buildContext) {
13
+ const newOutDir = '.edge/storage';
14
+ let outDir = 'dist';
15
15
 
16
- // check if an output path is specified in config file
17
- const configFileContent = await readFile('./astro.config.mjs', 'utf-8');
18
- const attributeMatch = Array.from(
19
- configFileContent.matchAll(/outDir:(.*)\n/g),
20
- (match) => match,
21
- )[0];
22
- if (attributeMatch) {
23
- // get the specified value in config
24
- outDir = attributeMatch[1].trim();
25
- }
16
+ // check if an output path is specified in config file
17
+ const configFileContent = await readFile('./astro.config.mjs', 'utf-8');
18
+ const attributeMatch = Array.from(
19
+ configFileContent.matchAll(/outDir:(.*)\n/g),
20
+ (match) => match,
21
+ )[0];
22
+ if (attributeMatch) {
23
+ // get the specified value in config
24
+ outDir = attributeMatch[1].trim();
25
+ }
26
26
 
27
- await exec(`${packageManager} run build`, 'Astro', true);
27
+ await exec(`${packageManager} run build`, 'Astro', true);
28
28
 
29
- // move files to vulcan default path
30
- copyDirectory(outDir, newOutDir);
31
- rm(outDir, { recursive: true, force: true });
32
- } catch (error) {
33
- feedback.prebuild.error(error);
34
- }
29
+ // move files to vulcan default path
30
+ copyDirectory(outDir, newOutDir);
31
+ rm(outDir, { recursive: true, force: true });
35
32
  }
36
33
 
37
34
  export default prebuild;
@@ -3,10 +3,8 @@
3
3
  */
4
4
  const config = {
5
5
  builder: 'webpack',
6
- webpack: {
7
- config: {},
8
- plugins: [],
9
- },
6
+ useNodePolyfills: false,
7
+ custom: {},
10
8
  };
11
9
 
12
10
  export default config;
@@ -1,8 +1,15 @@
1
- import { mountSSG, ErrorHTML } from "#edge";
1
+ import { mountSSG, ErrorHTML } from '#edge';
2
2
 
3
- try {
4
- const myApp = await mountSSG(event.request.url, AZION.VERSION_ID);
5
- return myApp;
6
- } catch (e) {
7
- return ErrorHTML("404");
3
+ /**
4
+ * Handles the 'fetch' event.
5
+ * @param {FetchEvent} event - The fetch event.
6
+ * @returns {Promise<Response>} The response for the request.
7
+ */
8
+ async function handle(event) {
9
+ try {
10
+ const myApp = await mountSSG(event.request.url, AZION_VERSION_ID);
11
+ return myApp;
12
+ } catch (e) {
13
+ return ErrorHTML('404');
14
+ }
8
15
  }
@@ -7,31 +7,28 @@ const packageManager = await getPackageManager();
7
7
 
8
8
  /**
9
9
  * Runs custom prebuild actions
10
+ * @param {object} buildContext - info about the build
10
11
  */
11
- async function prebuild() {
12
- try {
13
- const newOutDir = '.edge/statics';
14
- let outDir = 'public';
12
+ async function prebuild(buildContext) {
13
+ const newOutDir = '.edge/storage';
14
+ let outDir = 'public';
15
15
 
16
- // check if an output path is specified in config file
17
- const configFileContent = await readFile('./_config.yml', 'utf-8');
18
- const attributeMatch = Array.from(
19
- configFileContent.matchAll(/public_dir:(.*)\n/g),
20
- (match) => match,
21
- )[0];
22
- if (attributeMatch) {
23
- // get the specified value in config
24
- outDir = attributeMatch[1].trim().replace(/["']/g, '');
25
- }
16
+ // check if an output path is specified in config file
17
+ const configFileContent = await readFile('./_config.yml', 'utf-8');
18
+ const attributeMatch = Array.from(
19
+ configFileContent.matchAll(/public_dir:(.*)\n/g),
20
+ (match) => match,
21
+ )[0];
22
+ if (attributeMatch) {
23
+ // get the specified value in config
24
+ outDir = attributeMatch[1].trim().replace(/["']/g, '');
25
+ }
26
26
 
27
- await exec(`${packageManager} hexo generate`, 'Hexo', true);
27
+ await exec(`${packageManager} hexo generate`, 'Hexo', true);
28
28
 
29
- // move files to vulcan default path
30
- copyDirectory(outDir, newOutDir);
31
- rm(outDir, { recursive: true, force: true });
32
- } catch (error) {
33
- feedback.prebuild.error(error);
34
- }
29
+ // move files to vulcan default path
30
+ copyDirectory(outDir, newOutDir);
31
+ rm(outDir, { recursive: true, force: true });
35
32
  }
36
33
 
37
34
  export default prebuild;
@@ -3,12 +3,8 @@
3
3
  */
4
4
  const config = {
5
5
  builder: 'webpack',
6
- webpack: {
7
- config: {
8
- distDir: '.edge/statics',
9
- },
10
- plugins: [],
11
- },
6
+ useNodePolyfills: false,
7
+ custom: {},
12
8
  };
13
9
 
14
10
  export default config;
@@ -1,9 +1,16 @@
1
- import { mountSSG } from "#edge";
1
+ import { mountSSG } from '#edge';
2
2
 
3
- try {
4
- const myApp = await mountSSG(event.request.url, AZION.VERSION_ID);
5
- return myApp;
6
- } catch (e) {
7
- const notFoundError = new URL(`${AZION.VERSION_ID}/404.html`, 'file://')
8
- return fetch(notFoundError)
3
+ /**
4
+ * Handles the 'fetch' event.
5
+ * @param {FetchEvent} event - The fetch event.
6
+ * @returns {Promise<Response>} The response for the request.
7
+ */
8
+ async function handle(event) {
9
+ try {
10
+ const myApp = await mountSSG(event.request.url, AZION_VERSION_ID);
11
+ return myApp;
12
+ } catch (e) {
13
+ const notFoundError = new URL(`${AZION_VERSION_ID}/404.html`, 'file://');
14
+ return fetch(notFoundError);
15
+ }
9
16
  }
@@ -26,11 +26,11 @@ const packageManager = await getPackageManager();
26
26
  * @param {string} directory - The root directory to start moving files.
27
27
  * @example
28
28
  * // Before:
29
- * // Root directory: '/path/to/project/.edge/statics'
29
+ * // Root directory: '/path/to/project/.edge/storage'
30
30
  * // Files in the directory: ['home.html', 'about.html', 'contact.html', 'styles.css']
31
31
  *
32
- * // After calling moveFiles('/path/to/project/.edge/statics'):
33
- * // Root directory: '/path/to/project/.edge/statics'
32
+ * // After calling moveFiles('/path/to/project/.edge/storage'):
33
+ * // Root directory: '/path/to/project/.edge/storage'
34
34
  * // Files in the directory: ['styles.css']
35
35
  * // Subdirectory 'home':
36
36
  * // - index.html (previously home.html)
@@ -139,55 +139,52 @@ function validateStaticSiteMode(nextConfig) {
139
139
  }
140
140
 
141
141
  /**
142
- * Runs custom prebuild actions.
142
+ * Runs custom prebuild actions
143
+ * @param {object} buildContext - info about the build
143
144
  */
144
- async function prebuild() {
145
- try {
146
- feedback.prebuild.info('Starting Next.js static build process...');
145
+ async function prebuild(buildContext) {
146
+ feedback.prebuild.info('Starting Next.js static build process...');
147
147
 
148
- const nextVersion = getPackageVersion('next');
149
- feedback.prebuild.info('Detected Next.js version:', nextVersion);
148
+ const nextVersion = getPackageVersion('next');
149
+ feedback.prebuild.info('Detected Next.js version:', nextVersion);
150
150
 
151
- const staticsOutputDir = '.edge/statics';
151
+ const staticsOutputDir = '.edge/storage';
152
152
 
153
- if (isANewerVersion(nextVersion)) {
154
- const nextConfig = await getNextConfig();
153
+ if (isANewerVersion(nextVersion)) {
154
+ const nextConfig = await getNextConfig();
155
155
 
156
- validateStaticSiteMode(nextConfig);
156
+ validateStaticSiteMode(nextConfig);
157
157
 
158
- // check if an output path is specified in config file
159
- let outDir = 'out';
160
- const configFileContent = await readFile('./next.config.js', 'utf-8');
161
- const attributeMatch = Array.from(
162
- configFileContent.matchAll(/distDir:(.*),/g),
163
- (match) => match,
164
- )[0];
165
- if (attributeMatch) {
166
- // get the specified value in config
167
- outDir = attributeMatch[1].trim().replace(/["']/g, '');
168
- }
158
+ // check if an output path is specified in config file
159
+ let outDir = 'out';
160
+ const configFileContent = await readFile('./next.config.js', 'utf-8');
161
+ const attributeMatch = Array.from(
162
+ configFileContent.matchAll(/distDir:(.*),/g),
163
+ (match) => match,
164
+ )[0];
165
+ if (attributeMatch) {
166
+ // get the specified value in config
167
+ outDir = attributeMatch[1].trim().replace(/["']/g, '');
168
+ }
169
169
 
170
- await exec(`${packageManager} run build`, true);
170
+ await exec(`${packageManager} run build`, `Next ${nextVersion}`, true);
171
171
 
172
- // move files to vulcan default path
173
- copyDirectory(outDir, staticsOutputDir);
174
- rm(outDir, { recursive: true, force: true });
172
+ // move files to vulcan default path
173
+ copyDirectory(outDir, staticsOutputDir);
174
+ rm(outDir, { recursive: true, force: true });
175
175
 
176
- feedback.prebuild.info('Adapting Next.js build output...');
177
- await moveFiles(`${process.cwd()}/${staticsOutputDir}`);
178
- } else {
179
- await exec(`${packageManager} run build`, true);
176
+ feedback.prebuild.info('Adapting Next.js build output...');
177
+ await moveFiles(`${process.cwd()}/${staticsOutputDir}`);
178
+ } else {
179
+ await exec(`${packageManager} run build`, `Next ${nextVersion}`, true);
180
180
 
181
- await exec(`npx next export -o ${staticsOutputDir}`, true);
181
+ await exec(`npx next export -o ${staticsOutputDir}`, `Next ${nextVersion}`, true);
182
182
 
183
- feedback.prebuild.info('Adapting Next.js build output...');
184
- await moveFiles(`${process.cwd()}/${staticsOutputDir}`);
185
- }
186
-
187
- feedback.prebuild.success('Next.js build adaptation completed successfully.');
188
- } catch (error) {
189
- feedback.prebuild.error('Error occurred during Next.js build adaptation:', error);
183
+ feedback.prebuild.info('Adapting Next.js build output...');
184
+ await moveFiles(`${process.cwd()}/${staticsOutputDir}`);
190
185
  }
186
+
187
+ feedback.prebuild.success('Next.js build adaptation completed successfully.');
191
188
  }
192
189
 
193
190
  export default prebuild;
@@ -3,10 +3,8 @@
3
3
  */
4
4
  const config = {
5
5
  builder: 'webpack',
6
- webpack: {
7
- config: {},
8
- plugins: [],
9
- },
6
+ useNodePolyfills: false,
7
+ custom: {},
10
8
  };
11
9
 
12
10
  export default config;
@@ -1,8 +1,15 @@
1
- import { mountSPA, ErrorHTML } from "#edge";
1
+ import { mountSPA, ErrorHTML } from '#edge';
2
2
 
3
+ /**
4
+ * Handles the 'fetch' event.
5
+ * @param {FetchEvent} event - The fetch event.
6
+ * @returns {Promise<Response>} The response for the request.
7
+ */
8
+ async function handle(event) {
3
9
  try {
4
- const myApp = await mountSPA(event.request.url, AZION.VERSION_ID);
10
+ const myApp = await mountSPA(event.request.url, AZION_VERSION_ID);
5
11
  return myApp;
6
12
  } catch (e) {
7
- return ErrorHTML("404");
13
+ return ErrorHTML('404');
8
14
  }
15
+ }
@@ -1,16 +1,13 @@
1
- import { exec, feedback, getPackageManager } from '#utils';
1
+ import { exec, getPackageManager } from '#utils';
2
2
 
3
3
  const packageManager = await getPackageManager();
4
4
 
5
5
  /**
6
6
  * Runs custom prebuild actions
7
+ * @param {object} buildContext - info about the build
7
8
  */
8
- async function prebuild() {
9
- try {
10
- await exec(`BUILD_PATH="./.edge/statics" ${packageManager} run build`, 'React', true);
11
- } catch (error) {
12
- feedback.prebuild.error(error);
13
- }
9
+ async function prebuild(buildContext) {
10
+ await exec(`BUILD_PATH="./.edge/storage" ${packageManager} run build`, 'React', true);
14
11
  }
15
12
 
16
13
  export default prebuild;
@@ -3,10 +3,8 @@
3
3
  */
4
4
  const config = {
5
5
  builder: 'webpack',
6
- webpack: {
7
- config: {},
8
- plugins: [],
9
- },
6
+ useNodePolyfills: false,
7
+ custom: {},
10
8
  };
11
9
 
12
10
  export default config;
@@ -1,8 +1,15 @@
1
- import { mountSPA, ErrorHTML } from "#edge";
1
+ import { mountSPA, ErrorHTML } from '#edge';
2
2
 
3
+ /**
4
+ * Handles the 'fetch' event.
5
+ * @param {FetchEvent} event - The fetch event.
6
+ * @returns {Promise<Response>} The response for the request.
7
+ */
8
+ async function handle(event) {
3
9
  try {
4
- const myApp = await mountSPA(event.request.url, AZION.VERSION_ID);
10
+ const myApp = await mountSPA(event.request.url, AZION_VERSION_ID);
5
11
  return myApp;
6
12
  } catch (e) {
7
- return ErrorHTML("404");
13
+ return ErrorHTML('404');
8
14
  }
15
+ }
@@ -1,19 +1,75 @@
1
- import { exec, feedback, getPackageManager } from '#utils';
1
+ import {
2
+ exec, getPackageManager, copyDirectory,
3
+ } from '#utils';
4
+ import { lstat, readFile, rm } from 'fs/promises';
5
+ import { join } from 'path';
2
6
 
3
7
  const packageManager = await getPackageManager();
8
+ const edgeStorageDir = '.edge/storage';
9
+ const defaultViteOutDir = 'dist';
4
10
 
5
11
  /**
6
- * Runs custom prebuild actions
12
+ * Check if the vite.config.js file exists.
13
+ * @returns {boolean} True if the file exists, false otherwise.
7
14
  */
8
- async function prebuild() {
15
+ async function viteConfigExists() {
9
16
  try {
10
- // This is because npm interprets arguments passed directly
11
- // after the script as options for npm itself, not the script itself.
12
- const npmArgsForward = packageManager === 'npm' ? '--' : '';
13
- // support npm, yarn, pnpm
14
- await exec(`${packageManager} run build ${npmArgsForward} --dest .edge/statics`, 'Vue', true);
15
- } catch (error) {
16
- feedback.prebuild.error(error);
17
+ await lstat('./vite.config.js');
18
+ return true;
19
+ } catch (err) {
20
+ return false;
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Read and parse the vite.config.js file.
26
+ * @returns {object} The parsed configuration object or null if the file doesn't exist.
27
+ */
28
+ async function readViteConfig() {
29
+ try {
30
+ const configFileContent = await readFile('./vite.config.js', 'utf-8');
31
+ return JSON.parse(configFileContent.replace(/^module.exports = /, ''));
32
+ } catch (err) {
33
+ return null;
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Runs custom prebuild actions.
39
+ */
40
+ async function prebuild() {
41
+ const npmArgsForward = packageManager === 'npm' ? '--' : '';
42
+
43
+ let outDir = defaultViteOutDir;
44
+ let destPath = edgeStorageDir;
45
+
46
+ const isViteProject = await viteConfigExists();
47
+
48
+ if (isViteProject) {
49
+ await exec(
50
+ `${packageManager} run build ${npmArgsForward}`,
51
+ 'Vue/Vite',
52
+ true,
53
+ );
54
+
55
+ const config = await readViteConfig();
56
+
57
+ if (config?.build?.outDir) {
58
+ outDir = config.build.outDir;
59
+ }
60
+
61
+ const srcPath = join('.', outDir);
62
+ destPath = join(edgeStorageDir);
63
+ copyDirectory(outDir, srcPath);
64
+ rm(outDir, { recursive: true, force: true });
65
+ }
66
+
67
+ if (!isViteProject) {
68
+ await exec(
69
+ `${packageManager} run build ${npmArgsForward} --dest ${destPath}`,
70
+ 'Vue',
71
+ true,
72
+ );
17
73
  }
18
74
  }
19
75
 
@@ -4,10 +4,7 @@
4
4
  const config = {
5
5
  builder: 'webpack',
6
6
  useNodePolyfills: false,
7
- custom: {
8
- config: {},
9
- plugins: [],
10
- },
7
+ custom: {},
11
8
  };
12
9
 
13
10
  export default config;