Skip to content

Commit

Permalink
feat: support import attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
knightedcodemonkey committed Aug 5, 2023
1 parent d21c74c commit 11d894a
Show file tree
Hide file tree
Showing 8 changed files with 613 additions and 33 deletions.
544 changes: 537 additions & 7 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@knighted/duel",
"version": "1.0.0-rc.5",
"version": "1.0.0-rc.6",
"description": "TypeScript dual packages.",
"type": "module",
"main": "dist",
Expand Down Expand Up @@ -53,10 +53,11 @@
"eslint": "^8.45.0",
"eslint-plugin-n": "^16.0.1",
"prettier": "^3.0.1",
"typescript": "^5.2.0-dev.20230727"
"typescript": "^5.2.0-dev.20230727",
"vite": "^4.4.8"
},
"dependencies": {
"@knighted/specifier": "^1.0.0-rc.0",
"@knighted/specifier": "^1.0.0-rc.1",
"glob": "^10.3.3",
"read-pkg-up": "^10.0.0"
},
Expand Down
32 changes: 19 additions & 13 deletions src/duel.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ const duel = async args => {
const isCjsBuild = originalType !== 'commonjs'
const targetExt = isCjsBuild ? '.cjs' : '.mjs'
const hex = randomBytes(4).toString('hex')
const getOverrideTsConfig = dualOutDir => {
const getOverrideTsConfig = () => {
return {
...tsconfig,
compilerOptions: {
...tsconfig.compilerOptions,
outDir: dualOutDir,
module: 'NodeNext',
moduleResolution: 'NodeNext',
},
Expand All @@ -64,9 +63,9 @@ const duel = async args => {
configPath,
dirs
? isCjsBuild
? join(projectDir, outDir, 'esm')
: join(projectDir, outDir, 'cjs')
: undefined,
? join(absoluteOutDir, 'esm')
: join(absoluteOutDir, 'cjs')
: absoluteOutDir,
)
}
const updateSpecifiersAndFileExtensions = async filenames => {
Expand Down Expand Up @@ -140,8 +139,11 @@ const duel = async args => {
})

const dualConfigPath = join(paraTempDir, 'tsconfig.json')
const dualOutDir = isCjsBuild ? join(outDir, 'cjs') : join(outDir, 'esm')
const tsconfigDual = getOverrideTsConfig(dualOutDir)
const absoluteDualOutDir = join(
paraTempDir,
isCjsBuild ? join(outDir, 'cjs') : join(outDir, 'esm'),
)
const tsconfigDual = getOverrideTsConfig()

await writeFile(dualConfigPath, JSON.stringify(tsconfigDual))
await writeFile(
Expand All @@ -158,14 +160,16 @@ const duel = async args => {
const startTime = performance.now()

try {
await Promise.all([runPrimaryBuild(), runBuild(dualConfigPath)])
await Promise.all([
runPrimaryBuild(),
runBuild(dualConfigPath, absoluteDualOutDir),
])
success = true
} catch ({ message }) {
logError(message)
}

if (success) {
const absoluteDualOutDir = join(paraTempDir, dualOutDir)
const filenames = await glob(`${absoluteDualOutDir}/**/*{.js,.d.ts}`, {
ignore: 'node_modules/**',
})
Expand Down Expand Up @@ -194,8 +198,11 @@ const duel = async args => {

if (success) {
const dualConfigPath = join(projectDir, `tsconfig.${hex}.json`)
const dualOutDir = isCjsBuild ? join(outDir, 'cjs') : join(outDir, 'esm')
const tsconfigDual = getOverrideTsConfig(dualOutDir)
const absoluteDualOutDir = join(
projectDir,
isCjsBuild ? join(outDir, 'cjs') : join(outDir, 'esm'),
)
const tsconfigDual = getOverrideTsConfig()
const pkgRename = 'package.json.bak'

/**
Expand All @@ -217,7 +224,7 @@ const duel = async args => {
// Build dual
log('Starting dual build...')
try {
await runBuild(dualConfigPath)
await runBuild(dualConfigPath, absoluteDualOutDir)
} catch ({ message }) {
success = false
logError(message)
Expand All @@ -229,7 +236,6 @@ const duel = async args => {
await rename(join(pkgDir, pkgRename), pkg.path)

if (success) {
const absoluteDualOutDir = join(projectDir, dualOutDir)
const filenames = await glob(`${absoluteDualOutDir}/**/*{.js,.d.ts}`, {
ignore: 'node_modules/**',
})
Expand Down
4 changes: 4 additions & 0 deletions test/__fixtures__/plain/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": "0.0.0",
"type": "module"
}
1 change: 1 addition & 0 deletions test/__fixtures__/plain/src/enforce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const enforce = 'post'
10 changes: 10 additions & 0 deletions test/__fixtures__/plain/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { enforce } from './enforce.js'

import type { Plugin } from 'vite' assert { 'resolution-mode': 'import' }

export const plugin = (): Plugin => {
return {
name: 'plugin',
enforce
}
}
12 changes: 12 additions & 0 deletions test/__fixtures__/plain/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"esModuleInterop": true,
"declaration": true,
"strict": false,
"outDir": "dist",
"lib": ["DOM", "ES2015"]
},
"include": ["src"]
}
36 changes: 26 additions & 10 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { duel } from '../src/duel.js'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const plain = resolve(__dirname, '__fixtures__/plain')
const project = resolve(__dirname, '__fixtures__/project')
const parallel = resolve(__dirname, '__fixtures__/parallel')
const esmProject = resolve(__dirname, '__fixtures__/esmProject')
const cjsProject = resolve(__dirname, '__fixtures__/cjsProject')
const plainDist = join(plain, 'dist')
const proDist = join(project, 'dist')
const paraDist = join(parallel, 'dist')
const esmDist = join(esmProject, 'dist')
Expand All @@ -30,6 +32,7 @@ describe('duel', () => {
await rmDist(cjsDist)
await rmDist(errDist)
await rmDist(paraDist)
await rmDist(plainDist)
})

it('prints options help', async t => {
Expand Down Expand Up @@ -161,34 +164,47 @@ describe('duel', () => {
assert.equal(statusEsm, 0)
})

it('supports running builds in parallel', async t => {
it('supports both builds output to directories', async t => {
const spy = t.mock.method(global.console, 'log')

t.after(async () => {
await rmDist(paraDist)
await rmDist(proDist)
})
await duel(['-p', parallel, '-k', parallel, '-l', '-d'])
await duel(['-p', 'test/__fixtures__/project/tsconfig.json', '-k', project, '-d'])

assert.ok(
spy.mock.calls[3].arguments[0].startsWith('Successfully created a dual CJS build'),
)
assert.ok(existsSync(resolve(paraDist, 'esm/index.js')))
assert.ok(existsSync(resolve(paraDist, 'cjs/index.cjs')))
assert.ok(existsSync(resolve(proDist, 'esm/index.js')))
assert.ok(existsSync(resolve(proDist, 'cjs/index.cjs')))
})

it('supports both builds output to directories', async t => {
it('supports import attributes and ts import assertion resolution mode', async t => {
const spy = t.mock.method(global.console, 'log')

t.after(async () => {
await rmDist(proDist)
await rmDist(plainDist)
})
await duel(['-p', 'test/__fixtures__/project/tsconfig.json', '-k', project, '-d'])
await duel(['-p', plain, '-k', plain])

assert.ok(
spy.mock.calls[2].arguments[0].startsWith('Successfully created a dual CJS build'),
)
})

it('supports running builds in parallel', async t => {
const spy = t.mock.method(global.console, 'log')

t.after(async () => {
await rmDist(paraDist)
})
await duel(['-p', parallel, '-k', parallel, '-l', '-d'])

assert.ok(
spy.mock.calls[3].arguments[0].startsWith('Successfully created a dual CJS build'),
)
assert.ok(existsSync(resolve(proDist, 'esm/index.js')))
assert.ok(existsSync(resolve(proDist, 'cjs/index.cjs')))
assert.ok(existsSync(resolve(paraDist, 'esm/index.js')))
assert.ok(existsSync(resolve(paraDist, 'cjs/index.cjs')))
})

it('works as a cli script', async () => {
Expand Down

0 comments on commit 11d894a

Please sign in to comment.