Skip to content

Commit 250a244

Browse files
committed
fix typescript & styled-jsx
1 parent f297ea0 commit 250a244

27 files changed

+2014
-92
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ coverage
99
.idea
1010
.prep-tmp
1111
lib/
12-
.cache
12+
.cache
13+
public
14+
public*

custom.d.ts

Whitespace-only changes.

gatsby-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
},
3232
`gatsby-plugin-sharp`,
3333
`gatsby-plugin-styled-jsx`,
34-
`gatsby-plugin-typescript`
34+
`gatsby-plugin-typescript`,
35+
`styled-jsx-plugin`
3536
],
3637
}

gatsby-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
2424

2525
return new Promise((resolve, reject) => {
2626
const pages = []
27-
const tracks = path.resolve("src/templates/tracks.js")
27+
const tracks = path.resolve('src/templates/tracks.tsx')
2828
// Query for all markdown "nodes" and for the slug we previously created.
2929
resolve(
3030
graphql(

package.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"gatsby-link": "next",
55
"gatsby-plugin-sharp": "next",
66
"gatsby-plugin-styled-jsx": "next",
7-
"gatsby-plugin-typescript": "next",
87
"gatsby-remark-copy-linked-files": "next",
98
"gatsby-remark-prismjs": "next",
109
"gatsby-remark-responsive-iframe": "next",
@@ -13,9 +12,22 @@
1312
"gatsby-source-filesystem": "next",
1413
"gatsby-transformer-remark": "next",
1514
"graphcool-styles": "^0.1.23",
15+
"postcss-cssnext": "^2.11.0",
1616
"postcss-inherit": "git+https://github.com/timsuchanek/postcss-inherit#build3",
1717
"postcss-simple-vars": "^4.0.0",
18+
"prep-cloud": "^0.0.8",
1819
"styled-jsx": "git+https://github.com/timsuchanek/styled-jsx#build",
19-
"styled-jsx-postcss": "git+https://github.com/timsuchanek/styled-jsx-postcss#build3"
20+
"styled-jsx-postcss": "git+https://github.com/timsuchanek/styled-jsx-postcss#build3",
21+
"ts-loader": "^2.2.0",
22+
"typescript": "2.2.1"
23+
},
24+
"scripts": {
25+
"start": "gatsby develop",
26+
"build": "gatsby build"
27+
},
28+
"devDependencies": {
29+
"@types/react-router": "^4.0.11",
30+
"postcss-discard-comments": "^2.0.4",
31+
"tslint-microsoft-contrib": "^5.0.0"
2032
}
2133
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
typescript
3+
/index.js
4+
/gatsby-node.js
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27+
node_modules
28+
*.un~
29+
yarn.lock
30+
src
31+
flow-typed
32+
coverage
33+
decls
34+
examples
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# gatsby-plugin-typescript
2+
Provides drop-in support for TypeScript and TSX.
3+
4+
## Install
5+
`yarn add gatsby-plugin-typescript typescript`
6+
7+
## How to use
8+
1. Include the plugin in your `gatsby-config.js` file.
9+
1. Add `tsconfig.json` file on your root directory.
10+
1. Write your components in TSX or TypeScript.
11+
1. You're good to go.
12+
13+
`gatsby-config.js`
14+
```javascript
15+
plugins: [
16+
`gatsby-plugin-typescript`,
17+
]
18+
```
19+
20+
`tsconfig.json`
21+
```json
22+
{
23+
"compilerOptions": {
24+
"outDir": "./dist/",
25+
"sourceMap": true,
26+
"noImplicitAny": true,
27+
"module": "commonjs",
28+
"target": "esnext",
29+
"jsx": "react",
30+
"lib": ["dom", "es2015", "es2017"]
31+
},
32+
"include": [
33+
"./src/**/*"
34+
]
35+
}
36+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "gatsby-plugin-typescript",
3+
"version": "1.0.0-beta.1",
4+
"description": "Adds TypeScript support for Gatsby layouts and pages.",
5+
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
6+
"contributors": [
7+
"Noah Lange <noahrlange@gmail.com>"
8+
],
9+
"dependencies": {
10+
"transform-runtime": "^0.0.0",
11+
"ts-loader": "^2.0.3",
12+
"typescript": "^2.2.1"
13+
},
14+
"devDependencies": {
15+
"babel-cli": "^6.24.1"
16+
},
17+
"keywords": [
18+
"gatsby",
19+
"typescript"
20+
],
21+
"license": "MIT",
22+
"main": "index.js",
23+
"scripts": {
24+
"build": "babel src --out-dir .",
25+
"watch": "babel -w src --out-dir ."
26+
}
27+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const { transpileModule } = require(`typescript`)
2+
const path = require(`path`)
3+
4+
const test = /\.tsx?$/
5+
const compilerDefaults = {
6+
target: `esnext`,
7+
experimentalDecorators: true,
8+
jsx: `preserve`,
9+
}
10+
11+
module.exports.resolvableExtensions = () => [`.ts`, `.tsx`]
12+
13+
module.exports.modifyWebpackConfig = ({ config }, { compilerOptions }) => {
14+
// CommonJS to keep Webpack happy.
15+
const copts = Object.assign({}, compilerDefaults, compilerOptions, {
16+
module: `commonjs`,
17+
})
18+
19+
let tsConfig = {}
20+
21+
try {
22+
const tsConfigPath = path.resolve(__dirname, '../../tsconfig.json')
23+
tsConfig = require(tsConfigPath)
24+
} catch (e) {
25+
console.error('no tsconfig found')
26+
}
27+
28+
console.log('tsconfig')
29+
console.log(tsConfig)
30+
31+
// React-land is rather undertyped; nontrivial TS projects will most likely
32+
// error (i.e., not build) at something or other.
33+
const opts = Object.assign({}, { compilerOptions: copts }, tsConfig)
34+
35+
// Load gatsby babel plugin to extract graphql query
36+
const extractQueryPlugin = path.resolve(
37+
__dirname,
38+
`../../node_modules/gatsby/dist/utils/babel-plugin-extract-graphql.js`
39+
)
40+
41+
config.loader(`typescript`, {
42+
test,
43+
loaders: [
44+
`babel?${JSON.stringify({ plugins: [extractQueryPlugin] })}`,
45+
`ts-loader?${JSON.stringify(opts)}`,
46+
],
47+
})
48+
}
49+
50+
module.exports.preprocessSource = (
51+
{ contents, filename },
52+
{ compilerOptions }
53+
) => {
54+
// overwrite defaults with custom compiler options
55+
const copts = Object.assign({}, compilerDefaults, compilerOptions, {
56+
target: `esnext`,
57+
module: `es6`,
58+
})
59+
// return the transpiled source if it's TypeScript, otherwise null
60+
return test.test(filename)
61+
? transpileModule(contents, { compilerOptions: copts }).outputText
62+
: null
63+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// noop

0 commit comments

Comments
 (0)