Skip to content

Commit 43e59f2

Browse files
authored
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.
1 parent e668f1b commit 43e59f2

7 files changed

Lines changed: 95 additions & 14 deletions

File tree

packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ describe('ReactDOMFizzServer', () => {
2020
beforeEach(() => {
2121
jest.resetModules();
2222
React = require('react');
23-
ReactDOMFizzServer = require('react-dom/unstable-fizz.browser');
23+
if (__EXPERIMENTAL__) {
24+
ReactDOMFizzServer = require('react-dom/unstable-fizz.browser');
25+
}
2426
});
2527

2628
async function readResult(stream) {
@@ -35,6 +37,7 @@ describe('ReactDOMFizzServer', () => {
3537
}
3638
}
3739

40+
// @gate experimental
3841
it('should call renderToReadableStream', async () => {
3942
const stream = ReactDOMFizzServer.renderToReadableStream(
4043
<div>hello world</div>,

packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ describe('ReactDOMFizzServer', () => {
1818
beforeEach(() => {
1919
jest.resetModules();
2020
React = require('react');
21-
ReactDOMFizzServer = require('react-dom/unstable-fizz');
21+
if (__EXPERIMENTAL__) {
22+
ReactDOMFizzServer = require('react-dom/unstable-fizz');
23+
}
2224
Stream = require('stream');
2325
});
2426

@@ -30,6 +32,7 @@ describe('ReactDOMFizzServer', () => {
3032
return writable;
3133
}
3234

35+
// @gate experimental
3336
it('should call pipeToNodeWritable', () => {
3437
const writable = getTestWritable();
3538
ReactDOMFizzServer.pipeToNodeWritable(<div>hello world</div>, writable);

packages/react-fetch/src/__tests__/ReactFetchBrowser-test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ describe('ReactFetchBrowser', () => {
1313
let ReactFetchBrowser;
1414

1515
beforeEach(() => {
16-
ReactFetchBrowser = require('react-fetch');
16+
if (__EXPERIMENTAL__) {
17+
ReactFetchBrowser = require('react-fetch');
18+
}
1719
});
1820

1921
// TODO: test something useful.
22+
// @gate experimental
2023
it('exports something', () => {
2124
expect(ReactFetchBrowser.fetch).not.toBe(undefined);
2225
});

packages/react-fetch/src/__tests__/ReactFetchNode-test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ describe('ReactFetchNode', () => {
2020

2121
beforeEach(done => {
2222
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+
}
2530
http = require('http');
26-
fetch = ReactFetchNode.fetch;
2731

2832
server = http.createServer((req, res) => {
2933
serverImpl(req, res);
3034
});
3135
server.listen(done);
3236
serverEndpoint = `http://localhost:${server.address().port}/`;
33-
34-
// TODO: A way to pass load context.
35-
ReactCache.CacheProvider._context._currentValue = ReactCache.createCache();
3637
});
3738

3839
afterEach(done => {
@@ -54,6 +55,7 @@ describe('ReactFetchNode', () => {
5455
}
5556
}
5657

58+
// @gate experimental
5759
it('can read text', async () => {
5860
serverImpl = (req, res) => {
5961
res.write('ok');
@@ -70,6 +72,7 @@ describe('ReactFetchNode', () => {
7072
});
7173
});
7274

75+
// @gate experimental
7376
it('can read json', async () => {
7477
serverImpl = (req, res) => {
7578
res.write(JSON.stringify({name: 'Sema'}));

packages/react/src/__tests__/ReactCache-test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ describe('ReactCache', () => {
1313
let ReactCache;
1414

1515
beforeEach(() => {
16-
ReactCache = require('react/unstable-cache');
16+
if (__EXPERIMENTAL__) {
17+
ReactCache = require('react/unstable-cache');
18+
}
1719
});
1820

1921
// TODO: test something useful.
22+
// @gate experimental
2023
it('exports something', () => {
2124
expect(ReactCache.readCache).not.toBe(undefined);
2225
});

scripts/rollup/bundles.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
'use strict';
22

3+
const RELEASE_CHANNEL = process.env.RELEASE_CHANNEL;
4+
5+
const __EXPERIMENTAL__ =
6+
typeof RELEASE_CHANNEL === 'string'
7+
? RELEASE_CHANNEL === 'experimental'
8+
: true;
9+
310
const bundleTypes = {
411
UMD_DEV: 'UMD_DEV',
512
UMD_PROD: 'UMD_PROD',
@@ -108,7 +115,7 @@ const bundles = [
108115

109116
/******* React Cache (experimental, new) *******/
110117
{
111-
bundleTypes: [NODE_DEV, NODE_PROD, NODE_PROFILING],
118+
bundleTypes: __EXPERIMENTAL__ ? [NODE_DEV, NODE_PROD, NODE_PROFILING] : [],
112119
moduleType: ISOMORPHIC,
113120
entry: 'react/unstable-cache',
114121
global: 'ReactCache',
@@ -223,14 +230,16 @@ const bundles = [
223230

224231
/******* React DOM Fizz Server *******/
225232
{
226-
bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD],
233+
bundleTypes: __EXPERIMENTAL__
234+
? [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD]
235+
: [],
227236
moduleType: RENDERER,
228237
entry: 'react-dom/unstable-fizz.browser',
229238
global: 'ReactDOMFizzServer',
230239
externals: ['react', 'react-dom/server'],
231240
},
232241
{
233-
bundleTypes: [NODE_DEV, NODE_PROD],
242+
bundleTypes: __EXPERIMENTAL__ ? [NODE_DEV, NODE_PROD] : [],
234243
moduleType: RENDERER,
235244
entry: 'react-dom/unstable-fizz.node',
236245
global: 'ReactDOMFizzServer',

scripts/rollup/packaging.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'use strict';
22

3-
const {existsSync, readdirSync, unlinkSync} = require('fs');
3+
const {
4+
existsSync,
5+
readdirSync,
6+
unlinkSync,
7+
readFileSync,
8+
writeFileSync,
9+
} = require('fs');
410
const Bundles = require('./bundles');
511
const {
612
asyncCopyTo,
@@ -115,6 +121,56 @@ function getTarOptions(tgzName, packageName) {
115121
};
116122
}
117123

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+
118174
async function prepareNpmPackage(name) {
119175
await Promise.all([
120176
asyncCopyTo('LICENSE', `build/node_modules/${name}/LICENSE`),
@@ -128,6 +184,7 @@ async function prepareNpmPackage(name) {
128184
),
129185
asyncCopyTo(`packages/${name}/npm`, `build/node_modules/${name}`),
130186
]);
187+
filterOutEntrypoints(name);
131188
const tgzName = (
132189
await asyncExecuteCommand(`npm pack build/node_modules/${name}`)
133190
).trim();

0 commit comments

Comments
 (0)