import { rollup, VERSION } from 'rollup'; import { createFilter, stripAttributes } from '../'; const [rollupMajor] = VERSION.split('.').map(Number); async function captureAttributedId(entrySource: string, target: string): Promise { const ids: string[] = []; await rollup({ input: '/entry.js', plugins: [ { name: 'virtual', resolveId: (source, importer) => (importer ? target : source), load(id) { ids.push(id); return id === '/entry.js' ? entrySource : 'export const value = 42;\n'; } } ] }); const targetId = ids.find((id) => id !== '/entry.js' && id.includes('attributes=')); if (!targetId) { throw new Error(`no attributed id captured for ${target}, got ${ids.join(', ')}`); } return targetId; } const importTargetWithAttributes = "import { value } from './target.js' with { __proto__: 'json', type: 'json', '10': 'a', '2': 'b' };\nconsole.log(value);\n"; test.skipIf(rollupMajor < 5)( 'strips the attribute suffix Rollup generates for an imported module', async () => { const targetId = await captureAttributedId(importTargetWithAttributes, '/target.js'); expect(stripAttributes(targetId)).toBe('/target.js'); expect(createFilter(['**/target.js'])(targetId)).toBe(true); } ); test.skipIf(rollupMajor < 5)( 'strips the attribute suffix Rollup appends to an id that already has a query', async () => { const targetId = await captureAttributedId(importTargetWithAttributes, '/target.js?version=1'); expect(targetId).toContain('?version=1&attributes='); expect(stripAttributes(targetId)).toBe('/target.js?version=1'); expect(createFilter(['**/target.js'])(targetId)).toBe(false); expect(createFilter([/^\/target\.js\?version=1$/])(targetId)).toBe(true); } );