Skip to content

Commit

Permalink
feat: link module to itself during stub
Browse files Browse the repository at this point in the history
this way we can reference package by name in tests (similar to what lerna does)
  • Loading branch information
pi0 committed Apr 9, 2021
1 parent 5cb46c8 commit 5a1f9cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import rimraf from 'rimraf'
import defu from 'defu'
import prettyBytes from 'pretty-bytes'
import jiti from 'jiti'
import { dumpObject, symlink } from './utils'
import type { BuildContext } from './types'
import { validateDependencies } from './validate'
import { rollupBuild } from './builder/rollup'
Expand Down Expand Up @@ -79,6 +80,11 @@ export async function build (rootDir: string, stub: boolean) {
await mkdir(outDir).catch(() => { })
}

// selflink
if (ctx.stub) {
await symlink(resolve(ctx.rootDir), resolve(ctx.rootDir, 'node_modules', ctx.pkg.name))
}

// untyped
await typesBuild(ctx)

Expand All @@ -88,6 +94,11 @@ export async function build (rootDir: string, stub: boolean) {
// rollup
await rollupBuild(ctx)

// Skip rest for stub
if (ctx.stub) {
return
}

// Done info
consola.success(chalk.green('Build succeed for ' + pkg.name))
for (const entry of ctx.buildEntries) {
Expand All @@ -104,6 +115,3 @@ export async function build (rootDir: string, stub: boolean) {
consola.log('')
}

function dumpObject (obj: Record<string, any>) {
return '{ ' + Object.keys(obj).map(key => `${key}: ${JSON.stringify(obj[key])}`).join(', ') + ' }'
}
3 changes: 1 addition & 2 deletions src/builder/mkdist.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { unlink, symlink } from 'fs/promises'
import { mkdist } from 'mkdist'
import { resolve, join } from 'upath'
import { symlink } from '../utils'
import type { BuildContext } from '../types'

export async function mkdistBuild (ctx: BuildContext) {
for (const entry of ctx.entries.filter(e => e.builder === 'mkdist')) {
if (ctx.stub) {
const srcDir = resolve(ctx.rootDir, entry.input)
const outDir = resolve(ctx.rootDir, ctx.outDir, entry.name)
await unlink(outDir).catch(() => { })
await symlink(srcDir, outDir)
} else {
const { writtenFiles } = await mkdist({
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fsp from 'fs/promises'

export async function symlink (from: string, to: string, force: boolean = true) {
if (force) {
await fsp.unlink(to).catch(() => { })
}
await fsp.symlink(from, to)
}

export function dumpObject (obj: Record<string, any>) {
return '{ ' + Object.keys(obj).map(key => `${key}: ${JSON.stringify(obj[key])}`).join(', ') + ' }'
}

0 comments on commit 5a1f9cd

Please sign in to comment.