From 1bb3a2e8a5017892913e075016f2ac887f3b5464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Allen=20Zhang=20=28=E5=BC=A0=E6=B6=9B=29?= Date: Fri, 18 Oct 2024 18:56:59 +0800 Subject: [PATCH] feat: update README --- .../workflows/publish-swc-plugin-canyon.yml | 22 +++ plugins/swc-plugin-canyon/.editorconfig | 11 ++ plugins/swc-plugin-canyon/.gitignore | 131 ++++++++++++++++++ plugins/swc-plugin-canyon/build.config.ts | 13 ++ plugins/swc-plugin-canyon/package.json | 42 ++++++ plugins/swc-plugin-canyon/src/index.ts | 66 +++++++++ plugins/swc-plugin-canyon/tsconfig.json | 18 +++ 7 files changed, 303 insertions(+) create mode 100644 .github/workflows/publish-swc-plugin-canyon.yml create mode 100755 plugins/swc-plugin-canyon/.editorconfig create mode 100644 plugins/swc-plugin-canyon/.gitignore create mode 100644 plugins/swc-plugin-canyon/build.config.ts create mode 100644 plugins/swc-plugin-canyon/package.json create mode 100644 plugins/swc-plugin-canyon/src/index.ts create mode 100644 plugins/swc-plugin-canyon/tsconfig.json diff --git a/.github/workflows/publish-swc-plugin-canyon.yml b/.github/workflows/publish-swc-plugin-canyon.yml new file mode 100644 index 00000000..0505eb75 --- /dev/null +++ b/.github/workflows/publish-swc-plugin-canyon.yml @@ -0,0 +1,22 @@ +name: Publish swc-plugin-canyon + +on: + push: + branches: [ "*" ] + +jobs: + publish-npm: + runs-on: ubuntu-latest + steps: + - uses: pnpm/action-setup@v4 + with: + version: 9 + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 20 + registry-url: https://registry.npmjs.org/ + - run: pnpm install + - run: pnpm --filter=swc-plugin-canyon publish -f --no-git-checks --access=public --filter + env: + NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}} diff --git a/plugins/swc-plugin-canyon/.editorconfig b/plugins/swc-plugin-canyon/.editorconfig new file mode 100755 index 00000000..a1c0c006 --- /dev/null +++ b/plugins/swc-plugin-canyon/.editorconfig @@ -0,0 +1,11 @@ +# EditorConfig is awesome: https://EditorConfig.org + +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/plugins/swc-plugin-canyon/.gitignore b/plugins/swc-plugin-canyon/.gitignore new file mode 100644 index 00000000..35060845 --- /dev/null +++ b/plugins/swc-plugin-canyon/.gitignore @@ -0,0 +1,131 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +demo/ + +.idea/ + +pnpm-lock.yaml diff --git a/plugins/swc-plugin-canyon/build.config.ts b/plugins/swc-plugin-canyon/build.config.ts new file mode 100644 index 00000000..cd9dfa8e --- /dev/null +++ b/plugins/swc-plugin-canyon/build.config.ts @@ -0,0 +1,13 @@ +import { defineBuildConfig } from 'unbuild'; + +export default defineBuildConfig({ + entries: [ + 'src/index', + ], + clean: true, + declaration: true, + rollup: { + emitCJS: true, + inlineDependencies: true, + }, +}); diff --git a/plugins/swc-plugin-canyon/package.json b/plugins/swc-plugin-canyon/package.json new file mode 100644 index 00000000..65541495 --- /dev/null +++ b/plugins/swc-plugin-canyon/package.json @@ -0,0 +1,42 @@ +{ + "name": "swc-plugin-canyon", + "version": "0.0.1-alpha.1", + "license": "MIT", + "files": [ + "dist/*" + ], + "main": "./dist/index.cjs", + "module": "./dist/index.mjs", + "types": "./dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.mjs", + "require": "./dist/index.cjs" + } + }, + "scripts": { + "dev": "unbuild --stub", + "build": "unbuild", + "prepublishOnly": "npm run build" + }, + "keywords": [ + "vite", + "babel", + "plugin", + "istanbul", + "nyc", + "canyon" + ], + "dependencies": { + "picocolors": "^1.0.0" + }, + "peerDependencies": { + "vite": ">=2.9.1" + }, + "devDependencies": { + "@types/node": "^20.10.6", + "typescript": "^5.3.3", + "unbuild": "^2.0.0" + } +} diff --git a/plugins/swc-plugin-canyon/src/index.ts b/plugins/swc-plugin-canyon/src/index.ts new file mode 100644 index 00000000..ed626fc7 --- /dev/null +++ b/plugins/swc-plugin-canyon/src/index.ts @@ -0,0 +1,66 @@ +import {Plugin, createLogger} from 'vite'; +import picocolors from 'picocolors'; + +const {green} = picocolors; + +export interface canyonPluginOptions { + commitSha?: string; + projectID?: string; + compareTarget?: string; + dsn?: string; + reporter?: string; + instrumentCwd?: string; + branch?: string; +} + +// Custom extensions to include .vue files +const DEFAULT_EXTENSION = ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue']; +const PLUGIN_NAME = 'vite:canyon'; + +function resolveFilename(id: string): string { + // To remove the annoying query parameters from the filename + const [filename] = id.split('?vue'); + return filename; +} + +function shouldInstrument(filename: string) { + return DEFAULT_EXTENSION.some(ext => filename.endsWith(ext)); +} + +function instrumentedData(args: canyonPluginOptions): string { + const canyon = { + // gitlab流水线自带 + projectID: args.projectID || process.env['CI_PROJECT_ID'] || '', + commitSha: args.commitSha || process.env['CI_COMMIT_SHA'] || '', + sha: args.commitSha || process.env['CI_COMMIT_SHA'] || '', + branch: args.branch || process.env['CI_COMMIT_BRANCH'] || process.env['CI_COMMIT_REF_NAME'] ||'', + // 自己配置 + dsn: args.dsn || process.env['DSN'] || '', + reporter: args.reporter || process.env['REPORTER'] || '', + // 可选 + compareTarget: args.compareTarget, + // 自动获取 + instrumentCwd: args.instrumentCwd || process.cwd(), + } + return `(new Function("return this")()).__canyon__ = ${JSON.stringify(canyon)}`; +} + +export default function canyonPlugin(opts: canyonPluginOptions = {}): Plugin { + const logger = createLogger('info', {prefix: 'vite-plugin-canyon'}); + const canyonStr = instrumentedData(opts); + // logger.warn(`${PLUGIN_NAME}> ${green(`instrumented data: ${canyonStr}`)}`); + return { + name: PLUGIN_NAME, + enforce: 'post', + transform(srcCode, id, options) { + const newCode = `${canyonStr}\n${srcCode}` + const filename = resolveFilename(id); + if (shouldInstrument(filename)) { + return { + code: newCode, + map: null, + }; + } + }, + }; +} diff --git a/plugins/swc-plugin-canyon/tsconfig.json b/plugins/swc-plugin-canyon/tsconfig.json new file mode 100644 index 00000000..2a6e0a70 --- /dev/null +++ b/plugins/swc-plugin-canyon/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "target": "es2018", + "module": "commonjs", + "moduleResolution": "node", + "lib": [ "dom" ], + "rootDir": "src", + "outDir": "dist", + "sourceMap": true, + "declaration": true, + "declarationMap": true, + "strict": true, + "allowSyntheticDefaultImports": true, + "esModuleInterop": true + }, + "include": [ "src/*" ], + "exclude": [ "**/node_modules" ] +}