-
Notifications
You must be signed in to change notification settings - Fork 31
/
build-cdn.js
62 lines (53 loc) · 1.75 KB
/
build-cdn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import path from 'path';
import esbuild from 'esbuild';
import glob from 'tiny-glob';
import pkg from './packages/plainjs/package.json' assert { type: 'json' };
const replaceDependenciesByJsdelivr = {
name: 'replace-dependencies-by-jsdelivr',
setup(build) {
build.onResolve({ namespace: 'file', filter: /^@json2csv\/.*$/ }, async (args) => {
if (args.kind !== 'import-statement') return;
const [, relativePath] = args.path.match(/\@json2csv\/(.*)/);
return {
path: path.join('..', relativePath, 'index.js'),
external: true,
namespace: 'dependency',
};
});
build.onResolve({ namespace: 'file', filter: /^\.\/.*$/ }, (args) => {
if (args.kind !== 'import-statement') return;
return {
path: args.path,
external: true,
namespace: 'dependency',
};
});
const dependencies = {
'@streamparser/json': `https://cdn.jsdelivr.net/npm/@streamparser/json@${pkg.dependencies['@streamparser/json']}/dist/mjs/index.js`
};
build.onResolve({ namespace: 'file', filter: new RegExp(`(?:${Object.keys(dependencies).join('|')})`) }, (args) => {
if (args.kind !== 'import-statement') return;
if (dependencies[args.path]) {
return {
path: dependencies[args.path] || args.path,
external: true
};
}
});
},
};
const pkgs = ['plainjs', 'whatwg', 'transforms', 'formatters'];
pkgs.forEach(async (pkg) => {
const entryPoints = await glob(`packages/${pkg}/src/*.ts`);
esbuild.build({
entryPoints,
bundle: true,
target: 'es2019',
format: 'esm',
outdir: `dist/cdn/${pkg}`,
plugins: [replaceDependenciesByJsdelivr],
}).catch((err) => {
console.error(err);
process.exit(1);
});
});