Skip to content

Commit

Permalink
fix: kill worker in build mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 28, 2021
1 parent b4a221f commit e7810f0
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 22 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,4 @@ dist

#
lib/
temp/
coverage/
temp/
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"vetur.experimental.templateInterpolationService": true
"vetur.experimental.templateInterpolationService": true,
"typescript.tsdk": "node_modules/typescript/lib"
}
10 changes: 8 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
module.exports = {
// @ts-check

/** @type {import("@jest/types").Config.InitialOptions} */
const config = {
preset: 'ts-jest',
testMatch: ['**/*.spec.[jt]s?(x)'],
globalSetup: './scripts/jestGlobalSetup.js',
globalTeardown: './scripts/jestGlobalTeardown.js',
setupFilesAfterEnv: ['./scripts/jestPerTestSetup.ts'],
testTimeout: process.env.CI ? 60000 : 60000,
collectCoverage: true,
collectCoverage: false,
collectCoverageFrom: ['packages/*/src/**/*.ts'],
detectOpenHandles: true,
transform: {
'^.+\\.ts$': 'ts-jest',
},
}

module.exports = config
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"dev": "pnpm -r --filter ./packages --parallel run dev",
"build": "pnpm -r --filter ./packages run build",
"lint": "eslint --ext .js,.ts packages/*/src/**",
"test": "jest --runInBand --coverage=false",
"test:coverage": "jest --runInBand",
"test": "jest --runInBand",
"test:coverage": "jest --runInBand --coverage=true",
"changelog": "pnpm -r --filter ./packages run changelog",
"test:watch": "jest -w",
"type-check": "pnpm -r --parallel --filter ./packages exec -- tsc --noEmit -p tsconfig.build.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ export async function viteBuild({
}
}

export async function postTest() {
try {
// await fs.remove(tempDir)
} catch (e) {}
}
export function postTest() {}

export async function startServer(isBuild: boolean) {
// start dev server
Expand Down
4 changes: 3 additions & 1 deletion packages/vite-plugin-checker/__tests__/e2e/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import os from 'os'
import CheckerPlugin from '../../lib/main'
import { VlsChecker } from '../../../checker-vls/lib/main'
import { MockSandbox } from './MockSandbox/MockSandbox'
import { sleep, WORKER_CLEAN_TIMEOUT } from './testUtils'

// ref https://github.com/facebook/jest/issues/936#issuecomment-613220940
jest.mock('child_process', () => {
Expand Down Expand Up @@ -34,8 +35,9 @@ describe('build', () => {
jest.clearAllMocks()
})

afterAll(() => {
afterAll(async () => {
jest.restoreAllMocks()
await sleep(WORKER_CLEAN_TIMEOUT)
})

it('typescript', () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/vite-plugin-checker/__tests__/e2e/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export function slash(p: string): string {
return p.replace(/\\/g, '/')
}

export const WORKER_CLEAN_TIMEOUT = process.env.CI ? 6000 : 3000
export const testPath = expect.getState().testPath
export const testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1]
assert(testName, `should detect testName, but got null`)
export const testDir = path.resolve(process.env.JEST_ROOT_DIR!, `./temp/${testName}`)

export function editFile(
Expand All @@ -28,3 +28,11 @@ export function expectStdoutNotContains(str: string, unexpectedErrorMsg: string)
stdout: expect(str).not.toContain(unexpectedErrorMsg),
})
}

export async function sleep(millSeconds: number, callback?: Function) {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve()
}, millSeconds)
}).then(() => callback?.())
}
11 changes: 10 additions & 1 deletion packages/vite-plugin-checker/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConfigEnv } from 'vite'
import { parentPort, Worker, workerData } from 'worker_threads'
import invariant from 'tiny-invariant'

import { ACTION_TYPES } from './types'

Expand Down Expand Up @@ -30,15 +31,19 @@ export function createScript<T>({
buildBin,
serverChecker,
}: WorkerScriptOptions): Script<T> {
// let _env: ConfigEnv | undefined
type CheckerConfig = T & SharedConfig
let _env: ConfigEnv
return {
mainScript: () => {
// initialized in main thread
const createWorker = (
checkerConfig: CheckerConfig,
env: ConfigEnv
): ConfigureServeChecker => {
_env = env
const isBuild = env.command === 'build'

const worker = new Worker(absFilename, {
workerData: { env, checkerConfig },
})
Expand Down Expand Up @@ -73,14 +78,14 @@ export function createScript<T>({
if (!parentPort) throw Error('should have parentPort as file runs in worker thread')
const isBuild = workerData.env.command === 'build'
// only run bin command and do not listen message in build mode
if (isBuild) return

const port = parentPort.on(
'message',
(action: ConfigAction | ConfigureServerAction | UnrefAction) => {
switch (action.type) {
case ACTION_TYPES.config: {
const checkerConfig: T = workerData.checkerConfig
invariant(_env, 'env should be initialized in mainScript, but got undefined')
diagnostic = serverChecker.createDiagnostic(checkerConfig)
diagnostic.config(action.payload)
break
Expand All @@ -96,6 +101,10 @@ export function createScript<T>({
}
}
)

if (isBuild) {
port.unref()
}
},
}
}
11 changes: 9 additions & 2 deletions playground/react-ts/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import {
postTest,
viteBuild,
} from '../../../packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox'
import { editFile, testDir } from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils'
import {
editFile,
testDir,
sleep,
WORKER_CLEAN_TIMEOUT,
} from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils'

beforeAll(async () => {
await preTest()
})

afterAll(postTest)
afterAll(async () => {
await sleep(WORKER_CLEAN_TIMEOUT)
})

describe('typescript', () => {
// describe('dev', () => {
Expand Down
11 changes: 9 additions & 2 deletions playground/vue2-vls/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import {
postTest,
viteBuild,
} from '../../../packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox'
import { testDir, editFile } from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils'
import {
testDir,
editFile,
sleep,
WORKER_CLEAN_TIMEOUT,
} from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils'

beforeAll(async () => {
await preTest()
})

afterAll(postTest)
afterAll(async () => {
await sleep(WORKER_CLEAN_TIMEOUT)
})

describe('vue2-vls', () => {
// describe('dev', () => {
Expand Down
13 changes: 10 additions & 3 deletions playground/vue3-vue-tsc/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import {
preTest,
postTest,
preTest,
viteBuild,
} from '../../../packages/vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox'
import { testDir, editFile } from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils'
import {
editFile,
sleep,
testDir,
WORKER_CLEAN_TIMEOUT,
} from '../../../packages/vite-plugin-checker/__tests__/e2e/testUtils'

beforeAll(async () => {
await preTest()
})

afterAll(postTest)
afterAll(async () => {
await sleep(WORKER_CLEAN_TIMEOUT)
})

describe('vue3-vue-tsc', () => {
// describe('dev', () => {
Expand Down

0 comments on commit e7810f0

Please sign in to comment.