@doracli/rollup 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,934 @@
1
+ 'use strict';
2
+
3
+ var lang = require('@cclr/lang');
4
+ var filePro = require('@dorabag/file-pro');
5
+ var helper = require('@doracli/helper');
6
+ var path = require('path');
7
+ var alias = require('@rollup/plugin-alias');
8
+ var babel = require('@rollup/plugin-babel');
9
+ var commonjs = require('@rollup/plugin-commonjs');
10
+ var copy = require('rollup-plugin-copy');
11
+ var rollupPluginDts = require('rollup-plugin-dts');
12
+ var json = require('@rollup/plugin-json');
13
+ var terser = require('@rollup/plugin-terser');
14
+ var autoprefixer = require('autoprefixer');
15
+ var cssnanoPlugin = require('cssnano');
16
+ var fs = require('fs');
17
+ var promises = require('fs/promises');
18
+ var postcss = require('rollup-plugin-postcss');
19
+ var resolve = require('@rollup/plugin-node-resolve');
20
+ var presetBabel = require('@doracli/preset-babel');
21
+ var rollup = require('rollup');
22
+
23
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
24
+ function _arrayLikeToArray(r, a) {
25
+ (null == a || a > r.length) && (a = r.length);
26
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
27
+ return n;
28
+ }
29
+ function _arrayWithoutHoles(r) {
30
+ if (Array.isArray(r)) return _arrayLikeToArray(r);
31
+ }
32
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {
33
+ try {
34
+ var i = n[a](c),
35
+ u = i.value;
36
+ } catch (n) {
37
+ return void e(n);
38
+ }
39
+ i.done ? t(u) : Promise.resolve(u).then(r, o);
40
+ }
41
+ function _asyncToGenerator(n) {
42
+ return function () {
43
+ var t = this,
44
+ e = arguments;
45
+ return new Promise(function (r, o) {
46
+ var a = n.apply(t, e);
47
+ function _next(n) {
48
+ asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
49
+ }
50
+ function _throw(n) {
51
+ asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
52
+ }
53
+ _next(void 0);
54
+ });
55
+ };
56
+ }
57
+ function _classCallCheck(a, n) {
58
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
59
+ }
60
+ function _defineProperties(e, r) {
61
+ for (var t = 0; t < r.length; t++) {
62
+ var o = r[t];
63
+ o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
64
+ }
65
+ }
66
+ function _createClass(e, r, t) {
67
+ return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
68
+ writable: false
69
+ }), e;
70
+ }
71
+ function _defineProperty(e, r, t) {
72
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
73
+ value: t,
74
+ enumerable: true,
75
+ configurable: true,
76
+ writable: true
77
+ }) : e[r] = t, e;
78
+ }
79
+ function _iterableToArray(r) {
80
+ if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
81
+ }
82
+ function _nonIterableSpread() {
83
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
84
+ }
85
+ function ownKeys(e, r) {
86
+ var t = Object.keys(e);
87
+ if (Object.getOwnPropertySymbols) {
88
+ var o = Object.getOwnPropertySymbols(e);
89
+ r && (o = o.filter(function (r) {
90
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
91
+ })), t.push.apply(t, o);
92
+ }
93
+ return t;
94
+ }
95
+ function _objectSpread2(e) {
96
+ for (var r = 1; r < arguments.length; r++) {
97
+ var t = null != arguments[r] ? arguments[r] : {};
98
+ r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
99
+ _defineProperty(e, r, t[r]);
100
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
101
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
102
+ });
103
+ }
104
+ return e;
105
+ }
106
+ function _objectWithoutProperties(e, t) {
107
+ if (null == e) return {};
108
+ var o,
109
+ r,
110
+ i = _objectWithoutPropertiesLoose(e, t);
111
+ if (Object.getOwnPropertySymbols) {
112
+ var n = Object.getOwnPropertySymbols(e);
113
+ for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
114
+ }
115
+ return i;
116
+ }
117
+ function _objectWithoutPropertiesLoose(r, e) {
118
+ if (null == r) return {};
119
+ var t = {};
120
+ for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
121
+ if (-1 !== e.indexOf(n)) continue;
122
+ t[n] = r[n];
123
+ }
124
+ return t;
125
+ }
126
+ function _regenerator() {
127
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
128
+ var e,
129
+ t,
130
+ r = "function" == typeof Symbol ? Symbol : {},
131
+ n = r.iterator || "@@iterator",
132
+ o = r.toStringTag || "@@toStringTag";
133
+ function i(r, n, o, i) {
134
+ var c = n && n.prototype instanceof Generator ? n : Generator,
135
+ u = Object.create(c.prototype);
136
+ return _regeneratorDefine(u, "_invoke", function (r, n, o) {
137
+ var i,
138
+ c,
139
+ u,
140
+ f = 0,
141
+ p = o || [],
142
+ y = false,
143
+ G = {
144
+ p: 0,
145
+ n: 0,
146
+ v: e,
147
+ a: d,
148
+ f: d.bind(e, 4),
149
+ d: function (t, r) {
150
+ return i = t, c = 0, u = e, G.n = r, a;
151
+ }
152
+ };
153
+ function d(r, n) {
154
+ for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) {
155
+ var o,
156
+ i = p[t],
157
+ d = G.p,
158
+ l = i[2];
159
+ r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0));
160
+ }
161
+ if (o || r > 1) return a;
162
+ throw y = true, n;
163
+ }
164
+ return function (o, p, l) {
165
+ if (f > 1) throw TypeError("Generator is already running");
166
+ for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) {
167
+ i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u);
168
+ try {
169
+ if (f = 2, i) {
170
+ if (c || (o = "next"), t = i[o]) {
171
+ if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object");
172
+ if (!t.done) return t;
173
+ u = t.value, c < 2 && (c = 0);
174
+ } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1);
175
+ i = e;
176
+ } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break;
177
+ } catch (t) {
178
+ i = e, c = 1, u = t;
179
+ } finally {
180
+ f = 1;
181
+ }
182
+ }
183
+ return {
184
+ value: t,
185
+ done: y
186
+ };
187
+ };
188
+ }(r, o, i), true), u;
189
+ }
190
+ var a = {};
191
+ function Generator() {}
192
+ function GeneratorFunction() {}
193
+ function GeneratorFunctionPrototype() {}
194
+ t = Object.getPrototypeOf;
195
+ var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () {
196
+ return this;
197
+ }), t),
198
+ u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c);
199
+ function f(e) {
200
+ return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e;
201
+ }
202
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () {
203
+ return this;
204
+ }), _regeneratorDefine(u, "toString", function () {
205
+ return "[object Generator]";
206
+ }), (_regenerator = function () {
207
+ return {
208
+ w: i,
209
+ m: f
210
+ };
211
+ })();
212
+ }
213
+ function _regeneratorDefine(e, r, n, t) {
214
+ var i = Object.defineProperty;
215
+ try {
216
+ i({}, "", {});
217
+ } catch (e) {
218
+ i = 0;
219
+ }
220
+ _regeneratorDefine = function (e, r, n, t) {
221
+ function o(r, n) {
222
+ _regeneratorDefine(e, r, function (e) {
223
+ return this._invoke(r, n, e);
224
+ });
225
+ }
226
+ r ? i ? i(e, r, {
227
+ value: n,
228
+ enumerable: !t,
229
+ configurable: !t,
230
+ writable: !t
231
+ }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2));
232
+ }, _regeneratorDefine(e, r, n, t);
233
+ }
234
+ function _toConsumableArray(r) {
235
+ return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
236
+ }
237
+ function _toPrimitive(t, r) {
238
+ if ("object" != typeof t || !t) return t;
239
+ var e = t[Symbol.toPrimitive];
240
+ if (void 0 !== e) {
241
+ var i = e.call(t, r);
242
+ if ("object" != typeof i) return i;
243
+ throw new TypeError("@@toPrimitive must return a primitive value.");
244
+ }
245
+ return ("string" === r ? String : Number)(t);
246
+ }
247
+ function _toPropertyKey(t) {
248
+ var i = _toPrimitive(t, "string");
249
+ return "symbol" == typeof i ? i : i + "";
250
+ }
251
+ function _unsupportedIterableToArray(r, a) {
252
+ if (r) {
253
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
254
+ var t = {}.toString.call(r).slice(8, -1);
255
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
256
+ }
257
+ }
258
+
259
+ /**
260
+ * 获取external配置
261
+ * @param envParams 环境参数
262
+ * @param options 用户配置的external选项
263
+ * @returns TExternalOption
264
+ */
265
+ var getExternal = function getExternal(_envParams, options) {
266
+ var _ref = options || {},
267
+ dependencies = _ref.dependencies,
268
+ devDependencies = _ref.devDependencies,
269
+ peerDependencies = _ref.peerDependencies,
270
+ externals = _ref.externals,
271
+ internals = _ref.internals,
272
+ filter = _ref.filter;
273
+ if (filter) return filter;
274
+
275
+ // 需要打包的模块
276
+ var internalsMap = lang.isArray(internals) ? internals.reduce(function (prev, cur) {
277
+ prev[cur] = cur;
278
+ return prev;
279
+ }, {}) : internals || {};
280
+
281
+ // 不需要打包的模块
282
+ var extraExternals = lang.isArray(externals) ? externals : Object.keys(externals || {});
283
+ var obj = _objectSpread2(_objectSpread2(_objectSpread2({}, dependencies), peerDependencies), devDependencies);
284
+ var externalList = Object.keys(obj).concat(extraExternals).filter(function (key) {
285
+ return !internalsMap[key];
286
+ });
287
+ return function (source, _importer, _isResolved) {
288
+ if (/^@doracli\//.test(source)) return true;
289
+ var isExternal = externalList.find(function (externalModule) {
290
+ return source.startsWith(externalModule);
291
+ });
292
+ return !!isExternal;
293
+ };
294
+ };
295
+
296
+ /**
297
+ * 获取入口文件
298
+ * @description input 不传,则自动寻找
299
+ * @param envParams 环境参数
300
+ * @param input 入口文件路径
301
+ * @returnscj
302
+ */
303
+ var getInput = /*#__PURE__*/function () {
304
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(envParams, input) {
305
+ var workRootDir, rightFile;
306
+ return _regenerator().w(function (_context) {
307
+ while (1) switch (_context.n) {
308
+ case 0:
309
+ workRootDir = envParams.workRootDir;
310
+ rightFile = input;
311
+ if (!lang.isString(input)) {
312
+ _context.n = 2;
313
+ break;
314
+ }
315
+ _context.n = 1;
316
+ return filePro.isValidDir(input);
317
+ case 1:
318
+ if (_context.v) {
319
+ _context.n = 2;
320
+ break;
321
+ }
322
+ helper.cLog.warn('rollup', 'input', "\u65E0\u6709\u6548\u5165\u53E3\u8DEF\u5F84 ".concat(input, "\uFF0C\u5C06\u81EA\u52A8\u5BFB\u627E\u6709\u6548\u8DEF\u5F84"));
323
+ rightFile = undefined;
324
+ case 2:
325
+ if (!rightFile) {
326
+ rightFile = helper.findInput(workRootDir);
327
+ }
328
+ if (!rightFile) {
329
+ helper.cLog.error('common', 'find_input', '需要有一个入口文件 index 或者 src/index');
330
+ process.exit();
331
+ }
332
+ helper.cLog.log('rollup', 'input', "\u5165\u53E3\u6587\u4EF6\u8DEF\u5F84 ".concat(rightFile));
333
+ return _context.a(2, rightFile);
334
+ }
335
+ }, _callee);
336
+ }));
337
+ return function getInput(_x, _x2) {
338
+ return _ref.apply(this, arguments);
339
+ };
340
+ }();
341
+
342
+ var _excluded$3 = ["format"];
343
+ /**
344
+ * 生成单个output
345
+ * @description 如果不配置output,则默认生成cjs格式的output,输出路径为`lib/index.js`
346
+ * @description 自动组合完整的file
347
+ * @param optionsOutput
348
+ * @returns
349
+ */
350
+ var getOutput = function getOutput(envParams, options) {
351
+ var workRootDir = envParams.workRootDir;
352
+ if (!options) {
353
+ return {
354
+ file: path.join(workRootDir, 'lib/index.js'),
355
+ format: 'esm',
356
+ sourcemap: false
357
+ };
358
+ }
359
+ var format = options.format,
360
+ outputOptions = _objectWithoutProperties(options, _excluded$3);
361
+ var realFormat = format || 'esm';
362
+ return _objectSpread2({
363
+ format: realFormat,
364
+ sourcemap: false
365
+ }, outputOptions);
366
+ };
367
+
368
+ var _excluded$2 = ["entries"];
369
+
370
+ /**
371
+ * 获取别名插件
372
+ * @param config 快捷配置
373
+ * @param pluginConfig 标准配置
374
+ * @returns 别名插件
375
+ */
376
+ var getAliasPlugin = function getAliasPlugin(config, pluginConfig) {
377
+ var userConfig = config.alias,
378
+ tsConfigPath = config.tsConfigPath;
379
+ var aliasObj = helper.getTsPathAlias(tsConfigPath || []);
380
+ var _ref = pluginConfig || {},
381
+ entries = _ref.entries,
382
+ extPluginConfig = _objectWithoutProperties(_ref, _excluded$2);
383
+ return alias(_objectSpread2(_objectSpread2({}, extPluginConfig), {}, {
384
+ entries: _objectSpread2(_objectSpread2(_objectSpread2({}, entries), userConfig), aliasObj)
385
+ }));
386
+ };
387
+
388
+ /**
389
+ * 获取Babel插件
390
+ * @param _config 快捷配置
391
+ * @param pluginConfig 标准配置
392
+ * @returns Babel插件
393
+ */
394
+ var getBabelPlugin = function getBabelPlugin(_config, pluginConfig) {
395
+ return babel(_objectSpread2({
396
+ babelrc: false,
397
+ configFile: false,
398
+ compact: false,
399
+ cwd: filePro.getNodeModulesDir(filePro.getDirname(({ url: (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href)) }))),
400
+ extensions: ['.ts', '.tsx', '.js', '.jsx', '.json', '.mjs'],
401
+ ignore: ['node_modules/**'],
402
+ babelHelpers: 'bundled',
403
+ exclude: 'node_modules/**'
404
+ }, pluginConfig));
405
+ };
406
+
407
+ /**
408
+ * 获取CommonJS插件
409
+ * @description 将 CommonJS 转换成 ES2015 模块供 Rollup 处理
410
+ * @param _config
411
+ * @param pluginConfig
412
+ * @returns
413
+ */
414
+ var getCommonjsPlugin = function getCommonjsPlugin(_config, pluginConfig) {
415
+ return commonjs(pluginConfig);
416
+ };
417
+
418
+ var _excluded$1 = ["targets"];
419
+ /**
420
+ * 获取拷贝插件
421
+ * @param config 快捷配置
422
+ * @param pluginConfig 标准配置
423
+ * @returns 拷贝插件
424
+ */
425
+ var getCopyPlugin = function getCopyPlugin(config, pluginConfig) {
426
+ var _ref = config || {},
427
+ copyList = _ref.copyList;
428
+ var _ref2 = pluginConfig || {},
429
+ targets = _ref2.targets,
430
+ extPluginConfig = _objectWithoutProperties(_ref2, _excluded$1);
431
+ var resTargets = _toConsumableArray(targets || []);
432
+ if (copyList) {
433
+ resTargets.push.apply(resTargets, _toConsumableArray(copyList));
434
+ }
435
+ return copy(_objectSpread2(_objectSpread2({}, extPluginConfig), {}, {
436
+ targets: resTargets
437
+ }));
438
+ };
439
+
440
+ /**
441
+ * 获取 dts 插件
442
+ * @param _config
443
+ * @param pluginConfig
444
+ * @returns
445
+ */
446
+ var getDtsPlugin = function getDtsPlugin(_config, pluginConfig) {
447
+ return rollupPluginDts.dts(_objectSpread2({
448
+ respectExternal: false,
449
+ tsconfig: 'tsconfig.json',
450
+ compilerOptions: {}
451
+ }, pluginConfig));
452
+ };
453
+
454
+ /**
455
+ * 获取Json插件
456
+ * @description 允许将 .json 文件作为 ES6 模块导入
457
+ * @param _config
458
+ * @param pluginConfig
459
+ * @returns
460
+ */
461
+ var getJsonPlugin = function getJsonPlugin(_config, pluginConfig) {
462
+ return json(pluginConfig);
463
+ };
464
+
465
+ /**
466
+ * 获取代码压缩插件
467
+ * @returns Minimize插件
468
+ */
469
+ var getMinimizePlugin = function getMinimizePlugin() {
470
+ return terser();
471
+ };
472
+
473
+ var _excluded = ["plugins"];
474
+
475
+ /**
476
+ * 创建Postcss插件
477
+ * @param config 快捷配置
478
+ * @param pluginConfig 标准配置
479
+ * @returns Postcss插件
480
+ */
481
+ var getPostcssPlugin = function getPostcssPlugin(config, pluginConfig) {
482
+ var minimize = config.minimize,
483
+ output = config.output;
484
+ var _ref = pluginConfig || {},
485
+ injectPlugin = _ref.plugins,
486
+ otherConfig = _objectWithoutProperties(_ref, _excluded);
487
+ var outputWhole = path.isAbsolute(output) ? output : path.resolve(process.cwd(), output);
488
+ var plugins = [autoprefixer({
489
+ overrideBrowserslist: ['defaults', 'not ie < 8', 'last 2 versions', '> 1%', 'iOS 7', 'last 3 iOS versions']
490
+ })].concat(_toConsumableArray(injectPlugin || []));
491
+ if (minimize) {
492
+ plugins.push(cssnanoPlugin());
493
+ }
494
+
495
+ /**
496
+ * 输出文件锁,防止多次写入
497
+ * 当输出n个格式时,会被同时调用n次
498
+ */
499
+ var codeFileNameLock = '';
500
+ return postcss(_objectSpread2({
501
+ plugins: plugins,
502
+ extract: true,
503
+ onExtract: function () {
504
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(getExtracted) {
505
+ var _ref3, code, codeFileName, dir;
506
+ return _regenerator().w(function (_context) {
507
+ while (1) switch (_context.n) {
508
+ case 0:
509
+ _ref3 = getExtracted(), code = _ref3.code, codeFileName = _ref3.codeFileName;
510
+ if (!(codeFileNameLock === codeFileName)) {
511
+ _context.n = 1;
512
+ break;
513
+ }
514
+ return _context.a(2, false);
515
+ case 1:
516
+ codeFileNameLock = codeFileName;
517
+ if (filePro.isValidFilePathSync(outputWhole)) {
518
+ fs.rmSync(outputWhole, {
519
+ recursive: true,
520
+ force: true
521
+ });
522
+ }
523
+ dir = path.dirname(outputWhole);
524
+ _context.n = 2;
525
+ return filePro.mkdirRecursive(dir);
526
+ case 2:
527
+ _context.n = 3;
528
+ return promises.writeFile(outputWhole, code);
529
+ case 3:
530
+ helper.cLog.info('style', 'success', "\u8F93\u51FA\u7C7B\u578B => css\uFF1B", "\u8DEF\u5F84 =>".concat(path.relative(filePro.getCwd(), outputWhole)));
531
+ codeFileNameLock = '';
532
+ return _context.a(2, false);
533
+ }
534
+ }, _callee);
535
+ }));
536
+ return function onExtract(_x) {
537
+ return _ref2.apply(this, arguments);
538
+ };
539
+ }()
540
+ }, otherConfig));
541
+ };
542
+
543
+ /**
544
+ * 获取解析插件
545
+ * @param config 快捷配置
546
+ * @param pluginConfig 标准配置
547
+ * @returns 解析插件
548
+ */
549
+ var getResolvePlugin = function getResolvePlugin(config, pluginConfig) {
550
+ var extensions = config.extensions;
551
+ return resolve(_objectSpread2({
552
+ extensions: extensions,
553
+ preferBuiltins: true
554
+ }, pluginConfig));
555
+ };
556
+
557
+ /**
558
+ * 获取Node环境下的插件集合
559
+ * @param config 快捷配置
560
+ * @returns
561
+ */
562
+ var getNodePlugins = function getNodePlugins(config) {
563
+ var alias = config.alias,
564
+ minimize = config.minimize;
565
+ config.copyMap;
566
+ var plugins = [getResolvePlugin({
567
+ extensions: ['.js', '.ts', '.json']
568
+ }, {
569
+ preferBuiltins: true
570
+ }), getAliasPlugin({
571
+ alias: _objectSpread2({
572
+ '@': 'src'
573
+ }, alias)
574
+ }), getCopyPlugin({
575
+ }), getCommonjsPlugin(), getJsonPlugin()];
576
+ plugins.push(getBabelPlugin({
577
+ }, presetBabel.nodeBabelPreset()));
578
+ if (minimize) {
579
+ plugins.push(getMinimizePlugin());
580
+ }
581
+ return plugins;
582
+ };
583
+
584
+ /**
585
+ * 获取Node环境下的插件集合
586
+ * @param config 快捷配置
587
+ * @returns
588
+ */
589
+ var getReactPlugins = /*#__PURE__*/function () {
590
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(config) {
591
+ var alias, minimize, cssOutput, copy, plugins, _t, _t2, _t3, _t4, _t5, _t6;
592
+ return _regenerator().w(function (_context) {
593
+ while (1) switch (_context.n) {
594
+ case 0:
595
+ alias = config.alias, minimize = config.minimize, cssOutput = config.cssOutput, copy = config.copy;
596
+ _context.n = 1;
597
+ return getPostcssPlugin({
598
+ minimize: !!minimize,
599
+ output: cssOutput || 'lib/css/index.css'
600
+ });
601
+ case 1:
602
+ _t = _context.v;
603
+ _t2 = getResolvePlugin({
604
+ extensions: ['.ts', '.tsx', '.js', '.jsx', '.json', '.mjs']
605
+ }, {
606
+ preferBuiltins: true
607
+ });
608
+ _t3 = getAliasPlugin({
609
+ alias: _objectSpread2({
610
+ '@': 'src'
611
+ }, alias)
612
+ });
613
+ _t4 = getCopyPlugin({
614
+ copyList: copy
615
+ });
616
+ _t5 = getCommonjsPlugin();
617
+ _t6 = getJsonPlugin();
618
+ plugins = [_t, _t2, _t3, _t4, _t5, _t6];
619
+ plugins.push(getBabelPlugin({
620
+ }, presetBabel.reactBabelPreset()));
621
+ if (minimize) {
622
+ plugins.push(getMinimizePlugin());
623
+ }
624
+ return _context.a(2, plugins);
625
+ }
626
+ }, _callee);
627
+ }));
628
+ return function getReactPlugins(_x) {
629
+ return _ref.apply(this, arguments);
630
+ };
631
+ }();
632
+
633
+ /**
634
+ * 获取watch配置
635
+ * @param options
636
+ * @returns
637
+ */
638
+ var getWatch = function getWatch(envParams, options) {
639
+ var workRootDir = envParams.workRootDir;
640
+ return _objectSpread2({
641
+ exclude: 'node_modules/**',
642
+ include: path.join(workRootDir, 'src/**'),
643
+ clearScreen: false,
644
+ skipWrite: false,
645
+ buildDelay: 500,
646
+ chokidar: {
647
+ usePolling: true
648
+ }
649
+ }, options);
650
+ };
651
+
652
+ /**
653
+ * Create a Rollup build with the given options.
654
+ * @param options RollupOptions
655
+ * @returns
656
+ */
657
+ var createRollupBuild = function createRollupBuild(options) {
658
+ return rollup.rollup(options);
659
+ };
660
+
661
+ var Ctx = /*#__PURE__*/function () {
662
+ function Ctx(configCtx) {
663
+ _classCallCheck(this, Ctx);
664
+ _defineProperty(this, "configCtx", void 0);
665
+ _defineProperty(this, "rollupOptions", {});
666
+ _defineProperty(this, "rollupOutputList", []);
667
+ _defineProperty(this, "envParams", {
668
+ action: 'build',
669
+ workRootDir: filePro.getCwd()
670
+ });
671
+ this.configCtx = configCtx;
672
+ }
673
+ return _createClass(Ctx, [{
674
+ key: "updateEnvParams",
675
+ value: function updateEnvParams(argv) {
676
+ this.envParams = _objectSpread2(_objectSpread2({}, this.envParams), argv);
677
+ }
678
+ }, {
679
+ key: "updateRollupOptions",
680
+ value: function updateRollupOptions(options) {
681
+ this.rollupOptions = _objectSpread2(_objectSpread2({}, this.rollupOptions), options);
682
+ }
683
+ }, {
684
+ key: "updateRollupOutputList",
685
+ value: function updateRollupOutputList(list) {
686
+ var force = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
687
+ if (force) {
688
+ this.rollupOutputList = list;
689
+ } else {
690
+ this.rollupOutputList = [].concat(_toConsumableArray(this.rollupOutputList), _toConsumableArray(list));
691
+ }
692
+ }
693
+ }, {
694
+ key: "getDoraConfig",
695
+ value: function getDoraConfig() {
696
+ return this.configCtx.getDoraConfig();
697
+ }
698
+ }, {
699
+ key: "geTDoraConfigRollup",
700
+ value: function geTDoraConfigRollup() {
701
+ return this.configCtx.getDoraConfig().rollupConfig || {};
702
+ }
703
+ }]);
704
+ }();
705
+
706
+ /**
707
+ * Get a function to write the Rollup bundle with the given output options.
708
+ * @param rollupBuild RollupBuild
709
+ * @returns function that takes output options and writes the bundle
710
+ */
711
+ var getRollupWrite = function getRollupWrite(rollupBuild) {
712
+ return function (output) {
713
+ return rollupBuild.write(output);
714
+ };
715
+ };
716
+
717
+ /**
718
+ * 打包ts
719
+ * @param config 配置
720
+ */
721
+ var dealDts = /*#__PURE__*/function () {
722
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(config) {
723
+ var input, output, dtsBuild, _t;
724
+ return _regenerator().w(function (_context) {
725
+ while (1) switch (_context.p = _context.n) {
726
+ case 0:
727
+ input = config.input, output = config.output;
728
+ _context.p = 1;
729
+ _context.n = 2;
730
+ return createRollupBuild({
731
+ input: input,
732
+ external: function external(id) {
733
+ if (/\.(css|less|scss|sass|styl|png|jpe?g|svg|gif|webp|ico)$/.test(id)) {
734
+ return true;
735
+ }
736
+ return false;
737
+ },
738
+ plugins: [getResolvePlugin({
739
+ extensions: ['.ts']
740
+ }, {
741
+ preferBuiltins: true
742
+ }), getDtsPlugin()]
743
+ });
744
+ case 2:
745
+ dtsBuild = _context.v;
746
+ _context.n = 3;
747
+ return getRollupWrite(dtsBuild)({
748
+ file: output
749
+ });
750
+ case 3:
751
+ _context.n = 5;
752
+ break;
753
+ case 4:
754
+ _context.p = 4;
755
+ _t = _context.v;
756
+ helper.cLog.error('dts ', 'dts_error', 'dts构建捕获到错误');
757
+ console.log(_t);
758
+ throw new Error(_t);
759
+ case 5:
760
+ return _context.a(2);
761
+ }
762
+ }, _callee, null, [[1, 4]]);
763
+ }));
764
+ return function dealDts(_x) {
765
+ return _ref.apply(this, arguments);
766
+ };
767
+ }();
768
+
769
+ /**
770
+ * 从rollup配置中,获取第一个input
771
+ * @param inputOrigin
772
+ * @returns
773
+ */
774
+ var getFirstInput = function getFirstInput(inputOrigin) {
775
+ if (typeof inputOrigin === 'string') {
776
+ var name = filePro.getFilename(inputOrigin);
777
+ return {
778
+ path: inputOrigin,
779
+ name: name
780
+ };
781
+ } else if (Array.isArray(inputOrigin) && inputOrigin.length > 0) {
782
+ var firstInput = inputOrigin[0];
783
+ var _name = filePro.getFilename(firstInput);
784
+ return {
785
+ path: firstInput,
786
+ name: _name
787
+ };
788
+ } else if (lang.isObject(inputOrigin)) {
789
+ var firstKey = Object.keys(inputOrigin)[0];
790
+ var _firstInput = inputOrigin[firstKey];
791
+ return {
792
+ path: _firstInput,
793
+ name: firstKey
794
+ };
795
+ }
796
+ return undefined;
797
+ };
798
+
799
+ var rollupWatchLog = function rollupWatchLog(watcher) {
800
+ // event.code 可以是以下之一:
801
+ // START - 监视器正在(重新)启动
802
+ // BUNDLE_START - 单次打包
803
+ // * 如果存在,event.input 将是输入选项对象
804
+ // * event.output 包含生成的输出的 "file"
805
+ // 或 "dir" 选项值的数组
806
+ // BUNDLE_END - 完成打包
807
+ // * 如果存在,event.input 将是输入选项对象
808
+ // * event.output 包含生成的输出的 "file"
809
+ // 或 "dir" 选项值的数组
810
+ // * event.duration 是构建持续时间(以毫秒为单位)
811
+ // * event.result 包含 bundle 对象,
812
+ // 可以通过调用 bundle.generate
813
+ // 或 bundle.write 来生成其他输出。
814
+ // 当使用 watch.skipWrite 选项时,这尤其重要。
815
+ // 生成输出后,你应该调用 "event.result.close()",
816
+ // 或者如果你不生成输出,也应该调用。
817
+ // 这将允许插件通过
818
+ // "closeBundle" 钩子清理资源。
819
+ // END - 完成所有产物的构建
820
+ // ERROR - 在打包时遇到错误
821
+ // * event.error 包含抛出的错误
822
+ // * 对于构建错误,event.result 为 null,
823
+ // 对于输出生成错误,它包含 bundle 对象。
824
+ // 与 "BUNDLE_END" 一样,如果存在,
825
+ // 你应该在完成后调用 "event.result.close()"。
826
+ // 如果从事件处理程序返回一个 Promise,则 Rollup
827
+ // 将等待 Promise 解析后再继续。
828
+
829
+ watcher.on('event', function (event) {
830
+ var _event$result;
831
+ switch (event.code) {
832
+ case 'BUNDLE_END':
833
+ // 这将确保在每次运行后正确关闭打包
834
+ helper.cLog.info('serve', event.code, "\u5355\u6B21\u6253\u5305\u5B8C\u6210");
835
+ event.result.close();
836
+ break;
837
+ case 'ERROR':
838
+ helper.cLog.error('serve', event.code, "\u5355\u6B21\u6253\u5305\u6355\u83B7\u5230\u9519\u8BEF");
839
+ handleError(event.error, true);
840
+ (_event$result = event.result) === null || _event$result === void 0 || _event$result.close();
841
+ break;
842
+ case 'BUNDLE_START':
843
+ helper.cLog.log('serve', event.code, "\u5355\u6B21\u6253\u5305\u4E2D....");
844
+ break;
845
+ case 'END':
846
+ helper.cLog.info('serve', event.code, "\u5B8C\u6210\u5355\u6B21\u6784\u5EFA");
847
+ break;
848
+ case 'START':
849
+ helper.cLog.log('serve', event.code, "\u76D1\u89C6\u5668\u6B63\u5728\uFF08\u91CD\u65B0\uFF09\u542F\u52A8");
850
+ break;
851
+ }
852
+ });
853
+
854
+ // 此外,你可以挂钩以下内容。
855
+ // 同样,返回 Promise 以使 Rollup 在该阶段等待:
856
+ watcher.on('change', function (id, _ref) {
857
+ var event = _ref.event;
858
+ /* 更改了一个文件 */
859
+ helper.cLog.log('serve', event.toUpperCase(), "".concat('检测到文件变动'.padEnd(12, ' '), " \u27A9 ").concat(helper.picocolors.cyan(path.relative(filePro.getCwd(), id))));
860
+ });
861
+ watcher.on('restart', function () {
862
+ /* 新触发了一次运行 */
863
+ helper.cLog.log('serve', 'restart'.toUpperCase(), '新触发了一次运行');
864
+ });
865
+ watcher.on('close', function () {
866
+ /* 监视器被关闭了,请看下面的代码 */
867
+ });
868
+ };
869
+ var _picocolors$createCol = helper.picocolors.createColors(),
870
+ bold = _picocolors$createCol.bold,
871
+ cyan = _picocolors$createCol.cyan,
872
+ dim = _picocolors$createCol.dim,
873
+ red = _picocolors$createCol.red;
874
+
875
+ // log to stderr to keep `rollup main.js > bundle.js` from breaking
876
+ var stderr = function stderr() {
877
+ for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
878
+ parameters[_key] = arguments[_key];
879
+ }
880
+ return process.stderr.write("".concat(parameters.join(''), "\n"));
881
+ };
882
+ function handleError(error) {
883
+ var _error$cause;
884
+ var recover = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
885
+ var name = error.name || ((_error$cause = error.cause) === null || _error$cause === void 0 ? void 0 : _error$cause.name);
886
+ var nameSection = name ? "".concat(name, ": ") : '';
887
+ var pluginSection = error.plugin ? "(plugin ".concat(error.plugin, ") ") : '';
888
+ var message = "".concat(pluginSection).concat(nameSection).concat(error.message);
889
+ var outputLines = [bold(red("[!] ".concat(bold(message.toString()))))];
890
+ if (error.url) {
891
+ outputLines.push(cyan(error.url));
892
+ }
893
+ if (error.loc) {
894
+ outputLines.push(" (".concat(error.loc.line, ":").concat(error.loc.column, ")"));
895
+ }
896
+ if (error.frame) {
897
+ outputLines.push(dim(error.frame));
898
+ }
899
+ if (error.stack) {
900
+ var _error$stack;
901
+ outputLines.push(dim((_error$stack = error.stack) === null || _error$stack === void 0 ? void 0 : _error$stack.replace("".concat(nameSection).concat(error.message, "\n"), '')));
902
+ }
903
+ outputLines.push('', '');
904
+ stderr(outputLines.join('\n'));
905
+ if (!recover) process.exit(1);
906
+ }
907
+
908
+ var rollupWatch = function rollupWatch(config) {
909
+ return rollup.watch(config);
910
+ };
911
+
912
+ exports.Ctx = Ctx;
913
+ exports.createRollupBuild = createRollupBuild;
914
+ exports.dealDts = dealDts;
915
+ exports.getAliasPlugin = getAliasPlugin;
916
+ exports.getBabelPlugin = getBabelPlugin;
917
+ exports.getCommonjsPlugin = getCommonjsPlugin;
918
+ exports.getCopyPlugin = getCopyPlugin;
919
+ exports.getDtsPlugin = getDtsPlugin;
920
+ exports.getExternal = getExternal;
921
+ exports.getFirstInput = getFirstInput;
922
+ exports.getInput = getInput;
923
+ exports.getJsonPlugin = getJsonPlugin;
924
+ exports.getMinimizePlugin = getMinimizePlugin;
925
+ exports.getNodePlugins = getNodePlugins;
926
+ exports.getOutput = getOutput;
927
+ exports.getPostcssPlugin = getPostcssPlugin;
928
+ exports.getReactPlugins = getReactPlugins;
929
+ exports.getResolvePlugin = getResolvePlugin;
930
+ exports.getRollupWrite = getRollupWrite;
931
+ exports.getWatch = getWatch;
932
+ exports.handleError = handleError;
933
+ exports.rollupWatch = rollupWatch;
934
+ exports.rollupWatchLog = rollupWatchLog;