8000 feat: upgrade unenv 1.x to 2.x by jcbsfilho · Pull Request #159 · aziontech/lib · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: upgrade unenv 1.x to 2.x #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
361 changes: 312 additions & 49 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
"ajv": "^8.17.1",
"ajv-errors": "^3.0.0",
"ajv-keywords": "^5.1.0",
"assert-browserify": "^2.0.0",
"babel-loader": "^9.2.1",
"browserify-zlib": "^0.2.0",
"chalk": "^5.3.0",
Expand All @@ -249,11 +250,10 @@
"pcre-to-regexp": "^1.1.0",
"progress": "^2.0.3",
"signale": "^1.4.0",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"string_decoder": "^1.3.0",
"timers-browserify": "^2.0.12",
"unenv": "^1.10.0",
"unenv": "^2.0.0-rc.15",
"url": "^0.11.4",
"util": "^0.12.5",
"vm-browserify": "^1.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"string_decoder": "^1.3.0",
"timers-browserify": "^2.0.12",
"ts-loader": "^9.5.2",
"unenv": "^1.10.0",
"unenv": "^2.0.0-rc.15",
"url": "^0.11.4",
"util": "^0.12.5",
"vm-browserify": "^1.1.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/bundler/src/bundlers/esbuild/esbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default {
platform: 'browser',
mainFields: ['browser', 'module', 'main'],
target: 'es2022',
keepNames: true,
allowOverwrite: true,
loader: {
'.js': 'js',
},
Expand Down
13 changes: 10 additions & 3 deletions packages/bundler/src/bundlers/esbuild/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ const bundlerPlugins = createBundlerPlugins<ESBuildPluginClasses, ESBuildConfigu
* ESBuild-specific content injection
*/
const applyContentInjection =
(config: ESBuildConfig) =>
(config: ESBuildConfig, ctx: BuildContext) =>
(content: string | undefined): ESBuildConfig => {
if (!content) return config;

config.banner = config.banner || {};

config.banner.js = config.banner.js ? `${config.banner.js} ${content}` : content;
if (ctx.production) {
config.banner.js += `
// This file is generated by Azion. Do not edit it manually.
// Dynamic require is not supported in production mode.
import { createRequire as _createRequire } from "node:module";
const require = _createRequire(import.meta.url);`;
}

return config;
};
Expand Down Expand Up @@ -66,7 +73,7 @@ export const createAzionESBuildConfig = (buildConfig: BuildConfiguration, ctx: B
flow([
() => bundlerPlugins.applyPolyfills(ctx)(config)(buildConfig),
() => bundlerPlugins.applyAzionModule(ctx)(config),
() => applyContentInjection(config)(buildConfig?.setup?.contentToInject),
() => applyContentInjection(config, ctx)(buildConfig?.setup?.contentToInject),
() => applyDefineVars<ESBuildConfiguration>(config, 'esbuild')(buildConfig?.setup?.defineVars),
() => extendConfig(config)(buildConfig.extend as (config: ESBuildConfiguration) => ESBuildConfiguration),
])(config),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import fs from 'fs';
import { createRequire } from 'module';
import { builtinModules } from 'node:module';
import path from 'path';
import { env, nodeless } from 'unenv';
import { defineEnv } from 'unenv';
import helper from './helper/index';

const requireCustom = createRequire(import.meta.url);

const { alias, inject, polyfill, external } = env(nodeless, unenvPresetAzion);
const { env } = defineEnv({
nodeCompat: true,
resolve: false,
overrides: {
...unenvPresetAzion,
},
});

const { alias, inject, polyfill, external } = env;

interface BuildOptions {
define?: Record<string, string>;
Expand All @@ -21,14 +29,12 @@ interface GlobalInjectResult {
exportName: string;
}

type GlobalInjectValue = string | string[] | [string, string];

/**
* Get global inject
* @param {*} globalInject Global inject
* @returns {*} Return import statement and export name
*/
function getGlobalInject(globalInject: GlobalInjectValue): GlobalInjectResult {
function getGlobalInject(globalInject: string | string[]): GlobalInjectResult {
if (typeof globalInject === 'string') {
return {
importStatement: `import globalVar from "${globalInject}";`,
Expand Down Expand Up @@ -128,7 +134,7 @@ function nodeBuiltInModules(
// if polyfill is not found, check if the module is external
if (!polyfillResult && externalModule) {
return {
path: args.path,
path: args.path.startsWith('node:') ? args.path : `node:${args.path}`,
external: externalModule.includes(args.path),
};
}
Expand Down Expand Up @@ -186,7 +192,8 @@ function handleNodeJSGlobals(build: PluginBuild, getAbsolutePath: (moving: strin
if (!match?.[1]) throw new Error(`Invalid global name: ${args.path}`);

const globalName = match[1];
const { importStatement, exportName } = getGlobalInject(inject[globalName]);

const { importStatement, exportName } = getGlobalInject(inject[globalName] as string | string[]);

return {
contents: `
Expand Down Expand Up @@ -259,7 +266,7 @@ function defineNextJsRuntime(options: BuildOptions) {
// eslint-disable-next-line no-param-reassign
options.define = {
...options.define,
'process.env.NEXT_RUNTIME': '"edge"',
'process.env.NEXT_RUNTIME': options.define?.['process.env.NEXT_RUNTIME'] || '"edge"',
'process.env.NEXT_COMPUTE_JS': 'true',
'process.env.__NEXT_BUILD_ID': `"${buildId}"`,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { getAbsoluteDirPath } from 'azion/utils/node';
import fs from 'fs';
import { createRequire } from 'module';
import path from 'path';
import { env, nodeless } from 'unenv';
import { defineEnv } from 'unenv';
import { Compiler, WebpackPluginInstance } from 'webpack';

const require = createRequire(import.meta.url);

const { alias, inject, polyfill, external } = env(nodeless, unenvPresetAzion);
const { env } = defineEnv({
nodeCompat: true,
resolve: false,
overrides: {
...unenvPresetAzion,
},
});

const { alias, inject, polyfill, external } = env;

class NodePolyfillPlugin implements WebpackPluginInstance {
private INTERNAL_POLYFILL_PATH = '/polyfills';
Expand Down
68 changes: 68 additions & 0 deletions packages/bundler/src/polyfills/crypto/context/crypto.context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable */
import * as crypto from 'node:crypto';

export var { Cipher } = crypto;
export var { Decipher } = crypto;
export var { DiffieHellman } = crypto;
export var { DiffieHellmanGroup } = crypto;
export var { Hash } = crypto;
export var { Hmac } = crypto;
export var { Sign } = crypto;
export var { Verify } = crypto;
export var { constants } = crypto;
export var { createCipheriv } = crypto;
export var { createDecipheriv } = crypto;
export var { createDiffieHellman } = crypto;
export var { createDiffieHellmanGroup } = crypto;
export var { createECDH } = crypto;
export var { createHash } = crypto;
export var { createHmac } = crypto;
export var { createSign } = crypto;
export var { createVerify } = crypto;
export var { getCiphers } = crypto;
export var { getDiffieHellman } = crypto;
export var { getHashes } = crypto;
export var { pbkdf2 } = crypto;
export var { pbkdf2Sync } = crypto;
export var { privateDecrypt } = crypto;
export var { privateEncrypt } = crypto;
export var { pseudoRandomBytes } = crypto;
export var { publicDecrypt } = crypto;
export var { publicEncrypt } = crypto;
export var { randomBytes } = crypto;
export var { randomFill } = crypto;
export var { randomFillSync } = crypto;

export default {
Cipher,
Decipher,
DiffieHellman,
DiffieHellmanGroup,
Hash,
Hmac,
Sign,
Verify,
constants,
createCipheriv,
createDecipheriv,
createDiffieHellman,
createDiffieHellmanGroup,
createECDH,
createHash,
createHmac,
createSign,
createVerify,
getCiphers,
getDiffieHellman,
getHashes,
pbkdf2,
pbkdf2Sync,
privateDecrypt,
privateEncrypt,
pseudoRandomBytes,
publicDecrypt,
publicEncrypt,
randomBytes,
randomFill,
randomFillSync,
};
3 changes: 3 additions & 0 deletions packages/bundler/src/polyfills/crypto/context/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import cryptoContext from './crypto.context.js';

export default cryptoContext;
73 changes: 73 additions & 0 deletions packages/bundler/src/polyfills/crypto/crypto.polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable */
/** This polyfill is referenced in #build/bundlers/polyfills/polyfills-manager.js
*
* CRYPTO_CONTEXT is defined in runtime.env.js for use on the local server
*/

/* eslint-disable */

export var { Cipher } = CRYPTO_CONTEXT.cryptoContext;
export var { Decipher } = CRYPTO_CONTEXT.cryptoContext;
export var { DiffieHellman } = CRYPTO_CONTEXT.cryptoContext;
export var { DiffieHellmanGroup } = CRYPTO_CONTEXT.cryptoContext;
export var { Hash } = CRYPTO_CONTEXT.cryptoContext;
export var { Hmac } = CRYPTO_CONTEXT.cryptoContext;
export var { Sign } = CRYPTO_CONTEXT.cryptoContext;
export var { Verify } = CRYPTO_CONTEXT.cryptoContext;
export var { constants } = CRYPTO_CONTEXT.cryptoContext;
export var { createCipheriv } = CRYPTO_CONTEXT.cryptoContext;
export var { createDecipheriv } = CRYPTO_CONTEXT.cryptoContext;
export var { createDiffieHellman } = CRYPTO_CONTEXT.cryptoContext;
export var { createDiffieHellmanGroup } = CRYPTO_CONTEXT.cryptoContext;
export var { createECDH } = CRYPTO_CONTEXT.cryptoContext;
export var { createHash } = CRYPTO_CONTEXT.cryptoContext;
export var { createHmac } = CRYPTO_CONTEXT.cryptoContext;
export var { createSign } = CRYPTO_CONTEXT.cryptoContext;
export var { createVerify } = CRYPTO_CONTEXT.cryptoContext;
export var { getCiphers } = CRYPTO_CONTEXT.cryptoContext;
export var { getDiffieHellman } = CRYPTO_CONTEXT.cryptoContext;
export var { getHashes } = CRYPTO_CONTEXT.cryptoContext;
export var { pbkdf2 } = CRYPTO_CONTEXT.cryptoContext;
export var { pbkdf2Sync } = CRYPTO_CONTEXT.cryptoContext;
export var { privateDecrypt } = CRYPTO_CONTEXT.cryptoContext;
export var { privateEncrypt } = CRYPTO_CONTEXT.cryptoContext;
export var { pseudoRandomBytes } = CRYPTO_CONTEXT.cryptoContext;
export var { publicDecrypt } = CRYPTO_CONTEXT.cryptoContext;
export var { publicEncrypt } = CRYPTO_CONTEXT.cryptoContext;
export var { randomBytes } = CRYPTO_CONTEXT.cryptoContext;
export var { randomFill } = CRYPTO_CONTEXT.cryptoContext;
export var { randomFillSync } = CRYPTO_CONTEXT.cryptoContext;

export default {
Cipher,
Decipher,
DiffieHellman,
DiffieHellmanGroup,
Hash,
Hmac,
Sign,
Verify,
constants,
createCipheriv,
createDecipheriv,
createDiffieHellman,
createDiffieHellmanGroup,
createECDH,
createHash,
createHmac,
createSign,
createVerify,
getCiphers,
getDiffieHellman,
getHashes,
pbkdf2,
pbkdf2Sync,
privateDecrypt,
privateEncrypt,
pseudoRandomBytes,
publicDecrypt,
publicEncrypt,
randomBytes,
randomFill,
randomFillSync,
};
7 changes: 7 additions & 0 deletions packages/bundler/src/polyfills/crypto/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* We are not exporting the crypto.polyfill.js from this structure due to the context definition in runtime.env.js.
* As we are proxying the Node.js crypto lib, it is not possible to export the crypto.polyfill.js file.
*/
import cryptoContext from './context/index.js';

export default { cryptoContext };
2 changes: 2 additions & 0 deletions packages/bundler/src/polyfills/fs/context/fs.context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const {
openSync,
close,
closeSync,
existsSync,
stat,
statSync,
lstat,
Expand Down Expand Up @@ -55,6 +56,7 @@ localFs.open = open;
localFs.openSync = openSync;
localFs.close = close;
localFs.closeSync = closeSync;
localFs.existsSync = existsSync;
localFs.stat = stat;
localFs.statSync = statSync;
localFs.lstat = lstat;
Expand Down
Loading
0