Skip to content

Commit

Permalink
feat: adding the unenv package and removing the polyfill manager (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbsfilho authored Feb 4, 2025
1 parent 84f7d62 commit 529c267
Show file tree
Hide file tree
Showing 31 changed files with 1,199 additions and 2,469 deletions.
4 changes: 3 additions & 1 deletion lib/build/bundlers/esbuild/esbuild.bundlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ describe('Esbuild Bundler', () => {

const result = fs.readFileSync(tmpOutput.name, 'utf-8');

expect(result).toContain('vulcan-node-modules-polyfills:crypto');
expect(result).toContain(
'node-built-in-modules:unenv/runtime/node/crypto/index',
);
},
TIMEOUT,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/build/bundlers/esbuild/esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default {
target: 'es2022',
format: 'esm',
platform: 'browser',
mainFields: ['browser', 'main', 'module'],
mainFields: ['browser', 'module', 'main'],
loader: {
'.js': 'jsx',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable consistent-return */
import fs from 'fs';
import path from 'path';
import PolyfillsManager from '../../../polyfills/index.js';
import azionLibs from '../../../helpers/azion-libs.js';

/**
* ESBuild Azion Module Plugin for polyfilling node modules.
* @param {boolean} buildProd Parameter to identify whether the build is dev or prod
* @returns {object} - ESBuild plugin object.
*/
const ESBuildAzionModulePlugin = (buildProd) => {
const NAME = 'vulcan-azion-modules-polyfills';
const NAME = 'bundler-azion-modules-polyfills';
const NAMESPACE = NAME;
const filter = /^azion:/;
const prefix = 'azion:';
Expand All @@ -21,14 +21,12 @@ const ESBuildAzionModulePlugin = (buildProd) => {
*/
name: NAME,
setup: (build) => {
const polyfillManager = PolyfillsManager.buildPolyfills();

const options = build.initialOptions;

// external
if (buildProd) {
options.external = options.external || [];
[...polyfillManager.external].forEach(([key]) => {
[...azionLibs.external].forEach(([key]) => {
if (/^[^:]+:/.test(key) && !options.external.includes(key)) {
options.external.push(key);
}
Expand All @@ -38,8 +36,8 @@ const ESBuildAzionModulePlugin = (buildProd) => {
if (!buildProd) {
// build inject prefix (azion:) is not present and the polyfill is Azion
options.inject = options.inject || [];
if (polyfillManager.external) {
[...polyfillManager.external].forEach(([key, value]) => {
if (azionLibs.external) {
[...azionLibs.external].forEach(([key, value]) => {
const hasPrefix = /^[^:]+:/.test(key);
if (
!hasPrefix &&
Expand All @@ -57,13 +55,13 @@ const ESBuildAzionModulePlugin = (buildProd) => {
* @returns {object|undefined} - Object with path and namespace or undefined.
*/
build.onResolve({ filter }, async (args) => {
if (!buildProd && polyfillManager.external.has(args.path)) {
if (!buildProd && azionLibs.external.has(args.path)) {
return {
path: args.path,
namespace: NAMESPACE,
};
}
if (!polyfillManager.external.has(args.path)) {
if (!azionLibs.external.has(args.path)) {
return;
}

Expand All @@ -87,10 +85,10 @@ const ESBuildAzionModulePlugin = (buildProd) => {
* @returns {object} - Object with loader, contents, and resolve directory.
*/
build.onLoad({ filter, namespace: NAMESPACE }, async (args) => {
if (!polyfillManager.external.has(args.path)) {
if (!azionLibs.external.has(args.path)) {
return;
}
const resolved = polyfillManager.external.get(args.path);
const resolved = azionLibs.external.get(args.path);
const contents = await fs.promises.readFile(resolved, 'utf8');
const resolveDir = path.dirname(resolved);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Esbuild Azion Plugin', () => {
});

expect(results.outputFiles.at(0)?.text.trim()).toContain(
'vulcan-azion-modules-polyfills',
'bundler-azion-modules-polyfills',
);
});
});
Loading

0 comments on commit 529c267

Please sign in to comment.