Skip to content

Commit 0356ecc

Browse files
authored
Add CDN versions of modules (#3737)
Added CDN versions of modules compatible with older browsers.
1 parent d22be9f commit 0356ecc

File tree

4 files changed

+126
-8
lines changed

4 files changed

+126
-8
lines changed

babel.config.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
module.exports = {
2-
presets: ['@babel/preset-typescript'],
2+
presets: ["@babel/preset-typescript"],
33

44
env: {
55
cjs: {
66
presets: [
77
[
8-
'@babel/preset-env',
9-
{ targets: { node: 'current' }, modules: 'commonjs' },
8+
"@babel/preset-env",
9+
{ targets: { node: "current" }, modules: "commonjs" },
1010
],
1111
],
1212

1313
plugins: [
14-
['@babel/plugin-transform-modules-commonjs', { strict: true }],
15-
['babel-plugin-add-import-extension', { extension: 'js' }],
14+
["@babel/plugin-transform-modules-commonjs", { strict: true }],
15+
["babel-plugin-add-import-extension", { extension: "js" }],
1616
],
1717
},
1818

1919
esm: {
2020
presets: [
21-
['@babel/preset-env', { targets: { node: 'current' }, modules: false }],
21+
["@babel/preset-env", { targets: { node: "current" }, modules: false }],
2222
],
2323

24-
plugins: [['babel-plugin-add-import-extension', { extension: 'mjs' }]],
24+
plugins: [["babel-plugin-add-import-extension", { extension: "mjs" }]],
25+
},
26+
27+
cdn: {
28+
presets: [
29+
["@babel/preset-env", { targets: { ie: "11" }, modules: false }],
30+
],
2531
},
2632
},
2733

2834
ignore: [],
2935

3036
retainLines: true,
31-
}
37+
};

scripts/build/cdn.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

scripts/build/package.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ done
6969
if [ -z "$PACKAGE_SKIP_BEAUTIFY" ]; then
7070
# Make it prettier
7171
npx prettier "$dir" --write --ignore-path "" > /dev/null 2>&1 || exit 1
72+
fi
73+
74+
if [ -z "$PACKAGE_SKIP_BEAUTIFY" ]; then
75+
# Build CDN versions
76+
bun ./scripts/build/cdn.ts
7277
fi

scripts/test/types.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ root="$(pwd)/$(dirname "$0")/../.."
1111
export PACKAGE_OUTPUT_PATH="$root/tmp/types"
1212

1313
export PACKAGE_SKIP_BEAUTIFY=true
14+
export PACKAGE_SKIP_CDN=true
1415

1516
./scripts/build/package.sh
1617

0 commit comments

Comments
 (0)