Skip to content

Commit adb7300

Browse files
committed
feat: Re-enable typescript for production builds
build: Add .ts/.tsx extensions to webpack resolver chore: Add ending newline to Image.tsx build: Use typescript config from @edx/eslint-config chore: formatting
1 parent 97bf27f commit adb7300

12 files changed

+535
-57
lines changed

README.rst

+20
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ You may create a `.env.private` with any overrides of the environment settings c
157157

158158
**Note: .env.private should be added to your project's .gitignore so it does not get checked in.**
159159

160+
Local module configuration for TypeScript
161+
-----------------------------------------
162+
163+
#. Create file in repository `tsconfig.json`, with a clause `"extends": "@edx/frontend-build"`
164+
#. Set "rootDir" to the root of the source code folders
165+
#. Set "include" to wildcard patterns specifying the subdirectories/files under rootDir where source code can be found
166+
#. Include any wildcards under rootDir that should be excluded using "exclude"
167+
168+
```Sample json
169+
{
170+
"extends": "@edx/frontend-build",
171+
"compilerOptions": {
172+
"rootDir": ".",
173+
"outDir": "dist"
174+
},
175+
"include": ["src/**/*"],
176+
"exclude": ["dist", "node_modules"]
177+
}
178+
```
179+
160180
Development
161181
-----------
162182

config/.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const { babel } = require('../lib/presets');
22

33
module.exports = {
44
extends: '@edx/eslint-config',
5-
parser: '@babel/eslint-parser',
5+
plugins: ['@typescript-eslint'],
6+
parser: '@typescript-eslint/parser',
67
parserOptions: {
78
requireConfigFile: true,
89
babelOptions: {

config/jest.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path');
22
const fs = require('fs');
3+
const { jsWithTs: tsjPreset } = require('ts-jest/presets');
34

45
const presets = require('../lib/presets');
56

@@ -33,11 +34,12 @@ module.exports = {
3334
'/node_modules/(?!@edx)',
3435
],
3536
transform: {
36-
'^.+\\.[t|j]sx?$': [
37+
'^.+\\.jsx?$': [
3738
'babel-jest',
3839
{
3940
configFile: presets.babel.resolvedFilepath,
4041
},
4142
],
43+
...tsjPreset.transform,
4244
},
4345
};

config/webpack.common.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ module.exports = {
1717
// the application being built.
1818
'env.config': false,
1919
},
20-
extensions: ['.js', '.jsx'],
20+
extensions: ['.js', '.jsx', '.ts', '.tsx'],
2121
},
2222
};

config/webpack.prod.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ module.exports = merge(commonConfig, {
7171
// The babel-loader transforms newer ES2015+ syntax to older ES5 for older browsers.
7272
// Babel is configured with the .babelrc file at the root of the project.
7373
{
74-
test: /\.(js|jsx)$/,
74+
test: /\.(js|jsx|ts|tsx)$/,
7575
exclude: /node_modules\/(?!@edx)/,
7676
use: {
7777
loader: 'babel-loader',
7878
options: {
79-
configFile: presets.babel.resolvedFilepath,
79+
configFile: presets['babel-typescript'].resolvedFilepath,
8080
},
8181
},
8282
},

example/src/App.jsx

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import config from 'env.config';
2+
import Image from './Image';
23
import appleUrl, { ReactComponent as Apple } from './apple.svg';
34
import appleImg from './apple.jpg';
45

@@ -19,6 +20,8 @@ export default function App() {
1920
</ul>
2021
<h2>JSX parsing tests</h2>
2122
<Apple style={{ width: '10rem' }} />
23+
<h2>TSX parsing tests</h2>
24+
<Image src={appleUrl} alt="appleFromTsx" style={{ width: '10rem' }} />
2225
<h2>Asset import tests</h2>
2326
<img src={appleUrl} alt="apple" style={{ width: '10rem' }} />
2427
<img src={appleUrl} alt="apple" style={{ width: '10rem' }} />

example/src/Image.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React, { CSSProperties } from 'react';
2+
3+
type ImageProps = {
4+
src: string;
5+
alt?: string;
6+
style?: CSSProperties;
7+
};
8+
9+
const Image = ({ alt, ...rest }:ImageProps) => <img alt={alt} {...rest} />;
10+
11+
const defaultProps = {
12+
alt: undefined,
13+
style: undefined,
14+
};
15+
Image.defaultProps = defaultProps;
16+
export default Image;

example/src/__snapshots__/App.test.jsx.snap

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ exports[`Basic test should render 1`] = `
3939
}
4040
}
4141
/>
42+
<h2>
43+
TSX parsing tests
44+
</h2>
45+
<img
46+
alt="appleFromTsx"
47+
src="icon/mock/path"
48+
style={
49+
Object {
50+
"width": "10rem",
51+
}
52+
}
53+
/>
4254
<h2>
4355
Asset import tests
4456
</h2>

example/tsconfig.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": ".",
5+
"outDir": "dist"
6+
},
7+
"include": [
8+
".eslintrc.js",
9+
"env.config.js",
10+
"src"
11+
],
12+
"exclude": [
13+
"node_modules",
14+
"dist",
15+
]
16+
}

0 commit comments

Comments
 (0)