1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = void 0;
|
5 |
|
6 | var _helperModuleImports = require("@babel/helper-module-imports");
|
7 |
|
8 | var t = _interopRequireWildcard(require("@babel/types"));
|
9 |
|
10 | var _truthy = _interopRequireDefault(require("./truthy"));
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
15 |
|
16 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
17 |
|
18 | function findLast(array, predicate) {
|
19 | for (let i = array.length - 1; i >= 0; --i) {
|
20 | if (predicate(array[i], i, array)) return array[i];
|
21 | }
|
22 |
|
23 | return undefined;
|
24 | }
|
25 |
|
26 | function isRequire(path) {
|
27 | const isRequireExpression = p => p.isCallExpression() && p.get('callee').isIdentifier() && p.get('callee').node.name === 'require';
|
28 |
|
29 | if (!path) return false;
|
30 |
|
31 | if (path.isVariableDeclaration()) {
|
32 | return path.get('declarations').some(d => d && isRequireExpression(d.get('init')));
|
33 | }
|
34 |
|
35 | return isRequireExpression(path);
|
36 | }
|
37 |
|
38 | class StyleImportInjector {
|
39 | constructor(program) {
|
40 | this.nodes = new Set();
|
41 | this.code = new WeakMap();
|
42 | this.program = program;
|
43 | }
|
44 |
|
45 | add(style) {
|
46 | const {
|
47 | scope
|
48 | } = this.program;
|
49 | const source = style.requirePath;
|
50 | const useEsm = (0, _helperModuleImports.isModule)(this.program);
|
51 | const ident = scope.generateUidIdentifier(style.identifier);
|
52 |
|
53 | if (useEsm) {
|
54 | const importNode = t.importDeclaration([t.importDefaultSpecifier(ident)], t.stringLiteral(source));
|
55 | this.nodes.add(importNode);
|
56 | this.code.set(importNode, `import ${ident.name} from "${source}";`);
|
57 | } else {
|
58 | const importNode = t.variableDeclaration('const', [t.variableDeclarator(ident, t.callExpression(t.identifier('require'), [t.stringLiteral(source)]))]);
|
59 | this.nodes.add(importNode);
|
60 | this.code.set(importNode, `const ${ident.name} = require("${source}");`);
|
61 | }
|
62 |
|
63 | return ident;
|
64 | }
|
65 |
|
66 | inject() {
|
67 | var _targetPath$node;
|
68 |
|
69 | const targetPath = findLast(this.program.get('body'), p => {
|
70 | var _p$node$_blockHoist;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | const blockHoist = (_p$node$_blockHoist = p.node._blockHoist) != null ? _p$node$_blockHoist : 1;
|
79 |
|
80 | return blockHoist > 0 && (p.isImportDeclaration() || isRequire(p));
|
81 | });
|
82 | const nodes = Array.from(this.nodes).reverse();
|
83 | const end = (targetPath == null ? void 0 : (_targetPath$node = targetPath.node) == null ? void 0 : _targetPath$node.end) || 0;
|
84 | const changes = {
|
85 | type: 'style-imports',
|
86 | end,
|
87 | start: end,
|
88 | code: `\n${Array.from(this.nodes, n => this.code.get(n)).filter(_truthy.default).join('\n')}\n`
|
89 | };
|
90 |
|
91 | if (!targetPath) {
|
92 |
|
93 | this.program.unshiftContainer('body', nodes);
|
94 | } else {
|
95 | for (const node of nodes) {
|
96 | targetPath.insertAfter(node);
|
97 | }
|
98 | }
|
99 |
|
100 | this.code = new WeakMap();
|
101 | this.nodes.clear();
|
102 | return changes;
|
103 | }
|
104 |
|
105 | }
|
106 |
|
107 | exports.default = StyleImportInjector; |
\ | No newline at end of file |