From 155d77153f3fc589180ae17d51872a941fd644b9 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 18 Feb 2025 05:59:20 +0100 Subject: [PATCH] build!: drop support for `cjs` usage (#452) --- .github/workflows/ci.yml | 12 +---- package.json | 2 - packages/vite-plugin-checker/package.json | 17 +++---- .../vite-plugin-checker/src/client/index.ts | 5 +-- packages/vite-plugin-checker/src/main.ts | 6 +-- packages/vite-plugin-checker/tsup.config.ts | 45 +++++-------------- playground/vitestGlobalSetup.ts | 10 ----- pnpm-lock.yaml | 42 +++++++---------- scripts/patchCjs.mjs | 14 ------ 9 files changed, 41 insertions(+), 112 deletions(-) delete mode 100644 scripts/patchCjs.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cdf10751..f679ddf8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -113,16 +113,8 @@ jobs: - name: Test unit run: pnpm run test-unit - - name: Test serve (ESM) + - name: Test serve run: pnpm run test-serve - - name: Test build (ESM) + - name: Test build run: pnpm run test-build - - - name: Test serve (CJS) - if: ${{ matrix.node_version == 18 && matrix.os == 'ubuntu-latest' }} - run: pnpm run test-serve-cjs - - - name: Test build (CJS) - if: ${{ matrix.node_version == 18 && matrix.os == 'ubuntu-latest' }} - run: pnpm run test-build-cjs diff --git a/package.json b/package.json index 131fbf3f..6c769af6 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,8 @@ "sort-package-json": "npx sort-package-json \"packages/*/package.json\"", "test": "pnpm test-unit && pnpm test-serve && pnpm test-build", "test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts", - "test-build-cjs": "VITEST_TEST_CJS=1 pnpm run test-build", "test-knip": "knip", "test-serve": "vitest run -c vitest.config.e2e.ts", - "test-serve-cjs": "VITEST_TEST_CJS=1 pnpm run test-serve", "test-unit": "vitest run", "test-unit:watch": "vitest", "type-check": "pnpm -r --parallel --filter \"vite-plugin-checker\" exec tsc --noEmit" diff --git a/packages/vite-plugin-checker/package.json b/packages/vite-plugin-checker/package.json index 2ab466eb..98268625 100644 --- a/packages/vite-plugin-checker/package.json +++ b/packages/vite-plugin-checker/package.json @@ -17,19 +17,16 @@ "author": "fi3ework ", "type": "module", "exports": { - ".": { - "import": "./dist/esm/main.js", - "require": "./dist/cjs/main.cjs" - } + ".": "./dist/main.js" }, - "main": "./dist/cjs/main.cjs", - "types": "./dist/cjs/main.d.cts", + "main": "./dist/main.js", + "types": "./dist/main.d.ts", "files": [ "dist", "!dist/checkers/vueTsc/typescript-vue-tsc" ], "scripts": { - "build": "tsup && node ../../scripts/patchCjs.mjs", + "build": "tsup", "build:test": "tsup --sourcemap inline", "clean": "rimraf dist", "dev": "tsup --watch" @@ -38,10 +35,10 @@ "@babel/code-frame": "^7.26.2", "chokidar": "^3.6.0", "colorette": "^2.0.20", - "npm-run-path": "^4.0.1", - "strip-ansi": "^6.0.1", + "npm-run-path": "^6.0.0", + "strip-ansi": "^7.1.0", "tiny-invariant": "^1.3.3", - "tinyglobby": "^0.2.10", + "tinyglobby": "^0.2.11", "vscode-uri": "^3.1.0" }, "devDependencies": { diff --git a/packages/vite-plugin-checker/src/client/index.ts b/packages/vite-plugin-checker/src/client/index.ts index 0a6f419b..eac3b4aa 100644 --- a/packages/vite-plugin-checker/src/client/index.ts +++ b/packages/vite-plugin-checker/src/client/index.ts @@ -28,8 +28,5 @@ export const WS_CHECKER_ERROR_EVENT = 'vite-plugin-checker:error' export const WS_CHECKER_RECONNECT_EVENT = 'vite-plugin-checker:reconnect' // #endregion -export const runtimeSourceFilePath = import.meta.url.endsWith('.ts') - ? // for development only, maybe should use NODE_ENV to distinguish - _require.resolve('../@runtime/main.js') - : _require.resolve('../../@runtime/main.js') +export const runtimeSourceFilePath = _require.resolve('../@runtime/main.js') export const runtimeCode = `${fs.readFileSync(runtimeSourceFilePath, 'utf-8')};` diff --git a/packages/vite-plugin-checker/src/main.ts b/packages/vite-plugin-checker/src/main.ts index 2a456c34..79c78ec4 100644 --- a/packages/vite-plugin-checker/src/main.ts +++ b/packages/vite-plugin-checker/src/main.ts @@ -1,6 +1,6 @@ import { spawn } from 'node:child_process' import * as colors from 'colorette' -import npmRunPath from 'npm-run-path' +import { type ProcessEnv, npmRunPathEnv } from 'npm-run-path' import type { ConfigEnv, Logger, Plugin } from 'vite' import { Checker } from './Checker.js' @@ -154,7 +154,7 @@ export function checker(userConfig: UserPluginConfig): Plugin { // run a bin command in a separated process if (!isProduction || !enableBuild) return - const localEnv = npmRunPath.env({ + const localEnv = npmRunPathEnv({ env: process.env, cwd: process.cwd(), execPath: process.execPath, @@ -233,7 +233,7 @@ export function checker(userConfig: UserPluginConfig): Plugin { function spawnChecker( checker: ServeAndBuildChecker, userConfig: Partial, - localEnv: npmRunPath.ProcessEnv, + localEnv: ProcessEnv, ) { return new Promise((resolve) => { const buildBin = checker.build.buildBin diff --git a/packages/vite-plugin-checker/tsup.config.ts b/packages/vite-plugin-checker/tsup.config.ts index ad5d9a67..e543c580 100644 --- a/packages/vite-plugin-checker/tsup.config.ts +++ b/packages/vite-plugin-checker/tsup.config.ts @@ -1,7 +1,15 @@ import { copyFile } from 'node:fs/promises' -import { type Options, defineConfig } from 'tsup' +import { defineConfig } from 'tsup' -const shared: Options = { +export default defineConfig({ + format: ['esm'], + outDir: 'dist', + async onSuccess() { + await copyFile( + 'src/checkers/vueTsc/languagePlugins.cjs', + 'dist/checkers/vueTsc/languagePlugins.cjs', + ) + }, entry: ['src', '!src/checkers/vueTsc/languagePlugins.cjs'], splitting: false, bundle: false, @@ -10,35 +18,4 @@ const shared: Options = { target: 'node14', platform: 'node', dts: true, -} - -export default defineConfig([ - { - format: ['cjs'], - outDir: 'dist/cjs', - shims: true, - outExtension() { - return { - js: '.cjs', - } - }, - async onSuccess() { - await copyFile( - 'src/checkers/vueTsc/languagePlugins.cjs', - 'dist/cjs/checkers/vueTsc/languagePlugins.cjs', - ) - }, - ...shared, - }, - { - format: ['esm'], - outDir: 'dist/esm', - async onSuccess() { - await copyFile( - 'src/checkers/vueTsc/languagePlugins.cjs', - 'dist/esm/checkers/vueTsc/languagePlugins.cjs', - ) - }, - ...shared, - }, -]) +}) diff --git a/playground/vitestGlobalSetup.ts b/playground/vitestGlobalSetup.ts index 86872ea0..ee00921b 100644 --- a/playground/vitestGlobalSetup.ts +++ b/playground/vitestGlobalSetup.ts @@ -2,7 +2,6 @@ import { rmSync } from 'node:fs' import fs from 'node:fs/promises' import os from 'node:os' import path from 'node:path' -import { glob } from 'tinyglobby' import { chromium } from 'playwright-chromium' import type { BrowserServer } from 'playwright-chromium' @@ -40,15 +39,6 @@ export async function setup(): Promise { throw error } }) - - if (process.env['VITEST_TEST_CJS']) { - const packageJsons = await glob(`${tempDir}/**/package.json`) - for (const packageJson of packageJsons) { - const packageJsonContents = await fs.readFile(packageJson, 'utf-8').then(r => JSON.parse(r.toString())) - delete packageJsonContents['module'] - await fs.writeFile(packageJson, JSON.stringify(packageJsonContents, null, 2) + '\n') - } - } } export async function teardown(): Promise { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 94e217fd..812fc69e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -67,7 +67,7 @@ importers: version: 1.3.3 tinyglobby: specifier: ^0.2.10 - version: 0.2.10 + version: 0.2.11 typescript: specifier: ^5.7.3 version: 5.7.3 @@ -111,17 +111,17 @@ importers: specifier: ^2.0.20 version: 2.0.20 npm-run-path: - specifier: ^4.0.1 - version: 4.0.1 + specifier: ^6.0.0 + version: 6.0.0 strip-ansi: - specifier: ^6.0.1 - version: 6.0.1 + specifier: ^7.1.0 + version: 7.1.0 tiny-invariant: specifier: ^1.3.3 version: 1.3.3 tinyglobby: - specifier: ^0.2.10 - version: 0.2.10 + specifier: ^0.2.11 + version: 0.2.11 vscode-uri: specifier: ^3.1.0 version: 3.1.0 @@ -3217,8 +3217,8 @@ packages: fastq@1.17.1: resolution: {integrity: sha512-sRVD3lWVIXWg6By68ZN7vho9a1pQcN/WBFaAAsDDFzlJjvoGx0P8z7V1t72grFJfJhu3YPZBuu25f7Kaw2jN1w==} - fdir@6.4.2: - resolution: {integrity: sha512-KnhMXsKSPZlAhp7+IjUkRZKPb4fUyccpDrdFXbi4QL1qkmFh9kVY09Yox+n4MaOb3lHZ1Tv829C3oaaXoMYPDQ==} + fdir@6.4.3: + resolution: {integrity: sha512-PMXmW2y1hDDfTSRc9gaXIuCCRpuoz3Kaz8cUelp3smouvfT632ozg2vrT6lJsHKKOF59YLbOGfAWGUcKEfRMQw==} peerDependencies: picomatch: ^3 || ^4 peerDependenciesMeta: @@ -3893,10 +3893,6 @@ packages: resolution: {integrity: sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==} engines: {node: '>=0.10.0'} - npm-run-path@4.0.1: - resolution: {integrity: sha512-S48WzZW777zhNIrn7gxOlISNAqi9ZC/uQFnRdbeIHhZhCA6UqpkOT8T1G7BvfdgP4Er8gF4sUbaS0i7QvIfCWw==} - engines: {node: '>=8'} - npm-run-path@5.3.0: resolution: {integrity: sha512-ppwTtiJZq0O/ai0z7yfudtBpWIoxM8yE6nHi1X47eFR2EWORqfbu6CnPlNsjeN683eT0qG6H/Pyf9fCcvjnnnQ==} engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} @@ -4544,8 +4540,8 @@ packages: tinyexec@0.3.2: resolution: {integrity: sha512-KQQR9yN7R5+OSwaK0XQoj22pwHoTlgYqmUscPYoknOoWCWfj/5/ABTMRi69FrKU5ffPVh5QcFikpWJI/P1ocHA==} - tinyglobby@0.2.10: - resolution: {integrity: sha512-Zc+8eJlFMvgatPZTl6A9L/yht8QqdmUNtURHaKZLmKBE12hNPSrqNkUp2cs3M/UKmNVVAMFQYSjYIVHDjW5zew==} + tinyglobby@0.2.11: + resolution: {integrity: sha512-32TmKeeKUahv0Go8WmQgiEp9Y21NuxjwjqiRC1nrUB51YacfSwuB44xgXD+HdIppmMRgjQNPdrHyA6vIybYZ+g==} engines: {node: '>=12.0.0'} tinypool@1.0.2: @@ -7500,7 +7496,7 @@ snapshots: dependencies: reusify: 1.0.4 - fdir@6.4.2(picomatch@4.0.2): + fdir@6.4.3(picomatch@4.0.2): optionalDependencies: picomatch: 4.0.2 @@ -8133,10 +8129,6 @@ snapshots: normalize-path@3.0.0: {} - npm-run-path@4.0.1: - dependencies: - path-key: 3.1.1 - npm-run-path@5.3.0: dependencies: path-key: 4.0.0 @@ -8269,7 +8261,7 @@ snapshots: isbinaryfile: 5.0.4 pkg-types: 1.3.1 query-registry: 3.0.1 - tinyglobby: 0.2.10 + tinyglobby: 0.2.11 pkg-types@1.3.1: dependencies: @@ -8626,7 +8618,7 @@ snapshots: is-plain-obj: 4.1.0 semver: 7.6.2 sort-object-keys: 1.1.3 - tinyglobby: 0.2.10 + tinyglobby: 0.2.11 source-map-js@1.2.1: {} @@ -8809,9 +8801,9 @@ snapshots: tinyexec@0.3.2: {} - tinyglobby@0.2.10: + tinyglobby@0.2.11: dependencies: - fdir: 6.4.2(picomatch@4.0.2) + fdir: 6.4.3(picomatch@4.0.2) picomatch: 4.0.2 tinypool@1.0.2: {} @@ -8871,7 +8863,7 @@ snapshots: source-map: 0.8.0-beta.0 sucrase: 3.35.0 tinyexec: 0.3.2 - tinyglobby: 0.2.10 + tinyglobby: 0.2.11 tree-kill: 1.2.2 optionalDependencies: postcss: 8.5.1 diff --git a/scripts/patchCjs.mjs b/scripts/patchCjs.mjs deleted file mode 100644 index f41a575b..00000000 --- a/scripts/patchCjs.mjs +++ /dev/null @@ -1,14 +0,0 @@ -import fs from 'node:fs/promises' -import path from 'node:path' -import { fileURLToPath } from 'node:url' - -const cjsDist = fileURLToPath(new URL('../packages/vite-plugin-checker/dist/cjs', import.meta.url)) - -async function main() { - await fs.writeFile( - path.join(cjsDist, 'package.json'), - JSON.stringify({ type: 'commonjs' }, null, 2), - ) -} - -main()