Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript support to React Native #209

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Object {
"sourceExts": Array [
"js",
"json",
"ts",
"tsx",
],
"transformModulePath": "",
"watch": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Object {
"sourceExts": Array [
"js",
"json",
"ts",
"tsx",
],
},
"serializer": Object {
Expand Down Expand Up @@ -151,6 +153,8 @@ Object {
"sourceExts": Array [
"js",
"json",
"ts",
"tsx",
],
},
"serializer": Object {
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-config/src/defaults/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ exports.assetExts = [
'ttf',
];

exports.sourceExts = ['js', 'json'];
exports.sourceExts = ['js', 'json', 'ts', 'tsx'];

exports.moduleSystem = require.resolve('metro/src/lib/polyfills/require.js');

Expand Down
1 change: 1 addition & 0 deletions packages/metro-react-native-babel-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@babel/plugin-transform-spread": "7.0.0-beta.54",
"@babel/plugin-transform-sticky-regex": "7.0.0-beta.54",
"@babel/plugin-transform-template-literals": "7.0.0-beta.54",
"@babel/plugin-transform-typescript": "7.0.0-beta.54",
"@babel/plugin-transform-unicode-regex": "7.0.0-beta.54",
"@babel/template": "7.0.0-beta.54",
"metro-babel7-plugin-react-transform": "0.43.2"
Expand Down
12 changes: 12 additions & 0 deletions packages/metro-react-native-babel-preset/src/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

'use strict';

function isTypeScriptSource(fileName) {
return !!fileName && (fileName.endsWith('.ts') || fileName.endsWith('.tsx'));
}

const defaultPlugins = [
[require('@babel/plugin-proposal-optional-catch-binding')],
[require('@babel/plugin-transform-block-scoping')],
Expand Down Expand Up @@ -124,6 +128,14 @@ const getPreset = (src, options) => {
comments: false,
compact: true,
plugins: defaultPlugins.concat(extraPlugins),
overrides: [
{
test: isTypeScriptSource,
plugins: [
[require('@babel/plugin-transform-typescript'), {isTSX: true}],
],
},
],
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,14 @@ __d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {

var Foo = _$$_REQUIRE(_dependencyMap[1]);

var TypeScript = _$$_REQUIRE(_dependencyMap[2]);

module.exports = {
Foo: Foo,
Bar: Bar
Bar: Bar,
TypeScript: TypeScript
};
},0,[1,2]);
},0,[1,2,5]);
__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {
'use strict';

Expand Down Expand Up @@ -194,6 +197,16 @@ __d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {
__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {
'use strict';
},4,[]);
__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
exports.test = exports.type = undefined;
var type = 'TypeScript';
exports.type = type;
var test = true;
exports.test = test;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!!! 👍

},5,[]);
require(0);"
`;

Expand Down Expand Up @@ -337,11 +350,14 @@ __d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {

var Foo = _$$_REQUIRE(_dependencyMap[1]);

var TypeScript = _$$_REQUIRE(_dependencyMap[2]);

module.exports = {
Foo: Foo,
Bar: Bar
Bar: Bar,
TypeScript: TypeScript
};
},0,[1,2]);
},0,[1,2,5]);
__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {
'use strict';

Expand Down Expand Up @@ -377,5 +393,15 @@ __d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {
__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {
'use strict';
},4,[]);
__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
exports.test = exports.type = undefined;
var type = 'TypeScript';
exports.type = type;
var test = true;
exports.test = test;
},5,[]);
require(0);"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ Array [

var Foo = _$$_REQUIRE(_dependencyMap[1]);

var TypeScript = _$$_REQUIRE(_dependencyMap[2]);

module.exports = {
Foo: Foo,
Bar: Bar
Bar: Bar,
TypeScript: TypeScript
};
});",
"map": Array [
Expand Down Expand Up @@ -78,59 +81,98 @@ Array [
Array [
8,
0,
16,
17,
0,
"module",
],
Array [
8,
6,
17,
6,
"TypeScript",
],
Array [
8,
19,
17,
19,
"require",
],
Array [
8,
49,
17,
0,
],
Array [
10,
0,
19,
0,
"module",
],
Array [
10,
9,
16,
19,
7,
"exports",
],
Array [
8,
16,
10,
16,
19,
0,
],
Array [
8,
10,
19,
19,
16,
17,
],
Array [
9,
11,
0,
16,
19,
18,
"Foo",
],
Array [
9,
11,
12,
16,
19,
17,
],
Array [
10,
12,
0,
16,
19,
23,
"Bar",
],
Array [
11,
12,
12,
19,
17,
],
Array [
13,
0,
16,
19,
28,
"TypeScript",
],
Array [
14,
0,
19,
17,
],
Array [
11,
14,
3,
16,
19,
0,
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
exports[`should create a server 1`] = `
"var __DEV__=false,__BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(),process=this.process||{};process.env=process.env||{};process.env.NODE_ENV=\\"production\\";
!(function(r){'use strict';r.require=i,r.__d=function(r,i,n){if(null!=e[i])return;e[i]={dependencyMap:n,publicModule:{exports:void 0},factory:r,hasError:!1,isInitialized:!1}};var e='number'==typeof __NUM_MODULES__?Array(0|__NUM_MODULES__):Object.create(null);function i(r){var i=r,n=e[i];return n&&n.isInitialized?n.publicModule.exports:t(i,n)}var n=!1;function t(e,i){if(!n&&r.ErrorUtils){var t;n=!0;try{t=d(e,i)}catch(e){r.ErrorUtils.reportFatalError(e)}return n=!1,t}return d(e,i)}var o=16,u=65535;function a(r){return{segmentId:r>>>o,localId:r&u}}function d(n,t){!t&&r.__defineModule&&(r.__defineModule(n),t=e[n]);var o=r.nativeRequire;if(!t&&o){var u=a(n),d=u.segmentId;o(u.localId,d),t=e[n]}if(!t)throw Error('Requiring unknown module \\"'+n+'\\".');if(t.hasError)throw c(n,t.error);t.isInitialized=!0;var l={},p=t,s=p.factory,f=p.dependencyMap;try{var _=t.publicModule={exports:l};return s(r,i,_,l,f),t.factory=void 0,t.dependencyMap=void 0,_.exports}catch(r){throw t.hasError=!0,t.error=r,t.isInitialized=!1,t.publicModule.exports=void 0,r}}function c(r,e){return Error('Requiring module \\"'+r+'\\", which threw an exception: '+e)}i.unpackModuleId=a,i.packModuleId=function(r){return r.segmentId<<o+r.localId}})(this);
__d(function(g,r,m,e,d){'use strict';var o=r(d[0]),t=r(d[1]);m.exports={Foo:t,Bar:o}},0,[1,2]);
__d(function(g,r,m,e,d){'use strict';var t=r(d[0]),o=r(d[1]),c=r(d[2]);m.exports={Foo:o,Bar:t,TypeScript:c}},0,[1,2,5]);
__d(function(g,r,m,e,d){'use strict';var t=r(d[0]);m.exports={type:'bar',foo:t.type}},1,[2]);
__d(function(g,r,m,e,d){'use strict';var t=r(d[0]);m.exports={type:'foo',asset:t}},2,[3]);
__d(function(g,r,m,e,d){m.exports=r(d[0]).registerAsset({__packager_asset:!0,httpServerLocation:\\"/assets\\",width:8,height:8,scales:[1],hash:\\"77d45c1f7fa73c0f6c444a830dc42f67\\",name:\\"test\\",type:\\"png\\"})},3,[4]);
__d(function(g,r,m,e,d){'use strict'},4,[]);
__d(function(g,r,m,e,d){Object.defineProperty(e,\\"__esModule\\",{value:!0}),e.test=e.type=void 0;e.type='TypeScript';e.test=!0},5,[]);
require(0);
//# sourceMappingURL=/TestBundle.map?platform=ios&dev=false&minify=true"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ it('should build the dependency graph', async () => {
{file: 'Foo.js', types: ['js/module']},
{file: 'test.png', types: ['js/module/asset']},
{file: 'AssetRegistry.js', types: ['js/module']},
{file: 'TypeScript.ts', types: ['js/module']},
]);

expect(graph.dependencies.get(entryPoint)).toEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
const Bar = require('./Bar');
const Foo = require('./Foo');

module.exports = {Foo, Bar};
// $FlowFixMe: Flow doesn't understand TypeScript
const TypeScript = require('./TypeScript');

module.exports = {Foo, Bar, TypeScript};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

export const type = 'TypeScript' as string
export const test = true as boolean
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@
dependencies:
"@babel/helper-plugin-utils" "7.0.0-beta.54"

"@babel/plugin-syntax-typescript@7.0.0-beta.54":
version "7.0.0-beta.54"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.0.0-beta.54.tgz#7b01ddebccba8f78693bf2898e1f695bb8a76a7e"
dependencies:
"@babel/helper-plugin-utils" "7.0.0-beta.54"

"@babel/plugin-transform-arrow-functions@7.0.0-beta.54":
version "7.0.0-beta.54"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.0.0-beta.54.tgz#44a977b8e61e4efcc7658bbbe260f204ca1bcf72"
Expand Down Expand Up @@ -511,6 +517,13 @@
"@babel/helper-annotate-as-pure" "7.0.0-beta.54"
"@babel/helper-plugin-utils" "7.0.0-beta.54"

"@babel/plugin-transform-typescript@7.0.0-beta.54":
version "7.0.0-beta.54"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.0.0-beta.54.tgz#7b614ba0dbea88b70ae82df9c429e128928c9251"
dependencies:
"@babel/helper-plugin-utils" "7.0.0-beta.54"
"@babel/plugin-syntax-typescript" "7.0.0-beta.54"

"@babel/plugin-transform-unicode-regex@7.0.0-beta.54":
version "7.0.0-beta.54"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-regex/-/plugin-transform-unicode-regex-7.0.0-beta.54.tgz#1dc7e9150b39aaeb19fca1c863e082f6096afc60"
Expand Down