1 | 'use strict';
|
2 |
|
3 | const template = require('@babel/template').default;
|
4 |
|
5 | const config = require('./config');
|
6 |
|
7 | const {
|
8 | TEST_ID
|
9 | } = require('./const');
|
10 |
|
11 | const {
|
12 | getName,
|
13 | getId,
|
14 | getNode
|
15 | } = require('./utils');
|
16 |
|
17 | const build = template(`
|
18 | COMPONENT.PROP = ID
|
19 | `);
|
20 | const buildDefaultExport = template(`
|
21 | if (module.exports) {
|
22 | module.exports.default.PROP = ID
|
23 | }
|
24 | `);
|
25 |
|
26 | module.exports = ({
|
27 | types: t
|
28 | }) => ({
|
29 | visitor: {
|
30 | JSXElement(p, {
|
31 | file
|
32 | }) {
|
33 | const {
|
34 | openingElement,
|
35 | rootPath,
|
36 | componentNode
|
37 | } = getNode(t, p) || {};
|
38 | if (!openingElement) return;
|
39 | const {
|
40 | filename
|
41 | } = file.opts;
|
42 | const name = getName({
|
43 | rootPath,
|
44 | componentNode
|
45 | });
|
46 | const id = getId(filename, name);
|
47 | const propName = `${config.prefix}${id}`;
|
48 | const prop = config.env ? t.jSXSpreadAttribute(t.identifier(`process.env.RESELECTOR === "true" ? {'${propName}': true} : {}`)) : t.JSXAttribute(t.JSXIdentifier(propName));
|
49 | openingElement.attributes.push(prop);
|
50 |
|
51 | if (process.env.NODE_ENV !== 'test') {
|
52 | return;
|
53 | }
|
54 |
|
55 | rootPath.insertAfter(name === 'default' ? buildDefaultExport({
|
56 | ID: t.StringLiteral(id),
|
57 | PROP: t.identifier(TEST_ID)
|
58 | }) : build({
|
59 | COMPONENT: t.identifier(name),
|
60 | ID: t.StringLiteral(id),
|
61 | PROP: t.identifier(TEST_ID)
|
62 | }));
|
63 | }
|
64 |
|
65 | }
|
66 | }); |
\ | No newline at end of file |