UNPKG

2.03 kBJavaScriptView Raw
1import esbuild from 'esbuild'
2
3const minify = true
4const ext = [minify && 'min', 'js'].filter(Boolean).join('.')
5
6/**
7 *
8 * @param {import('esbuild').BuildOptions} conf
9 * @returns {import('esbuild').BuildOptions}
10 */
11const defaultConfig = (conf = {}) => ({
12 bundle: true,
13 minify,
14 metafile: true,
15 logLevel: 'info',
16 ...conf,
17})
18
19/**
20 * @type {[import('esbuild').BuildOptions]}
21 */
22const buildConfigs = [
23 // Build all middlewares as ESM
24 {
25 format: 'esm',
26 entryPoints: ['./src/main.js'],
27 outfile: `./dist/fetch-xhr.module.${ext}`,
28 },
29 // Build all middlewares as IIFE
30 {
31 format: 'iife',
32 globalName: 'xfetch',
33 entryPoints: ['./src/main.js'],
34 outfile: `./dist/fetch-xhr.${ext}`,
35 },
36
37 /* Build each middleware as ESM and IIFE */
38 // fetch ESM
39 {
40 format: 'esm',
41 entryPoints: ['./src/fetch-middleware.js'],
42 outfile: `./dist/fetch.module.${ext}`,
43 },
44 // fetch IIFE
45 {
46 format: 'iife',
47 globalName: 'xfetch.fetchHook',
48 entryPoints: ['./src/fetch-middleware.js'],
49 outfile: `./dist/fetch.${ext}`,
50 footer: {
51 js: `xfetch.fetchHook = xfetch.fetchHook.default;`,
52 },
53 },
54 // xhr ESM
55 {
56 format: 'esm',
57 entryPoints: ['./src/xhr-middleware.js'],
58 outfile: `./dist/xhr.module.${ext}`,
59 },
60 // xhr IIFE
61 {
62 format: 'iife',
63 globalName: 'xfetch.xhrHook',
64 entryPoints: ['./src/xhr-middleware.js'],
65 outfile: `./dist/xhr.${ext}`,
66 footer: {
67 js: `xfetch.xhrHook = xfetch.xhrHook.default;`,
68 },
69 },
70]
71
72function buildAllInParallel(params) {
73 return Promise.all(
74 buildConfigs.map(conf => esbuild.build(defaultConfig(conf)))
75 )
76}
77
78function buildOneByOne(params) {
79 return buildConfigs.reduce(
80 (p, conf) => p.then(() => esbuild.build(defaultConfig(conf))),
81 Promise.resolve()
82 )
83}
84
85console.log(`Building ${buildConfigs.length} bundles ...`)
86buildOneByOne()
87 .then(() => {
88 console.log('Successfully built the bundles!')
89 })
90 .catch(e => {
91 throw new Error('Failed during the build - ', e.message)
92 })