UNPKG

12.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.config = exports.COPY_OPTIONS_KEYS = void 0;
4var tslib_1 = require("tslib");
5var fs_1 = tslib_1.__importDefault(require("fs"));
6var path_1 = tslib_1.__importDefault(require("path"));
7var es_modules_1 = require("@pkgr/es-modules");
8var umd_globals_1 = require("@pkgr/umd-globals");
9var utils_1 = require("@pkgr/utils");
10var plugin_babel_1 = tslib_1.__importDefault(require("@rollup/plugin-babel"));
11var plugin_commonjs_1 = tslib_1.__importDefault(require("@rollup/plugin-commonjs"));
12var plugin_json_1 = tslib_1.__importDefault(require("@rollup/plugin-json"));
13var plugin_node_resolve_1 = tslib_1.__importDefault(require("@rollup/plugin-node-resolve"));
14var plugin_replace_1 = tslib_1.__importDefault(require("@rollup/plugin-replace"));
15var plugin_typescript_1 = tslib_1.__importDefault(require("@rollup/plugin-typescript"));
16var plugin_url_1 = tslib_1.__importDefault(require("@rollup/plugin-url"));
17var rollup_plugin_alias_1 = tslib_1.__importDefault(require("@rxts/rollup-plugin-alias"));
18var builtin_modules_1 = tslib_1.__importDefault(require("builtin-modules"));
19var debug_1 = tslib_1.__importDefault(require("debug"));
20var is_glob_1 = tslib_1.__importDefault(require("is-glob"));
21var flatMap_1 = tslib_1.__importDefault(require("lodash/flatMap"));
22var micromatch_1 = require("micromatch");
23var rollup_plugin_copy_1 = tslib_1.__importDefault(require("rollup-plugin-copy"));
24var rollup_plugin_postcss_1 = tslib_1.__importDefault(require("rollup-plugin-postcss"));
25var rollup_plugin_terser_1 = require("rollup-plugin-terser");
26var vue = utils_1.tryRequirePkg('rollup-plugin-vue');
27var info = debug_1.default('r:info');
28var STYLE_EXTENSIONS = [
29 '.css',
30 '.less',
31 '.pcss',
32 '.sass',
33 '.scss',
34 '.styl',
35 '.stylus',
36];
37var IMAGE_EXTENSIONS = [
38 '.bmp',
39 '.gif',
40 '.jpeg',
41 '.jpg',
42 '.png',
43 '.svg',
44 '.webp',
45];
46var ASSETS_EXTENSIONS = STYLE_EXTENSIONS.concat(IMAGE_EXTENSIONS);
47var resolve = function (_a) {
48 var _b = _a === void 0 ? {} : _a, deps = _b.deps, node = _b.node;
49 return plugin_node_resolve_1.default({
50 dedupe: node ? undefined : deps,
51 mainFields: [
52 !node && 'browser',
53 'esnext',
54 'es2015',
55 'esm2015',
56 'fesm2015',
57 'esm5',
58 'fesm5',
59 'module',
60 'jsnext:main',
61 'main',
62 ].filter(Boolean),
63 preferBuiltins: node,
64 });
65};
66var cjs = function (sourceMap) {
67 return plugin_commonjs_1.default({
68 // TODO: add package @pkgr/cjs-ignore ?
69 // see also: https://github.com/rollup/rollup-plugin-commonjs/issues/244#issuecomment-536168280
70 // hard-coded temporarily
71 ignore: ['invariant', 'react-draggable'],
72 sourceMap: sourceMap,
73 });
74};
75var DEFAULT_FORMATS = ['cjs', 'es2015', 'esm'];
76var regExpCacheMap = new Map();
77var tryRegExp = function (exp) {
78 if (typeof exp === 'string' && (exp = exp.trim())) {
79 var cached = regExpCacheMap.get(exp);
80 if (cached != null) {
81 return cached;
82 }
83 var matched = /^\/(.*)\/([gimsuy]*)$/.exec(exp);
84 if (matched) {
85 try {
86 var regExp = new RegExp(matched[1], matched[2]);
87 regExpCacheMap.set(exp, regExp);
88 return regExp;
89 }
90 catch (_a) { }
91 }
92 regExpCacheMap.set(exp, exp);
93 }
94 return exp;
95};
96var onwarn = function (warning, warn) {
97 if (warning.code === 'THIS_IS_UNDEFINED') {
98 return;
99 }
100 warn(warning);
101};
102exports.COPY_OPTIONS_KEYS = [
103 'targets',
104 'verbose',
105 'hook',
106 'copyOnce',
107];
108var isCopyOptions = function (copies) {
109 return !Array.isArray(copies) &&
110 Object.keys(copies).every(function (key) {
111 return exports.COPY_OPTIONS_KEYS.includes(key);
112 });
113};
114exports.config = function (_a) {
115 var _b = _a === void 0 ? {} : _a, formats = _b.formats, monorepo = _b.monorepo, input = _b.input, _c = _b.exclude, exclude = _c === void 0 ? [] : _c, _d = _b.outputDir, outputDir = _d === void 0 ? 'lib' : _d, exports = _b.exports, external = _b.external, _e = _b.externals, externals = _e === void 0 ? external || [] : _e, umdGlobals = _b.globals, _f = _b.aliasEntries, aliasEntries = _f === void 0 ? [] : _f, _g = _b.copies, copies = _g === void 0 ? [] : _g, _h = _b.sourceMap, sourceMap = _h === void 0 ? false : _h, typescriptOptions = _b.typescript, postcssOptions = _b.postcss, vueOptions = _b.vue, define = _b.define, terserOptions = _b.terser, _j = _b.prod, prod = _j === void 0 ? utils_1.__PROD__ : _j;
116 var pkgs = monorepo === false
117 ? [utils_1.CWD]
118 : Array.isArray(monorepo)
119 ? utils_1.tryGlob(monorepo)
120 : utils_1.monorepoPkgs;
121 if (monorepo == null && pkgs.length === 0) {
122 pkgs = [utils_1.CWD];
123 }
124 var globals = umd_globals_1.getGlobals({
125 globals: umdGlobals,
126 });
127 var aliasOptions = {
128 resolve: utils_1.EXTENSIONS.concat(ASSETS_EXTENSIONS),
129 entries: (Array.isArray(aliasEntries)
130 ? aliasEntries.map(function (_a) {
131 var find = _a.find, replacement = _a.replacement;
132 return ({
133 find: tryRegExp(find),
134 replacement: replacement,
135 });
136 })
137 : Object.entries(aliasEntries).map(function (_a) {
138 var find = _a[0], replacement = _a[1];
139 return ({
140 find: tryRegExp(find),
141 replacement: replacement,
142 });
143 })).concat(es_modules_1.entries),
144 };
145 var copyOptions = isCopyOptions(copies)
146 ? copies
147 : {
148 targets: Array.isArray(copies)
149 ? copies
150 : Object.entries(copies).map(function (_a) {
151 var src = _a[0], dest = _a[1];
152 return ({
153 src: src,
154 dest: dest,
155 });
156 }),
157 };
158 var configs = flatMap_1.default(pkgs, function (pkg) {
159 var srcPath = path_1.default.resolve(pkg, 'src');
160 var pkgInput = input;
161 var pkgOutputDir = outputDir;
162 if (!fs_1.default.existsSync(srcPath) && pkgInput == null) {
163 pkgInput = 'index';
164 }
165 pkgInput = utils_1.tryExtensions(path_1.default.resolve(pkg, pkgInput || 'src/index'));
166 if (pkgOutputDir && !pkgOutputDir.endsWith('/')) {
167 pkgOutputDir = pkgOutputDir + '/';
168 }
169 if (!pkgInput || !pkgInput.startsWith(pkg)) {
170 return [];
171 }
172 var pkgJson = utils_1.tryRequirePkg(path_1.default.resolve(pkg, 'package.json'));
173 if (!pkgJson ||
174 exclude.includes(pkgJson.name) ||
175 utils_1.tryGlob(exclude, path_1.default.resolve(pkg, '..')).includes(pkg)) {
176 return [];
177 }
178 var name = pkgJson.name, _a = pkgJson.engines, _b = (_a === void 0 ? {} : _a).node, node = _b === void 0 ? null : _b, _c = pkgJson.dependencies, dependencies = _c === void 0 ? {} : _c, _d = pkgJson.peerDependencies, peerDependencies = _d === void 0 ? {} : _d;
179 var deps = Object.keys(dependencies);
180 var collectedExternals = typeof externals === 'function'
181 ? []
182 : utils_1.arrayify(externals).concat(Object.keys(peerDependencies), node ? deps.concat(builtin_modules_1.default) : []);
183 var isTsInput = /\.tsx?/.test(pkgInput);
184 var pkgFormats = formats && formats.length > 0
185 ? formats
186 : DEFAULT_FORMATS.concat(node ? [] : 'umd');
187 var pkgGlobals = collectedExternals.reduce(function (pkgGlobals, pkg) {
188 if (pkgGlobals[pkg] == null) {
189 pkgGlobals[pkg] = umd_globals_1.upperCamelCase(umd_globals_1.normalizePkg(pkg));
190 }
191 return pkgGlobals;
192 }, globals);
193 var defineValues;
194 if (define) {
195 defineValues = Object.entries(define).reduce(function (acc, _a) {
196 var _b;
197 var key = _a[0], value = _a[1];
198 return Object.assign(acc, (_b = {},
199 _b[key] = JSON.stringify(value),
200 _b));
201 }, {});
202 }
203 return pkgFormats.map(function (format) {
204 var isEsVersion = /^es(\d+|next)$/.test(format) && format !== 'es5';
205 return {
206 input: pkgInput,
207 output: {
208 file: path_1.default.resolve(pkg, "" + pkgOutputDir + format + (prod ? '.min' : '') + ".js"),
209 format: isEsVersion ? 'esm' : format,
210 name: pkgGlobals[name] || umd_globals_1.upperCamelCase(umd_globals_1.normalizePkg(name)),
211 globals: globals,
212 exports: exports,
213 sourcemap: sourceMap,
214 },
215 external: function (id) {
216 if (typeof externals === 'function') {
217 return externals.call(this, id, collectedExternals);
218 }
219 return collectedExternals.some(function (pkg) {
220 var pkgRegExp = tryRegExp(pkg);
221 return pkgRegExp instanceof RegExp
222 ? pkgRegExp.test(id)
223 : is_glob_1.default(pkg)
224 ? micromatch_1.isMatch(id, pkg)
225 : id === pkg || id.startsWith(pkg + "/");
226 });
227 },
228 onwarn: onwarn,
229 plugins: [
230 rollup_plugin_alias_1.default(aliasOptions),
231 utils_1.isTsAvailable && isTsInput
232 ? plugin_typescript_1.default(tslib_1.__assign(tslib_1.__assign({
233 // @ts-ignore
234 jsx: 'react',
235 // @ts-ignore
236 module: 'esnext' }, typescriptOptions), { target: isEsVersion ? format : 'es5', sourceMap: sourceMap }))
237 : plugin_babel_1.default({
238 exclude: ['*.min.js', '*.production.js'],
239 presets: [
240 [
241 '@babel/preset-env',
242 isEsVersion
243 ? {
244 targets: {
245 esmodules: true,
246 },
247 }
248 : undefined,
249 ],
250 ],
251 }),
252 resolve({
253 deps: deps,
254 node: !!node,
255 }),
256 cjs(sourceMap),
257 rollup_plugin_copy_1.default(copyOptions),
258 plugin_json_1.default(),
259 plugin_url_1.default({ include: IMAGE_EXTENSIONS.map(function (ext) { return "**/*" + ext; }) }),
260 rollup_plugin_postcss_1.default(postcssOptions),
261 ].concat([
262 vue && vue(vueOptions),
263 // __DEV__ and __PROD__ will always be replaced while `process.env.NODE_ENV` will be preserved except on production
264 define &&
265 plugin_replace_1.default(prod
266 ? tslib_1.__assign(tslib_1.__assign({}, defineValues), { __DEV__: JSON.stringify(false), __PROD__: JSON.stringify(true), 'process.env.NODE_ENV': JSON.stringify(utils_1.PROD) }) : tslib_1.__assign(tslib_1.__assign({}, defineValues), { __DEV__: JSON.stringify(utils_1.__DEV__), __PROD__: JSON.stringify(utils_1.__PROD__) })),
267 prod && rollup_plugin_terser_1.terser(terserOptions),
268 ].filter(utils_1.identify)),
269 };
270 });
271 });
272 console.assert(configs.length, "No configuration resolved, mark sure you've setup correctly");
273 return configs;
274};
275exports.default = (function (options) {
276 if (options === void 0) { options = {}; }
277 var configs = exports.config(options).concat(options.prod ? exports.config(Object.assign({}, options, { prod: false })) : []);
278 info('configs: %O', configs);
279 return configs;
280});
281//# sourceMappingURL=config.js.map
\No newline at end of file