edge-functions 2.2.0-stage.1 → 2.2.0-stage.2
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.
- package/CHANGELOG.md +15 -0
- package/README.md +1 -1
- package/jest.config.e2e.js +8 -0
- package/jest.config.unit.js +7 -0
- package/lib/build/bundlers/bundler-base.js +102 -0
- package/lib/build/bundlers/esbuild/esbuild.config.js +2 -0
- package/lib/build/bundlers/esbuild/index.js +46 -33
- package/lib/build/bundlers/esbuild/index.test.js +90 -0
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/index.js +92 -131
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/index.test.js +38 -0
- package/lib/build/bundlers/polyfills/node/_empty.js +0 -0
- package/lib/build/bundlers/polyfills/node/crypto.js +70 -0
- package/lib/build/bundlers/polyfills/node/globals/buffer.js +4 -0
- package/lib/build/bundlers/polyfills/node/globals/path-dirname.js +6 -0
- package/lib/build/{polyfills → bundlers/polyfills}/node/globals/process.js +76 -24
- package/lib/build/bundlers/polyfills/polyfills-manager.js +138 -0
- package/lib/build/bundlers/{esbuild/plugins/node-polyfills/node-pollyfills-paths.test.js → polyfills/polyfills-manager.test.js} +34 -21
- package/lib/build/bundlers/webpack/index.js +44 -28
- package/lib/build/bundlers/webpack/index.test.js +101 -0
- package/lib/build/bundlers/webpack/plugins/node-polyfills/index.js +65 -0
- package/lib/build/bundlers/webpack/plugins/node-polyfills/index.test.js +55 -0
- package/lib/build/bundlers/webpack/webpack.config.js +31 -2
- package/lib/build/dispatcher/dispatcher.js +203 -118
- package/lib/build/dispatcher/dispatcher.test.js +0 -1
- package/lib/constants/messages/build.messages.js +4 -0
- package/lib/presets/custom/next/compute/config.js +2 -87
- package/lib/presets/custom/next/compute/node/custom-server/12.3.1/server/require.js +3 -0
- package/package.json +12 -5
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/node-polyfills-paths.js +0 -122
- /package/lib/build/{polyfills → bundlers/polyfills}/node/dns.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/fs.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/globals/navigator.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/globals/performance.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/http2.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/module.js +0 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign,class-methods-use-this */
|
|
2
|
+
import { generateWebpackBanner } from '#utils';
|
|
3
|
+
import PolyfillsManager from '../../../polyfills/polyfills-manager.js';
|
|
4
|
+
|
|
5
|
+
class NodePolyfillPlugin {
|
|
6
|
+
apply(compiler) {
|
|
7
|
+
const polyfillsManager = PolyfillsManager.buildPolyfills();
|
|
8
|
+
|
|
9
|
+
if (!compiler.options.plugins?.length) {
|
|
10
|
+
compiler.options.plugins = [];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// additional plugin to handle "node:" URIs
|
|
14
|
+
compiler.options.plugins.push(
|
|
15
|
+
new compiler.webpack.NormalModuleReplacementPlugin(
|
|
16
|
+
/node:/,
|
|
17
|
+
(resource) => {
|
|
18
|
+
const mod = resource.request.replace(/^node:/, '');
|
|
19
|
+
resource.request = mod;
|
|
20
|
+
},
|
|
21
|
+
),
|
|
22
|
+
);
|
|
23
|
+
// globals
|
|
24
|
+
compiler.options.plugins.push(
|
|
25
|
+
new compiler.webpack.ProvidePlugin({
|
|
26
|
+
Buffer: ['buffer', 'Buffer'],
|
|
27
|
+
process: polyfillsManager.globals.get('process'),
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// env
|
|
32
|
+
compiler.options.plugins.push(
|
|
33
|
+
new compiler.webpack.EnvironmentPlugin({
|
|
34
|
+
NEXT_RUNTIME: 'edge',
|
|
35
|
+
NEXT_COMPUTE_JS: true,
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
compiler.options.plugins.push(
|
|
40
|
+
new compiler.webpack.BannerPlugin({
|
|
41
|
+
banner: generateWebpackBanner([
|
|
42
|
+
polyfillsManager.globals.get('navigator'),
|
|
43
|
+
polyfillsManager.globals.get('performance'),
|
|
44
|
+
]),
|
|
45
|
+
raw: true,
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
compiler.options.resolve.alias = {
|
|
50
|
+
...Object.fromEntries(
|
|
51
|
+
[...polyfillsManager.alias].map(([key, value]) => [key, value]),
|
|
52
|
+
),
|
|
53
|
+
...compiler.options.resolve.alias,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
compiler.options.resolve.fallback = {
|
|
57
|
+
...Object.fromEntries(
|
|
58
|
+
[...polyfillsManager.libs].map(([key, value]) => [key, value]),
|
|
59
|
+
),
|
|
60
|
+
...compiler.options.resolve.fallback,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default NodePolyfillPlugin;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { promisify } from 'util';
|
|
2
|
+
import webpack from 'webpack';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import tmp from 'tmp';
|
|
5
|
+
import NodePolyfillPlugin from './index.js';
|
|
6
|
+
|
|
7
|
+
describe('Webpack Node Plugin', () => {
|
|
8
|
+
let tmpDir;
|
|
9
|
+
let tmpEntry;
|
|
10
|
+
let tmpOutput;
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
tmpDir = tmp.dirSync();
|
|
14
|
+
tmpEntry = tmp.fileSync({
|
|
15
|
+
postfix: '.js',
|
|
16
|
+
dir: tmpDir.name,
|
|
17
|
+
name: 'entry.js',
|
|
18
|
+
});
|
|
19
|
+
tmpOutput = tmp.fileSync({
|
|
20
|
+
postfix: '.js',
|
|
21
|
+
dir: tmpDir.name,
|
|
22
|
+
name: 'output.js',
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterAll(async () => {
|
|
27
|
+
tmpEntry.removeCallback();
|
|
28
|
+
tmpOutput.removeCallback();
|
|
29
|
+
tmpDir.removeCallback();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should check the (NODE_ENV) in Environment and resolve module crypto', async () => {
|
|
33
|
+
const code = `import crypto from 'crypto';console.log(process.env.NODE_ENV);`;
|
|
34
|
+
await fs.promises.writeFile(tmpEntry.name, code);
|
|
35
|
+
|
|
36
|
+
const runWebpack = promisify(webpack);
|
|
37
|
+
|
|
38
|
+
await runWebpack({
|
|
39
|
+
entry: tmpEntry.name,
|
|
40
|
+
mode: 'production',
|
|
41
|
+
optimization: {
|
|
42
|
+
minimize: false,
|
|
43
|
+
},
|
|
44
|
+
output: {
|
|
45
|
+
path: tmpDir.name,
|
|
46
|
+
filename: 'output.js',
|
|
47
|
+
},
|
|
48
|
+
target: ['webworker', 'es2022'],
|
|
49
|
+
plugins: [new NodePolyfillPlugin()],
|
|
50
|
+
});
|
|
51
|
+
const bundle = fs.readFileSync(tmpOutput.name, 'utf-8');
|
|
52
|
+
expect(bundle).toContain('console.log("production")');
|
|
53
|
+
expect(bundle).toContain('./lib/build/bundlers/polyfills/node/crypto.js');
|
|
54
|
+
}, 20000);
|
|
55
|
+
});
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import webpack from 'webpack';
|
|
2
2
|
import { join } from 'path';
|
|
3
3
|
import { fileURLToPath } from 'url';
|
|
4
|
+
import { createRequire } from 'module';
|
|
5
|
+
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
4
7
|
|
|
5
8
|
const projectRoot = process.cwd();
|
|
6
9
|
const isWindows = process.platform === 'win32';
|
|
@@ -12,9 +15,35 @@ export default {
|
|
|
12
15
|
output: {
|
|
13
16
|
path: outputPath,
|
|
14
17
|
filename: 'worker.js',
|
|
15
|
-
globalObject: '
|
|
18
|
+
globalObject: 'globalThis',
|
|
19
|
+
},
|
|
20
|
+
resolve: {
|
|
21
|
+
extensions: ['.tsx', '.ts', '.js'],
|
|
22
|
+
mainFields: ['browser', 'main', 'module'],
|
|
23
|
+
},
|
|
24
|
+
// loaders
|
|
25
|
+
module: {
|
|
26
|
+
rules: [
|
|
27
|
+
{
|
|
28
|
+
test: /\.ts?$/,
|
|
29
|
+
use: [
|
|
30
|
+
{
|
|
31
|
+
loader: require.resolve('ts-loader'),
|
|
32
|
+
options: {
|
|
33
|
+
transpileOnly: true,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
exclude: /node_modules/,
|
|
38
|
+
},
|
|
39
|
+
],
|
|
16
40
|
},
|
|
17
41
|
mode: 'production',
|
|
18
42
|
target: ['webworker', 'es2022'],
|
|
19
|
-
plugins: [
|
|
43
|
+
plugins: [
|
|
44
|
+
new webpack.ProgressPlugin(),
|
|
45
|
+
new webpack.optimize.LimitChunkCountPlugin({
|
|
46
|
+
maxChunks: 1,
|
|
47
|
+
}),
|
|
48
|
+
],
|
|
20
49
|
};
|
|
@@ -23,42 +23,6 @@ import {
|
|
|
23
23
|
|
|
24
24
|
const isWindows = process.platform === 'win32';
|
|
25
25
|
|
|
26
|
-
/**
|
|
27
|
-
* Check if the project has a package.json file and if it has dependencies or
|
|
28
|
-
* devDependencies, it then checks if the node_modules folder exists and exits the process if it
|
|
29
|
-
* doesn't.
|
|
30
|
-
* @returns {Promise<void>} The function does not have an explicit return statement. Therefore, it does not return any
|
|
31
|
-
* value.
|
|
32
|
-
*/
|
|
33
|
-
async function checkNodeModules() {
|
|
34
|
-
let projectJson;
|
|
35
|
-
try {
|
|
36
|
-
projectJson = getProjectJsonFile('package.json');
|
|
37
|
-
} catch (error) {
|
|
38
|
-
if (error.code === 'ENOENT') {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
feedback.prebuild.error(error);
|
|
43
|
-
process.exit(1);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
projectJson &&
|
|
48
|
-
(projectJson.dependencies || projectJson.devDependencies)
|
|
49
|
-
) {
|
|
50
|
-
const pkgManager = await getPackageManager();
|
|
51
|
-
const existNodeModules = await folderExistsInProject('node_modules');
|
|
52
|
-
|
|
53
|
-
if (!existNodeModules) {
|
|
54
|
-
feedback.prebuild.error(
|
|
55
|
-
Messages.build.error.install_dependencies_failed(pkgManager),
|
|
56
|
-
);
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
26
|
/**
|
|
63
27
|
* Class representing a Dispatcher for build operations.
|
|
64
28
|
* @example
|
|
@@ -90,10 +54,10 @@ class Dispatcher {
|
|
|
90
54
|
this.entry = config.entry;
|
|
91
55
|
this.builder = config.builder;
|
|
92
56
|
this.preset = config.preset;
|
|
93
|
-
this.useNodePolyfills =
|
|
94
|
-
config.useNodePolyfills
|
|
95
|
-
|
|
96
|
-
|
|
57
|
+
this.useNodePolyfills = Dispatcher.checkBooleanValue(
|
|
58
|
+
config.useNodePolyfills,
|
|
59
|
+
);
|
|
60
|
+
this.useOwnWorker = Dispatcher.checkBooleanValue(config.useOwnWorker);
|
|
97
61
|
this.memoryFS = config.memoryFS;
|
|
98
62
|
this.custom = config.custom;
|
|
99
63
|
/* generate */
|
|
@@ -101,6 +65,42 @@ class Dispatcher {
|
|
|
101
65
|
this.vulcanLibPath = vulcanLibPath;
|
|
102
66
|
}
|
|
103
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Check if the project has a package.json file and if it has dependencies or
|
|
70
|
+
* devDependencies, it then checks if the node_modules folder exists and exits the process if it
|
|
71
|
+
* doesn't.
|
|
72
|
+
* @returns {Promise<void>} The function does not have an explicit return statement. Therefore, it does not return any
|
|
73
|
+
* value.
|
|
74
|
+
*/
|
|
75
|
+
static async checkNodeModules() {
|
|
76
|
+
let projectJson;
|
|
77
|
+
try {
|
|
78
|
+
projectJson = getProjectJsonFile('package.json');
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error.code === 'ENOENT') {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
feedback.prebuild.error(error);
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (
|
|
89
|
+
projectJson &&
|
|
90
|
+
(projectJson.dependencies || projectJson.devDependencies)
|
|
91
|
+
) {
|
|
92
|
+
const pkgManager = await getPackageManager();
|
|
93
|
+
const existNodeModules = await folderExistsInProject('node_modules');
|
|
94
|
+
|
|
95
|
+
if (!existNodeModules) {
|
|
96
|
+
feedback.prebuild.error(
|
|
97
|
+
Messages.build.error.install_dependencies_failed(pkgManager),
|
|
98
|
+
);
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
104
|
/**
|
|
105
105
|
* Get a build context based on arguments
|
|
106
106
|
* @returns {object} - Preset files
|
|
@@ -236,107 +236,192 @@ class Dispatcher {
|
|
|
236
236
|
}
|
|
237
237
|
|
|
238
238
|
/**
|
|
239
|
-
*
|
|
239
|
+
* Checks whether the input value is a boolean `true` or a string 'true'.
|
|
240
|
+
* @param {boolean|string} value - The value to be checked.
|
|
241
|
+
* @returns {boolean} - Returns `true` if the value is a boolean `true` or a string 'true', otherwise `false`.
|
|
240
242
|
*/
|
|
241
|
-
|
|
242
|
-
|
|
243
|
+
static checkBooleanValue(value) {
|
|
244
|
+
return value === true || value === 'true';
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Configures the build based on the provided configuration.
|
|
249
|
+
* @param {object} currentConfig - The configuration object.
|
|
250
|
+
* @param {string} currentConfig.build - The current build id.
|
|
251
|
+
* @param {boolean} currentConfig.useNodePolyfills - The current useNodePolyfills.
|
|
252
|
+
* @param {boolean} currentConfig.useOwnWorker - The current useOwnWorker.
|
|
253
|
+
* @param {object} currentConfig.preset - The current preset.
|
|
254
|
+
* @param {object} currentConfig.localCustom - The current localCustom.
|
|
255
|
+
* @param {object} currentConfig.memoryFS - The current memoryFS.
|
|
256
|
+
* @param {object} config - The configuration object.
|
|
257
|
+
* @typedef {object} BuildConfig
|
|
258
|
+
* @property {string} buildId - Identifier for the build.
|
|
259
|
+
* @property {boolean} useNodePolyfills - Indicates whether to use Node polyfills.
|
|
260
|
+
* @property {boolean} useOwnWorker - Indicates whether to use a custom worker.
|
|
261
|
+
* @property {*} localCustom - Custom configuration specific to the local environment.
|
|
262
|
+
* @property {*} preset - Preset configuration for the build.
|
|
263
|
+
* @property {string} entry - Path to the temporary entry file for the build.
|
|
264
|
+
* @returns {BuildConfig} - Returns the configured build object.
|
|
265
|
+
*/
|
|
266
|
+
static configureBuild(currentConfig, config) {
|
|
267
|
+
const {
|
|
268
|
+
buildId,
|
|
269
|
+
useNodePolyfills,
|
|
270
|
+
useOwnWorker,
|
|
271
|
+
preset,
|
|
272
|
+
localCustom,
|
|
273
|
+
memoryFS,
|
|
274
|
+
} = currentConfig;
|
|
275
|
+
const buildConfig = { ...config };
|
|
276
|
+
|
|
277
|
+
buildConfig.buildId = buildId;
|
|
278
|
+
// include config preset to priority
|
|
279
|
+
buildConfig.useNodePolyfills =
|
|
280
|
+
Dispatcher.checkBooleanValue(useNodePolyfills) ||
|
|
281
|
+
Dispatcher.checkBooleanValue(buildConfig?.useNodePolyfills);
|
|
282
|
+
// include config preset to priority
|
|
283
|
+
buildConfig.useOwnWorker =
|
|
284
|
+
Dispatcher.checkBooleanValue(useOwnWorker) ||
|
|
285
|
+
Dispatcher.checkBooleanValue(buildConfig?.useOwnWorker);
|
|
286
|
+
buildConfig.localCustom = localCustom;
|
|
287
|
+
buildConfig.preset = preset;
|
|
288
|
+
buildConfig.memoryFS = memoryFS || buildConfig?.memoryFS;
|
|
289
|
+
|
|
290
|
+
const currentDir = process.cwd();
|
|
291
|
+
let tempEntryFile = `vulcan-${buildId}.temp.`;
|
|
292
|
+
tempEntryFile += preset.name === 'typescript' ? 'ts' : 'js';
|
|
293
|
+
const tempBuilderEntryPath = join(currentDir, tempEntryFile);
|
|
294
|
+
|
|
295
|
+
buildConfig.entry = tempBuilderEntryPath;
|
|
243
296
|
|
|
244
|
-
|
|
245
|
-
|
|
297
|
+
return buildConfig;
|
|
298
|
+
}
|
|
246
299
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
300
|
+
/**
|
|
301
|
+
* Runs prebuild tasks using the provided context and build configuration.
|
|
302
|
+
* @param {object} context - The context for prebuilding.
|
|
303
|
+
* @param {string} context.builId - The build id.
|
|
304
|
+
* @param {string} context.handler - The handler preset file.
|
|
305
|
+
* @param {string} context.prebuild - The prebuild preset file.
|
|
306
|
+
* @param {BuildConfig} buildConfig - The build configuration object.
|
|
307
|
+
* @returns {Promise<object>} - A promise that resolves to an object containing the prebuild context.
|
|
308
|
+
*/
|
|
309
|
+
static async runPrebuild(context, buildConfig) {
|
|
310
|
+
const { buildId, handler, prebuild } = context;
|
|
311
|
+
const prebuildContext = {
|
|
312
|
+
buildId,
|
|
313
|
+
entry: buildConfig.entry,
|
|
314
|
+
useNodePolyfills: buildConfig.useNodePolyfills,
|
|
315
|
+
useOwnWorker: buildConfig.useOwnWorker,
|
|
316
|
+
memoryFS: buildConfig.memoryFS,
|
|
253
317
|
preset: {
|
|
254
|
-
name:
|
|
255
|
-
mode:
|
|
318
|
+
name: buildConfig.preset.name,
|
|
319
|
+
mode: buildConfig.preset.mode,
|
|
256
320
|
files: {
|
|
257
321
|
handler,
|
|
258
|
-
config,
|
|
322
|
+
config: buildConfig,
|
|
259
323
|
prebuild,
|
|
260
324
|
},
|
|
261
325
|
},
|
|
262
326
|
};
|
|
327
|
+
feedback.prebuild.info(Messages.build.info.prebuild_starting);
|
|
328
|
+
await prebuild(prebuildContext);
|
|
329
|
+
createDotEnvFile(buildId, isWindows);
|
|
330
|
+
feedback.prebuild.success(Messages.build.success.prebuild_succeeded);
|
|
331
|
+
|
|
332
|
+
feedback.build.info(Messages.build.info.vulcan_build_starting);
|
|
333
|
+
|
|
334
|
+
const { injectionDirs, removePathPrefix } = buildConfig.memoryFS;
|
|
335
|
+
if (injectionDirs && injectionDirs.length > 0) {
|
|
336
|
+
const content = await injectFilesInMem(injectionDirs);
|
|
337
|
+
|
|
338
|
+
const prefix =
|
|
339
|
+
removePathPrefix &&
|
|
340
|
+
typeof removePathPrefix === 'string' &&
|
|
341
|
+
removePathPrefix !== ''
|
|
342
|
+
? `'${removePathPrefix}'`
|
|
343
|
+
: `''`;
|
|
344
|
+
|
|
345
|
+
// eslint-disable-next-line
|
|
346
|
+
buildConfig.contentToInject = `globalThis.vulcan = {}; globalThis.vulcan.FS_PATH_PREFIX_TO_REMOVE = ${prefix};\n${content}`;
|
|
347
|
+
}
|
|
348
|
+
return Promise.resolve({ prebuildContext });
|
|
349
|
+
}
|
|
263
350
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
351
|
+
/**
|
|
352
|
+
* Executes the build process based on the provided build configuration.
|
|
353
|
+
* @param {string} originalEntry - The original entry e.g ./index.js.
|
|
354
|
+
* @param {string} builderCurrent - The builder current e.g esbuild.
|
|
355
|
+
* @param {BuildConfig} buildConfig - The build configuration object.
|
|
356
|
+
* @returns {Promise<void>} - A promise that resolves when the build process is completed.
|
|
357
|
+
*/
|
|
358
|
+
static async executeBuild(originalEntry, builderCurrent, buildConfig) {
|
|
359
|
+
const builderSelected = builderCurrent || buildConfig.builder;
|
|
360
|
+
let builder;
|
|
361
|
+
switch (builderSelected) {
|
|
362
|
+
case 'webpack':
|
|
363
|
+
builder = new Webpack(buildConfig);
|
|
364
|
+
break;
|
|
365
|
+
case 'esbuild':
|
|
366
|
+
builder = new Esbuild(buildConfig);
|
|
367
|
+
break;
|
|
368
|
+
default:
|
|
369
|
+
builder = new Webpack(buildConfig);
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
268
372
|
|
|
269
|
-
|
|
373
|
+
// Run common build
|
|
374
|
+
await builder.run();
|
|
270
375
|
|
|
271
|
-
//
|
|
272
|
-
|
|
273
|
-
feedback.prebuild.info(Messages.build.info.prebuild_starting);
|
|
274
|
-
await prebuild(context);
|
|
275
|
-
createDotEnvFile(this.buildId, isWindows);
|
|
276
|
-
feedback.prebuild.success(Messages.build.success.prebuild_succeeded);
|
|
277
|
-
|
|
278
|
-
feedback.build.info(Messages.build.info.vulcan_build_starting);
|
|
279
|
-
|
|
280
|
-
// builder entry
|
|
281
|
-
config.entry = tempBuilderEntryPath;
|
|
282
|
-
config.buildId = this.buildId;
|
|
283
|
-
config.useNodePolyfills = this.useNodePolyfills;
|
|
284
|
-
config.useOwnWorker = this.useOwnWorker;
|
|
285
|
-
config.localCustom = this.custom;
|
|
286
|
-
|
|
287
|
-
const builderSelected = this.builder || config.builder;
|
|
288
|
-
|
|
289
|
-
const { injectionDirs, removePathPrefix } = this.memoryFS;
|
|
290
|
-
if (injectionDirs && injectionDirs.length > 0) {
|
|
291
|
-
const content = await injectFilesInMem(injectionDirs);
|
|
292
|
-
|
|
293
|
-
const prefix =
|
|
294
|
-
removePathPrefix &&
|
|
295
|
-
typeof removePathPrefix === 'string' &&
|
|
296
|
-
removePathPrefix !== ''
|
|
297
|
-
? `'${removePathPrefix}'`
|
|
298
|
-
: `''`;
|
|
299
|
-
|
|
300
|
-
config.contentToInject = `globalThis.vulcan = {}; globalThis.vulcan.FS_PATH_PREFIX_TO_REMOVE = ${prefix};\n${content}`;
|
|
301
|
-
}
|
|
376
|
+
// delete .temp files
|
|
377
|
+
rmSync(buildConfig.entry);
|
|
302
378
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
}
|
|
379
|
+
await vulcan.createVulcanEnv(
|
|
380
|
+
{
|
|
381
|
+
entry: originalEntry, // original entry
|
|
382
|
+
preset: buildConfig.preset.name,
|
|
383
|
+
mode: buildConfig.preset.mode,
|
|
384
|
+
useNodePolyfills: buildConfig.useNodePolyfills,
|
|
385
|
+
useOwnWorker: buildConfig.useOwnWorker,
|
|
386
|
+
},
|
|
387
|
+
'local',
|
|
388
|
+
);
|
|
389
|
+
}
|
|
315
390
|
|
|
316
|
-
|
|
317
|
-
|
|
391
|
+
/**
|
|
392
|
+
* Run the build process.
|
|
393
|
+
*/
|
|
394
|
+
async run() {
|
|
395
|
+
let buildEntryTemp;
|
|
396
|
+
try {
|
|
397
|
+
await Dispatcher.checkNodeModules();
|
|
318
398
|
|
|
319
|
-
//
|
|
320
|
-
|
|
399
|
+
// Load files from preset
|
|
400
|
+
const { handler, prebuild, config } = await this.loadPreset();
|
|
321
401
|
|
|
322
|
-
|
|
402
|
+
const buildConfig = Dispatcher.configureBuild(
|
|
323
403
|
{
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
...(this.useOwnWorker && {
|
|
331
|
-
useOwnWorker: this.useOwnWorker,
|
|
332
|
-
}),
|
|
404
|
+
buildId: this.buildId,
|
|
405
|
+
localCustom: this.custom,
|
|
406
|
+
preset: this.preset,
|
|
407
|
+
useNodePolyfills: this.useNodePolyfills,
|
|
408
|
+
useOwnWorker: this.useOwnWorker,
|
|
409
|
+
memoryFS: this.memoryFS,
|
|
333
410
|
},
|
|
334
|
-
|
|
411
|
+
config,
|
|
335
412
|
);
|
|
413
|
+
buildEntryTemp = buildConfig.entry;
|
|
414
|
+
// temp entry
|
|
415
|
+
writeFileSync(buildConfig?.entry, handler);
|
|
336
416
|
|
|
417
|
+
await Dispatcher.runPrebuild(
|
|
418
|
+
{ buildId: this.buildId, handler, prebuild },
|
|
419
|
+
buildConfig,
|
|
420
|
+
);
|
|
421
|
+
await Dispatcher.executeBuild(this.entry, this.builder, buildConfig);
|
|
337
422
|
feedback.build.success(Messages.build.success.vulcan_build_succeeded);
|
|
338
423
|
} catch (error) {
|
|
339
|
-
rmSync(
|
|
424
|
+
rmSync(buildEntryTemp);
|
|
340
425
|
feedback.build.error(error);
|
|
341
426
|
process.exit(1);
|
|
342
427
|
}
|
|
@@ -23,6 +23,10 @@ const build = {
|
|
|
23
23
|
install_dependencies_failed: (pckManager) =>
|
|
24
24
|
`Please run command to install dependencies. e.g ${pckManager} install.`,
|
|
25
25
|
},
|
|
26
|
+
warning: {
|
|
27
|
+
use_node_polyfill_mode_deliver:
|
|
28
|
+
'The use of useNodePolyfills is not available for this deliver mode preset.',
|
|
29
|
+
},
|
|
26
30
|
};
|
|
27
31
|
|
|
28
32
|
export default build;
|
|
@@ -1,96 +1,11 @@
|
|
|
1
|
-
// import path from 'path';
|
|
2
|
-
import webpack from 'webpack';
|
|
3
|
-
|
|
4
|
-
import { createRequire } from 'module';
|
|
5
|
-
|
|
6
|
-
import { getAbsoluteLibDirPath, generateWebpackBanner } from '#utils';
|
|
7
|
-
|
|
8
|
-
const require = createRequire(import.meta.url);
|
|
9
|
-
const libDirPath = getAbsoluteLibDirPath();
|
|
10
|
-
const polyfillsPath = `${libDirPath}/build/polyfills`;
|
|
11
|
-
const nodePolyfillsPath = `${polyfillsPath}/node`;
|
|
12
|
-
const nextNodePresetPath = `${libDirPath}/presets/custom/next/compute/node`;
|
|
13
|
-
|
|
14
1
|
/**
|
|
15
2
|
* Config to be used in build context.
|
|
16
3
|
*/
|
|
17
4
|
const config = {
|
|
18
5
|
builder: 'webpack',
|
|
19
|
-
useNodePolyfills:
|
|
6
|
+
useNodePolyfills: true,
|
|
20
7
|
custom: {
|
|
21
|
-
|
|
22
|
-
topLevelAwait: true,
|
|
23
|
-
},
|
|
24
|
-
optimization: {
|
|
25
|
-
minimize: true,
|
|
26
|
-
},
|
|
27
|
-
module: {
|
|
28
|
-
// Asset modules are modules that allow the use asset files (fonts, icons, etc)
|
|
29
|
-
// without additional configuration or dependencies.
|
|
30
|
-
rules: [
|
|
31
|
-
// asset/source exports the source code of the asset.
|
|
32
|
-
// Usage: e.g., import notFoundPage from "./page_404.html"
|
|
33
|
-
{
|
|
34
|
-
test: /\.(txt|html)/,
|
|
35
|
-
type: 'asset/source',
|
|
36
|
-
},
|
|
37
|
-
],
|
|
38
|
-
},
|
|
39
|
-
plugins: [
|
|
40
|
-
// Polyfills go here.
|
|
41
|
-
// Used for, e.g., any cross-platform WHATWG,
|
|
42
|
-
// or core nodejs modules needed for your application.
|
|
43
|
-
new webpack.ProvidePlugin({
|
|
44
|
-
Buffer: ['buffer', 'Buffer'],
|
|
45
|
-
}),
|
|
46
|
-
new webpack.BannerPlugin({
|
|
47
|
-
banner: generateWebpackBanner([
|
|
48
|
-
`${nodePolyfillsPath}/globals/navigator.js`,
|
|
49
|
-
`${nodePolyfillsPath}/globals/performance.js`,
|
|
50
|
-
`${nodePolyfillsPath}/globals/process.js`,
|
|
51
|
-
]),
|
|
52
|
-
raw: true,
|
|
53
|
-
}),
|
|
54
|
-
new webpack.EnvironmentPlugin({
|
|
55
|
-
NEXT_RUNTIME: 'edge',
|
|
56
|
-
NEXT_COMPUTE_JS: true,
|
|
57
|
-
}),
|
|
58
|
-
new webpack.optimize.LimitChunkCountPlugin({
|
|
59
|
-
maxChunks: 1,
|
|
60
|
-
}),
|
|
61
|
-
],
|
|
62
|
-
resolve: {
|
|
63
|
-
mainFields: ['browser', 'main', 'module'],
|
|
64
|
-
alias: {
|
|
65
|
-
util: require.resolve('util/'),
|
|
66
|
-
},
|
|
67
|
-
fallback: {
|
|
68
|
-
async_hooks: false,
|
|
69
|
-
tls: false,
|
|
70
|
-
net: false,
|
|
71
|
-
child_process: false,
|
|
72
|
-
https: require.resolve('https-browserify'),
|
|
73
|
-
tty: require.resolve('tty-browserify'),
|
|
74
|
-
http: require.resolve('stream-http'),
|
|
75
|
-
buffer: require.resolve('buffer/'),
|
|
76
|
-
crypto: require.resolve('crypto-browserify/'),
|
|
77
|
-
events: require.resolve('events/'),
|
|
78
|
-
os: require.resolve('os-browserify/browser'),
|
|
79
|
-
path: require.resolve('path-browserify'),
|
|
80
|
-
querystring: require.resolve('querystring-es3'),
|
|
81
|
-
stream: require.resolve('stream-browserify'),
|
|
82
|
-
url: require.resolve('url/'),
|
|
83
|
-
zlib: require.resolve('browserify-zlib'),
|
|
84
|
-
dns: `${nodePolyfillsPath}/dns.js`,
|
|
85
|
-
http2: `${nodePolyfillsPath}/http2.js`,
|
|
86
|
-
module: `${nodePolyfillsPath}/module.js`,
|
|
87
|
-
fs: `${nodePolyfillsPath}/fs.js`,
|
|
88
|
-
'next/dist/compiled/etag': `${nextNodePresetPath}/custom-server/12.3.1/util/etag.js`,
|
|
89
|
-
'@fastly/http-compute-js': require.resolve('@fastly/http-compute-js'),
|
|
90
|
-
accepts: require.resolve('accepts'),
|
|
91
|
-
string_decoder: require.resolve('string_decoder/'),
|
|
92
|
-
},
|
|
93
|
-
},
|
|
8
|
+
plugins: [],
|
|
94
9
|
},
|
|
95
10
|
};
|
|
96
11
|
|
|
@@ -150,6 +150,9 @@ export function readAssetManifest(assets, path, dir) {
|
|
|
150
150
|
export function readAssetModule(assets, path, dir) {
|
|
151
151
|
const relativePath = relative(dir, path);
|
|
152
152
|
const file = assets[`/${relativePath}`];
|
|
153
|
+
if (file.module instanceof Promise) {
|
|
154
|
+
return Promise.resolve(file.module?.default || file.module);
|
|
155
|
+
}
|
|
153
156
|
return file.module;
|
|
154
157
|
}
|
|
155
158
|
|