Quellcode durchsuchen

chore(build): add esbuild script

daiwei vor 2 Monaten
Ursprung
Commit
867bfef57c
3 geänderte Dateien mit 170 neuen und 6 gelöschten Zeilen
  1. 3 1
      package.json
  2. 5 5
      pnpm-lock.yaml
  3. 162 0
      scripts/dev-esbuild.js

+ 3 - 1
package.json

@@ -5,6 +5,7 @@
   "type": "module",
   "scripts": {
     "dev": "node scripts/dev.js",
+    "dev-esbuild": "node scripts/dev-esbuild.js",
     "build": "node scripts/build.js",
     "build-rollup": "node scripts/build-with-rollup.js",
     "build-dts": "node scripts/build-types.js",
@@ -43,6 +44,7 @@
     "dev-sfc": "run-s dev-prepare-cjs dev-sfc-run",
     "dev-sfc-serve": "vite packages-private/sfc-playground",
     "dev-sfc-run": "run-p \"dev compiler-sfc -f esm-browser\" \"dev vue -if esm-browser-vapor\" \"dev vue -ipf esm-browser-vapor\" \"dev server-renderer -if esm-bundler\" dev-sfc-serve",
+    "dev-sfc-run-esbuild": "run-p \"dev-esbuild compiler-sfc -f esm-browser\" \"dev-esbuild vue -if esm-browser-vapor\" \"dev-esbuild vue -ipf esm-browser-vapor\" \"dev-esbuild server-renderer -if esm-bundler\" dev-sfc-serve",
     "dev-vapor": "pnpm -C packages-private/local-playground run dev",
     "serve": "serve",
     "open": "open http://localhost:3000/packages-private/template-explorer/local.html",
@@ -71,7 +73,7 @@
   "devDependencies": {
     "@babel/parser": "catalog:",
     "@babel/types": "catalog:",
-    "@rolldown/plugin-node-polyfills": "^1.0.0",
+    "@rolldown/plugin-node-polyfills": "^1.0.3",
     "@rollup/plugin-alias": "^6.0.0",
     "@rollup/plugin-commonjs": "^29.0.0",
     "@rollup/plugin-json": "^6.1.0",

+ 5 - 5
pnpm-lock.yaml

@@ -39,8 +39,8 @@ importers:
         specifier: 'catalog:'
         version: 7.28.5
       '@rolldown/plugin-node-polyfills':
-        specifier: ^1.0.0
-        version: 1.0.0
+        specifier: ^1.0.3
+        version: 1.0.3
       '@rollup/plugin-alias':
         specifier: ^6.0.0
         version: 6.0.0(rollup@4.54.0)
@@ -1789,8 +1789,8 @@ packages:
     cpu: [x64]
     os: [win32]
 
-  '@rolldown/plugin-node-polyfills@1.0.0':
-    resolution: {integrity: sha512-JUmEhvz+B6+0AaeUGvZ7SnJec7wpwYqZ+FcAng1I3UVGYKA1uSmE1H4vKHUewU665Nb37znDd9N4o/iImmKeVg==}
+  '@rolldown/plugin-node-polyfills@1.0.3':
+    resolution: {integrity: sha512-V7iWE6UI+InppK51NfuHlaCpep7Rzv4w92pZif6BtRP3ULDHfQjU6HNyIS41Plrx1tqzcANcAsnjDS6ye+1kFQ==}
 
   '@rolldown/pluginutils@1.0.0-beta.53':
     resolution: {integrity: sha512-vENRlFU4YbrwVqNDZ7fLvy+JR1CRkyr01jhSiDpE1u6py3OMzQfztQU2jxykW3ALNxO4kSlqIDeYyD0Y9RcQeQ==}
@@ -5068,7 +5068,7 @@ snapshots:
   '@rolldown/binding-win32-x64-msvc@1.0.0-rc.1':
     optional: true
 
-  '@rolldown/plugin-node-polyfills@1.0.0': {}
+  '@rolldown/plugin-node-polyfills@1.0.3': {}
 
   '@rolldown/pluginutils@1.0.0-beta.53': {}
 

+ 162 - 0
scripts/dev-esbuild.js

@@ -0,0 +1,162 @@
+// @ts-check
+
+// Using esbuild for faster dev builds.
+// We are still using Rollup for production builds because it generates
+// smaller files and provides better tree-shaking.
+
+import esbuild from 'esbuild'
+import fs from 'node:fs'
+import { dirname, relative, resolve } from 'node:path'
+import { fileURLToPath } from 'node:url'
+import { createRequire } from 'node:module'
+import { parseArgs } from 'node:util'
+import { polyfillNode } from 'esbuild-plugin-polyfill-node'
+
+const require = createRequire(import.meta.url)
+const __dirname = dirname(fileURLToPath(import.meta.url))
+
+const {
+  values: { format: rawFormat, prod, inline: inlineDeps },
+  positionals,
+} = parseArgs({
+  allowPositionals: true,
+  options: {
+    format: {
+      type: 'string',
+      short: 'f',
+      default: 'global',
+    },
+    prod: {
+      type: 'boolean',
+      short: 'p',
+      default: false,
+    },
+    inline: {
+      type: 'boolean',
+      short: 'i',
+      default: false,
+    },
+  },
+})
+
+const format = rawFormat || 'global'
+const targets = positionals.length ? positionals : ['vue']
+
+// resolve output
+const outputFormat = format.startsWith('global')
+  ? 'iife'
+  : format === 'cjs'
+    ? 'cjs'
+    : 'esm'
+
+const postfix = format.endsWith('-runtime')
+  ? `runtime.${format.replace(/-runtime$/, '')}`
+  : format
+
+const privatePackages = fs.readdirSync('packages-private')
+
+for (const target of targets) {
+  const pkgBase = privatePackages.includes(target)
+    ? `packages-private`
+    : `packages`
+  const pkgBasePath = `../${pkgBase}/${target}`
+  const pkg = require(`${pkgBasePath}/package.json`)
+  const outfile = resolve(
+    __dirname,
+    `${pkgBasePath}/dist/${
+      target === 'vue-compat' ? `vue` : target
+    }.${postfix}.${prod ? `prod.` : ``}js`,
+  )
+  const relativeOutfile = relative(process.cwd(), outfile)
+
+  // resolve externals
+  // TODO this logic is largely duplicated from rollup.config.js
+  /** @type {string[]} */
+  let external = []
+  if (!inlineDeps) {
+    // cjs & esm-bundler: external all deps
+    if (format === 'cjs' || format.includes('esm-bundler')) {
+      external = [
+        ...external,
+        ...Object.keys(pkg.dependencies || {}),
+        ...Object.keys(pkg.peerDependencies || {}),
+        // for @vue/compiler-sfc / server-renderer
+        'path',
+        'url',
+        'stream',
+      ]
+    }
+
+    if (target === 'compiler-sfc') {
+      const consolidatePkgPath = require.resolve(
+        '@vue/consolidate/package.json',
+        {
+          paths: [resolve(__dirname, `../packages/${target}/`)],
+        },
+      )
+      const consolidateDeps = Object.keys(
+        require(consolidatePkgPath).devDependencies,
+      )
+      external = [
+        ...external,
+        ...consolidateDeps,
+        'fs',
+        'vm',
+        'crypto',
+        'react-dom/server',
+        'teacup/lib/express',
+        'arc-templates/dist/es5',
+        'then-pug',
+        'then-jade',
+      ]
+    }
+  }
+  /** @type {Array<import('esbuild').Plugin>} */
+  const plugins = [
+    {
+      name: 'log-rebuild',
+      setup(build) {
+        build.onEnd(() => {
+          console.log(`built: ${relativeOutfile}`)
+        })
+      },
+    },
+  ]
+
+  if (format !== 'cjs' && pkg.buildOptions?.enableNonBrowserBranches) {
+    plugins.push(polyfillNode())
+  }
+
+  esbuild
+    .context({
+      entryPoints: [resolve(__dirname, `${pkgBasePath}/src/index.ts`)],
+      outfile,
+      bundle: true,
+      external,
+      sourcemap: true,
+      format: outputFormat,
+      globalName: pkg.buildOptions?.name,
+      platform: format === 'cjs' ? 'node' : 'browser',
+      plugins,
+      define: {
+        __COMMIT__: `"dev"`,
+        __VERSION__: `"${pkg.version}"`,
+        __DEV__: prod ? `false` : `true`,
+        __TEST__: `false`,
+        __BROWSER__: String(
+          format !== 'cjs' && !pkg.buildOptions?.enableNonBrowserBranches,
+        ),
+        __GLOBAL__: String(format === 'global'),
+        __ESM_BUNDLER__: String(format.includes('esm-bundler')),
+        __ESM_BROWSER__: String(format.includes('esm-browser')),
+        __CJS__: String(format === 'cjs'),
+        __SSR__: String(format !== 'global'),
+        __COMPAT__: String(target === 'vue-compat'),
+        __FEATURE_SUSPENSE__: `true`,
+        __FEATURE_OPTIONS_API__: `true`,
+        __FEATURE_PROD_DEVTOOLS__: `false`,
+        __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: `true`,
+      },
+    })
+    .then(ctx => ctx.watch())
+}