|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { buildAndGetResults, queryContent } from 'test-helper'; |
| 4 | +import { describe, expect, test } from 'vitest'; |
| 5 | + |
| 6 | +describe('JSON', async () => { |
| 7 | + const fixturePath = join(__dirname, '.'); |
| 8 | + const { contents, files } = await buildAndGetResults({ fixturePath }); |
| 9 | + |
| 10 | + test('bundle', async () => { |
| 11 | + const { content: bundle } = queryContent(contents.esm0!, /index\.js/); |
| 12 | + expect(bundle).toMatchInlineSnapshot(` |
| 13 | + "var foo_namespaceObject = { |
| 14 | + S: "foo" |
| 15 | + }; |
| 16 | + const src = foo_namespaceObject.S + '1'; |
| 17 | + export { src as default }; |
| 18 | + " |
| 19 | + `); |
| 20 | + const bundleResult = await import(files.esm0![0]!); |
| 21 | + expect(bundleResult.default).toBe('foo1'); |
| 22 | + }); |
| 23 | + |
| 24 | + test('bundleless default', async () => { |
| 25 | + const bundlelessFiles = Object.keys(contents.esm1!); |
| 26 | + expect(bundlelessFiles).toMatchInlineSnapshot(` |
| 27 | + [ |
| 28 | + "<ROOT>/tests/integration/json/dist/bundleless-default/foo.js", |
| 29 | + "<ROOT>/tests/integration/json/dist/bundleless-default/index.js", |
| 30 | + ] |
| 31 | + `); |
| 32 | + const bundlelessResult = await import( |
| 33 | + files.esm1!.find((file) => file.endsWith('index.js'))! |
| 34 | + ); |
| 35 | + expect(bundlelessResult.default).toBe('foo1'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('bundleless preserver JSON', async () => { |
| 39 | + const { content: bundlelessPreserveJson } = queryContent( |
| 40 | + contents.esm2!, |
| 41 | + /index\.js/, |
| 42 | + ); |
| 43 | + expect(bundlelessPreserveJson).toMatchInlineSnapshot(` |
| 44 | + "import * as __WEBPACK_EXTERNAL_MODULE__foo_json_16d256d4__ from "./foo.json"; |
| 45 | + const src = __WEBPACK_EXTERNAL_MODULE__foo_json_16d256d4__.value + '1'; |
| 46 | + export { src as default }; |
| 47 | + " |
| 48 | + `); |
| 49 | + |
| 50 | + expect( |
| 51 | + readFileSync( |
| 52 | + join(fixturePath, 'dist/bundleless-preserve-json/foo.json'), |
| 53 | + 'utf-8', |
| 54 | + ), |
| 55 | + ).toMatchInlineSnapshot(` |
| 56 | + "{ |
| 57 | + "value": "foo", |
| 58 | + "value_unused": "noop" |
| 59 | + } |
| 60 | + " |
| 61 | + `); |
| 62 | + }); |
| 63 | +}); |
0 commit comments