|
| 1 | +import { testSuite, expect } from 'manten'; |
| 2 | +import { createFixture } from 'fs-fixture'; |
| 3 | +import { execa } from 'execa'; |
| 4 | +import { outdent } from 'outdent'; |
| 5 | +import { pkgroll } from '../../utils.js'; |
| 6 | +import { createPackageJson } from '../../fixtures.js'; |
| 7 | + |
| 8 | +export default testSuite(({ describe }, nodePath: string) => { |
| 9 | + describe('imports - non-# import handling', async ({ test }) => { |
| 10 | + test('non-# imports are skipped', async () => { |
| 11 | + const packagePath = 'node_modules/test-pkg'; |
| 12 | + const consumedPackage = { |
| 13 | + 'package.json': createPackageJson({ |
| 14 | + name: 'test-pkg', |
| 15 | + type: 'module', |
| 16 | + imports: { |
| 17 | + // @ts-expect-error - Testing non-# import (not spec-compliant but we handle it) |
| 18 | + 'my-alias': './src/internal.js', |
| 19 | + }, |
| 20 | + exports: './dist/index.js', |
| 21 | + }), |
| 22 | + src: { |
| 23 | + 'index.ts': 'export const value = 456;', |
| 24 | + 'internal.js': 'export const internal = true;', |
| 25 | + }, |
| 26 | + }; |
| 27 | + await using fixture = await createFixture({ |
| 28 | + [packagePath]: consumedPackage, |
| 29 | + 'load-pkg.mjs': outdent` |
| 30 | + import { value } from "test-pkg"; |
| 31 | + console.log(value); |
| 32 | + `, |
| 33 | + }); |
| 34 | + |
| 35 | + const result = await pkgroll([], { |
| 36 | + cwd: fixture.getPath(packagePath), |
| 37 | + nodePath, |
| 38 | + }); |
| 39 | + |
| 40 | + expect(result.exitCode).toBe(0); |
| 41 | + expect(result.stderr).toBe(''); |
| 42 | + |
| 43 | + const indexExists = await fixture.exists(`${packagePath}/dist/index.js`); |
| 44 | + expect(indexExists).toBe(true); |
| 45 | + |
| 46 | + const internalExists = await fixture.exists(`${packagePath}/dist/internal.js`); |
| 47 | + expect(internalExists).toBe(false); |
| 48 | + |
| 49 | + const { stdout } = await execa('node', ['load-pkg.mjs'], { cwd: fixture.path }); |
| 50 | + expect(stdout).toBe('456'); |
| 51 | + }); |
| 52 | + |
| 53 | + test('mixed # and non-# imports - only # are built', async () => { |
| 54 | + const packagePath = 'node_modules/test-pkg'; |
| 55 | + const consumedPackage = { |
| 56 | + 'package.json': createPackageJson({ |
| 57 | + name: 'test-pkg', |
| 58 | + type: 'module', |
| 59 | + imports: { |
| 60 | + '#valid': './dist/valid.js', |
| 61 | + // @ts-expect-error - Testing non-# import (not spec-compliant) |
| 62 | + 'invalid-alias': './src/invalid.js', |
| 63 | + }, |
| 64 | + exports: './dist/index.js', |
| 65 | + }), |
| 66 | + src: { |
| 67 | + 'valid.ts': 'export const valid = "works";', |
| 68 | + 'invalid.js': 'export const invalid = "skipped";', |
| 69 | + 'index.ts': 'export { valid } from "#valid";', |
| 70 | + }, |
| 71 | + }; |
| 72 | + await using fixture = await createFixture({ |
| 73 | + [packagePath]: consumedPackage, |
| 74 | + 'load-pkg.mjs': outdent` |
| 75 | + import { valid } from "test-pkg"; |
| 76 | + console.log(valid); |
| 77 | + `, |
| 78 | + }); |
| 79 | + |
| 80 | + const result = await pkgroll([], { |
| 81 | + cwd: fixture.getPath(packagePath), |
| 82 | + nodePath, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(result.exitCode).toBe(0); |
| 86 | + expect(result.stderr).toBe(''); |
| 87 | + |
| 88 | + const validExists = await fixture.exists(`${packagePath}/dist/valid.js`); |
| 89 | + expect(validExists).toBe(true); |
| 90 | + |
| 91 | + const invalidExists = await fixture.exists(`${packagePath}/dist/invalid.js`); |
| 92 | + expect(invalidExists).toBe(false); |
| 93 | + |
| 94 | + const distContent = await fixture.readFile(`${packagePath}/dist/index.js`, 'utf8'); |
| 95 | + expect(distContent).toMatch('#valid'); |
| 96 | + |
| 97 | + const { stdout } = await execa('node', ['load-pkg.mjs'], { cwd: fixture.path }); |
| 98 | + expect(stdout).toBe('works'); |
| 99 | + }); |
| 100 | + |
| 101 | + test('condition keys (node/default) processed despite not starting with #', async () => { |
| 102 | + const packagePath = 'node_modules/test-pkg'; |
| 103 | + const consumedPackage = { |
| 104 | + 'package.json': createPackageJson({ |
| 105 | + name: 'test-pkg', |
| 106 | + type: 'module', |
| 107 | + imports: { |
| 108 | + '#platform': { |
| 109 | + node: './dist/node.js', |
| 110 | + default: './dist/browser.js', |
| 111 | + }, |
| 112 | + }, |
| 113 | + exports: './dist/index.js', |
| 114 | + }), |
| 115 | + src: { |
| 116 | + 'node.ts': 'export const env = "node";', |
| 117 | + 'browser.ts': 'export const env = "browser";', |
| 118 | + 'index.ts': 'export { env } from "#platform";', |
| 119 | + }, |
| 120 | + }; |
| 121 | + await using fixture = await createFixture({ |
| 122 | + [packagePath]: consumedPackage, |
| 123 | + 'load-pkg.mjs': outdent` |
| 124 | + import { env } from "test-pkg"; |
| 125 | + console.log(env); |
| 126 | + `, |
| 127 | + }); |
| 128 | + |
| 129 | + const result = await pkgroll([], { |
| 130 | + cwd: fixture.getPath(packagePath), |
| 131 | + nodePath, |
| 132 | + }); |
| 133 | + |
| 134 | + expect(result.exitCode).toBe(0); |
| 135 | + expect(result.stderr).toBe(''); |
| 136 | + |
| 137 | + const nodeExists = await fixture.exists(`${packagePath}/dist/node.js`); |
| 138 | + expect(nodeExists).toBe(true); |
| 139 | + |
| 140 | + const browserExists = await fixture.exists(`${packagePath}/dist/browser.js`); |
| 141 | + expect(browserExists).toBe(true); |
| 142 | + |
| 143 | + const distContent = await fixture.readFile(`${packagePath}/dist/index.js`, 'utf8'); |
| 144 | + expect(distContent).toMatch('#platform'); |
| 145 | + |
| 146 | + const { stdout } = await execa('node', ['load-pkg.mjs'], { cwd: fixture.path }); |
| 147 | + expect(stdout).toBe('node'); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments