1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
6 |
|
7 | const Asset = require('../Asset');
|
8 |
|
9 | const micromatch = require('micromatch');
|
10 |
|
11 | const path = require('path');
|
12 |
|
13 | const _require = require('../utils/glob'),
|
14 | glob = _require.glob;
|
15 |
|
16 | class GlobAsset extends Asset {
|
17 | constructor(name, options) {
|
18 | super(name, options);
|
19 | this.type = null;
|
20 | }
|
21 |
|
22 | load() {
|
23 | var _this = this;
|
24 |
|
25 | return (0, _asyncToGenerator2.default)(function* () {
|
26 | let regularExpressionSafeName = _this.name;
|
27 | if (process.platform === 'win32') regularExpressionSafeName = regularExpressionSafeName.replace(/\\/g, '/');
|
28 | let files = yield glob(regularExpressionSafeName, {
|
29 | onlyFiles: true
|
30 | });
|
31 | let re = micromatch.makeRe(regularExpressionSafeName, {
|
32 | capture: true
|
33 | });
|
34 | let matches = {};
|
35 | var _iteratorNormalCompletion = true;
|
36 | var _didIteratorError = false;
|
37 | var _iteratorError = undefined;
|
38 |
|
39 | try {
|
40 | for (var _iterator = files[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
41 | let file = _step.value;
|
42 | let match = file.match(re);
|
43 | let parts = match.slice(1).filter(Boolean).reduce((a, p) => a.concat(p.split('/')), []);
|
44 | let relative = './' + path.relative(path.dirname(_this.name), file.normalize('NFC'));
|
45 | set(matches, parts, relative);
|
46 |
|
47 | _this.addDependency(relative);
|
48 | }
|
49 | } catch (err) {
|
50 | _didIteratorError = true;
|
51 | _iteratorError = err;
|
52 | } finally {
|
53 | try {
|
54 | if (!_iteratorNormalCompletion && _iterator.return != null) {
|
55 | _iterator.return();
|
56 | }
|
57 | } finally {
|
58 | if (_didIteratorError) {
|
59 | throw _iteratorError;
|
60 | }
|
61 | }
|
62 | }
|
63 |
|
64 | return matches;
|
65 | })();
|
66 | }
|
67 |
|
68 | generate() {
|
69 | return [{
|
70 | type: 'js',
|
71 | value: 'module.exports = ' + generate(this.contents) + ';'
|
72 | }];
|
73 | }
|
74 |
|
75 | }
|
76 |
|
77 | function generate(matches, indent = '') {
|
78 | if (typeof matches === 'string') {
|
79 | return `require(${JSON.stringify(matches)})`;
|
80 | }
|
81 |
|
82 | let res = indent + '{';
|
83 | let first = true;
|
84 |
|
85 | for (let key in matches) {
|
86 | if (!first) {
|
87 | res += ',';
|
88 | }
|
89 |
|
90 | res += `\n${indent} ${JSON.stringify(key)}: ${generate(matches[key], indent + ' ')}`;
|
91 | first = false;
|
92 | }
|
93 |
|
94 | res += '\n' + indent + '}';
|
95 | return res;
|
96 | }
|
97 |
|
98 | function set(obj, path, value) {
|
99 | for (let i = 0; i < path.length - 1; i++) {
|
100 | let part = path[i];
|
101 |
|
102 | if (obj[part] == null) {
|
103 | obj[part] = {};
|
104 | }
|
105 |
|
106 | obj = obj[part];
|
107 | }
|
108 |
|
109 | obj[path[path.length - 1]] = value;
|
110 | }
|
111 |
|
112 | module.exports = GlobAsset; |
\ | No newline at end of file |