Skip to content

Commit 3789295

Browse files
lucasterrajaredpalmer
authored andcommitted
Razzle plugins + razzle-typescript-plugin (#605)
* Add a plugin system * Add razzle-typescript-plugin * Change TypeScript example to use razzle-typescript-plugin * Small refactor to helpers * Add myself to contributors * Use fork-ts-checker-webpack-plugin instead of tslint-loader * Make useBabel false by default * Add esModuleInterop, remove console tslint rules * Improve README * Moved loaderFinder to razzle-dev-utils
1 parent 77e7796 commit 3789295

25 files changed

Lines changed: 403 additions & 124 deletions

.all-contributorsrc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@
6565
"contributions": [
6666
"example"
6767
]
68+
},
69+
{
70+
"login": "lucasterra",
71+
"name": "Lucas Terra",
72+
"avatar_url": "https://avatars2.githubusercontent.com/u/441058?v=4",
73+
"profile": "https://www.linkedin.com/in/lucasterra7/",
74+
"contributions": [
75+
"code",
76+
"example",
77+
"plugin"
78+
]
6879
}
6980
]
70-
}
81+
}

examples/with-typescript/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
"@types/express": "^4.11.1",
1919
"@types/jest": "^22.2.3",
2020
"@types/node": "9.6.6",
21-
"@types/react": "^16.3.12",
21+
"@types/react": "^16.3.13",
2222
"@types/react-dom": "^16.0.5",
2323
"@types/react-router-dom": "^4.2.6",
2424
"@types/webpack-env": "^1.13.6",
2525
"razzle": "^2.0.0-alpha.12",
26-
"ts-jest": "^22.4.4",
27-
"ts-loader": "^4.2.0",
28-
"tslint": "^5.9.1",
29-
"tslint-loader": "^4.2.0",
26+
"razzle-plugin-typescript": "^2.0.0-alpha.12",
27+
"ts-jest": "^22.4.5",
28+
"tslint": "^5.10.0",
3029
"tslint-react": "^3.5.1",
3130
"typescript": "^2.8.3"
3231
},
Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,5 @@
11
'use strict';
22

33
module.exports = {
4-
modify(baseConfig, { target, dev }, webpack) {
5-
const config = Object.assign({}, baseConfig);
6-
7-
config.resolve.extensions = config.resolve.extensions.concat([
8-
'.ts',
9-
'.tsx',
10-
]);
11-
12-
13-
14-
// Locate eslint-loader and remove it (we're using tslint instead)
15-
config.module.rules = config.module.rules.filter(
16-
rule =>
17-
!(
18-
Array.isArray(rule.use) &&
19-
rule.use.length > 0 &&
20-
rule.use[0].options &&
21-
'useEslintrc' in rule.use[0].options
22-
)
23-
);
24-
// Add tslint-loader
25-
config.module.rules.push({
26-
include,
27-
enforce: 'pre',
28-
test: /\.tsx?$/,
29-
loader: 'tslint-loader',
30-
options: {
31-
emitErrors: true,
32-
configFile: './tslint.json',
33-
},
34-
});
35-
36-
// Safely locate Babel-Loader in Razzle's webpack internals
37-
const babelLoader = config.module.rules.findIndex(
38-
rule => rule.use[1].options && rule.use[1].options.babelrc
39-
);
40-
41-
// Get the correct `include` option, since that hasn't changed.
42-
// This tells Razzle which directories to transform.
43-
const { include } = config.module.rules[babelLoader];
44-
45-
// Declare our TypeScript loader configuration
46-
const tsLoader = {
47-
include,
48-
test: /\.tsx?$/,
49-
loader: 'ts-loader',
50-
options: {
51-
transpileOnly: true,
52-
},
53-
};
54-
// Add loader
55-
config.module.rules.push(tsLoader)
56-
57-
// Additional options found at https://github.com/TypeStrong/ts-loader#faster-builds
58-
// Add async typechecking errors
59-
// config.plugins.push(new require('fork-ts-checker-webpack-plugin')())
60-
61-
// If you want to replace Babel with typescript to fully speed up build
62-
// then do the following:
63-
//
64-
// - COMMENT out line 55
65-
// - UNCOMMENT line 67
66-
//
67-
// config.module.rules[babelLoader] = tsLoader;
68-
69-
return config;
70-
},
4+
plugins: ['typescript'],
715
};

examples/with-typescript/src/App.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as React from 'react';
2-
import * as ReactDOM from 'react-dom';
1+
import React from 'react';
2+
import { render } from 'react-dom';
33

44
import App from './App';
55

@@ -8,7 +8,7 @@ import { MemoryRouter } from 'react-router-dom';
88
describe('<App />', () => {
99
test('renders without exploding', () => {
1010
const div = document.createElement('div');
11-
ReactDOM.render(
11+
render(
1212
<MemoryRouter>
1313
<App />
1414
</MemoryRouter>,

examples/with-typescript/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react';
1+
import React from 'react';
22
import { Route, Switch } from 'react-router-dom';
33
import Home from './Home';
44

examples/with-typescript/src/Home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react';
1+
import React from 'react';
22
import logo from './react.svg';
33

44
import './Home.css';

examples/with-typescript/src/client.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import * as React from 'react';
2-
import * as ReactDOM from 'react-dom';
1+
import React from 'react';
2+
import { hydrate } from 'react-dom';
33
import { BrowserRouter } from 'react-router-dom';
44

55
import App from './App';
66

7-
ReactDOM.hydrate(
7+
hydrate(
88
<BrowserRouter>
99
<App />
1010
</BrowserRouter>,
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from 'express';
22
import app from './server';
33

44
if (module.hot) {
5-
module.hot.accept('./server', function() {
5+
module.hot.accept('./server', () => {
66
console.log('🔁 HMR Reloading `./server`...');
77
});
88
console.info('✅ Server-side HMR Enabled!');
@@ -11,8 +11,8 @@ if (module.hot) {
1111
const port = process.env.PORT || 3000;
1212

1313
export default express()
14-
.use((req, res) => app.handle(req, res))
15-
.listen(port, function(err) {
14+
.use(app)
15+
.listen(port, (err: Error) => {
1616
if (err) {
1717
console.error(err);
1818
return;

examples/with-typescript/src/server.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as express from 'express';
2-
import * as React from 'react';
1+
import express from 'express';
2+
import React from 'react';
33
import { renderToString } from 'react-dom/server';
44
import { StaticRouter } from 'react-router-dom';
55

@@ -8,13 +8,11 @@ import App from './App';
88
let assets: any;
99

1010
const syncLoadAssets = () => {
11-
assets = require(process.env.RAZZLE_ASSETS_MANIFEST!);
11+
assets = require(process.env.RAZZLE_ASSETS_MANIFEST!);
1212
};
1313
syncLoadAssets();
1414

15-
const server = express();
16-
17-
server
15+
const server = express()
1816
.disable('x-powered-by')
1917
.use(express.static(process.env.RAZZLE_PUBLIC_DIR!))
2018
.get('/*', (req: express.Request, res: express.Response) => {

examples/with-typescript/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"noFallthroughCasesInSwitch": true,
66
"allowSyntheticDefaultImports": true,
77
"downlevelIteration": true,
8+
"esModuleInterop": true,
89
"forceConsistentCasingInFileNames": true,
910
"jsx": "react",
1011
"lib": ["es6", "es2015", "es2017", "dom"],

0 commit comments

Comments
 (0)