-
-
Notifications
You must be signed in to change notification settings - Fork 866
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
Razzle plugins + razzle-typescript-plugin #605
Changes from all commits
5ac85e7
bd93f70
e97cd40
3e1df87
c7e09cb
f511a75
dc172d6
8a92ab8
4c27e1c
38b0a1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
modify(baseConfig, { target, dev }, webpack) { | ||
const config = Object.assign({}, baseConfig); | ||
|
||
config.resolve.extensions = config.resolve.extensions.concat([ | ||
'.ts', | ||
'.tsx', | ||
]); | ||
|
||
|
||
|
||
// Locate eslint-loader and remove it (we're using tslint instead) | ||
config.module.rules = config.module.rules.filter( | ||
rule => | ||
!( | ||
Array.isArray(rule.use) && | ||
rule.use.length > 0 && | ||
rule.use[0].options && | ||
'useEslintrc' in rule.use[0].options | ||
) | ||
); | ||
// Add tslint-loader | ||
config.module.rules.push({ | ||
include, | ||
enforce: 'pre', | ||
test: /\.tsx?$/, | ||
loader: 'tslint-loader', | ||
options: { | ||
emitErrors: true, | ||
configFile: './tslint.json', | ||
}, | ||
}); | ||
|
||
// Safely locate Babel-Loader in Razzle's webpack internals | ||
const babelLoader = config.module.rules.findIndex( | ||
rule => rule.use[1].options && rule.use[1].options.babelrc | ||
); | ||
|
||
// Get the correct `include` option, since that hasn't changed. | ||
// This tells Razzle which directories to transform. | ||
const { include } = config.module.rules[babelLoader]; | ||
|
||
// Declare our TypeScript loader configuration | ||
const tsLoader = { | ||
include, | ||
test: /\.tsx?$/, | ||
loader: 'ts-loader', | ||
options: { | ||
transpileOnly: true, | ||
}, | ||
}; | ||
// Add loader | ||
config.module.rules.push(tsLoader) | ||
|
||
// Additional options found at https://github.com/TypeStrong/ts-loader#faster-builds | ||
// Add async typechecking errors | ||
// config.plugins.push(new require('fork-ts-checker-webpack-plugin')()) | ||
|
||
// If you want to replace Babel with typescript to fully speed up build | ||
// then do the following: | ||
// | ||
// - COMMENT out line 55 | ||
// - UNCOMMENT line 67 | ||
// | ||
// config.module.rules[babelLoader] = tsLoader; | ||
|
||
return config; | ||
}, | ||
plugins: ['typescript'], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import * as React from 'react'; | ||
import React from 'react'; | ||
import logo from './react.svg'; | ||
|
||
import './Home.css'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import React from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix react imports There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ignore |
||
import { hydrate } from 'react-dom'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
|
||
import App from './App'; | ||
|
||
ReactDOM.hydrate( | ||
hydrate( | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import * as express from 'express'; | ||
import * as React from 'react'; | ||
import express from 'express'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. turn on |
||
import React from 'react'; | ||
import { renderToString } from 'react-dom/server'; | ||
import { StaticRouter } from 'react-router-dom'; | ||
|
||
|
@@ -8,13 +8,11 @@ import App from './App'; | |
let assets: any; | ||
|
||
const syncLoadAssets = () => { | ||
assets = require(process.env.RAZZLE_ASSETS_MANIFEST!); | ||
assets = require(process.env.RAZZLE_ASSETS_MANIFEST!); | ||
}; | ||
syncLoadAssets(); | ||
|
||
const server = express(); | ||
|
||
server | ||
const server = express() | ||
.disable('x-powered-by') | ||
.use(express.static(process.env.RAZZLE_PUBLIC_DIR!)) | ||
.get('/*', (req: express.Request, res: express.Response) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const makeLoaderFinder = loaderName => rule => { | ||
// i.e.: /eslint-loader/ | ||
const loaderRegex = new RegExp(`[/\\\\]${loaderName}[/\\\\]`); | ||
|
||
// Checks if there's a loader string in rule.loader matching loaderRegex. | ||
const inLoaderString = | ||
typeof rule.loader === 'string' && rule.loader.match(loaderRegex); | ||
|
||
// Checks if there is an object inside rule.use with loader matching loaderRegex, OR | ||
const inUseArray = | ||
Array.isArray(rule.use) && | ||
rule.use.find( | ||
loader => | ||
typeof loader.loader === 'string' && loader.loader.match(loaderRegex) | ||
); | ||
|
||
return inUseArray || inLoaderString; | ||
}; | ||
|
||
module.exports = makeLoaderFinder; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"env": { | ||
"jest": true, | ||
"node": true | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should actually move eslint out of here. I think we ought to move it of the core as well, making a razzle-plugin-eslint. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, this was just to make linting work on the code and test files. It won't be used by plugin users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But yeah. I agree with removing eslint from the default webpack config. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh sorry I just looked at the filename |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# razzle-plugin-typescript | ||
|
||
This package contains a plugin for using TypeScript with Razzle | ||
|
||
## Usage in Razzle Projects | ||
|
||
``` | ||
yarn add razzle-plugin-typescript | ||
``` | ||
|
||
Using the plugin with the default options | ||
|
||
```js | ||
// razzle.config.js | ||
|
||
module.exports = { | ||
plugins: [ | ||
'typescript' | ||
], | ||
}; | ||
``` | ||
|
||
### With custom options: | ||
|
||
```js | ||
// razzle.config.js | ||
|
||
module.exports = { | ||
plugins: [ | ||
{ | ||
name: 'typescript', | ||
options: { | ||
useBabel: false, | ||
tsLoader: { | ||
transpileOnly: true, | ||
experimentalWatchApi: true, | ||
}, | ||
forkTsChecker: { | ||
tsconfig: './tsconfig.json', | ||
tslint: './tslint.json', | ||
watch: './src', | ||
typeCheck: true, | ||
} | ||
}, | ||
}, | ||
], | ||
}; | ||
``` | ||
|
||
## Options | ||
|
||
__useBabel: _boolean___ (defaults: false) | ||
|
||
Set `useBabel` to `true` if you want to keep using `babel` for _JS_/_TS_ interoperability, or if you want to apply any babel transforms to typescript files. (i.e.: [`babel-plugin-styled-components`](https://github.com/styled-components/babel-plugin-styled-components)). | ||
|
||
__tsLoader: _TSLoaderOptions___ (defaults: { transpileOnly: true, experimentalWatchApi: true }) | ||
|
||
Use this to override [`ts-loader`](https://github.com/TypeStrong/ts-loader) options. Check all the options here: [ts-loader options](https://github.com/TypeStrong/ts-loader#loader-options). | ||
|
||
__forkTsChecker: _TSCheckerOptions___ (defaults: { tsconfig: './tsconfig.json', tslint: './tslint.json', watch: './src', typeCheck: true }) | ||
|
||
Use this to override [`fork-ts-checker-webpack-plugin`](https://github.com/Realytics/fork-ts-checker-webpack-plugin) options. Check all the options here: [fork-ts-checker-webpack-plugin options](https://github.com/Realytics/fork-ts-checker-webpack-plugin#options). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
const makeLoaderFinder = require('razzle-dev-utils/makeLoaderFinder'); | ||
|
||
const babelLoaderFinder = makeLoaderFinder('babel-loader'); | ||
const tsLoaderFinder = makeLoaderFinder('ts-loader'); | ||
const eslintLoaderFinder = makeLoaderFinder('eslint-loader'); | ||
|
||
module.exports = { | ||
eslintLoaderFinder, | ||
babelLoaderFinder, | ||
tsLoaderFinder, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put this back?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use
import * as React
when we haveesModuleInterop
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You right sorry. I got confused for a sec