11import { join } from 'node:path' ;
2+ import { pathToFileURL } from 'node:url' ;
23import { buildAndGetResults } from 'test-helper' ;
34import { describe , expect , test } from 'vitest' ;
45
@@ -21,25 +22,20 @@ test('shims for __dirname and __filename in ESM', async () => {
2122describe ( 'shims for `import.meta.url` in CJS' , ( ) => {
2223 test ( 'CJS should apply shims' , async ( ) => {
2324 const fixturePath = join ( __dirname , 'cjs' ) ;
24- const { entries } = await buildAndGetResults ( fixturePath ) ;
25- for ( const shim of [
26- `var __rslib_import_meta_url__ = /*#__PURE__*/ function() {
27- return 'undefined' == typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
28- }();` ,
29- 'console.log(__rslib_import_meta_url__);' ,
30- ] ) {
31- expect ( entries . cjs ) . toContain ( shim ) ;
32- }
25+ const { entryFiles } = await buildAndGetResults ( fixturePath ) ;
26+ const exported = await import ( entryFiles . cjs ) ;
27+ const fileUrl = pathToFileURL ( entryFiles . cjs ) . href ;
28+ expect ( exported . default ) . toBe ( fileUrl ) ;
3329 } ) ;
3430
3531 test ( 'ESM should not be affected by CJS shims configuration' , async ( ) => {
3632 const fixturePath = join ( __dirname , 'cjs' ) ;
3733 const { entries } = await buildAndGetResults ( fixturePath ) ;
3834 expect ( entries . esm ) . toMatchInlineSnapshot ( `
39- "const foo = ()=>{
40- console.log(import.meta. url) ;
41- } ;
42- export { foo };
35+ "const url = import.meta.url;
36+ const readUrl = url;
37+ /* harmony default export */ const src = readUrl ;
38+ export { src as default };
4339 "
4440 ` ) ;
4541 } ) ;
0 commit comments