-
Notifications
You must be signed in to change notification settings - Fork 193
/
assetLoader.ts
255 lines (226 loc) · 7.61 KB
/
assetLoader.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import utils from 'loader-utils';
import size from 'image-size';
import path from 'path';
import dedent from 'dedent';
import hasha from 'hasha';
import escapeStringRegexp from 'escape-string-regexp';
import Runtime from '../../runtime/Runtime';
import AssetResolver from '../resolvers/AssetResolver';
type Config = {
platform: string;
bundle?: boolean;
root: string;
outputPath?: string | ((path: string) => string);
publicPath?: string | ((path: string) => string);
};
async function assetLoader(this: any) {
this.cacheable();
const callback = this.async();
const { runtime: _runtime = new Runtime(), ...query } =
utils.getOptions(this) || {};
const runtime: Runtime = _runtime;
const config: Config = {
platform: 'ios',
root: process.cwd(),
...query,
};
const pathSepPattern = new RegExp(`\\${path.sep}`, 'g');
const filePath: string = this.resourcePath;
const dirname = path.dirname(filePath);
// Relative path to root without any ../ due to https://github.com/callstack/haul/issues/474
// Assets from from outside of root, should still be placed inside bundle output directory.
// Example:
// filePath = monorepo/node_modules/my-module/image.png
// dirname = monorepo/node_modules/my-module
// root = monorepo/packages/my-app/
// url = ../../node_modules/my-module (original)
// So when we calculate destination for the asset for iOS ('assets' + url + filename),
// it will end up outside of `assets` directory, so we have to make sure it's:
// url = node_modules/my-module (tweaked)
const url = path
.relative(config.root, dirname)
.replace(new RegExp(`^[\\.\\${path.sep}]+`), '');
const type = path.extname(filePath).replace(/^\./, '');
const assets = path.join('assets', config.bundle ? '' : config.platform);
const suffix = `(@\\d+(\\.\\d+)?x)?(\\.(${config.platform}|native))?\\.${type}$`;
const filename = path.basename(filePath).replace(new RegExp(suffix), '');
const normalizedName =
url.length === 0
? filename
: `${url.replace(pathSepPattern, '_')}_${filename}`;
const longName = `${normalizedName
.toLowerCase()
.replace(/[^a-z0-9_]/g, '')}.${type}`;
const result: string[] = await new Promise((resolve, reject) =>
this.fs.readdir(dirname, (err: Error | null, res: string[]) => {
if (err) {
reject(err);
} else {
resolve(res);
}
})
);
const map = AssetResolver.collect(result, {
name: filename,
type,
platform: config.platform,
});
const scales = Object.keys(map)
.map(s => Number(s.replace(/[^\d.]/g, '')))
.sort();
const pairs: Array<{
destination: string;
content: string;
}> = await Promise.all(
Object.keys(map).map(scale => {
this.addDependency(path.join(dirname, map[scale].name));
return new Promise((resolve, reject) =>
this.fs.readFile(
path.join(dirname, map[scale].name),
(err: Error | null, res: any) => {
runtime.logger.debug(`Asset: ${scale} for ${filePath}`);
if (err) {
reject(err);
} else {
let dest;
if (config.bundle && config.platform === 'android') {
const testXml = /\.(xml)$/;
const testMP4 = /\.(mp4)$/;
const testImages = /\.(png|jpg|gif|webp)$/;
const testFonts = /\.(ttf|otf|ttc)$/;
// found font family
if (
testXml.test(longName) &&
res.indexOf('font-family') !== -1
) {
dest = 'font';
} else if (testFonts.test(longName)) {
// font extensions
dest = 'font';
} else if (testMP4.test(longName)) {
// video files extensions
dest = 'raw';
} else if (
testImages.test(longName) ||
testXml.test(longName)
) {
// images extensions
switch (scale) {
case '@0.75x':
dest = 'drawable-ldpi';
break;
case '@1x':
dest = 'drawable-mdpi';
break;
case '@1.5x':
dest = 'drawable-hdpi';
break;
case '@2x':
dest = 'drawable-xhdpi';
break;
case '@3x':
dest = 'drawable-xxhdpi';
break;
case '@4x':
dest = 'drawable-xxxhdpi';
break;
default:
throw new Error(`Unknown scale ${scale} for ${filePath}`);
}
} else {
// everything else is going to RAW
dest = 'raw';
}
dest = path.join(dest, longName);
} else {
const name = `${filename}${
scale === '@1x' ? '' : scale
}.${type}`;
dest = path.join(assets, url, name);
}
runtime.logger.debug(
`Asset: file ${filePath} --> destination: ${dest}`
);
resolve({
destination: dest,
content: res,
});
}
}
)
);
})
);
pairs.forEach(item => {
let dest = item.destination;
if (config.outputPath) {
// support functions as outputPath to generate them dynamically
dest =
typeof config.outputPath === 'function'
? config.outputPath(dest)
: path.join(config.outputPath, dest);
}
this.emitFile(dest, item.content);
});
let publicPath = JSON.stringify(
path.join(assets, url).replace(pathSepPattern, '/')
);
if (config.publicPath) {
// support functions as publicPath to generate them dynamically
publicPath = JSON.stringify(
typeof config.publicPath === 'function'
? config.publicPath(url)
: path.join(config.publicPath, url)
);
}
const hashes = pairs.map(item =>
hasha(item.content, {
algorithm: 'md5',
})
);
let info:
| {
width: number;
height: number;
type: string;
}
| undefined;
try {
runtime.logger.debug(`Asset: path ${this.resourcePath}`);
info = size(this.resourcePath);
const match = path
.basename(this.resourcePath)
.match(new RegExp(`^${escapeStringRegexp(filename)}${suffix}`));
if (match && match[1]) {
const scale = Number(match[1].replace(/[^\d.]/g, ''));
if (typeof scale === 'number' && Number.isFinite(scale)) {
info.width /= scale;
info.height /= scale;
}
}
} catch (e) {
// Asset is not an image
runtime.logger.error(`Asset: unable to process ${this.resourcePath}`);
}
callback(
null,
dedent`
var AssetRegistry = require('react-native/Libraries/Image/AssetRegistry');
module.exports = AssetRegistry.registerAsset({
__packager_asset: true,
scales: ${JSON.stringify(scales)},
name: ${JSON.stringify(filename)},
type: ${JSON.stringify(type)},
hash: ${JSON.stringify(hashes.join())},
httpServerLocation: ${publicPath},
${
config.bundle ? '' : `fileSystemLocation: ${JSON.stringify(dirname)},`
}
${info ? `height: ${info.height},` : ''}
${info ? `width: ${info.width},` : ''}
});
`
);
}
assetLoader.raw = true;
export default assetLoader;