1 | import fs from 'fs';
|
2 | import path from 'path';
|
3 | import alias from '@rxts/rollup-plugin-alias';
|
4 | import builtinModules from 'builtin-modules';
|
5 | import debug from 'debug';
|
6 | import { flatMap } from 'lodash';
|
7 | import babel from 'rollup-plugin-babel';
|
8 | import commonjs from 'rollup-plugin-commonjs';
|
9 | import json from 'rollup-plugin-json';
|
10 | import postcss from 'rollup-plugin-postcss';
|
11 | import nodeBuiltins from 'rollup-plugin-node-builtins';
|
12 | import nodeGlobals from 'rollup-plugin-node-globals';
|
13 | import replace from 'rollup-plugin-replace';
|
14 | import nodeResolve from 'rollup-plugin-node-resolve';
|
15 | import { terser } from 'rollup-plugin-terser';
|
16 | import typescript from 'rollup-plugin-typescript';
|
17 | import url from 'rollup-plugin-url';
|
18 | import { getGlobals, upperCamelCase, normalizePkg } from '@pkgr/umd-globals';
|
19 | import { namedExports } from '@pkgr/named-exports';
|
20 |
|
21 | var info = debug('r:info');
|
22 | var PRODUCTION = 'production';
|
23 | var STYLE_EXTENSIONS = ['.css', '.less', 'sass', '.scss', '.styl', '.stylus'];
|
24 | var IMAGE_EXTENSIONS = ['.gif', '.jpeg', '.jpg', '.png', '.svg', '.webp'];
|
25 | var ASSETS_EXTENSIONS = STYLE_EXTENSIONS.concat(IMAGE_EXTENSIONS);
|
26 |
|
27 | var resolve = function resolve() {
|
28 | var {
|
29 | deps,
|
30 | node
|
31 | } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
32 | return nodeResolve({
|
33 | dedupe: node ? undefined : deps,
|
34 | mainFields: [!node && 'browser', 'esnext', 'es2015', 'esm2015', 'fesm2015', 'esm5', 'fesm5', 'module', 'jsnext:main', 'main'].filter(Boolean)
|
35 | });
|
36 | };
|
37 |
|
38 | var cjs = sourceMap => commonjs({
|
39 |
|
40 |
|
41 |
|
42 | ignore: ['react-draggable'],
|
43 | namedExports,
|
44 | sourceMap
|
45 | });
|
46 |
|
47 | var BASIC_PLUGINS = [json(), url({
|
48 | include: IMAGE_EXTENSIONS.map(ext => "**/*".concat(ext))
|
49 | })];
|
50 | var DEFAULT_FORMATS = ['cjs', 'es2015', 'esm'];
|
51 | var isTsAvailable = false;
|
52 |
|
53 | try {
|
54 | isTsAvailable = !!require.resolve('typescript');
|
55 | } catch (e) {}
|
56 |
|
57 |
|
58 | var EXTENSIONS = Object.keys(require.extensions);
|
59 |
|
60 | if (isTsAvailable) {
|
61 | EXTENSIONS.unshift('.ts', '.tsx');
|
62 | }
|
63 |
|
64 | var tryExtensions = filepath => {
|
65 | var ext = EXTENSIONS.find(ext => fs.existsSync(filepath + ext));
|
66 | return ext ? filepath + ext : filepath;
|
67 | };
|
68 |
|
69 | var tryRegExp = exp => {
|
70 | if (typeof exp !== 'string' || !(exp = exp.trim())) {
|
71 | return exp;
|
72 | }
|
73 |
|
74 | var matched = /^\/(.*)\/([gimsuy]*)$/.exec(exp);
|
75 |
|
76 | if (matched) {
|
77 | try {
|
78 | return new RegExp(matched[1], matched[2]);
|
79 | } catch (_unused) {}
|
80 | }
|
81 |
|
82 | return exp;
|
83 | };
|
84 |
|
85 | var onwarn = (warning, warn) => {
|
86 | if (warning.code === 'THIS_IS_UNDEFINED') {
|
87 | return;
|
88 | }
|
89 |
|
90 | warn(warning);
|
91 | };
|
92 |
|
93 | var config = function config() {
|
94 | var {
|
95 | formats,
|
96 | monorepo,
|
97 | input,
|
98 | outputDir = 'lib',
|
99 | exports,
|
100 | externals = [],
|
101 | globals: umdGlobals,
|
102 | aliases = [],
|
103 | sourceMap = false,
|
104 | postcss: postcssOpts,
|
105 | prod = process.env.NODE_ENV === PRODUCTION
|
106 | } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
107 | var pkgsPath = path.resolve(typeof monorepo === 'string' ? monorepo : 'packages');
|
108 |
|
109 | if (monorepo !== false) {
|
110 | monorepo = fs.existsSync(pkgsPath);
|
111 | }
|
112 |
|
113 | var pkgs = monorepo ? fs.readdirSync(pkgsPath) : [''];
|
114 | var globals = getGlobals({
|
115 | globals: umdGlobals
|
116 | });
|
117 | var configs = flatMap(pkgs, pkg => {
|
118 | var pkgPath = path.resolve(monorepo ? pkgsPath : '', pkg);
|
119 | var srcPath = path.resolve(pkgPath, 'src');
|
120 | var pkgInput = input;
|
121 | var pkgOutputDir = outputDir;
|
122 |
|
123 | if (!fs.existsSync(srcPath) && pkgInput == null) {
|
124 | pkgInput = 'index';
|
125 | }
|
126 |
|
127 | pkgInput = tryExtensions(path.resolve(pkgPath, pkgInput || 'src/index'));
|
128 | var isAbsolute = path.isAbsolute(pkgInput);
|
129 |
|
130 | if (pkgOutputDir && !pkgOutputDir.endsWith('/')) {
|
131 | pkgOutputDir = pkgOutputDir + '/';
|
132 | }
|
133 |
|
134 | if (!fs.existsSync(pkgInput) || isAbsolute && !pkgInput.startsWith(pkgPath)) {
|
135 | return [];
|
136 | }
|
137 |
|
138 | var {
|
139 | name,
|
140 | engines: {
|
141 | node
|
142 | } = {},
|
143 | dependencies = {},
|
144 | peerDependencies = {}
|
145 | } = require(path.resolve(pkgPath, 'package.json'));
|
146 |
|
147 | var deps = Object.keys(dependencies);
|
148 |
|
149 | var _external = externals.concat(Object.keys(peerDependencies), node ? deps.concat(builtinModules) : []);
|
150 |
|
151 | var isTsInput = /\.tsx?/.test(pkgInput);
|
152 | var pkgFormats = formats && formats.length ? formats : DEFAULT_FORMATS.concat(node ? [] : 'umd');
|
153 |
|
154 | var pkgGlobals = _external.reduce((pkgGlobals, pkg) => {
|
155 | if (pkgGlobals[pkg] == null) {
|
156 | pkgGlobals[pkg] = upperCamelCase(normalizePkg(pkg));
|
157 | }
|
158 |
|
159 | return pkgGlobals;
|
160 | }, globals);
|
161 |
|
162 | return pkgFormats.map(format => {
|
163 | var isEsVersion = /^es(\d+|next)$/.test(format) && format !== 'es5';
|
164 | return {
|
165 | input: pkgInput,
|
166 | output: {
|
167 | file: path.resolve(pkgPath, "".concat(pkgOutputDir).concat(format).concat(prod ? '.min' : '', ".js")),
|
168 | format: isEsVersion ? 'esm' : format,
|
169 | name: pkgGlobals[name] || upperCamelCase(normalizePkg(name)),
|
170 | globals,
|
171 | exports
|
172 | },
|
173 | external: id => _external.some(pkg => id === pkg || id.startsWith(pkg + '/')),
|
174 | onwarn,
|
175 | plugins: [alias({
|
176 | resolve: EXTENSIONS.concat(ASSETS_EXTENSIONS),
|
177 | entries: Array.isArray(aliases) ? aliases.map((_ref) => {
|
178 | var {
|
179 | find,
|
180 | replacement
|
181 | } = _ref;
|
182 | return {
|
183 | find: tryRegExp(find),
|
184 | replacement
|
185 | };
|
186 | }) : Object.entries(aliases).map((_ref2) => {
|
187 | var [find, replacement] = _ref2;
|
188 | return {
|
189 | find: tryRegExp(find),
|
190 | replacement
|
191 | };
|
192 | })
|
193 | }), isTsAvailable && isTsInput ? typescript({
|
194 | target: isEsVersion ? format : 'es5'
|
195 | }) : babel({
|
196 | exclude: ['*.min.js', '*.production.js'],
|
197 | presets: [['@babel/preset-env', isEsVersion ? {
|
198 | targets: {
|
199 | esmodules: true
|
200 | }
|
201 | } : undefined]]
|
202 | })].concat(!node && [nodeGlobals(), nodeBuiltins({
|
203 | crypto: true
|
204 | })], resolve({
|
205 | deps,
|
206 | node: !!node
|
207 | }), cjs(sourceMap), BASIC_PLUGINS, postcss(postcssOpts), prod && [replace({
|
208 | 'process.env.NODE_ENV': JSON.stringify(PRODUCTION)
|
209 | }), terser()]).filter(Boolean)
|
210 | };
|
211 | });
|
212 | });
|
213 | console.assert(configs.length, "No configuration resolved, mark sure you've setup correctly");
|
214 | return configs;
|
215 | };
|
216 |
|
217 | var config$1 = (function () {
|
218 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
219 | var configs = config(options).concat(options.prod ? config(Object.assign({}, options, {
|
220 | prod: false
|
221 | })) : []);
|
222 | info('configs: %O', configs);
|
223 | return configs;
|
224 | });
|
225 |
|
226 | export default config$1;
|