-
Notifications
You must be signed in to change notification settings - Fork 48.3k
/
Copy pathpackaging.js
291 lines (276 loc) · 8.41 KB
/
packaging.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict';
const {
existsSync,
readdirSync,
unlinkSync,
readFileSync,
writeFileSync,
} = require('fs');
const Bundles = require('./bundles');
const {
asyncCopyTo,
asyncExecuteCommand,
asyncExtractTar,
asyncRimRaf,
} = require('./utils');
const {
NODE_ES2015,
ESM_DEV,
ESM_PROD,
NODE_DEV,
NODE_PROD,
NODE_PROFILING,
BUN_DEV,
BUN_PROD,
FB_WWW_DEV,
FB_WWW_PROD,
FB_WWW_PROFILING,
RN_OSS_DEV,
RN_OSS_PROD,
RN_OSS_PROFILING,
RN_FB_DEV,
RN_FB_PROD,
RN_FB_PROFILING,
BROWSER_SCRIPT,
CJS_DTS,
ESM_DTS,
} = Bundles.bundleTypes;
function getPackageName(name) {
if (name.indexOf('/') !== -1) {
return name.split('/')[0];
}
return name;
}
function getBundleOutputPath(bundle, bundleType, filename, packageName) {
switch (bundleType) {
case NODE_ES2015:
return `build/node_modules/${packageName}/cjs/${filename}`;
case ESM_DEV:
case ESM_PROD:
case ESM_DTS:
return `build/node_modules/${packageName}/esm/${filename}`;
case BUN_DEV:
case BUN_PROD:
return `build/node_modules/${packageName}/cjs/${filename}`;
case NODE_DEV:
case NODE_PROD:
case NODE_PROFILING:
case CJS_DTS:
return `build/node_modules/${packageName}/cjs/${filename}`;
case FB_WWW_DEV:
case FB_WWW_PROD:
case FB_WWW_PROFILING:
return `build/facebook-www/${filename}`;
case RN_OSS_DEV:
case RN_OSS_PROD:
case RN_OSS_PROFILING:
switch (packageName) {
case 'react-native-renderer':
return `build/react-native/implementations/${filename}`;
default:
throw new Error('Unknown RN package.');
}
case RN_FB_DEV:
case RN_FB_PROD:
case RN_FB_PROFILING:
switch (packageName) {
case 'scheduler':
case 'react':
case 'react-dom':
case 'react-is':
case 'react-test-renderer':
return `build/facebook-react-native/${packageName}/cjs/${filename}`;
case 'react-native-renderer':
return `build/react-native/implementations/${filename.replace(
/\.js$/,
'.fb.js'
)}`;
default:
throw new Error('Unknown RN package.');
}
case BROWSER_SCRIPT: {
// Bundles that are served as browser scripts need to be able to be sent
// straight to the browser with any additional bundling. We shouldn't use
// a module to re-export. Depending on how they are served, they also may
// not go through package.json module resolution, so we shouldn't rely on
// that either. We should consider the output path as part of the public
// contract, and explicitly specify its location within the package's
// directory structure.
const outputPath = bundle.outputPath;
if (!outputPath) {
throw new Error(
'Bundles with type BROWSER_SCRIPT must specific an explicit ' +
'output path.'
);
}
return `build/node_modules/${packageName}/${outputPath}`;
}
default:
throw new Error('Unknown bundle type.');
}
}
async function copyWWWShims() {
await asyncCopyTo(
`${__dirname}/shims/facebook-www`,
'build/facebook-www/shims'
);
}
async function copyRNShims() {
await asyncCopyTo(
`${__dirname}/shims/react-native`,
'build/react-native/shims'
);
await asyncCopyTo(
require.resolve('react-native-renderer/src/ReactNativeTypes.js'),
'build/react-native/shims/ReactNativeTypes.js'
);
}
async function copyAllShims() {
await Promise.all([copyWWWShims(), copyRNShims()]);
}
function getTarOptions(tgzName, packageName) {
// Files inside the `npm pack`ed archive start
// with "package/" in their paths. We'll undo
// this during extraction.
const CONTENTS_FOLDER = 'package';
return {
src: tgzName,
dest: `build/node_modules/${packageName}`,
tar: {
entries: [CONTENTS_FOLDER],
map(header) {
if (header.name.indexOf(CONTENTS_FOLDER + '/') === 0) {
header.name = header.name.slice(CONTENTS_FOLDER.length + 1);
}
},
},
};
}
let entryPointsToHasBundle = new Map();
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const bundle of Bundles.bundles) {
let hasBundle = entryPointsToHasBundle.get(bundle.entry);
if (!hasBundle) {
const hasNonFBBundleTypes = bundle.bundleTypes.some(
type =>
type !== FB_WWW_DEV && type !== FB_WWW_PROD && type !== FB_WWW_PROFILING
);
entryPointsToHasBundle.set(bundle.entry, hasNonFBBundleTypes);
}
}
function filterOutEntrypoints(name) {
// Remove entry point files that are not built in this configuration.
let jsonPath = `build/node_modules/${name}/package.json`;
let packageJSON = JSON.parse(readFileSync(jsonPath));
let files = packageJSON.files;
let exportsJSON = packageJSON.exports;
let browserJSON = packageJSON.browser;
if (!Array.isArray(files)) {
throw new Error('expected all package.json files to contain a files field');
}
let changed = false;
for (let i = 0; i < files.length; i++) {
let filename = files[i];
let entry =
filename === 'index.js'
? name
: name + '/' + filename.replace(/\.js$/, '');
let hasBundle = entryPointsToHasBundle.get(entry);
if (hasBundle === undefined) {
// This entry doesn't exist in the bundles. Check if something similar exists.
hasBundle =
entryPointsToHasBundle.get(entry + '.node') ||
entryPointsToHasBundle.get(entry + '.browser');
// The .react-server and .rsc suffixes may not have a bundle representation but
// should infer their bundle status from the non-suffixed entry point.
if (entry.endsWith('.react-server')) {
hasBundle = entryPointsToHasBundle.get(
entry.slice(0, '.react-server'.length * -1)
);
} else if (entry.endsWith('.rsc')) {
hasBundle = entryPointsToHasBundle.get(
entry.slice(0, '.rsc'.length * -1)
);
}
}
if (hasBundle === undefined) {
// This doesn't exist in the bundles. It's an extra file.
} else if (hasBundle === true) {
// This is built in this release channel.
} else {
// This doesn't have any bundleTypes in this release channel.
// Let's remove it.
files.splice(i, 1);
i--;
try {
unlinkSync(`build/node_modules/${name}/${filename}`);
} catch (err) {
// If the file doesn't exist we can just move on. Otherwise throw the halt the build
if (err.code !== 'ENOENT') {
throw err;
}
}
changed = true;
// Remove it from the exports field too if it exists.
if (exportsJSON) {
if (filename === 'index.js') {
delete exportsJSON['.'];
} else {
delete exportsJSON['./' + filename.replace(/\.js$/, '')];
}
}
if (browserJSON) {
delete browserJSON['./' + filename];
}
}
// We only export the source directory so Jest and Rollup can access them
// during local development and at build time. The files don't exist in the
// public builds, so we don't need the export entry, either.
const sourceWildcardExport = './src/*';
if (exportsJSON && exportsJSON[sourceWildcardExport]) {
delete exportsJSON[sourceWildcardExport];
changed = true;
}
}
if (changed) {
let newJSON = JSON.stringify(packageJSON, null, ' ');
writeFileSync(jsonPath, newJSON);
}
}
async function prepareNpmPackage(name) {
await Promise.all([
asyncCopyTo('LICENSE', `build/node_modules/${name}/LICENSE`),
asyncCopyTo(
`packages/${name}/package.json`,
`build/node_modules/${name}/package.json`
),
asyncCopyTo(
`packages/${name}/README.md`,
`build/node_modules/${name}/README.md`
),
asyncCopyTo(`packages/${name}/npm`, `build/node_modules/${name}`),
]);
filterOutEntrypoints(name);
const tgzName = (
await asyncExecuteCommand(`npm pack build/node_modules/${name}`)
).trim();
await asyncRimRaf(`build/node_modules/${name}`);
await asyncExtractTar(getTarOptions(tgzName, name));
unlinkSync(tgzName);
}
async function prepareNpmPackages() {
if (!existsSync('build/node_modules')) {
// We didn't build any npm packages.
return;
}
const builtPackageFolders = readdirSync('build/node_modules').filter(
dir => dir.charAt(0) !== '.'
);
await Promise.all(builtPackageFolders.map(prepareNpmPackage));
}
module.exports = {
copyAllShims,
getPackageName,
getBundleOutputPath,
prepareNpmPackages,
};