|
| 1 | +/* eslint-disable consistent-return */ |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import PolyfillsManager from '../../../polyfills/polyfills-manager.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * ESBuild Azion Module Plugin for polyfilling node modules. |
| 8 | + * @param {boolean} buildProd Parameter to identify whether the build is dev or prod |
| 9 | + * @returns {object} - ESBuild plugin object. |
| 10 | + */ |
| 11 | +const ESBuildAzionModulePlugin = (buildProd) => { |
| 12 | + const NAME = 'vulcan-azion-modules-polyfills'; |
| 13 | + const NAMESPACE = NAME; |
| 14 | + const filter = /^azion:/; |
| 15 | + |
| 16 | + return { |
| 17 | + /** |
| 18 | + * Name and setup of the ESBuild plugin. |
| 19 | + * @param {object} build - ESBuild build object. |
| 20 | + */ |
| 21 | + name: NAME, |
| 22 | + setup: (build) => { |
| 23 | + const polyfillManager = PolyfillsManager.buildPolyfills(); |
| 24 | + |
| 25 | + const options = build.initialOptions; |
| 26 | + |
| 27 | + // filter external no prefix |
| 28 | + const filteredExternal = new Map( |
| 29 | + [...polyfillManager.external].filter(([key]) => { |
| 30 | + const hasPrefix = /^[^:]+:/.test(key); |
| 31 | + return hasPrefix; |
| 32 | + }), |
| 33 | + ); |
| 34 | + |
| 35 | + // external |
| 36 | + if (buildProd) { |
| 37 | + options.external = options.external || []; |
| 38 | + [...filteredExternal].forEach(([key]) => { |
| 39 | + if (!options.external.includes(key)) { |
| 40 | + options.external.push(key); |
| 41 | + } |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Resolve callback for ESBuild. |
| 47 | + * @param {object} args - Arguments object. |
| 48 | + * @returns {object|undefined} - Object with path and namespace or undefined. |
| 49 | + */ |
| 50 | + build.onResolve({ filter }, async (args) => { |
| 51 | + if (!buildProd && polyfillManager.external.has(args.path)) { |
| 52 | + return { |
| 53 | + path: args.path, |
| 54 | + namespace: NAMESPACE, |
| 55 | + }; |
| 56 | + } |
| 57 | + if (!polyfillManager.external.has(args.path)) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // external bypass |
| 62 | + if ( |
| 63 | + options?.external?.length > 0 && |
| 64 | + options?.external?.includes(args.path) |
| 65 | + ) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + return { |
| 70 | + path: args.path, |
| 71 | + namespace: NAMESPACE, |
| 72 | + }; |
| 73 | + }); |
| 74 | + |
| 75 | + /** |
| 76 | + * Load callback for node module files. |
| 77 | + * @param {object} args - Arguments object. |
| 78 | + * @returns {object} - Object with loader, contents, and resolve directory. |
| 79 | + */ |
| 80 | + build.onLoad({ filter, namespace: NAMESPACE }, async (args) => { |
| 81 | + if (!polyfillManager.external.has(args.path)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + const resolved = polyfillManager.external.get(args.path); |
| 85 | + const contents = await fs.promises.readFile(resolved, 'utf8'); |
| 86 | + const resolveDir = path.dirname(resolved); |
| 87 | + |
| 88 | + return { |
| 89 | + loader: 'js', |
| 90 | + contents, |
| 91 | + resolveDir, |
| 92 | + }; |
| 93 | + }); |
| 94 | + }, |
| 95 | + }; |
| 96 | +}; |
| 97 | + |
| 98 | +export default ESBuildAzionModulePlugin; |
0 commit comments