-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.js
More file actions
118 lines (102 loc) · 2.92 KB
/
Copy pathrollup.config.js
File metadata and controls
118 lines (102 loc) · 2.92 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { resolve } from 'path';
import sourceMaps from 'rollup-plugin-sourcemaps';
import nodeResolve from 'rollup-plugin-node-resolve';
import json from 'rollup-plugin-json';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
import { terser } from 'rollup-plugin-terser';
import { getIfUtils, removeEmpty } from 'webpack-config-utils';
import pkg from './package.json';
const { pascalCase, normalizePackageName, getOutputFileName } = require('./config/helpers');
/**
* @typedef {import('./config/types').RollupConfig} Config
*/
/**
* @typedef {import('./config/types').RollupPlugin} Plugin
*/
const env = process.env.NODE_ENV || 'development';
const { ifProduction } = getIfUtils(env);
const LIB_NAME = pascalCase(normalizePackageName(pkg.name));
const ROOT = resolve(__dirname);
const DIST = resolve(ROOT, 'dist');
/**
* Object literals are open-ended for js checking, so we need to be explicit
* @type {{entry:{esm5: string, esm2015: string},bundles:string}}
*/
const PATHS = {
entry: {
esm5: resolve(DIST, 'esm5'),
esm2015: resolve(DIST, 'esm2015'),
},
bundles: resolve(DIST, 'bundles'),
};
/**
* @type {string[]}
*/
const external = [...Object.keys(pkg.peerDependencies)];
/**
* @type {Plugin[]}
*/
const plugins = /** @type {Plugin[]} */ ([
// Allow json resolution
json(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
nodeResolve(),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
// Resolve source maps to the original source
sourceMaps(),
// properly set process.env.NODE_ENV within `./environment.ts`
replace({
exclude: 'node_modules/**',
'process.env.NODE_ENV': JSON.stringify(env),
}),
]);
const globals = {
graphql: 'graphql',
tslib: 'tslib',
};
/**
* @type {Config}
*/
const CommonConfig = {
input: {},
output: {},
inlineDynamicImports: true,
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external,
};
/**
* @type {Config}
*/
const UMDconfig = {
...CommonConfig,
input: resolve(PATHS.entry.esm5, 'index.js'),
output: {
file: getOutputFileName(resolve(PATHS.bundles, 'index.umd.js'), ifProduction()),
format: 'umd',
name: LIB_NAME,
globals: globals,
sourcemap: true,
},
plugins: removeEmpty(/** @type {Plugin[]} */ ([...plugins, ifProduction(terser())])),
};
/**
* @type {Config}
*/
const FESMconfig = {
...CommonConfig,
input: resolve(PATHS.entry.esm2015, 'index.js'),
output: [
{
file: getOutputFileName(resolve(PATHS.bundles, 'index.esm.js'), ifProduction()),
format: 'es',
sourcemap: true,
globals: globals,
},
],
plugins: removeEmpty(/** @type {Plugin[]} */ ([...plugins, ifProduction(terser())])),
};
export default [UMDconfig, FESMconfig];