Skip to content

Commit 0d9834c

Browse files
authored
build: add support to the rollup build for building typescript packages (#32393)
1 parent e670e72 commit 0d9834c

File tree

9 files changed

+459
-151
lines changed

9 files changed

+459
-151
lines changed

babel.config-ts.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* This file is purely being used for local jest runs, and doesn't participate in the build process.
3+
*/
4+
'use strict';
5+
6+
module.exports = {
7+
plugins: [
8+
'@babel/plugin-syntax-jsx',
9+
'@babel/plugin-transform-flow-strip-types',
10+
],
11+
presets: [
12+
['@babel/preset-env', {targets: {node: 'current'}}],
13+
'@babel/preset-typescript',
14+
],
15+
};

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
"@babel/plugin-transform-template-literals": "^7.10.5",
3636
"@babel/preset-flow": "^7.10.4",
3737
"@babel/preset-react": "^7.23.3",
38+
"@babel/preset-typescript": "^7.26.0",
3839
"@babel/traverse": "^7.11.0",
3940
"@rollup/plugin-babel": "^6.0.3",
4041
"@rollup/plugin-commonjs": "^24.0.1",
4142
"@rollup/plugin-node-resolve": "^15.0.1",
4243
"@rollup/plugin-replace": "^5.0.2",
44+
"@rollup/plugin-typescript": "^12.1.2",
4345
"abortcontroller-polyfill": "^1.7.5",
4446
"art": "0.10.1",
4547
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
@@ -90,6 +92,7 @@
9092
"react-lifecycles-compat": "^3.0.4",
9193
"rimraf": "^3.0.0",
9294
"rollup": "^3.29.5",
95+
"rollup-plugin-dts": "^6.1.1",
9396
"rollup-plugin-prettier": "^4.1.1",
9497
"rollup-plugin-strip-banner": "^3.0.0",
9598
"semver": "^7.1.1",
@@ -98,7 +101,7 @@
98101
"targz": "^1.0.1",
99102
"through2": "^3.0.1",
100103
"tmp": "^0.1.0",
101-
"typescript": "^3.7.5",
104+
"typescript": "^5.4.3",
102105
"undici": "^5.28.4",
103106
"web-streams-polyfill": "^3.1.1",
104107
"yargs": "^15.3.1"

packages/react/src/__tests__/ReactTypeScriptClass-test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ describe('ReactTypeScriptClass', function () {
529529
'StateBasedOnContext uses the legacy contextTypes API which will soon be removed. ' +
530530
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
531531
(ReactFeatureFlags.enableOwnerStacks
532-
? ' in ProvideChildContextTypes.Object.<anonymous>.ProvideChildContextTypes (at **)'
532+
? ' in ProvideChildContextTypes.createElement (at **)'
533533
: ' in StateBasedOnContext (at **)\n') +
534534
' in ProvideChildContextTypes (at **)',
535535
]);
@@ -725,7 +725,7 @@ describe('ReactTypeScriptClass', function () {
725725
'ReadContext uses the legacy contextTypes API which will soon be removed. ' +
726726
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
727727
(ReactFeatureFlags.enableOwnerStacks
728-
? ' in ProvideContext.Object.<anonymous>.ProvideContext (at **)'
728+
? ' in ProvideContext.createElement (at **)'
729729
: ' in ReadContext (at **)\n') +
730730
' in ProvideContext (at **)',
731731
]);

scripts/jest/config.base.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ module.exports = {
77
'<rootDir>/scripts/bench/',
88
],
99
transform: {
10-
'.*': require.resolve('./preprocessor.js'),
10+
'^.+\\.ts$': [
11+
'babel-jest',
12+
{configFile: require.resolve('../../babel.config-ts.js')},
13+
],
14+
'.(?!ts$)': require.resolve('./preprocessor.js'),
1115
},
1216
prettierPath: require.resolve('prettier-2'),
1317
setupFiles: [require.resolve('./setupEnvironment.js')],

scripts/rollup/build.js

+33-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const rollup = require('rollup');
44
const babel = require('@rollup/plugin-babel').babel;
55
const closure = require('./plugins/closure-plugin');
66
const flowRemoveTypes = require('flow-remove-types');
7+
const {dts} = require('rollup-plugin-dts');
78
const prettier = require('rollup-plugin-prettier');
89
const replace = require('@rollup/plugin-replace');
10+
const typescript = require('@rollup/plugin-typescript');
911
const stripBanner = require('rollup-plugin-strip-banner');
1012
const chalk = require('chalk');
1113
const resolve = require('@rollup/plugin-node-resolve').nodeResolve;
@@ -61,6 +63,8 @@ const {
6163
RN_FB_PROD,
6264
RN_FB_PROFILING,
6365
BROWSER_SCRIPT,
66+
CJS_DTS,
67+
ESM_DTS,
6468
} = Bundles.bundleTypes;
6569

6670
const {getFilename} = Bundles;
@@ -250,9 +254,11 @@ function getFormat(bundleType) {
250254
case RN_FB_DEV:
251255
case RN_FB_PROD:
252256
case RN_FB_PROFILING:
257+
case CJS_DTS:
253258
return `cjs`;
254259
case ESM_DEV:
255260
case ESM_PROD:
261+
case ESM_DTS:
256262
return `es`;
257263
case BROWSER_SCRIPT:
258264
return `iife`;
@@ -281,6 +287,8 @@ function isProductionBundleType(bundleType) {
281287
case RN_FB_PROD:
282288
case RN_FB_PROFILING:
283289
case BROWSER_SCRIPT:
290+
case CJS_DTS:
291+
case ESM_DTS:
284292
return true;
285293
default:
286294
throw new Error(`Unknown type: ${bundleType}`);
@@ -303,6 +311,8 @@ function isProfilingBundleType(bundleType) {
303311
case ESM_DEV:
304312
case ESM_PROD:
305313
case BROWSER_SCRIPT:
314+
case CJS_DTS:
315+
case ESM_DTS:
306316
return false;
307317
case FB_WWW_PROFILING:
308318
case NODE_PROFILING:
@@ -368,27 +378,36 @@ function getPlugins(
368378
pureExternalModules,
369379
bundle
370380
) {
381+
// Short-circuit if we're only building a .d.ts bundle
382+
if (bundleType === CJS_DTS || bundleType === ESM_DTS) {
383+
return [dts({tsconfig: bundle.tsconfig})];
384+
}
371385
try {
372386
const forks = Modules.getForks(bundleType, entry, moduleType, bundle);
373387
const isProduction = isProductionBundleType(bundleType);
374388
const isProfiling = isProfilingBundleType(bundleType);
375389

376390
const needsMinifiedByClosure =
377-
bundleType !== ESM_PROD && bundleType !== ESM_DEV;
391+
bundleType !== ESM_PROD &&
392+
bundleType !== ESM_DEV &&
393+
// TODO(@poteto) figure out ICE in closure compiler for eslint-plugin-react-hooks (ts)
394+
bundle.tsconfig == null;
378395

379396
return [
380397
// Keep dynamic imports as externals
381398
dynamicImports(),
382-
{
383-
name: 'rollup-plugin-flow-remove-types',
384-
transform(code) {
385-
const transformed = flowRemoveTypes(code);
386-
return {
387-
code: transformed.toString(),
388-
map: null,
389-
};
390-
},
391-
},
399+
bundle.tsconfig != null
400+
? typescript({tsconfig: bundle.tsconfig})
401+
: {
402+
name: 'rollup-plugin-flow-remove-types',
403+
transform(code) {
404+
const transformed = flowRemoveTypes(code);
405+
return {
406+
code: transformed.toString(),
407+
map: null,
408+
};
409+
},
410+
},
392411
// Shim any modules that need forking in this environment.
393412
useForks(forks),
394413
// Ensure we don't try to bundle any fbjs modules.
@@ -839,7 +858,9 @@ async function buildEverything() {
839858
[bundle, RN_FB_DEV],
840859
[bundle, RN_FB_PROD],
841860
[bundle, RN_FB_PROFILING],
842-
[bundle, BROWSER_SCRIPT]
861+
[bundle, BROWSER_SCRIPT],
862+
[bundle, CJS_DTS],
863+
[bundle, ESM_DTS]
843864
);
844865
}
845866

scripts/rollup/bundles.js

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const bundleTypes = {
2626
RN_FB_PROD: 'RN_FB_PROD',
2727
RN_FB_PROFILING: 'RN_FB_PROFILING',
2828
BROWSER_SCRIPT: 'BROWSER_SCRIPT',
29+
CJS_DTS: 'CJS_DTS',
30+
ESM_DTS: 'ESM_DTS',
2931
};
3032

3133
const {
@@ -47,6 +49,8 @@ const {
4749
RN_FB_PROD,
4850
RN_FB_PROFILING,
4951
BROWSER_SCRIPT,
52+
CJS_DTS,
53+
ESM_DTS,
5054
} = bundleTypes;
5155

5256
const moduleTypes = {
@@ -1270,6 +1274,9 @@ function getFilename(bundle, bundleType) {
12701274
return `${globalName}-profiling.js`;
12711275
case BROWSER_SCRIPT:
12721276
return `${name}.js`;
1277+
case CJS_DTS:
1278+
case ESM_DTS:
1279+
return `${name}.d.ts`;
12731280
}
12741281
}
12751282

scripts/rollup/packaging.js

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const {
3434
RN_FB_PROD,
3535
RN_FB_PROFILING,
3636
BROWSER_SCRIPT,
37+
CJS_DTS,
38+
ESM_DTS,
3739
} = Bundles.bundleTypes;
3840

3941
function getPackageName(name) {
@@ -49,13 +51,15 @@ function getBundleOutputPath(bundle, bundleType, filename, packageName) {
4951
return `build/node_modules/${packageName}/cjs/${filename}`;
5052
case ESM_DEV:
5153
case ESM_PROD:
54+
case ESM_DTS:
5255
return `build/node_modules/${packageName}/esm/${filename}`;
5356
case BUN_DEV:
5457
case BUN_PROD:
5558
return `build/node_modules/${packageName}/cjs/${filename}`;
5659
case NODE_DEV:
5760
case NODE_PROD:
5861
case NODE_PROFILING:
62+
case CJS_DTS:
5963
return `build/node_modules/${packageName}/cjs/${filename}`;
6064
case FB_WWW_DEV:
6165
case FB_WWW_PROD:

scripts/rollup/wrappers.js

+36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const {
2121
RN_FB_PROD,
2222
RN_FB_PROFILING,
2323
BROWSER_SCRIPT,
24+
CJS_DTS,
25+
ESM_DTS,
2426
} = bundleTypes;
2527

2628
const {RECONCILER} = moduleTypes;
@@ -248,6 +250,16 @@ ${source}
248250
Object.defineProperty(module.exports, "__esModule", { value: true });
249251
`;
250252
},
253+
254+
/***************** CJS_DTS *****************/
255+
[CJS_DTS](source, globalName, filename, moduleType) {
256+
return source;
257+
},
258+
259+
/***************** ESM_DTS *****************/
260+
[ESM_DTS](source, globalName, filename, moduleType) {
261+
return source;
262+
},
251263
};
252264

253265
const licenseHeaderWrappers = {
@@ -464,6 +476,30 @@ ${license}
464476
* @preventMunge
465477
*/
466478
479+
${source}`;
480+
},
481+
482+
/***************** CJS_DTS *****************/
483+
[CJS_DTS](source, globalName, filename, moduleType) {
484+
return `/**
485+
* @license React
486+
* ${filename}
487+
*
488+
${license}
489+
*/
490+
491+
${source}`;
492+
},
493+
494+
/***************** ESM_DTS *****************/
495+
[ESM_DTS](source, globalName, filename, moduleType) {
496+
return `/**
497+
* @license React
498+
* ${filename}
499+
*
500+
${license}
501+
*/
502+
467503
${source}`;
468504
},
469505
};

0 commit comments

Comments
 (0)