-
Notifications
You must be signed in to change notification settings - Fork 598
/
inline-plugin.js
153 lines (127 loc) Β· 4.15 KB
/
inline-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
const createInlinePlatformChecks = require('./utils/createInlinePlatformChecks');
import type {Ast} from '@babel/core';
import type {Path} from '@babel/traverse';
import type {Types} from '@babel/types';
export type Options = {
dev: boolean,
inlinePlatform: boolean,
isWrapped: boolean,
requireName?: string,
platform: string,
...
};
type State = {opts: Options, ...};
export type Visitors = {|
visitor: {|
CallExpression: (path: Path, state: State) => void,
Identifier: (path: Path, state: State) => void,
MemberExpression: (path: Path, state: State) => void,
|},
|};
const env = {name: 'env'};
const nodeEnv = {name: 'NODE_ENV'};
const processId = {name: 'process'};
const dev = {name: '__DEV__'};
function inlinePlugin(
{types: t}: {types: Types, ...},
options: Options,
): Visitors {
const {isPlatformNode, isPlatformSelectNode} = createInlinePlatformChecks(
t,
options.requireName || 'require',
);
const isGlobal = binding => !binding;
const isFlowDeclared = binding => t.isDeclareVariable(binding.path);
const isGlobalOrFlowDeclared = binding =>
isGlobal(binding) || isFlowDeclared(binding);
const isLeftHandSideOfAssignmentExpression = (node: Ast, parent) =>
t.isAssignmentExpression(parent) && parent.left === node;
const isProcessEnvNodeEnv = (node: Ast, scope) =>
t.isIdentifier(node.property, nodeEnv) &&
t.isMemberExpression(node.object) &&
t.isIdentifier(node.object.property, env) &&
t.isIdentifier(node.object.object, processId) &&
isGlobal(scope.getBinding(processId.name));
const isDev = (node: Ast, parent, scope) =>
t.isIdentifier(node, dev) &&
isGlobalOrFlowDeclared(scope.getBinding(dev.name)) &&
!t.isMemberExpression(parent);
function findProperty(objectExpression, key: string, fallback) {
const property = objectExpression.properties.find(p => {
if (t.isIdentifier(p.key) && p.key.name === key) {
return true;
}
if (t.isStringLiteral(p.key) && p.key.value === key) {
return true;
}
return false;
});
return property ? property.value : fallback();
}
function hasStaticProperties(objectExpression): boolean {
if (!t.isObjectExpression(objectExpression)) {
return false;
}
return objectExpression.properties.every(p => {
if (p.computed) {
return false;
}
return t.isIdentifier(p.key) || t.isStringLiteral(p.key);
});
}
return {
visitor: {
Identifier(path: Path, state: State): void {
if (!state.opts.dev && isDev(path.node, path.parent, path.scope)) {
path.replaceWith(t.booleanLiteral(state.opts.dev));
}
},
MemberExpression(path: Path, state: State): void {
const node = path.node;
const scope = path.scope;
const opts = state.opts;
if (!isLeftHandSideOfAssignmentExpression(node, path.parent)) {
if (
opts.inlinePlatform &&
isPlatformNode(node, scope, !!opts.isWrapped)
) {
path.replaceWith(t.stringLiteral(opts.platform));
} else if (!opts.dev && isProcessEnvNodeEnv(node, scope)) {
path.replaceWith(
t.stringLiteral(opts.dev ? 'development' : 'production'),
);
}
}
},
CallExpression(path: Path, state: State): void {
const node = path.node;
const scope = path.scope;
const arg = node.arguments[0];
const opts = state.opts;
if (
opts.inlinePlatform &&
isPlatformSelectNode(node, scope, !!opts.isWrapped)
) {
if (hasStaticProperties(arg)) {
const fallback = () =>
findProperty(arg, 'native', () =>
findProperty(arg, 'default', () => t.identifier('undefined')),
);
path.replaceWith(findProperty(arg, opts.platform, fallback));
}
}
},
},
};
}
module.exports = inlinePlugin;