|
| 1 | +import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { createUnplugin } from 'unplugin' |
| 5 | +import { afterAll, describe, expect, it } from 'vitest' |
| 6 | +import { unescapeResourcePath } from '../../../src/utils/webpack-like' |
| 7 | +import { runRspack, runWebpack } from '../utils' |
| 8 | + |
| 9 | +const root = realpathSync(mkdtempSync(join(tmpdir(), 'unplugin-escaped-path-'))) |
| 10 | +const hashFile = join(root, 'msg#hash.js') |
| 11 | +const virtualId = '\0virtual-msg' |
| 12 | + |
| 13 | +mkdirSync(root, { recursive: true }) |
| 14 | +writeFileSync(join(root, 'entry.js'), [ |
| 15 | + `import hash from './msg#hash.js'`, |
| 16 | + `import virtual from 'virtual-msg'`, |
| 17 | + `console.log(hash, virtual)`, |
| 18 | +].join('\n')) |
| 19 | +writeFileSync(hashFile, `export default 'hash'`) |
| 20 | + |
| 21 | +afterAll(() => rmSync(root, { recursive: true, force: true })) |
| 22 | + |
| 23 | +function createPlugin() { |
| 24 | + const seen: Record<'resolveId' | 'load' | 'transform', string[]> = { resolveId: [], load: [], transform: [] } |
| 25 | + const plugin = createUnplugin(() => ({ |
| 26 | + name: 'escaped-path', |
| 27 | + resolveId(id) { |
| 28 | + seen.resolveId.push(id) |
| 29 | + if (id === 'virtual-msg') |
| 30 | + return virtualId |
| 31 | + }, |
| 32 | + load(id) { |
| 33 | + seen.load.push(id) |
| 34 | + if (id === virtualId) |
| 35 | + return `export default 'virtual'` |
| 36 | + }, |
| 37 | + transform(code, id) { |
| 38 | + seen.transform.push(id) |
| 39 | + return code |
| 40 | + }, |
| 41 | + })) |
| 42 | + return { seen, plugin } |
| 43 | +} |
| 44 | + |
| 45 | +const frameworks = [ |
| 46 | + [ |
| 47 | + 'webpack', |
| 48 | + (plugin: ReturnType<typeof createPlugin>['plugin']) => runWebpack({ |
| 49 | + mode: 'development', |
| 50 | + context: root, |
| 51 | + entry: join(root, 'entry.js'), |
| 52 | + output: { |
| 53 | + path: join(root, 'dist-webpack'), |
| 54 | + }, |
| 55 | + plugins: [ |
| 56 | + plugin.webpack(), |
| 57 | + ], |
| 58 | + }), |
| 59 | + ], |
| 60 | + [ |
| 61 | + 'rspack', |
| 62 | + (plugin: ReturnType<typeof createPlugin>['plugin']) => runRspack({ |
| 63 | + mode: 'development', |
| 64 | + context: root, |
| 65 | + entry: join(root, 'entry.js'), |
| 66 | + output: { |
| 67 | + path: join(root, 'dist-rspack'), |
| 68 | + }, |
| 69 | + plugins: [ |
| 70 | + plugin.rspack(), |
| 71 | + ], |
| 72 | + }), |
| 73 | + ], |
| 74 | +] as const |
| 75 | + |
| 76 | +describe('escaped paths', () => { |
| 77 | + for (const [name, run] of frameworks) { |
| 78 | + it(name, async () => { |
| 79 | + const { seen, plugin } = createPlugin() |
| 80 | + await run(plugin) |
| 81 | + |
| 82 | + // A `#` in a real file name must reach the hooks as is, without the loader escape |
| 83 | + for (const hook of ['load', 'transform'] as const) { |
| 84 | + expect(seen[hook]).toContain(hashFile) |
| 85 | + } |
| 86 | + // and a virtual id with a leading `\0` must stay untouched |
| 87 | + expect(seen.load).toContain(virtualId) |
| 88 | + }) |
| 89 | + } |
| 90 | +}) |
| 91 | + |
| 92 | +describe('unescapeResourcePath', () => { |
| 93 | + it('removes the escape character of each bundler', () => { |
| 94 | + expect(unescapeResourcePath('/a/msg\0#hash.js', 'webpack')).toBe('/a/msg#hash.js') |
| 95 | + expect(unescapeResourcePath('/a/msg\0?x\0\0y.js', 'webpack')).toBe('/a/msg?x\0y.js') |
| 96 | + expect(unescapeResourcePath('/a/msg\u200B#hash.js', 'rspack')).toBe('/a/msg#hash.js') |
| 97 | + expect(unescapeResourcePath('/a/msg\u200B?x\u200B\u200By.js', 'rspack')).toBe('/a/msg?x\u200By.js') |
| 98 | + }) |
| 99 | + |
| 100 | + it('leaves the escape character of the other bundler alone', () => { |
| 101 | + expect(unescapeResourcePath('/a/msg\u200B#hash.js', 'webpack')).toBe('/a/msg\u200B#hash.js') |
| 102 | + expect(unescapeResourcePath('/a/msg\0#hash.js', 'rspack')).toBe('/a/msg\0#hash.js') |
| 103 | + }) |
| 104 | +}) |
0 commit comments