|
| 1 | +/** |
| 2 | + * The script builds the CDN version of the library. |
| 3 | + */ |
| 4 | + |
| 5 | +import { $ } from "bun"; |
| 6 | +import { writeFile } from "fs/promises"; |
| 7 | +import { dirname, join, relative } from "path"; |
| 8 | +import { listLocales, type LocaleFile } from "../_lib/listLocales"; |
| 9 | +import { availableParallelism } from "node:os"; |
| 10 | +import { promiseQueue } from "../test/_lib/queue"; |
| 11 | + |
| 12 | +if (!process.env.PACKAGE_OUTPUT_PATH) |
| 13 | + throw new Error("PACKAGE_OUTPUT_PATH is not set"); |
| 14 | + |
| 15 | +const out = relative(process.cwd(), process.env.PACKAGE_OUTPUT_PATH); |
| 16 | + |
| 17 | +const indexPath = join(out, "cdn.js"); |
| 18 | +const fpIndexPath = join(out, "fp", "cdn.js"); |
| 19 | +const localesIndexPath = join(out, "locale", "cdn.js"); |
| 20 | + |
| 21 | +Promise.all([ |
| 22 | + listLocales().then((locales) => |
| 23 | + Promise.all( |
| 24 | + locales.map(async (locale) => { |
| 25 | + const localePath = join(out, "locale", locale.code, "cdn.js"); |
| 26 | + await $`mkdir -p ${dirname(localePath)}`; |
| 27 | + await writeFile(localePath, localeTemplate(locale)); |
| 28 | + return localePath; |
| 29 | + }), |
| 30 | + ), |
| 31 | + ), |
| 32 | + |
| 33 | + writeFile(indexPath, indexTemplate()).then(() => indexPath), |
| 34 | + |
| 35 | + writeFile(fpIndexPath, fpIndexTemplate()).then(() => fpIndexPath), |
| 36 | + |
| 37 | + writeFile(localesIndexPath, localesIndexTemplate()).then( |
| 38 | + () => localesIndexPath, |
| 39 | + ), |
| 40 | +]) |
| 41 | + .then(([localePaths, ...indexPaths]) => localePaths.concat(indexPaths)) |
| 42 | + .then(async (paths) => { |
| 43 | + const buildOptions = { |
| 44 | + entrypoints: paths, |
| 45 | + outdir: ".", |
| 46 | + sourcemap: "external" as const, |
| 47 | + root: ".", |
| 48 | + }; |
| 49 | + |
| 50 | + // First bundle code |
| 51 | + await Bun.build(buildOptions); |
| 52 | + |
| 53 | + // Make it compatible with older browser |
| 54 | + await promiseQueue( |
| 55 | + paths.map( |
| 56 | + (path) => () => |
| 57 | + $`env BABEL_ENV=cdn npx babel ${path} --out-file ${path} --source-maps`, |
| 58 | + ), |
| 59 | + availableParallelism(), |
| 60 | + ); |
| 61 | + |
| 62 | + // Now generate min versions |
| 63 | + await Bun.build({ |
| 64 | + ...buildOptions, |
| 65 | + minify: true, |
| 66 | + naming: "/[dir]/[name].min.[ext]", |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | +function indexTemplate() { |
| 71 | + return `import * as dateFns from "./index.mjs"; |
| 72 | +window.dateFns = { |
| 73 | + ...window.dateFns, |
| 74 | + dateFns |
| 75 | +};`; |
| 76 | +} |
| 77 | + |
| 78 | +function fpIndexTemplate() { |
| 79 | + return `import * as fp from "../fp.mjs"; |
| 80 | +window.dateFns = { |
| 81 | + ...window.dateFns, |
| 82 | + fp |
| 83 | +};`; |
| 84 | +} |
| 85 | + |
| 86 | +function localesIndexTemplate() { |
| 87 | + return ` import * as locales from "../locale.mjs"; |
| 88 | +window.dateFns = { |
| 89 | + ...window.dateFns, |
| 90 | + locale: { |
| 91 | + ...window.dateFns.locale, |
| 92 | + ...locales |
| 93 | + } |
| 94 | +};`; |
| 95 | +} |
| 96 | + |
| 97 | +function localeTemplate({ name, code }: LocaleFile) { |
| 98 | + return `import { ${name} } from "../${code}.mjs"; |
| 99 | +window.dateFns = { |
| 100 | + ...window.dateFns, |
| 101 | + locale: { |
| 102 | + ...window.dateFns.locale, |
| 103 | + ${name} |
| 104 | + } |
| 105 | +};`; |
| 106 | +} |
0 commit comments