Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a monorepo (project references) support for via the --build flag #19

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
matrix:
node-version: [18.x, 20.x, 21.x]
os: [ubuntu-latest, windows-latest]
exclude:
- os: windows-latest
node-version: 21.x
steps:
- uses: actions/checkout@v4

Expand Down
35 changes: 35 additions & 0 deletions fixtures/monorepo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions fixtures/monorepo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "project-build",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"workspaces": [
"package*"
],
"license": "ISC"
}
13 changes: 13 additions & 0 deletions fixtures/monorepo/package1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "package1",
"version": "1.0.0",
"description": "",
"main": "dist/src/lib/add.js",
"types": "dist/src/lib/add.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
4 changes: 4 additions & 0 deletions fixtures/monorepo/package1/src/lib/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function add (x: number, y: number): number {
return x + y
}
7 changes: 7 additions & 0 deletions fixtures/monorepo/package1/src/test/add.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('add', () => {
strictEqual(add(1, 2), 3)
})
7 changes: 7 additions & 0 deletions fixtures/monorepo/package1/src/test/add2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('add2', () => {
strictEqual(add(3, 2), 5)
})
10 changes: 10 additions & 0 deletions fixtures/monorepo/package1/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
},
"references": [
]
}

15 changes: 15 additions & 0 deletions fixtures/monorepo/package2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "package2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"package1": "file:../package1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
4 changes: 4 additions & 0 deletions fixtures/monorepo/package2/src/lib/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { add as add2 } from 'package1'
export function add (x: number, y: number): number {
return add2(x, y)
}
7 changes: 7 additions & 0 deletions fixtures/monorepo/package2/src/test/add.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('package2-add', () => {
strictEqual(add(1, 2), 3)
})
7 changes: 7 additions & 0 deletions fixtures/monorepo/package2/src/test/add2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('package2-add2', () => {
strictEqual(add(3, 2), 5)
})
11 changes: 11 additions & 0 deletions fixtures/monorepo/package2/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
},
"references": [
{ "path": "../package1" }
]
}

24 changes: 24 additions & 0 deletions fixtures/monorepo/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"sourceMap": true,
"target": "ES2022",
"composite": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"strict": true,
"resolveJsonModule": true,
"removeComments": true,
"newLine": "lf",
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
"isolatedModules": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"lib": [
"ESNext"
],
"incremental": true
}
}
7 changes: 7 additions & 0 deletions fixtures/monorepo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": [],
"references": [
{ "path": "package1" },
{ "path": "package2" }
]
}
23 changes: 16 additions & 7 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export default async function runWithTypeScript (config) {

let prefix = ''
let tscPath
const typescriptCliArgs = []

if (tsconfigPath && config.typescript !== false) {
const tsconfig = JSON.parse(await readFile(tsconfigPath))
const _require = createRequire(tsconfigPath)
const typescriptPathCWD = _require.resolve('typescript')
tscPath = join(typescriptPathCWD, '..', '..', 'bin', 'tsc')
Expand All @@ -38,19 +40,22 @@ export default async function runWithTypeScript (config) {

// Watch is handled aftterwards
if (!config.watch) {
if (Array.isArray(tsconfig.references) && tsconfig.references.length > 0) {
typescriptCliArgs.push('--build')
}
const start = Date.now()
await execa('node', [tscPath], { cwd: dirname(tsconfigPath) })
await execa('node', [tscPath, ...typescriptCliArgs], { cwd: dirname(tsconfigPath) })
process.stdout.write(`TypeScript compilation complete (${Date.now() - start}ms)\n`)
pushable.push({
type: 'test:diagnostic',
data: {
nesting: 0,
message: `TypeScript compilation complete (${Date.now() - start}ms)`
message: `TypeScript compilation complete (${Date.now() - start}ms)`,
typescriptCliArgs
}
})
}
}
const tsconfig = JSON.parse(await readFile(tsconfigPath))
const outDir = tsconfig.compilerOptions.outDir
if (outDir) {
prefix = join(dirname(tsconfigPath), outDir)
Expand Down Expand Up @@ -83,17 +88,21 @@ export default async function runWithTypeScript (config) {
let p

if (config.watch) {
typescriptCliArgs.push('--watch')
p = deferred()
const start = Date.now()
tscChild = execa('node', [tscPath, '--watch'], { cwd })
let start = Date.now()
tscChild = execa('node', [tscPath, ...typescriptCliArgs], { cwd })
tscChild.stdout.setEncoding('utf8')
tscChild.stdout.on('data', (data) => {
if (data.includes('Watching for file changes')) {
if (data.includes('File change detected')) {
start = Date.now()
} else if (data.includes('Watching for file changes')) {
pushable.push({
type: 'test:diagnostic',
data: {
nesting: 0,
message: `TypeScript compilation complete (${Date.now() - start}ms)`
message: `TypeScript compilation complete (${Date.now() - start}ms)`,
typescriptCliArgs
}
})

Expand Down
25 changes: 25 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { test } from 'node:test'
import { tspl } from '@matteo.collina/tspl'
import runWithTypeScript from '../lib/run.js'
import { join } from 'desm'
import { execa } from 'execa'

test('ts-esm', async (t) => {
const { strictEqual, completed, match } = tspl(t, { plan: 4 })
Expand Down Expand Up @@ -141,7 +142,31 @@ test('src-to-dist', async (t) => {

await completed
})
test('monorepo', async (t) => {
const { strictEqual, completed, match, deepEqual } = tspl(t, { plan: 5 })
const config = {
files: [],
cwd: join(import.meta.url, '..', 'fixtures', 'monorepo/package2')
}

await execa('npm', ['install'], { cwd: join(import.meta.url, '..', 'fixtures', 'monorepo') })
const stream = await runWithTypeScript(config)

const names = new Set(['package2-add', 'package2-add2'])

stream.once('data', (test) => {
strictEqual(test.type, 'test:diagnostic')
match(test.data.message, /TypeScript compilation complete \(\d+ms\)/)
deepEqual(test.data.typescriptCliArgs, ['--build'])
})

stream.on('test:pass', (test) => {
strictEqual(names.has(test.name), true)
names.delete(test.name)
})

await completed
})
test('only-src', async (t) => {
const { strictEqual, completed, match } = tspl(t, { plan: 4 })
const config = {
Expand Down
Loading