1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | 'use strict';
|
21 |
|
22 | Object.defineProperty(exports, "__esModule", {
|
23 | value: true
|
24 | });
|
25 | exports.isReactNativeEntry = isReactNativeEntry;
|
26 | exports.isAssetModule = isAssetModule;
|
27 | exports.isEmptyStmt = isEmptyStmt;
|
28 | exports.getAssetConfig = getAssetConfig;
|
29 | exports.isModuleCall = isModuleCall;
|
30 | exports.isRequirePolyfillCondition = isRequirePolyfillCondition;
|
31 | exports.isPolyfillCall = isPolyfillCall;
|
32 | exports.isModuleDeclaration = isModuleDeclaration;
|
33 | exports.replaceModuleIdWithName = replaceModuleIdWithName;
|
34 | exports.getModuleDependency = getModuleDependency;
|
35 | exports.getModuleDependencyCodeRange = getModuleDependencyCodeRange;
|
36 | exports.ensureFolder = ensureFolder;
|
37 | exports.mkdirsSync = mkdirsSync;
|
38 | var fs = require('fs');
|
39 | var path = require('path');
|
40 | var MODULE_REGEX = /require\s?\(([0-9]+)[^)]*\)/g;
|
41 | var EXPR_STMT = 'ExpressionStatement';
|
42 | var EMPTY_STMT = 'EmptyStatement';
|
43 | var IF_STMT = 'IfStatement';
|
44 |
|
45 | var BINARY_EXPR = 'BinaryExpression';
|
46 | var LOGICAL_EXPR = 'LogicalExpression';
|
47 | var UNARY_EXPR = 'UnaryExpression';
|
48 | var CALL_EXPR = 'CallExpression';
|
49 | var FUNC_EXPR = 'FunctionExpression';
|
50 | var COND_EXPR = 'ConditionalExpression';
|
51 | var IDENTIFIER = 'Identifier';
|
52 | var LITERAL_NUM = 'NumericLiteral';
|
53 | var LITERAL_STR = 'StringLiteral';
|
54 |
|
55 | var DEFAULT_ASSET_EXTS = ['bmp', 'gif', 'jpg', 'jpeg', 'png', 'psd', 'svg', 'webp',
|
56 | 'm4v', 'mov', 'mp4', 'mpeg', 'mpg', 'webm',
|
57 | 'aac', 'aiff', 'caf', 'm4a', 'mp3', 'wav',
|
58 | 'html', 'pdf'];
|
59 |
|
60 | function isReactNativeEntry(moduleName) {
|
61 | return moduleName === 'react-native-implementation' || moduleName === 'react-native/Libraries/react-native/react-native.js';
|
62 | }
|
63 |
|
64 | function isAssetModule(moduleName) {
|
65 | var ext = moduleName.substring(moduleName.lastIndexOf('.') + 1);
|
66 | return DEFAULT_ASSET_EXTS.indexOf(ext) > 0;
|
67 | }
|
68 |
|
69 | function isEmptyStmt(node) {
|
70 | try {
|
71 | return node.type === EMPTY_STMT;
|
72 | } catch (e) {
|
73 | return false;
|
74 | }
|
75 | }
|
76 |
|
77 | function getAssetConfig(node) {
|
78 | var func = node.expression.arguments[0];
|
79 | var rhs = func.body.body[0].expression.right;
|
80 | var propNode = rhs.arguments[0].properties;
|
81 | var assetConfig = {
|
82 | code: {
|
83 | start: rhs.arguments[0].start,
|
84 | end: rhs.arguments[0].end
|
85 | }
|
86 | };
|
87 | propNode.forEach(function (prop) {
|
88 | var key = prop.key.value ? prop.key.value : prop.key.name;
|
89 | if (key === 'scales') {
|
90 | var value = [];
|
91 | prop.value.elements.forEach(function (scaleNode) {
|
92 | value.push(scaleNode.value);
|
93 | });
|
94 | assetConfig[key] = value;
|
95 | } else {
|
96 | assetConfig[key] = prop.value.value;
|
97 | }
|
98 | });
|
99 | return assetConfig;
|
100 | }
|
101 |
|
102 | function isModuleCall(node) {
|
103 | try {
|
104 | return node.type === EXPR_STMT && node.expression.type === CALL_EXPR && node.expression.callee.type === IDENTIFIER && node.expression.callee.name === 'require' && node.expression.arguments.length === 1 && node.expression.arguments[0].type === LITERAL_NUM;
|
105 | } catch (e) {
|
106 | return false;
|
107 | }
|
108 | }
|
109 |
|
110 | function isRequirePolyfillCondition(node, dev) {
|
111 | if (node.type === IF_STMT && node.test.type === LOGICAL_EXPR && node.test.left.name === '__DEV__' && node.test.operator === '&&' && node.test.right.type === BINARY_EXPR) {
|
112 | var binaryExpr = node.test.right;
|
113 | if (dev) {
|
114 | return binaryExpr.left.operator === 'typeof' && binaryExpr.operator === '===' && binaryExpr.right.type === LITERAL_STR;
|
115 | } else {
|
116 | return binaryExpr.left.type === LITERAL_STR && binaryExpr.operator === '==' && binaryExpr.right.operator === 'typeof';
|
117 | }
|
118 | }
|
119 | }
|
120 |
|
121 | function isPolyfillCall(node, dev) {
|
122 | try {
|
123 | var isPolyfillCallExpr = function isPolyfillCallExpr(expr) {
|
124 | return expr.type === CALL_EXPR && expr.callee.type === FUNC_EXPR && expr.callee.params.length === 1 && expr.callee.params[0].type === IDENTIFIER && expr.arguments.length === 1 && expr.arguments[0].type === COND_EXPR;
|
125 | };
|
126 | if (dev) {
|
127 | return node.type === EXPR_STMT && isPolyfillCallExpr(node.expression);
|
128 | } else {
|
129 | return node.type === EXPR_STMT && node.expression.type === UNARY_EXPR && isPolyfillCallExpr(node.expression.argument);
|
130 | }
|
131 | } catch (e) {
|
132 | return false;
|
133 | }
|
134 | }
|
135 |
|
136 | function isModuleDeclaration(node) {
|
137 | try {
|
138 | return node.type === EXPR_STMT && node.expression.type === CALL_EXPR && node.expression.callee.type === IDENTIFIER && node.expression.callee.name === '__d';
|
139 | } catch (e) {
|
140 | return false;
|
141 | }
|
142 | }
|
143 |
|
144 | function replaceModuleIdWithName(codeBlob, modules) {
|
145 | var dependencies = getModuleDependencyCodeRange(codeBlob, 0, codeBlob.length);
|
146 | if (dependencies) {
|
147 | dependencies.forEach(function (deps) {
|
148 | var moduleName = modules[deps.module].name;
|
149 | codeBlob = codeBlob.replace(deps.code, 'require(\"' + moduleName + '\")');
|
150 | });
|
151 | }
|
152 | return codeBlob;
|
153 | }
|
154 |
|
155 | function getModuleDependency(codeBlob, start, end) {
|
156 | var dependency = [];
|
157 | var bodyString = codeBlob.substring(start, end);
|
158 | var result = void 0;
|
159 | while (result = MODULE_REGEX.exec(bodyString)) {
|
160 | dependency.push(parseInt(result[1]));
|
161 | }
|
162 | return dependency;
|
163 | }
|
164 |
|
165 | function getModuleDependencyCodeRange(codeBlob, start, end) {
|
166 | var dependency = [];
|
167 | var bodyString = codeBlob.substring(start, end);
|
168 | var result = void 0;
|
169 | while (result = MODULE_REGEX.exec(bodyString)) {
|
170 | dependency.push({
|
171 | code: result[0],
|
172 | module: parseInt(result[1])
|
173 | });
|
174 | }
|
175 | return dependency;
|
176 | }
|
177 |
|
178 | function ensureFolder(dir) {
|
179 | try {
|
180 | fs.accessSync(dir, fs.F_OK);
|
181 | return true;
|
182 | } catch (e) {
|
183 | fs.mkdirSync(dir);
|
184 | return false;
|
185 | }
|
186 | }
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | function mkdirsSync(dirname) {
|
192 |
|
193 | if (fs.existsSync(dirname)) {
|
194 | return true;
|
195 | } else {
|
196 | if (mkdirsSync(path.dirname(dirname))) {
|
197 | fs.mkdirSync(dirname);
|
198 | return true;
|
199 | }
|
200 | }
|
201 | }
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
\ | No newline at end of file |