-
Notifications
You must be signed in to change notification settings - Fork 9
/
rollup.config.factory.js
157 lines (136 loc) · 3.89 KB
/
rollup.config.factory.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const RollupPluginCommonJs = require('@rollup/plugin-commonjs');
const RollupPluginJson = require('@rollup/plugin-json');
const RollupPluginNodeResolve = require('@rollup/plugin-node-resolve').default;
const RollupPluginReplace = require('@rollup/plugin-replace');
const RollupPluginTs = require('@wessberg/rollup-plugin-ts');
const { resolve } = require('path');
const { terser } = require('rollup-plugin-terser');
const Typescript = require('typescript');
const SPEC_RX = /^((@[^/]+\/[^/@]+|[^./@][^/@]*)(?:@([^/]+))?)(.*)?$/;
function parseBareModuleSpec(bareModuleSpec) {
const matches = bareModuleSpec.match(SPEC_RX);
if (matches) {
const [, nameSpec, name, spec, path = ''] = matches;
return {
nameSpec,
name,
spec,
path,
};
}
return null;
}
function toUmdName(name) {
let umdName = 'Velcro.';
for (const segment of name.split(/[^a-z]/)) {
umdName += segment.charAt(0).toUpperCase() + segment.slice(1);
}
return umdName;
}
function createIsExternal(packageJson) {
const dependencies = new Set(
Object.keys({
...packageJson.devDependencies,
...packageJson.dependencies,
})
);
return function isExternal(id) {
const spec = parseBareModuleSpec(id);
if (!spec) return false;
const result = dependencies.has(spec.name);
return result;
};
}
/**
* Create a generic rollup config for a given directory
*
* @param {string} dirname
* @param {any} packageJson
* @return {import('rollup').RollupOptions[]}
*/
exports.rollupConfigFactory = function rollupConfigFactory(dirname, packageJson) {
const createTypescriptPlugin = (emitDeclarations = false) =>
RollupPluginTs({
cwd: dirname,
tsconfig: {
fileName: resolve(dirname, './tsconfig.json'),
hook: (config) => ({
...config,
declaration: emitDeclarations,
declarationMap: emitDeclarations,
}),
},
transpileOnly: !emitDeclarations,
transpiler: 'typescript',
typescript: Typescript,
// exclude: ['node_modules/**', '**/*.mjs'],
});
/** @type {import('rollup').RollupOptions[]} */
const configs = [];
if (packageJson.main || packageJson.module) {
/** @type {import('rollup').OutputOptions[]} */
const output = [];
if (packageJson.main) {
output.push({
file: resolve(dirname, packageJson.main),
format: 'commonjs',
sourcemap: true,
});
}
if (packageJson.module) {
output.push({
file: resolve(dirname, packageJson.module),
format: 'esm',
sourcemap: true,
});
}
configs.push({
input: resolve(dirname, './src/index.ts'),
output,
external: createIsExternal(packageJson),
onwarn: (msg, warn) => {
if (!/Circular/.test(msg)) {
warn(msg);
}
},
plugins: [
RollupPluginJson(),
createTypescriptPlugin(true),
RollupPluginNodeResolve({
mainFields: ['module', 'main', 'unpkg'],
}),
RollupPluginCommonJs(),
RollupPluginReplace({ __VERSION__: packageJson.version }),
],
});
}
if (packageJson.unpkg) {
configs.push({
input: resolve(dirname, './src/index.ts'),
output: {
file: resolve(dirname, packageJson.unpkg),
format: 'iife',
name: packageJson.name.replace(/^@velcro\/(.*)$/, (_match, name) => toUmdName(name)),
sourcemap: true,
},
onwarn: (msg, warn) => {
if (!/Circular/.test(msg)) {
warn(msg);
}
},
plugins: [
RollupPluginJson(),
createTypescriptPlugin(false),
RollupPluginNodeResolve({
mainFields: ['module', 'main', 'unpkg'],
}),
RollupPluginCommonJs(),
RollupPluginReplace({
__VERSION__: process.env.npm_package_version || packageJson.version,
}),
terser(),
],
});
}
return configs;
};