Delete Entries without Build Output from package.json and the build directory (#19029)
* Gate test * Delete entrypoints without Build Outputs from package.json and build output If an entry point exists in bundles.js but doesn't have any bundleTypes, I delete that entry point file from the build directory. I also remove it from the files field in package.json if it exists. This allows us to remove bundles from being built in the stable release channel.
Sebastian Markbåge committed
May 27, 2020 at 19:43 UTC
43e59f29d6edbd5b968a895e1a8bef8af1479a77
7 files changed
+95
-14
packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js
+4
-1
@@ -20,7 +20,9 @@ describe('ReactDOMFizzServer', () => {
20
beforeEach(() => {
21
jest.resetModules();
22
React = require('react');
23
- ReactDOMFizzServer = require('react-dom/unstable-fizz.browser');
23
+ if (__EXPERIMENTAL__) {
24
+ ReactDOMFizzServer = require('react-dom/unstable-fizz.browser');
25
+ }
26
});
27
28
async function readResult(stream) {
@@ -35,6 +37,7 @@ describe('ReactDOMFizzServer', () => {
37
}
38
}
39
40
+ // @gate experimental
41
it('should call renderToReadableStream', async () => {
42
const stream = ReactDOMFizzServer.renderToReadableStream(
43
<div>hello world</div>,
packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js
+4
-1
@@ -18,7 +18,9 @@ describe('ReactDOMFizzServer', () => {
18
beforeEach(() => {
19
jest.resetModules();
20
React = require('react');
21
- ReactDOMFizzServer = require('react-dom/unstable-fizz');
21
+ if (__EXPERIMENTAL__) {
22
+ ReactDOMFizzServer = require('react-dom/unstable-fizz');
23
+ }
24
Stream = require('stream');
25
});
26
@@ -30,6 +32,7 @@ describe('ReactDOMFizzServer', () => {
32
return writable;
33
}
34
35
+ // @gate experimental
36
it('should call pipeToNodeWritable', () => {
37
const writable = getTestWritable();
38
ReactDOMFizzServer.pipeToNodeWritable(<div>hello world</div>, writable);
packages/react-fetch/src/__tests__/ReactFetchBrowser-test.js
+4
-1
@@ -13,10 +13,13 @@ describe('ReactFetchBrowser', () => {
13
let ReactFetchBrowser;
14
15
beforeEach(() => {
16
- ReactFetchBrowser = require('react-fetch');
16
+ if (__EXPERIMENTAL__) {
17
+ ReactFetchBrowser = require('react-fetch');
18
+ }
19
});
20
21
// TODO: test something useful.
22
+ // @gate experimental
23
it('exports something', () => {
24
expect(ReactFetchBrowser.fetch).not.toBe(undefined);
25
});
packages/react-fetch/src/__tests__/ReactFetchNode-test.js
+9
-6
@@ -20,19 +20,20 @@ describe('ReactFetchNode', () => {
20
21
beforeEach(done => {
22
jest.resetModules();
23
- ReactCache = require('react/unstable-cache');
24
- ReactFetchNode = require('react-fetch');
23
+ if (__EXPERIMENTAL__) {
24
+ ReactCache = require('react/unstable-cache');
25
+ // TODO: A way to pass load context.
26
+ ReactCache.CacheProvider._context._currentValue = ReactCache.createCache();
27
+ ReactFetchNode = require('react-fetch');
28
+ fetch = ReactFetchNode.fetch;
29
+ }
30
http = require('http');
26
- fetch = ReactFetchNode.fetch;
31
32
server = http.createServer((req, res) => {
33
serverImpl(req, res);
34
});
35
server.listen(done);
36
serverEndpoint = `http://localhost:${server.address().port}/`;
33
-
34
- // TODO: A way to pass load context.
35
- ReactCache.CacheProvider._context._currentValue = ReactCache.createCache();
37
});
38
39
afterEach(done => {
@@ -54,6 +55,7 @@ describe('ReactFetchNode', () => {
55
}
56
}
57
58
+ // @gate experimental
59
it('can read text', async () => {
60
serverImpl = (req, res) => {
61
res.write('ok');
@@ -70,6 +72,7 @@ describe('ReactFetchNode', () => {
72
});
73
});
74
75
+ // @gate experimental
76
it('can read json', async () => {
77
serverImpl = (req, res) => {
78
res.write(JSON.stringify({name: 'Sema'}));
packages/react/src/__tests__/ReactCache-test.js
+4
-1
@@ -13,10 +13,13 @@ describe('ReactCache', () => {
13
let ReactCache;
14
15
beforeEach(() => {
16
- ReactCache = require('react/unstable-cache');
16
+ if (__EXPERIMENTAL__) {
17
+ ReactCache = require('react/unstable-cache');
18
+ }
19
});
20
21
// TODO: test something useful.
22
+ // @gate experimental
23
it('exports something', () => {
24
expect(ReactCache.readCache).not.toBe(undefined);
25
});
scripts/rollup/bundles.js
+12
-3
@@ -1,5 +1,12 @@
1
'use strict';
2
3
+const RELEASE_CHANNEL = process.env.RELEASE_CHANNEL;
4
+
5
+const __EXPERIMENTAL__ =
6
+ typeof RELEASE_CHANNEL === 'string'
7
+ ? RELEASE_CHANNEL === 'experimental'
8
+ : true;
9
+
10
const bundleTypes = {
11
UMD_DEV: 'UMD_DEV',
12
UMD_PROD: 'UMD_PROD',
@@ -108,7 +115,7 @@ const bundles = [
115
116
/******* React Cache (experimental, new) *******/
117
{
111
- bundleTypes: [NODE_DEV, NODE_PROD, NODE_PROFILING],
118
+ bundleTypes: __EXPERIMENTAL__ ? [NODE_DEV, NODE_PROD, NODE_PROFILING] : [],
119
moduleType: ISOMORPHIC,
120
entry: 'react/unstable-cache',
121
global: 'ReactCache',
@@ -223,14 +230,16 @@ const bundles = [
230
231
/******* React DOM Fizz Server *******/
232
{
226
- bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD],
233
+ bundleTypes: __EXPERIMENTAL__
234
+ ? [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD]
235
+ : [],
236
moduleType: RENDERER,
237
entry: 'react-dom/unstable-fizz.browser',
238
global: 'ReactDOMFizzServer',
239
externals: ['react', 'react-dom/server'],
240
},
241
{
233
- bundleTypes: [NODE_DEV, NODE_PROD],
242
+ bundleTypes: __EXPERIMENTAL__ ? [NODE_DEV, NODE_PROD] : [],
243
moduleType: RENDERER,
244
entry: 'react-dom/unstable-fizz.node',
245
global: 'ReactDOMFizzServer',
scripts/rollup/packaging.js
+58
-1
@@ -1,6 +1,12 @@
1
'use strict';
2
3
-const {existsSync, readdirSync, unlinkSync} = require('fs');
3
+const {
4
+ existsSync,
5
+ readdirSync,
6
+ unlinkSync,
7
+ readFileSync,
8
+ writeFileSync,
9
+} = require('fs');
10
const Bundles = require('./bundles');
11
const {
12
asyncCopyTo,
@@ -115,6 +121,56 @@ function getTarOptions(tgzName, packageName) {
121
};
122
}
123
124
+let entryPointsToHasBundle = new Map();
125
+// eslint-disable-next-line no-for-of-loops/no-for-of-loops
126
+for (const bundle of Bundles.bundles) {
127
+ let hasBundle = entryPointsToHasBundle.get(bundle.entry);
128
+ if (!hasBundle) {
129
+ entryPointsToHasBundle.set(bundle.entry, bundle.bundleTypes.length > 0);
130
+ }
131
+}
132
+
133
+function filterOutEntrypoints(name) {
134
+ // Remove entry point files that are not built in this configuration.
135
+ let jsonPath = `build/node_modules/${name}/package.json`;
136
+ let packageJSON = JSON.parse(readFileSync(jsonPath));
137
+ let files = packageJSON.files;
138
+ if (!Array.isArray(files)) {
139
+ throw new Error('expected all package.json files to contain a files field');
140
+ }
141
+ let changed = false;
142
+ for (let i = 0; i < files.length; i++) {
143
+ let filename = files[i];
144
+ let entry =
145
+ filename === 'index.js'
146
+ ? name
147
+ : name + '/' + filename.replace(/\.js$/, '');
148
+ let hasBundle = entryPointsToHasBundle.get(entry);
149
+ if (hasBundle === undefined) {
150
+ // This entry doesn't exist in the bundles. Check if something similar exists.
151
+ hasBundle =
152
+ entryPointsToHasBundle.get(entry + '.node') ||
153
+ entryPointsToHasBundle.get(entry + '.browser');
154
+ }
155
+ if (hasBundle === undefined) {
156
+ // This doesn't exist in the bundles. It's an extra file.
157
+ } else if (hasBundle === true) {
158
+ // This is built in this release channel.
159
+ } else {
160
+ // This doesn't have any bundleTypes in this release channel.
161
+ // Let's remove it.
162
+ files.splice(i, 1);
163
+ i--;
164
+ unlinkSync(`build/node_modules/${name}/${filename}`);
165
+ changed = true;
166
+ }
167
+ }
168
+ if (changed) {
169
+ let newJSON = JSON.stringify(packageJSON, null, ' ');
170
+ writeFileSync(jsonPath, newJSON);
171
+ }
172
+}
173
+
174
async function prepareNpmPackage(name) {
175
await Promise.all([
176
asyncCopyTo('LICENSE', `build/node_modules/${name}/LICENSE`),
@@ -128,6 +184,7 @@ async function prepareNpmPackage(name) {
184
),
185
asyncCopyTo(`packages/${name}/npm`, `build/node_modules/${name}`),
186
]);
187
+ filterOutEntrypoints(name);
188
const tgzName = (
189
await asyncExecuteCommand(`npm pack build/node_modules/${name}`)
190
).trim();