11import path from 'path' ;
2+ import { fileURLToPath } from 'node:url' ;
23import fs from 'fs' ;
34import { minify } from 'terser' ;
45import type { PolyfillsLoaderConfig , PolyfillConfig , PolyfillFile } from './types.ts' ;
@@ -25,60 +26,76 @@ export async function createPolyfillsData(cfg: PolyfillsLoaderConfig): Promise<P
2526 }
2627
2728 if ( polyfills . coreJs ) {
29+ // @ts -ignore import.meta works at runtime on Node 24
30+ const coreJsPath = await import . meta. resolve ( 'core-js-bundle/minified.js' ) ;
2831 addPolyfillConfig ( {
2932 name : 'core-js' ,
30- path : require . resolve ( 'core-js-bundle/minified.js' ) ,
33+ path : fileURLToPath ( coreJsPath ) ,
3134 test : noModuleSupportTest ,
3235 } ) ;
3336 }
3437
3538 if ( polyfills . URLPattern ) {
39+ // @ts -ignore import.meta works at runtime on Node 24
40+ const urlPatternPath = await import . meta. resolve ( 'urlpattern-polyfill' ) ;
3641 addPolyfillConfig ( {
3742 name : 'urlpattern-polyfill' ,
3843 test : '"URLPattern" in window' ,
39- path : require . resolve ( 'urlpattern-polyfill' ) ,
44+ path : fileURLToPath ( urlPatternPath ) ,
4045 } ) ;
4146 }
4247
4348 if ( polyfills . esModuleShims ) {
49+ // @ts -ignore import.meta works at runtime on Node 24
50+ const esModuleShimsPath = await import . meta. resolve ( 'es-module-shims' ) ;
4451 addPolyfillConfig ( {
4552 name : 'es-module-shims' ,
4653 test : polyfills . esModuleShims !== 'always' ? '1' : undefined ,
47- path : require . resolve ( 'es-module-shims' ) ,
54+ path : fileURLToPath ( esModuleShimsPath ) ,
4855 minify : true ,
4956 } ) ;
5057 }
5158
5259 if ( polyfills . constructibleStylesheets ) {
60+ // @ts -ignore import.meta works at runtime on Node 24
61+ const constructibleStylesheetsPath = await import . meta
62+ . resolve ( 'construct-style-sheets-polyfill' ) ;
5363 addPolyfillConfig ( {
5464 name : 'constructible-style-sheets-polyfill' ,
5565 test : '!("adoptedStyleSheets" in document)' ,
56- path : require . resolve ( 'construct-style-sheets-polyfill' ) ,
66+ path : fileURLToPath ( constructibleStylesheetsPath ) ,
5767 } ) ;
5868 }
5969
6070 if ( polyfills . regeneratorRuntime ) {
71+ // @ts -ignore import.meta works at runtime on Node 24
72+ const regeneratorRuntimePath = await import . meta. resolve ( 'regenerator-runtime/runtime' ) ;
6173 addPolyfillConfig ( {
6274 name : 'regenerator-runtime' ,
6375 test : polyfills . regeneratorRuntime !== 'always' ? noModuleSupportTest : undefined ,
64- path : require . resolve ( 'regenerator-runtime/runtime' ) ,
76+ path : fileURLToPath ( regeneratorRuntimePath ) ,
6577 } ) ;
6678 }
6779
6880 if ( polyfills . fetch ) {
81+ // @ts -ignore import.meta works at runtime on Node 24
82+ const fetchPath = await import . meta. resolve ( 'whatwg-fetch/dist/fetch.umd.js' ) ;
83+ const paths = [ fileURLToPath ( fetchPath ) ] ;
84+
85+ if ( polyfills . abortController ) {
86+ // @ts -ignore import.meta works at runtime on Node 24
87+ const abortPath = await import . meta. resolve ( 'abortcontroller-polyfill/dist/umd-polyfill.js' ) ;
88+ paths . push ( fileURLToPath ( abortPath ) ) ;
89+ }
90+
6991 addPolyfillConfig ( {
7092 name : 'fetch' ,
7193 test : `!('fetch' in window)${
7294 polyfills . abortController
7395 ? " || !('Request' in window) || !('signal' in window.Request.prototype)"
7496 : ''
7597 } `,
76- path : polyfills . abortController
77- ? [
78- require . resolve ( 'whatwg-fetch/dist/fetch.umd.js' ) ,
79- require . resolve ( 'abortcontroller-polyfill/dist/umd-polyfill.js' ) ,
80- ]
81- : [ require . resolve ( 'whatwg-fetch/dist/fetch.umd.js' ) ] ,
98+ path : paths ,
8299 minify : true ,
83100 } ) ;
84101 }
@@ -102,22 +119,29 @@ export async function createPolyfillsData(cfg: PolyfillsLoaderConfig): Promise<P
102119
103120 if ( polyfills . systemjsExtended ) {
104121 // full systemjs, including import maps polyfill
122+ // @ts -ignore import.meta works at runtime on Node 24
123+ const systemjsPath = await import . meta. resolve ( 'systemjs/dist/system.min.js' ) ;
105124 addPolyfillConfig ( {
106125 name,
107126 test,
108- path : require . resolve ( 'systemjs/dist/system.min.js' ) ,
127+ path : fileURLToPath ( systemjsPath ) ,
109128 } ) ;
110129 } else {
111130 // plain systemjs as es module polyfill
131+ // @ts -ignore import.meta works at runtime on Node 24
132+ const systemjsPath = await import . meta. resolve ( 'systemjs/dist/s.min.js' ) ;
112133 addPolyfillConfig ( {
113134 name,
114135 test,
115- path : require . resolve ( 'systemjs/dist/s.min.js' ) ,
136+ path : fileURLToPath ( systemjsPath ) ,
116137 } ) ;
117138 }
118139 }
119140
120141 if ( polyfills . dynamicImport ) {
142+ // @ts -ignore import.meta works at runtime on Node 24
143+ const dynamicImportPath = await import . meta
144+ . resolve ( 'dynamic-import-polyfill/dist/dynamic-import-polyfill.umd.js' ) ;
121145 addPolyfillConfig ( {
122146 name : 'dynamic-import' ,
123147 /**
@@ -131,64 +155,89 @@ export async function createPolyfillsData(cfg: PolyfillsLoaderConfig): Promise<P
131155 test :
132156 "'noModule' in HTMLScriptElement.prototype && " +
133157 "(function () { try { Function('window.importShim = s => import(s);').call(); return false; } catch (_) { return true; } })()" ,
134- path : require . resolve ( 'dynamic-import-polyfill/dist/dynamic-import-polyfill.umd.js' ) ,
158+ path : fileURLToPath ( dynamicImportPath ) ,
135159 initializer : "window.dynamicImportPolyfill.initialize({ importFunctionName: 'importShim' });" ,
136160 } ) ;
137161 }
138162
139163 if ( polyfills . intersectionObserver ) {
164+ // @ts -ignore import.meta works at runtime on Node 24
165+ const intersectionObserverPath = await import . meta
166+ . resolve ( 'intersection-observer/intersection-observer.js' ) ;
140167 addPolyfillConfig ( {
141168 name : 'intersection-observer' ,
142169 test : "!('IntersectionObserver' in window && 'IntersectionObserverEntry' in window && 'intersectionRatio' in window.IntersectionObserverEntry.prototype)" ,
143- path : require . resolve ( 'intersection-observer/intersection-observer.js' ) ,
170+ path : fileURLToPath ( intersectionObserverPath ) ,
144171 minify : true ,
145172 } ) ;
146173 }
147174
148175 if ( polyfills . resizeObserver ) {
176+ // @ts -ignore import.meta works at runtime on Node 24
177+ const resizeObserverPath = await import . meta
178+ . resolve ( 'resize-observer-polyfill/dist/ResizeObserver.global.js' ) ;
149179 addPolyfillConfig ( {
150180 name : 'resize-observer' ,
151181 test : "!('ResizeObserver' in window)" ,
152- path : require . resolve ( 'resize-observer-polyfill/dist/ResizeObserver.global.js' ) ,
182+ path : fileURLToPath ( resizeObserverPath ) ,
153183 minify : true ,
154184 } ) ;
155185 }
156186
157187 if ( polyfills . scopedCustomElementRegistry ) {
188+ // @ts -ignore import.meta works at runtime on Node 24
189+ const scopedRegistryPath = await import . meta
190+ . resolve ( '@webcomponents/scoped-custom-element-registry/scoped-custom-element-registry.min.js' ) ;
158191 addPolyfillConfig ( {
159192 name : 'scoped-custom-element-registry' ,
160193 test : "!('createElement' in ShadowRoot.prototype)" ,
161- path : require . resolve ( '@webcomponents/scoped-custom-element-registry/scoped-custom-element-registry.min.js' ) ,
194+ path : fileURLToPath ( scopedRegistryPath ) ,
162195 } ) ;
163196 }
164197
165198 if ( polyfills . webcomponents && ! polyfills . shadyCssCustomStyle ) {
199+ // @ts -ignore import.meta works at runtime on Node 24
200+ const webcomponentsPath = await import . meta
201+ . resolve ( '@webcomponents/webcomponentsjs/webcomponents-bundle.js' ) ;
166202 addPolyfillConfig ( {
167203 name : 'webcomponents' ,
168204 test : "!('attachShadow' in Element.prototype) || !('getRootNode' in Element.prototype) || (window.ShadyDOM && window.ShadyDOM.force)" ,
169- path : require . resolve ( '@webcomponents/webcomponentsjs/webcomponents-bundle.js' ) ,
205+ path : fileURLToPath ( webcomponentsPath ) ,
170206 } ) ;
171207
172208 // If a browser does not support nomodule attribute, but does support custom elements, we need
173209 // to load the custom elements es5 adapter. This is the case for Safari 10.1
210+ // @ts -ignore import.meta works at runtime on Node 24
211+ const es5AdapterPath = await import . meta
212+ . resolve ( '@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js' ) ;
174213 addPolyfillConfig ( {
175214 name : 'custom-elements-es5-adapter' ,
176215 test : "!('noModule' in HTMLScriptElement.prototype) && 'getRootNode' in Element.prototype" ,
177- path : require . resolve ( '@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js' ) ,
216+ path : fileURLToPath ( es5AdapterPath ) ,
178217 } ) ;
179218 }
180219
181220 if ( polyfills . webcomponents && polyfills . shadyCssCustomStyle ) {
182221 // shadycss/custom-style-interface polyfill *must* load after the webcomponents polyfill or it doesn't work.
183222 // to get around that, concat the two together.
184223
224+ // @ts -ignore import.meta works at runtime on Node 24
225+ const webcomponentsBundlePath = await import . meta
226+ . resolve ( '@webcomponents/webcomponentsjs/webcomponents-bundle.js' ) ;
227+ // @ts -ignore import.meta works at runtime on Node 24
228+ const customStylePath = await import . meta
229+ . resolve ( '@webcomponents/shadycss/custom-style-interface.min.js' ) ;
230+ // @ts -ignore import.meta works at runtime on Node 24
231+ const shadyCssScopedPath = await import . meta
232+ . resolve ( 'shady-css-scoped-element/shady-css-scoped-element.min.js' ) ;
233+
185234 addPolyfillConfig ( {
186235 name : 'webcomponents-shady-css-custom-style' ,
187236 test : "!('attachShadow' in Element.prototype) || !('getRootNode' in Element.prototype)" ,
188237 path : [
189- require . resolve ( '@webcomponents/webcomponentsjs/webcomponents-bundle.js' ) ,
190- require . resolve ( '@webcomponents/shadycss/custom-style-interface.min.js' ) ,
191- require . resolve ( 'shady-css-scoped-element/shady-css-scoped-element.min.js' ) ,
238+ fileURLToPath ( webcomponentsBundlePath ) ,
239+ fileURLToPath ( customStylePath ) ,
240+ fileURLToPath ( shadyCssScopedPath ) ,
192241 ] ,
193242 } ) ;
194243 }
0 commit comments