UNPKG

2.72 kBJavaScriptView Raw
1import resolve from '@rollup/plugin-node-resolve';
2import commonjs from '@rollup/plugin-commonjs';
3import {terser} from "rollup-plugin-terser";
4import json from '@rollup/plugin-json';
5import { babel } from '@rollup/plugin-babel';
6import autoExternal from 'rollup-plugin-auto-external';
7import bundleSize from 'rollup-plugin-bundle-size'
8import path from 'path';
9
10const lib = require("./package.json");
11const outputFileName = 'axios';
12const name = "axios";
13const namedInput = './index.js';
14const defaultInput = './lib/axios.js';
15
16const buildConfig = ({es5, browser = true, minifiedVersion = true, ...config}) => {
17 const {file} = config.output;
18 const ext = path.extname(file);
19 const basename = path.basename(file, ext);
20 const extArr = ext.split('.');
21 extArr.shift();
22
23
24 const build = ({minified}) => ({
25 input: namedInput,
26 ...config,
27 output: {
28 ...config.output,
29 file: `${path.dirname(file)}/${basename}.${(minified ? ['min', ...extArr] : extArr).join('.')}`
30 },
31 plugins: [
32 json(),
33 resolve({browser}),
34 commonjs(),
35 minified && terser(),
36 minified && bundleSize(),
37 ...(es5 ? [babel({
38 babelHelpers: 'bundled',
39 presets: ['@babel/preset-env']
40 })] : []),
41 ...(config.plugins || []),
42 ]
43 });
44
45 const configs = [
46 build({minified: false}),
47 ];
48
49 if (minifiedVersion) {
50 configs.push(build({minified: true}))
51 }
52
53 return configs;
54};
55
56export default async () => {
57 const year = new Date().getFullYear();
58 const banner = `// Axios v${lib.version} Copyright (c) ${year} ${lib.author} and contributors`;
59
60 return [
61 // browser ESM bundle for CDN
62 ...buildConfig({
63 input: namedInput,
64 output: {
65 file: `dist/esm/${outputFileName}.js`,
66 format: "esm",
67 preferConst: true,
68 exports: "named",
69 banner
70 }
71 }),
72
73 // Browser UMD bundle for CDN
74 ...buildConfig({
75 input: defaultInput,
76 es5: true,
77 output: {
78 file: `dist/${outputFileName}.js`,
79 name,
80 format: "umd",
81 exports: "default",
82 banner
83 }
84 }),
85
86 // Browser CJS bundle
87 ...buildConfig({
88 input: defaultInput,
89 es5: false,
90 minifiedVersion: false,
91 output: {
92 file: `dist/browser/${name}.cjs`,
93 name,
94 format: "cjs",
95 exports: "default",
96 banner
97 }
98 }),
99
100 // Node.js commonjs bundle
101 {
102 input: defaultInput,
103 output: {
104 file: `dist/node/${name}.cjs`,
105 format: "cjs",
106 preferConst: true,
107 exports: "default",
108 banner
109 },
110 plugins: [
111 autoExternal(),
112 resolve(),
113 commonjs()
114 ]
115 }
116 ]
117};