From ad6047d6f6696dd3f88c632cd04d73b9f7e6e31e Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 24 Sep 2021 16:21:04 -0400 Subject: [PATCH] workflow: switch to pnpm (#5060) --- .github/workflows/ci.yml | 59 +++++++++++++------------------- .prettierignore | 3 +- package.json | 20 ++++++++--- packages/plugin-vue/package.json | 4 +-- pnpm-workspace.yaml | 3 ++ scripts/jestGlobalSetup.js | 12 ++++++- scripts/jestGlobalTeardown.js | 2 +- scripts/jestPerTestSetup.ts | 25 +++++--------- scripts/patchFileDeps.js | 30 ++++++++++++++++ scripts/preinstall.js | 7 ++++ scripts/release.js | 6 ++-- 11 files changed, 108 insertions(+), 63 deletions(-) create mode 100644 pnpm-workspace.yaml create mode 100644 scripts/patchFileDeps.js create mode 100644 scripts/preinstall.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b83701f3..ef4a33af 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,44 +33,34 @@ jobs: - name: Checkout uses: actions/checkout@v2 + - name: Install pnpm + uses: pnpm/action-setup@v2.0.1 + with: + version: 6.15.1 + - name: Set node version to ${{ matrix.node_version }} uses: actions/setup-node@v2 with: node-version: ${{ matrix.node_version }} + cache: "pnpm" - - name: Get yarn cache directory - id: yarn-cache - run: echo "::set-output name=dir::$(yarn cache dir)" - - - name: Set dependencies cache - uses: actions/cache@v2 - with: - path: ${{ steps.yarn-cache.outputs.dir }} - key: ${{ runner.os }}-${{ matrix.node_version }}-${{ hashFiles('yarn.lock') }} - restore-keys: | - ${{ runner.os }}-${{ matrix.node_version }}-${{ hashFiles('yarn.lock') }} - ${{ runner.os }}-${{ matrix.node_version }}- - - - name: Versions - run: yarn versions - - - name: Install dependencies - run: yarn install --frozen-lockfile + - name: Install deps + run: pnpm install - name: Build vite - run: yarn ci-build-vite + run: pnpm run ci-build-vite - name: Build plugin-vue - run: yarn build-plugin-vue + run: pnpm run build-plugin-vue - name: Build plugin-react - run: yarn build-plugin-react + run: pnpm run build-plugin-react - name: Test serve - run: yarn test-serve --runInBand + run: pnpm run test-serve -- --runInBand - name: Test build - run: yarn test-build --runInBand + run: pnpm run test-build -- --runInBand lint: runs-on: ubuntu-latest @@ -80,25 +70,24 @@ jobs: with: fetch-depth: 0 + - name: Install pnpm + uses: pnpm/action-setup@v2.0.1 + with: + version: 6.15.1 + - name: Set node version to 14 uses: actions/setup-node@v2 with: node-version: 14 + cache: "pnpm" - - name: Set dependencies cache - uses: actions/cache@v2 - with: - path: ~/.cache/yarn - key: lint-dependencies-${{ hashFiles('yarn.lock') }} - restore-keys: | - lint-dependencies-${{ hashFiles('yarn.lock') }} - lint-dependencies- + - name: Install deps + run: pnpm install - name: Prepare run: | - yarn install --frozen-lockfile - yarn ci-build-vite - yarn build-plugin-vue + pnpm run ci-build-vite + pnpm run build-plugin-vue - name: Lint - run: yarn lint + run: pnpm run lint diff --git a/.prettierignore b/.prettierignore index 102b615c..76d0e7f0 100644 --- a/.prettierignore +++ b/.prettierignore @@ -6,5 +6,6 @@ packages/plugin-vue/dist/ packages/*/CHANGELOG.md LICENSE.md .prettierignore -yarn.lock +pnpm-lock.yaml +pnpm-workspace.yaml packages/playground/tsconfig-json-load-error/has-error/tsconfig.json diff --git a/package.json b/package.json index 1919038f..aa00cb25 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "node": ">=12.0.0" }, "scripts": { + "preinstall": "node scripts/preinstall", "format": "prettier --write .", "lint": "eslint packages/*/{src,types}/**", "test": "run-s test-serve test-build", @@ -20,10 +21,10 @@ "build-docs": "vitepress build docs", "serve-docs": "vitepress serve docs", "build": "run-s build-vite build-plugin-vue build-plugin-react", - "build-vite": "cd packages/vite && yarn build", - "build-plugin-vue": "cd packages/plugin-vue && yarn build", - "build-plugin-react": "cd packages/plugin-react && yarn build", - "ci-build-vite": "cd packages/vite && yarn ci-build", + "build-vite": "cd packages/vite && npm run build", + "build-plugin-vue": "cd packages/plugin-vue && npm run build", + "build-plugin-react": "cd packages/plugin-react && npm run build", + "ci-build-vite": "cd packages/vite && npm run ci-build", "ci-docs": "run-s build-vite build-plugin-vue build-docs" }, "devDependencies": { @@ -57,7 +58,10 @@ "ts-node": "^10.1.0", "typescript": "~4.3.5", "vitepress": "^0.19.1", - "yorkie": "^2.0.0" + "yorkie": "^2.0.0", + "rollup": "^2.57.0", + "esbuild": "^0.13.2", + "vite": "workspace:*" }, "gitHooks": { "pre-commit": "lint-staged --concurrent false", @@ -73,5 +77,11 @@ "packages/**/*.d.ts": [ "eslint --ext .ts" ] + }, + "pnpm": { + "overrides": { + "vite": "workspace:*", + "@vitejs/plugin-vue": "workspace:*" + } } } diff --git a/packages/plugin-vue/package.json b/packages/plugin-vue/package.json index b1c7db79..e8c64779 100644 --- a/packages/plugin-vue/package.json +++ b/packages/plugin-vue/package.json @@ -39,7 +39,7 @@ "rollup": "^2.57.0", "slash": "^3.0.0", "source-map": "^0.6.1", - "vue": "^3.2.14", - "@vue/compiler-sfc": "^3.2.14" + "vue": "^3.2.16", + "@vue/compiler-sfc": "^3.2.16" } } diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml new file mode 100644 index 00000000..f8a7dd53 --- /dev/null +++ b/pnpm-workspace.yaml @@ -0,0 +1,3 @@ +packages: + - 'packages/*' + - 'packages/playground/**' diff --git a/scripts/jestGlobalSetup.js b/scripts/jestGlobalSetup.js index be126d7d..9640f70c 100644 --- a/scripts/jestGlobalSetup.js +++ b/scripts/jestGlobalSetup.js @@ -1,3 +1,4 @@ +// @ts-check const os = require('os') const fs = require('fs-extra') const path = require('path') @@ -17,5 +18,14 @@ module.exports = async () => { await fs.mkdirp(DIR) await fs.writeFile(path.join(DIR, 'wsEndpoint'), browserServer.wsEndpoint()) - await fs.remove(path.resolve(__dirname, '../temp')) + + const tempDir = path.resolve(__dirname, '../packages/temp') + await fs.remove(tempDir) + await fs.copy(path.resolve(__dirname, '../packages/playground'), tempDir, { + dereference: false, + filter(file) { + file = file.replace(/\\/g, '/') + return !file.includes('__tests__') && !file.match(/dist(\/|$)/) + } + }) } diff --git a/scripts/jestGlobalTeardown.js b/scripts/jestGlobalTeardown.js index 5f1cd3f2..583aa1d1 100644 --- a/scripts/jestGlobalTeardown.js +++ b/scripts/jestGlobalTeardown.js @@ -4,6 +4,6 @@ const path = require('path') module.exports = async () => { await global.__BROWSER_SERVER__.close() if (!process.env.VITE_PRESERVE_BUILD_ARTIFACTS) { - await fs.remove(path.resolve(__dirname, '../temp')) + await fs.remove(path.resolve(__dirname, '../packages/temp')) } } diff --git a/scripts/jestPerTestSetup.ts b/scripts/jestPerTestSetup.ts index f2abe872..a21df143 100644 --- a/scripts/jestPerTestSetup.ts +++ b/scripts/jestPerTestSetup.ts @@ -62,19 +62,7 @@ beforeAll(async () => { // start a vite server in that directory. if (testName) { const playgroundRoot = resolve(__dirname, '../packages/playground') - const srcDir = resolve(playgroundRoot, testName) - tempDir = resolve(__dirname, '../temp', testName) - await fs.copy(srcDir, tempDir, { - dereference: true, - filter(file) { - file = slash(file) - return ( - !file.includes('__tests__') && - !file.includes('node_modules') && - !file.match(/dist(\/|$)/) - ) - } - }) + tempDir = resolve(__dirname, '../packages/temp/', testName) // when `root` dir is present, use it as vite's root let testCustomRoot = resolve(tempDir, 'root') @@ -83,9 +71,14 @@ beforeAll(async () => { const testCustomServe = resolve(dirname(testPath), 'serve.js') if (fs.existsSync(testCustomServe)) { // test has custom server configuration. - const { serve } = require(testCustomServe) - server = await serve(rootDir, isBuildTest) - return + const { serve, preServe } = require(testCustomServe) + if (preServe) { + await preServe(rootDir, isBuildTest) + } + if (serve) { + server = await serve(rootDir, isBuildTest) + return + } } const options: UserConfig = { diff --git a/scripts/patchFileDeps.js b/scripts/patchFileDeps.js new file mode 100644 index 00000000..dbb42caa --- /dev/null +++ b/scripts/patchFileDeps.js @@ -0,0 +1,30 @@ +// pnpm treats file: protocols as link:, so it doesn't copy the actual files +// into node_modules, causing Vite to still consider those deps linked. +// This script is called from postinstall hooks in playground packages that +// uses the file: protocol, and copies the file: deps into node_modules. + +const fs = require('fs-extra') +const path = require('path') +const root = process.cwd() +const pkg = require(path.join(root, 'package.json')) + +let hasPatched +for (const [key, val] of Object.entries(pkg.dependencies)) { + if (val.startsWith('file:')) { + hasPatched = true + const src = path.resolve(root, val.slice('file:'.length)) + const dest = path.resolve(root, 'node_modules', key) + fs.removeSync(dest) + fs.copySync(src, dest, { + dereference: true + }) + console.log(`patched ${val}`) + } +} + +if (hasPatched) { + // remove node_modules/.ignored as pnpm will think our patched files are + // installed by another package manager and move them into this directory. + // On further installs it will error out if this directory is not empty. + fs.removeSync(path.resolve(root, 'node_modules', '.ignored')) +} diff --git a/scripts/preinstall.js b/scripts/preinstall.js new file mode 100644 index 00000000..a1269cc0 --- /dev/null +++ b/scripts/preinstall.js @@ -0,0 +1,7 @@ +if (!/pnpm/.test(process.env.npm_execpath || '')) { + console.warn( + `\u001b[33mThis repository requires using pnpm as the package manager ` + + ` for scripts to work properly.\u001b[39m\n` + ) + process.exit(1) +} diff --git a/scripts/release.js b/scripts/release.js index 61c0fc70..95ce8a5a 100644 --- a/scripts/release.js +++ b/scripts/release.js @@ -141,13 +141,13 @@ async function main() { step('\nBuilding package...') if (!skipBuild && !isDryRun) { - await run('yarn', ['build']) + await run('pnpm', ['run', 'build']) } else { console.log(`(skipped)`) } step('\nGenerating changelog...') - await run('yarn', ['changelog']) + await run('pnpm', ['run', 'changelog']) const { stdout } = await run('git', ['diff'], { stdio: 'pipe' }) if (stdout) { @@ -199,6 +199,8 @@ async function publishPackage(version, runIfNotDry) { publicArgs.push(`--tag`, args.tag) } try { + // important: we still use Yarn 1 to publish since we rely on its specific + // behavior await runIfNotDry('yarn', publicArgs, { stdio: 'pipe' })