1 | import esbuild from 'esbuild'
|
2 |
|
3 | const minify = true
|
4 | const ext = [minify && 'min', 'js'].filter(Boolean).join('.')
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const defaultConfig = (conf = {}) => ({
|
12 | bundle: true,
|
13 | minify,
|
14 | metafile: true,
|
15 | logLevel: 'info',
|
16 | ...conf,
|
17 | })
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const buildConfigs = [
|
23 |
|
24 | {
|
25 | format: 'esm',
|
26 | entryPoints: ['./src/main.js'],
|
27 | outfile: `./dist/fetch-xhr.module.${ext}`,
|
28 | },
|
29 |
|
30 | {
|
31 | format: 'iife',
|
32 | globalName: 'xfetch',
|
33 | entryPoints: ['./src/main.js'],
|
34 | outfile: `./dist/fetch-xhr.${ext}`,
|
35 | },
|
36 |
|
37 |
|
38 |
|
39 | {
|
40 | format: 'esm',
|
41 | entryPoints: ['./src/fetch-middleware.js'],
|
42 | outfile: `./dist/fetch.module.${ext}`,
|
43 | },
|
44 |
|
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 |
|
55 | {
|
56 | format: 'esm',
|
57 | entryPoints: ['./src/xhr-middleware.js'],
|
58 | outfile: `./dist/xhr.module.${ext}`,
|
59 | },
|
60 |
|
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 |
|
72 | function buildAllInParallel(params) {
|
73 | return Promise.all(
|
74 | buildConfigs.map(conf => esbuild.build(defaultConfig(conf)))
|
75 | )
|
76 | }
|
77 |
|
78 | function buildOneByOne(params) {
|
79 | return buildConfigs.reduce(
|
80 | (p, conf) => p.then(() => esbuild.build(defaultConfig(conf))),
|
81 | Promise.resolve()
|
82 | )
|
83 | }
|
84 |
|
85 | console.log(`Building ${buildConfigs.length} bundles ...`)
|
86 | buildOneByOne()
|
87 | .then(() => {
|
88 | console.log('Successfully built the bundles!')
|
89 | })
|
90 | .catch(e => {
|
91 | throw new Error('Failed during the build - ', e.message)
|
92 | })
|