|
1 | | -import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'; |
| 1 | +import { readFileSync, writeFileSync } from 'node:fs'; |
2 | 2 | import { dirname } from 'node:path'; |
3 | 3 | import { fileURLToPath } from 'node:url'; |
4 | 4 | import * as esbuild from 'esbuild'; |
@@ -24,11 +24,13 @@ const external = { |
24 | 24 | '@jridgewell/resolve-uri': 'resolveURI', |
25 | 25 | }; |
26 | 26 |
|
| 27 | +const externalSpec = /^[^./]/; |
| 28 | + |
27 | 29 | /** @type {esbuild.Plugin} */ |
28 | 30 | const externalize = { |
29 | 31 | name: 'externalize', |
30 | 32 | setup(build) { |
31 | | - build.onResolve({ filter: /^[^./]/ }, ({ path }) => { |
| 33 | + build.onResolve({ filter: externalSpec }, ({ path }) => { |
32 | 34 | if (!external[path]) { |
33 | 35 | throw new Error(`unregistered external module "${path}"`); |
34 | 36 | } |
@@ -38,20 +40,21 @@ const externalize = { |
38 | 40 | }; |
39 | 41 |
|
40 | 42 | // Babel still supports Node v6, which doesn't have getOwnPropertyDescriptors. |
41 | | -const getOwnPropertyDescriptorsPolyfill = `if (!Object.getOwnPropertyDescriptors) Object.getOwnPropertyDescriptors = function(value) { |
42 | | - return Reflect.ownKeys(value).reduce(function (acc, key) { |
43 | | - Object.defineProperty(acc, key, Object.getOwnPropertyDescriptor(value, key)) |
44 | | - return acc; |
45 | | - }, {}); |
46 | | -}`; |
| 43 | +const getOwnPropertyDescriptorsPolyfill = ` |
| 44 | +(v) => |
| 45 | + Reflect.ownKeys(v).reduce((o, k) => |
| 46 | + Object.defineProperty(o, k, Object.getOwnPropertyDescriptor(value, k)), {}) |
| 47 | +` |
| 48 | + .trim() |
| 49 | + .replace(/\n\s*/g, ' '); |
47 | 50 |
|
48 | 51 | /** @type {esbuild.Plugin} */ |
49 | 52 | const umd = { |
50 | 53 | name: 'umd', |
51 | 54 | setup(build) { |
52 | 55 | const dependencies = Object.keys(packageJson.dependencies || {}); |
53 | 56 | const browserDeps = dependencies.map((d) => `global.${external[d]}`); |
54 | | - const requireDeps = dependencies.map((d) => `require_keep('${d}')`); |
| 57 | + const requireDeps = dependencies.map((d) => `require('${d}')`); |
55 | 58 | const amdDeps = dependencies.map((d) => `'${d}'`); |
56 | 59 | const locals = dependencies.map((d) => `require_${external[d]}`); |
57 | 60 | const browserGlobal = external[packageJson.name]; |
@@ -65,18 +68,29 @@ const umd = { |
65 | 68 |
|
66 | 69 | build.initialOptions.banner = { |
67 | 70 | js: ` |
68 | | -(function (global, factory, e, m) { |
69 | | - typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, module${requireDeps}) : |
70 | | - typeof define === 'function' && define.amd ? define(['exports', 'module'${amdDeps}], factory) : |
71 | | - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(e = {}, m = { exports: e }${browserDeps}), global.${browserGlobal} = 'default' in m.exports ? m.exports.default : m.exports); |
72 | | -})(this, (function (exports, module${locals}) { |
73 | | -"use strict"; |
74 | | -${getOwnPropertyDescriptorsPolyfill} |
| 71 | +(function (global, factory, m) { |
| 72 | + typeof exports === 'object' && typeof module !== 'undefined' ? factory(module${requireDeps.join(', ')}) : |
| 73 | + typeof define === 'function' && define.amd ? define(['module'${amdDeps.join(', ')}], factory) : |
| 74 | + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(m = { exports: {} }${browserDeps.join(', ')}), global.${browserGlobal} = 'default' in m.exports ? m.exports.default : m.exports); |
| 75 | +})(this, (function (module${locals.join(', ')}) { |
75 | 76 | `.trim(), |
76 | 77 | }; |
77 | 78 | build.initialOptions.footer = { |
78 | 79 | js: '}));', |
79 | 80 | }; |
| 81 | + |
| 82 | + build.onResolve({ filter: externalSpec }, ({ path }) => { |
| 83 | + if (!external[path]) { |
| 84 | + throw new Error(`unregistered external module "${path}"`); |
| 85 | + } |
| 86 | + return { path, namespace: 'umd' }; |
| 87 | + }); |
| 88 | + |
| 89 | + build.onLoad({ filter: /.*/, namespace: 'umd' }, ({ path }) => { |
| 90 | + return { |
| 91 | + contents: `module.exports = require_${external[path]}`, |
| 92 | + }; |
| 93 | + }); |
80 | 94 | }, |
81 | 95 | }; |
82 | 96 |
|
@@ -88,7 +102,7 @@ async function build(esm) { |
88 | 102 | sourcemap: 'linked', |
89 | 103 | sourcesContent: false, |
90 | 104 | format: esm ? 'esm' : 'cjs', |
91 | | - plugins: esm ? [externalize] : [externalize, umd], |
| 105 | + plugins: esm ? [externalize] : [umd], |
92 | 106 | outExtension: esm ? { '.js': '.mjs' } : { '.js': '.umd.js' }, |
93 | 107 | target: tsconfig.compilerOptions.target, |
94 | 108 | write: false, |
@@ -100,19 +114,17 @@ async function build(esm) { |
100 | 114 | } |
101 | 115 | process.exit(1); |
102 | 116 | } |
103 | | - mkdirSync('dist', { recursive: true }); |
104 | 117 |
|
105 | 118 | for (const file of build.outputFiles) { |
106 | 119 | if (!file.path.endsWith('.umd.js')) { |
107 | 120 | writeFileSync(file.path, file.contents); |
108 | 121 | continue; |
109 | 122 | } |
110 | 123 |
|
| 124 | + const getOwnPropDescsHelper = '__getOwnPropDescs = Object.getOwnPropertyDescriptors'; |
111 | 125 | const contents = file.text.replace( |
112 | | - /\brequire(_keep)?\(['"]([^'"]*)['"]\)/g, |
113 | | - (_match, keep, spec) => { |
114 | | - return keep ? `require('${spec}')` : `require_${external[spec]}`; |
115 | | - }, |
| 126 | + getOwnPropDescsHelper, |
| 127 | + `${getOwnPropDescsHelper} || (${getOwnPropertyDescriptorsPolyfill})`, |
116 | 128 | ); |
117 | 129 | writeFileSync(file.path, contents); |
118 | 130 | } |
|