|
| 1 | +// @ts-expect-error |
| 2 | +import evalCode from 'eval'; |
| 3 | + |
| 4 | +import { stringifyExports } from './processVanillaFile'; |
| 5 | + |
| 6 | +describe('stringifyExports', () => { |
| 7 | + test('with plain object exports', () => { |
| 8 | + const recipeImports = new Set<string>(); |
| 9 | + const value = { |
| 10 | + string: 'value', |
| 11 | + boolean: true, |
| 12 | + number: 42, |
| 13 | + array: [420], |
| 14 | + object: { hello: 'world' }, |
| 15 | + }; |
| 16 | + |
| 17 | + expect(stringifyExports(recipeImports, value, null)).toMatchInlineSnapshot( |
| 18 | + `"{string:'value','boolean':true,number:42,array:[420],object:{hello:'world'}}"`, |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + // https://rollupjs.org/guide/en/#outputgeneratedcode -- see output.generatedCode.symbols |
| 23 | + // https://github.com/vitejs/vite/pull/5018 |
| 24 | + // https://github.com/vitejs/vite/blob/757a92f1c7c4fa961ed963edd245df77382dfde6/packages/vite/src/node/build.ts#L466-L469 |
| 25 | + test('with exports generated by Rollup', () => { |
| 26 | + const recipeImports = new Set<string>(); |
| 27 | + const value = Object.freeze( |
| 28 | + Object.defineProperty( |
| 29 | + { |
| 30 | + __proto__: null, |
| 31 | + some: 'export', |
| 32 | + }, |
| 33 | + Symbol.toStringTag, |
| 34 | + { value: 'Module' }, |
| 35 | + ), |
| 36 | + ); |
| 37 | + |
| 38 | + expect(stringifyExports(recipeImports, value, null)).toMatchInlineSnapshot( |
| 39 | + `"{some:'export'}"`, |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + test('with unusedCompositionRegex', () => { |
| 44 | + const recipeImports = new Set<string>(); |
| 45 | + const value = { |
| 46 | + compositionOnly: |
| 47 | + 'features_compositionOnly__1o6ek504 features_mergedStyle__1o6ek500 features_styleWithComposition__1o6ek501', |
| 48 | + mergedStyle: 'features_mergedStyle__1o6ek500', |
| 49 | + styleCompositionInSelector: |
| 50 | + 'features_styleCompositionInSelector__1o6ek507 features__1o6ek505 features__1o6ek506', |
| 51 | + styleVariantsCompositionInSelector: { |
| 52 | + variant: |
| 53 | + 'features_styleVariantsCompositionInSelector_variant__1o6ek50a features__1o6ek508 features__1o6ek509', |
| 54 | + }, |
| 55 | + styleVariantsWithComposition: { |
| 56 | + variant: |
| 57 | + 'features_styleVariantsWithComposition_variant__1o6ek502 features_mergedStyle__1o6ek500', |
| 58 | + }, |
| 59 | + styleVariantsWithMappedComposition: { |
| 60 | + variant: |
| 61 | + 'features_styleVariantsWithMappedComposition_variant__1o6ek503 features_mergedStyle__1o6ek500', |
| 62 | + }, |
| 63 | + styleWithComposition: |
| 64 | + 'features_styleWithComposition__1o6ek501 features_mergedStyle__1o6ek500', |
| 65 | + }; |
| 66 | + |
| 67 | + const exports = stringifyExports( |
| 68 | + recipeImports, |
| 69 | + value, |
| 70 | + /(features_compositionOnly__1o6ek504|features_styleCompositionInSelector__1o6ek507|features_styleVariantsCompositionInSelector_variant__1o6ek50a)\s/g, |
| 71 | + ); |
| 72 | + const exportsObject = evalCode(`module.exports = ${exports}`, 'dummy.js'); |
| 73 | + expect(exportsObject).toMatchInlineSnapshot(` |
| 74 | + Object { |
| 75 | + "compositionOnly": "features_mergedStyle__1o6ek500 features_styleWithComposition__1o6ek501", |
| 76 | + "mergedStyle": "features_mergedStyle__1o6ek500", |
| 77 | + "styleCompositionInSelector": "features__1o6ek505 features__1o6ek506", |
| 78 | + "styleVariantsCompositionInSelector": Object { |
| 79 | + "variant": "features__1o6ek508 features__1o6ek509", |
| 80 | + }, |
| 81 | + "styleVariantsWithComposition": Object { |
| 82 | + "variant": "features_styleVariantsWithComposition_variant__1o6ek502 features_mergedStyle__1o6ek500", |
| 83 | + }, |
| 84 | + "styleVariantsWithMappedComposition": Object { |
| 85 | + "variant": "features_styleVariantsWithMappedComposition_variant__1o6ek503 features_mergedStyle__1o6ek500", |
| 86 | + }, |
| 87 | + "styleWithComposition": "features_styleWithComposition__1o6ek501 features_mergedStyle__1o6ek500", |
| 88 | + } |
| 89 | + `); |
| 90 | + }); |
| 91 | +}); |
0 commit comments