UNPKG

6.83 kBJavaScriptView Raw
1/**
2 * Copyright 2015-present Desmond Yao
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Created by desmond on 4/16/17.
17 *
18 */
19
20'use strict';
21
22Object.defineProperty(exports, "__esModule", {
23 value: true
24});
25exports.isReactNativeEntry = isReactNativeEntry;
26exports.isAssetModule = isAssetModule;
27exports.isEmptyStmt = isEmptyStmt;
28exports.getAssetConfig = getAssetConfig;
29exports.isModuleCall = isModuleCall;
30exports.isRequirePolyfillCondition = isRequirePolyfillCondition;
31exports.isPolyfillCall = isPolyfillCall;
32exports.isModuleDeclaration = isModuleDeclaration;
33exports.replaceModuleIdWithName = replaceModuleIdWithName;
34exports.getModuleDependency = getModuleDependency;
35exports.getModuleDependencyCodeRange = getModuleDependencyCodeRange;
36exports.ensureFolder = ensureFolder;
37exports.mkdirsSync = mkdirsSync;
38var fs = require('fs');
39var path = require('path');
40var MODULE_REGEX = /require\s?\(([0-9]+)[^)]*\)/g;
41var EXPR_STMT = 'ExpressionStatement';
42var EMPTY_STMT = 'EmptyStatement';
43var IF_STMT = 'IfStatement';
44
45var BINARY_EXPR = 'BinaryExpression';
46var LOGICAL_EXPR = 'LogicalExpression';
47var UNARY_EXPR = 'UnaryExpression';
48var CALL_EXPR = 'CallExpression';
49var FUNC_EXPR = 'FunctionExpression';
50var COND_EXPR = 'ConditionalExpression';
51var IDENTIFIER = 'Identifier';
52var LITERAL_NUM = 'NumericLiteral';
53var LITERAL_STR = 'StringLiteral';
54
55var DEFAULT_ASSET_EXTS = ['bmp', 'gif', 'jpg', 'jpeg', 'png', 'psd', 'svg', 'webp', // Image formats
56'm4v', 'mov', 'mp4', 'mpeg', 'mpg', 'webm', // Video formats
57'aac', 'aiff', 'caf', 'm4a', 'mp3', 'wav', // Audio formats
58'html', 'pdf'];
59
60function isReactNativeEntry(moduleName) {
61 return moduleName === 'react-native-implementation' || moduleName === 'react-native/Libraries/react-native/react-native.js';
62}
63
64function isAssetModule(moduleName) {
65 var ext = moduleName.substring(moduleName.lastIndexOf('.') + 1);
66 return DEFAULT_ASSET_EXTS.indexOf(ext) > 0;
67}
68
69function isEmptyStmt(node) {
70 try {
71 return node.type === EMPTY_STMT;
72 } catch (e) {
73 return false;
74 }
75}
76
77function getAssetConfig(node) {
78 var func = node.expression.arguments[0];
79 var rhs = func.body.body[0].expression.right; //require(240).registerAsset({...})
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
102function 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
110function 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
121function 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
136function 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
144function 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
155function 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
165function 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
178function 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 */
191function mkdirsSync(dirname) {
192 //console.log(dirname);
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// export function resolvePathArrays(root: string, array : Array<any>, val ?: string) : Array<any> {
204// const newArr = [];
205// array.forEach(item => {
206// if (val) {
207// let newItem = Object.assign({}, item);
208// newItem[val] = path.resolve(root, item[val]);
209// newArr.push(newItem);
210// } else if (typeof item === 'string') {
211// newArr.push(path.resolve(root, item));
212// }
213// });
214// return newArr;
215// }
\No newline at end of file