-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbundle-size.js
190 lines (161 loc) · 4.5 KB
/
bundle-size.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
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
const chalk = require('chalk');
const execa = require('execa');
const filesize = require('filesize');
const fs = require('fs');
const ora = require('ora');
const prependFile = require('prepend-file');
const tmp = require('tmp');
const {v4: uuidv4} = require('uuid');
async function action(text, promise) {
const spinner = ora(text).start();
try {
await promise;
spinner.succeed();
} catch (error) {
spinner.fail(`${text}: ${error}`);
}
}
async function generateSourceMapExplorer(
filename,
sourceDirectory,
outputDirectory,
) {
const output = `${outputDirectory}/${filename}.html`;
await execa(
`source-map-explorer ${sourceDirectory}/${filename}.jsbundle --html > ${output}`,
{shell: true},
);
return output;
}
(async () => {
const {
name: tempDirectory,
removeCallback: clearTempDirectory,
} = tmp.dirSync({unsafeCleanup: true});
const tempProjectDirectory = `${tempDirectory}/BundleSize`;
console.log('temp dir', tempProjectDirectory);
await action(
'Creating a React Native sample app',
execa.command(
`yarn react-native init BundleSize --directory ${tempProjectDirectory}`,
),
);
const tarballName = `react-native-url-polyfill-${uuidv4()}.tgz`;
await action(
'Bundling freshly initialized React Native app',
execa.command(
'yarn react-native bundle --entry-file index.js --platform ios --dev false --bundle-output original.jsbundle --sourcemap-output original.map',
{cwd: tempProjectDirectory},
),
);
await action(
'Packing react-native-url-polyfill',
execa('yarn', ['pack', '--filename', tarballName]),
);
await action(
'Adding react-native-url-polyfill',
execa(
'yarn',
['add', `react-native-url-polyfill@file:${__dirname}/../${tarballName}`],
{
cwd: tempProjectDirectory,
},
),
);
await action(
'Importing react-native-url-polyfill',
new Promise((resolve, reject) =>
prependFile(
`${tempProjectDirectory}/index.js`,
`import 'react-native-url-polyfill';
`,
(err) => {
if (err) {
reject(err);
}
resolve();
},
),
),
);
await action(
'Bundling React Native app with react-native-url-polyfill',
execa.command(
'yarn react-native bundle --entry-file index.js --platform ios --dev false --bundle-output withURLPolyfill.jsbundle --sourcemap-output withURLPolyfill.map',
{cwd: tempProjectDirectory},
),
);
await action(
'Comparing size of bundles',
new Promise(async (resolve, reject) => {
const originalSize = await new Promise((statsResolve) =>
fs.stat(
`${tempProjectDirectory}/original.jsbundle`,
[],
(err, stats) => {
if (err) {
reject(err);
}
statsResolve(stats.size);
},
),
);
const polyfillSize = await new Promise((statsResolve) =>
fs.stat(
`${tempProjectDirectory}/withURLPolyfill.jsbundle`,
[],
(err, stats) => {
if (err) {
reject(err);
}
statsResolve(stats.size);
},
),
);
resolve();
setTimeout(() => {
console.log(
'📦 The original bundle is:',
chalk.bold(filesize(originalSize)),
);
console.log(
'📦 The bundle with react-native-url-polyfill is:',
chalk.bold(filesize(polyfillSize)),
);
console.log(
'⚖️ Therefore, react-native-url-polyfill adds',
chalk.bold.red(filesize(polyfillSize - originalSize)),
'to the JavaScript bundle',
);
});
}),
);
await action(
'Generating sources map explorer',
new Promise(async (resolve, reject) => {
const {name: sourceMapOutputDirectory} = tmp.dirSync();
const originalOutput = await generateSourceMapExplorer(
'original',
tempProjectDirectory,
sourceMapOutputDirectory,
);
const withURLPolyfillOutput = await generateSourceMapExplorer(
'withURLPolyfill',
tempProjectDirectory,
sourceMapOutputDirectory,
);
resolve();
setTimeout(() => {
console.log(
'Original source map explorer:',
chalk.underline(originalOutput),
);
console.log(
'With react-native-url-polyfill:',
chalk.underline(withURLPolyfillOutput),
);
});
}),
);
clearTempDirectory();
})();