|
1 | 1 | import { describe, test, expect } from 'manten'; |
2 | 2 | import { createFsRequire } from 'fs-require'; |
| 3 | +import { createFixture } from 'fs-fixture'; |
| 4 | +import { execaNode } from 'execa'; |
3 | 5 | import { Volume } from 'memfs'; |
4 | 6 | import outdent from 'outdent'; |
5 | 7 | import { transform, transformSync } from '../../src/utils/transform/index.js'; |
| 8 | +import { inlineSourceMap } from '../../src/source-map.js'; |
6 | 9 |
|
7 | 10 | const base64Module = (code: string) => `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`; |
8 | 11 |
|
@@ -62,6 +65,103 @@ export const transformSpec = () => describe('transform', () => { |
62 | 65 | }); |
63 | 66 | }); |
64 | 67 |
|
| 68 | + test('import.meta helper cannot be shadowed by user bindings', () => { |
| 69 | + const transformed = transformSync( |
| 70 | + ` |
| 71 | + const define_import_meta_default = { url: 'shadowed' }; |
| 72 | + export const url = import.meta.url; |
| 73 | + export const local = define_import_meta_default.url; |
| 74 | + `, |
| 75 | + 'file.js', |
| 76 | + { format: 'cjs' }, |
| 77 | + ); |
| 78 | + |
| 79 | + const fsRequire = createFsRequire(Volume.fromJSON({ |
| 80 | + '/file.js': transformed.code, |
| 81 | + })); |
| 82 | + |
| 83 | + expect(fsRequire('/file.js')).toEqual({ |
| 84 | + local: 'shadowed', |
| 85 | + url: expect.stringMatching(/^file:\/\/\/.*\/file.js$/), |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + test('doesnt emit import.meta helper without import.meta syntax', () => { |
| 90 | + const transformed = transformSync( |
| 91 | + ` |
| 92 | + // import.meta.url |
| 93 | + const text = 'import.meta.url'; |
| 94 | + export const value = text; |
| 95 | + `, |
| 96 | + 'file.js', |
| 97 | + { format: 'cjs' }, |
| 98 | + ); |
| 99 | + |
| 100 | + expect(transformed.code).not.toContain('import_meta'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('supports import.meta object access', () => { |
| 104 | + const transformed = transformSync( |
| 105 | + ` |
| 106 | + export const meta = import.meta; |
| 107 | + export const computed = import.meta['url']; |
| 108 | + export const destructured = (() => { |
| 109 | + const { url } = import.meta; |
| 110 | + return url; |
| 111 | + })(); |
| 112 | + export const prototype = Object.getPrototypeOf(import.meta); |
| 113 | + export const hasProto = '__proto__' in import.meta; |
| 114 | + export const urlDescriptor = Object.getOwnPropertyDescriptor(import.meta, 'url'); |
| 115 | + `, |
| 116 | + 'file.js', |
| 117 | + { format: 'cjs' }, |
| 118 | + ); |
| 119 | + const loaded = createFsRequire(Volume.fromJSON({ |
| 120 | + '/file.js': transformed.code, |
| 121 | + }))('/file.js'); |
| 122 | + |
| 123 | + expect(loaded.meta.url).toMatch(/^file:\/\/\/.*\/file.js$/); |
| 124 | + expect(loaded.computed).toMatch(/^file:\/\/\/.*\/file.js$/); |
| 125 | + expect(loaded.destructured).toMatch(/^file:\/\/\/.*\/file.js$/); |
| 126 | + expect(loaded.urlDescriptor).toStrictEqual({ |
| 127 | + configurable: true, |
| 128 | + enumerable: true, |
| 129 | + value: expect.stringMatching(/^file:\/\/\/.*\/file.js$/), |
| 130 | + writable: true, |
| 131 | + }); |
| 132 | + |
| 133 | + // TODO: Match native Node import.meta shape without adding another |
| 134 | + // source-map transform pass. |
| 135 | + // expect(Object.getPrototypeOf(loaded.meta)).toBe(null); |
| 136 | + // expect(loaded.prototype).toBe(null); |
| 137 | + // expect(loaded.hasProto).toBe(false); |
| 138 | + }); |
| 139 | + |
| 140 | + test('import.meta helper preserves source map columns', async () => { |
| 141 | + await using fixture = await createFixture({}); |
| 142 | + const fileName = fixture.getPath('source-map-check.ts'); |
| 143 | + const transformed = transformSync( |
| 144 | + outdent` |
| 145 | + export const meta = import.meta; |
| 146 | + const value = 'x'; |
| 147 | + throw new Error('source-map-check'); |
| 148 | + `, |
| 149 | + fileName, |
| 150 | + { format: 'cjs' }, |
| 151 | + ); |
| 152 | + await fixture.writeFile('source-map-check.cjs', inlineSourceMap(transformed)); |
| 153 | + |
| 154 | + const { exitCode, stderr } = await execaNode( |
| 155 | + fixture.getPath('source-map-check.cjs'), |
| 156 | + { |
| 157 | + nodeOptions: ['--enable-source-maps'], |
| 158 | + reject: false, |
| 159 | + }, |
| 160 | + ); |
| 161 | + expect(exitCode).toBe(1); |
| 162 | + expect(stderr).toContain(`${fileName}:3:7`); |
| 163 | + }); |
| 164 | + |
65 | 165 | test('dynamic import', () => { |
66 | 166 | const dynamicImport = transformSync( |
67 | 167 | 'import((0, _url.pathToFileURL)(path).href)', |
|
0 commit comments