Browse Source

feat: add vapor support

daiwei 3 months ago
parent
commit
9ded37c045
3 changed files with 55 additions and 29 deletions
  1. 26 19
      scripts/build.js
  2. 28 9
      scripts/create-rolldown-config.js
  3. 1 1
      scripts/trim-vapor-exports.js

+ 26 - 19
scripts/build.js

@@ -92,7 +92,15 @@ const {
   size,
 } = values
 
-const formats = rawFormats?.split(',')
+/**
+ * @type {string[] | undefined}
+ */
+let formats
+let isNegation = false
+if (rawFormats) {
+  isNegation = rawFormats.startsWith('~')
+  formats = (isNegation ? rawFormats.slice(1) : rawFormats).split('+')
+}
 const sizeDir = path.resolve('temp/size')
 
 run()
@@ -172,22 +180,22 @@ function createConfigsForTarget(target) {
     return
   }
 
-  // let resolvedFormats
-  // if (formats) {
-  //   const isNegation = formats.startsWith('~')
-  //   resolvedFormats = (isNegation ? formats.slice(1) : formats).split('+')
-  //   const pkgFormats = pkg.buildOptions?.formats
-  //   if (pkgFormats) {
-  //     if (isNegation) {
-  //       resolvedFormats = pkgFormats.filter(f => !resolvedFormats.includes(f))
-  //     } else {
-  //       resolvedFormats = resolvedFormats.filter(f => pkgFormats.includes(f))
-  //     }
-  //   }
-  //   if (!resolvedFormats.length) {
-  //     return
-  //   }
-  // }
+  let resolvedFormats
+  if (formats) {
+    const pkgFormats = pkg.buildOptions?.formats
+    if (pkgFormats) {
+      if (isNegation) {
+        resolvedFormats = pkgFormats.filter(
+          (/** @type {string} */ f) => !formats.includes(f),
+        )
+      } else {
+        resolvedFormats = formats.filter(f => pkgFormats.includes(f))
+      }
+    }
+    if (!resolvedFormats.length) {
+      return
+    }
+  }
 
   // if building a specific format, do not remove dist.
   if (!formats && existsSync(`${pkgDir}/dist`)) {
@@ -197,8 +205,7 @@ function createConfigsForTarget(target) {
   return createConfigsForPackage({
     target,
     commit,
-    // @ts-expect-error
-    formats,
+    formats: resolvedFormats,
     prodOnly,
     devOnly:
       (pkg.buildOptions && pkg.buildOptions.env === 'development') || devOnly,

+ 28 - 9
scripts/create-rolldown-config.js

@@ -8,6 +8,7 @@ import pico from 'picocolors'
 import polyfillNode from '@rolldown/plugin-node-polyfills'
 import { entries } from './aliases.js'
 import { inlineEnums } from './inline-enums.js'
+import { trimVaporExportsPlugin } from './trim-vapor-exports.js'
 
 const require = createRequire(import.meta.url)
 const __dirname = fileURLToPath(new URL('.', import.meta.url))
@@ -17,7 +18,7 @@ const consolidatePkg = require('@vue/consolidate/package.json')
 
 const packagesDir = path.resolve(__dirname, '../packages')
 
-/** @typedef {'cjs' | 'esm-bundler' | 'global' | 'global-runtime' | 'esm-browser' | 'esm-bundler-runtime' | 'esm-browser-runtime'} PackageFormat */
+/** @typedef {'cjs' | 'esm-bundler' | 'global' | 'global-runtime' | 'esm-browser' | 'esm-bundler-runtime' | 'esm-browser-runtime' | 'esm-browser-vapor'} PackageFormat */
 
 /**
  * @param {{
@@ -86,6 +87,12 @@ export function createConfigsForPackage({
       file: resolve(`dist/${name}.runtime.global.js`),
       format: 'iife',
     },
+    // The vapor format is a esm-browser + runtime only build that is meant for
+    // the SFC playground only.
+    'esm-browser-vapor': {
+      file: resolve(`dist/${name}.runtime-with-vapor.esm-browser.js`),
+      format: 'es',
+    },
   }
 
   /** @type {PackageFormat[]} */
@@ -106,7 +113,10 @@ export function createConfigsForPackage({
       if (format === 'cjs') {
         packageConfigs.push(createProductionConfig(format))
       }
-      if (/^(global|esm-browser)(-runtime)?/.test(format)) {
+      if (
+        format === 'esm-browser-vapor' ||
+        /^(global|esm-browser)(-runtime)?/.test(format)
+      ) {
         packageConfigs.push(createMinifiedConfig(format))
       }
     })
@@ -147,23 +157,31 @@ export function createConfigsForPackage({
 
     output.externalLiveBindings = false
 
-    // @ts-expect-error Not supported yet
-    output.reexportProtoFromExternal = false
+    // TODO rolldown Not supported yet
+    // output.reexportProtoFromExternal = false
 
     if (isGlobalBuild) {
       output.name = packageOptions.name
     }
 
-    let entryFile = /runtime$/.test(format) ? `src/runtime.ts` : `src/index.ts`
+    let entryFile = 'index.ts'
+    if (pkg.name === 'vue') {
+      if (format === 'esm-browser-vapor' || format === 'esm-bundler-runtime') {
+        entryFile = 'runtime-with-vapor.ts'
+      } else if (format === 'esm-bundler') {
+        entryFile = 'index-with-vapor.ts'
+      } else if (format.includes('runtime')) {
+        entryFile = 'runtime.ts'
+      }
+    }
 
     // the compat build needs both default AND named exports. This will cause
-    // Rolldown to complain for non-ESM targets, so we use separate entries for
+    // Rollup to complain for non-ESM targets, so we use separate entries for
     // esm vs. non-esm builds.
     if (isCompatPackage && (isBrowserESMBuild || isBundlerESMBuild)) {
-      entryFile = /runtime$/.test(format)
-        ? `src/esm-runtime.ts`
-        : `src/esm-index.ts`
+      entryFile = `esm-${entryFile}`
     }
+    entryFile = 'src/' + entryFile
 
     function resolveDefine() {
       /** @type {Record<string, string>} */
@@ -322,6 +340,7 @@ export function createConfigsForPackage({
         alias: entries,
       },
       plugins: [
+        ...trimVaporExportsPlugin(format, pkg.name),
         ...(localDev ? [] : [enumPlugin]),
         ...resolveReplace(),
         ...resolveNodePlugins(),

+ 1 - 1
scripts/trim-vapor-exports.js

@@ -8,7 +8,7 @@
  *
  * @param {string} format
  * @param {string} pkgName
- * @returns {import('rollup').Plugin[]}
+ * @returns {import('rolldown').Plugin[]}
  */
 export function trimVaporExportsPlugin(format, pkgName) {
   if (