|
10 | 10 |
|
11 | 11 | 'use strict'; |
12 | 12 |
|
| 13 | +const template = require('@babel/template').default; |
13 | 14 | const babelTypes = require('@babel/types'); |
14 | 15 | const babylon = require('babylon'); |
15 | 16 |
|
@@ -58,33 +59,17 @@ function generateAssetCodeFileAst( |
58 | 59 | ); |
59 | 60 | const t = babelTypes; |
60 | 61 |
|
61 | | - // module.exports |
62 | | - const moduleExports = t.memberExpression( |
63 | | - t.identifier('module'), |
64 | | - t.identifier('exports'), |
65 | | - ); |
66 | | - |
67 | | - // require('AssetRegistry') |
68 | | - const requireCall = t.callExpression(t.identifier('require'), [ |
69 | | - t.stringLiteral(assetRegistryPath), |
70 | | - ]); |
71 | | - |
72 | | - // require('AssetRegistry').registerAsset |
73 | | - const registerAssetFunction = t.memberExpression( |
74 | | - requireCall, |
75 | | - t.identifier('registerAsset'), |
76 | | - ); |
77 | | - |
78 | 62 | // require('AssetRegistry').registerAsset({...}) |
79 | | - const registerAssetCall = t.callExpression(registerAssetFunction, [ |
80 | | - descriptorAst, |
81 | | - ]); |
| 63 | + const buildRequire = template(` |
| 64 | + module.exports = require(ASSET_REGISTRY_PATH).registerAsset(DESCRIPTOR_AST) |
| 65 | + `); |
82 | 66 |
|
83 | 67 | return t.file( |
84 | 68 | t.program([ |
85 | | - t.expressionStatement( |
86 | | - t.assignmentExpression('=', moduleExports, registerAssetCall), |
87 | | - ), |
| 69 | + buildRequire({ |
| 70 | + ASSET_REGISTRY_PATH: t.stringLiteral(assetRegistryPath), |
| 71 | + DESCRIPTOR_AST: descriptorAst, |
| 72 | + }), |
88 | 73 | ]), |
89 | 74 | ); |
90 | 75 | } |
@@ -114,56 +99,39 @@ function generateRemoteAssetCodeFileAst( |
114 | 99 | data[+scale] = descriptor[+scale].handle; |
115 | 100 | } |
116 | 101 |
|
117 | | - // require('AssetSourceResolver') |
118 | | - const requireCall = t.callExpression(t.identifier('require'), [ |
119 | | - t.stringLiteral(assetSourceResolverPath), |
120 | | - ]); |
121 | | - |
122 | | - // require('AssetSourceResolver').pickScale |
123 | | - const pickScale = t.memberExpression(requireCall, t.identifier('pickScale')); |
124 | | - |
125 | | - // require('AssetSourceResolver').pickScale([2, 3, ...]) |
126 | | - const call = t.callExpression(pickScale, [ |
127 | | - t.arrayExpression( |
128 | | - Object.keys(descriptor) |
129 | | - .map(Number) |
130 | | - .sort((a, b) => a - b) |
131 | | - .map(scale => t.numericLiteral(scale)), |
132 | | - ), |
133 | | - ]); |
134 | | - |
135 | 102 | // {2: 'path/to/image@2x', 3: 'path/to/image@3x', ...} |
136 | 103 | const astData = babylon.parseExpression(JSON.stringify(data)); |
137 | 104 |
|
138 | | - // ({2: '...', 3: '...'})[require(...).pickScale(...)] |
139 | | - const handler = t.memberExpression(astData, call, true); |
140 | | - |
141 | | - // 'https://remote.server.com/' + ({2: ...})[require(...).pickScale(...)] |
142 | | - const uri = t.binaryExpression('+', t.stringLiteral(remoteServer), handler); |
| 105 | + // URI to remote server |
| 106 | + const URI = t.stringLiteral(remoteServer); |
143 | 107 |
|
144 | 108 | // Size numbers. |
145 | | - const width = t.numericLiteral(assetDescriptor.width); |
146 | | - const height = t.numericLiteral(assetDescriptor.height); |
| 109 | + const WIDTH = t.numericLiteral(assetDescriptor.width); |
| 110 | + const HEIGHT = t.numericLiteral(assetDescriptor.height); |
147 | 111 |
|
148 | | - // module.exports |
149 | | - const moduleExports = t.memberExpression( |
150 | | - t.identifier('module'), |
151 | | - t.identifier('exports'), |
152 | | - ); |
| 112 | + const buildRequire = template(` |
| 113 | + module.exports = { |
| 114 | + "width": WIDTH, |
| 115 | + "height": HEIGHT, |
| 116 | + "uri": URI + OBJECT_AST[require(ASSET_SOURCE_RESOLVER_PATH).pickScale(SCALE_ARRAY)] |
| 117 | + }; |
| 118 | + `); |
153 | 119 |
|
154 | 120 | return t.file( |
155 | 121 | t.program([ |
156 | | - t.expressionStatement( |
157 | | - t.assignmentExpression( |
158 | | - '=', |
159 | | - moduleExports, |
160 | | - t.objectExpression([ |
161 | | - t.objectProperty(t.stringLiteral('width'), width), |
162 | | - t.objectProperty(t.stringLiteral('height'), height), |
163 | | - t.objectProperty(t.stringLiteral('uri'), uri), |
164 | | - ]), |
| 122 | + buildRequire({ |
| 123 | + WIDTH, |
| 124 | + HEIGHT, |
| 125 | + URI, |
| 126 | + OBJECT_AST: astData, |
| 127 | + ASSET_SOURCE_RESOLVER_PATH: t.stringLiteral(assetSourceResolverPath), |
| 128 | + SCALE_ARRAY: t.arrayExpression( |
| 129 | + Object.keys(descriptor) |
| 130 | + .map(Number) |
| 131 | + .sort((a, b) => a - b) |
| 132 | + .map(scale => t.numericLiteral(scale)), |
165 | 133 | ), |
166 | | - ), |
| 134 | + }), |
167 | 135 | ]), |
168 | 136 | ); |
169 | 137 | } |
|
0 commit comments