|  | 
|  | 1 | +import esbuild from "esbuild"; | 
|  | 2 | +import { copyFile, readFile, writeFile, rm } from "node:fs/promises"; | 
|  | 3 | +import { glob } from "glob"; | 
|  | 4 | + | 
|  | 5 | +const sharedOptions = { | 
|  | 6 | +  sourcemap: "external", | 
|  | 7 | +  sourcesContent: true, | 
|  | 8 | +  minify: false, | 
|  | 9 | +  allowOverwrite: true, | 
|  | 10 | +  packages: "external", | 
|  | 11 | +}; | 
|  | 12 | + | 
|  | 13 | +async function main() { | 
|  | 14 | +  // Start with a clean slate | 
|  | 15 | +  await rm("pkg", { recursive: true, force: true }); | 
|  | 16 | +  // Build the source code for a neutral platform as ESM | 
|  | 17 | +  await esbuild.build({ | 
|  | 18 | +    entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]), | 
|  | 19 | +    outdir: "pkg/dist-src", | 
|  | 20 | +    bundle: false, | 
|  | 21 | +    platform: "neutral", | 
|  | 22 | +    format: "esm", | 
|  | 23 | +    ...sharedOptions, | 
|  | 24 | +    sourcemap: false, | 
|  | 25 | +  }); | 
|  | 26 | + | 
|  | 27 | +  // Remove the types file from the dist-src folder | 
|  | 28 | +  const typeFiles = await glob([ | 
|  | 29 | +    "./pkg/dist-src/**/types.js.map", | 
|  | 30 | +    "./pkg/dist-src/**/types.js", | 
|  | 31 | +  ]); | 
|  | 32 | +  for (const typeFile of typeFiles) { | 
|  | 33 | +    await rm(typeFile); | 
|  | 34 | +  } | 
|  | 35 | + | 
|  | 36 | +  const entryPoints = ["./pkg/dist-src/index.js"]; | 
|  | 37 | + | 
|  | 38 | +  await Promise.all([ | 
|  | 39 | +    // Build the a CJS Node.js bundle | 
|  | 40 | +    esbuild.build({ | 
|  | 41 | +      entryPoints, | 
|  | 42 | +      outdir: "pkg/dist-node", | 
|  | 43 | +      bundle: true, | 
|  | 44 | +      platform: "node", | 
|  | 45 | +      target: "node14", | 
|  | 46 | +      format: "cjs", | 
|  | 47 | +      ...sharedOptions, | 
|  | 48 | +    }), | 
|  | 49 | +    // Build an ESM browser bundle | 
|  | 50 | +    esbuild.build({ | 
|  | 51 | +      entryPoints, | 
|  | 52 | +      outdir: "pkg/dist-web", | 
|  | 53 | +      bundle: true, | 
|  | 54 | +      platform: "browser", | 
|  | 55 | +      format: "esm", | 
|  | 56 | +      ...sharedOptions, | 
|  | 57 | +    }), | 
|  | 58 | +  ]); | 
|  | 59 | + | 
|  | 60 | +  // Copy the README, LICENSE to the pkg folder | 
|  | 61 | +  await copyFile("LICENSE", "pkg/LICENSE"); | 
|  | 62 | +  await copyFile("README.md", "pkg/README.md"); | 
|  | 63 | + | 
|  | 64 | +  // Handle the package.json | 
|  | 65 | +  let pkg = JSON.parse((await readFile("package.json", "utf8")).toString()); | 
|  | 66 | +  // Remove unnecessary fields from the package.json | 
|  | 67 | +  delete pkg.scripts; | 
|  | 68 | +  delete pkg.prettier; | 
|  | 69 | +  delete pkg.release; | 
|  | 70 | +  delete pkg.jest; | 
|  | 71 | +  await writeFile( | 
|  | 72 | +    "pkg/package.json", | 
|  | 73 | +    JSON.stringify( | 
|  | 74 | +      { | 
|  | 75 | +        ...pkg, | 
|  | 76 | +        files: ["dist-*/**", "bin/**"], | 
|  | 77 | +        main: "dist-node/index.js", | 
|  | 78 | +        browser: "dist-web/index.js", | 
|  | 79 | +        types: "dist-types/index.d.ts", | 
|  | 80 | +        module: "dist-src/index.js", | 
|  | 81 | +        sideEffects: false, | 
|  | 82 | +      }, | 
|  | 83 | +      null, | 
|  | 84 | +      2 | 
|  | 85 | +    ) | 
|  | 86 | +  ); | 
|  | 87 | +} | 
|  | 88 | +main(); | 
0 commit comments