-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds plugin support for CoffeeScript 2.x. (#745)
- modifies Webpack config to add appropriate loaders - adds '.coffee', '.cjsx' to resolvable extensions - transpiles source before Babylon parsing
- Loading branch information
1 parent
bc8d810
commit 5e425f6
Showing
7 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
coffee | ||
/gatsby-node.js | ||
/index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules | ||
*.un~ | ||
yarn.lock | ||
src | ||
flow-typed | ||
coverage | ||
decls | ||
examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# gatsby-plugin-coffeescript | ||
Provides drop-in support for CoffeeScript and CJSX. | ||
|
||
## Install | ||
`yarn add gatsby-plugin-coffeescript` | ||
|
||
## How to use | ||
1. Include the plugin in your `gatsby-config.js` file. | ||
2. Write your components in CJSX or CoffeeScript. | ||
|
||
```javascript | ||
// in gatsby-config.js | ||
plugins: [ | ||
// no configuration | ||
`gatsby-plugin-coffeescript`, | ||
// custom configuration | ||
{ | ||
resolve: `gatsby-plugin-coffeescript`, | ||
// options are passed directly to the compiler | ||
options: {} | ||
} | ||
] | ||
``` | ||
|
||
## Notes | ||
First, note that CoffeeScript + React is a troubled combination. This plugin | ||
relies upon deprecated modules that may someday prove to be dysfunctional or | ||
otherwise deficient. | ||
|
||
Furthermore, note that the installed version of CoffeeScript is @next. This is | ||
not optional - *named exports are required for page queries to work properly.* | ||
|
||
You will need to manually edit your `coffee-loader` installation and install | ||
`coffeescript` separately in your project directory to ensure that | ||
CoffeeScript@next is being loaded. The very first line of source in the | ||
former's `index.js` should be the following: note the lack of dash. | ||
|
||
```js | ||
var coffee = require("coffeescript"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "gatsby-plugin-coffeescript", | ||
"description": "Adds CoffeeScript support for Gatsby layouts and pages.", | ||
"version": "1.0.0-alpha12", | ||
"author": "Kyle Mathews <mathews.kyle@gmail.com>", | ||
"contributors": [ | ||
"Noah Lange <noahrlange@gmail.com>" | ||
], | ||
"dependencies": { | ||
"coffee-react-transform": "^5.0.0", | ||
"coffeescript": "next" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.22.2" | ||
}, | ||
"keywords": [ | ||
"gatsby", | ||
"coffeescript" | ||
], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"readme": "README.md", | ||
"scripts": { | ||
"build": "babel src --out-dir .", | ||
"watch": "babel -w src --out-dir ." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import transform from 'coffee-react-transform' | ||
import { compile } from 'coffeescript' | ||
|
||
const COFFEE = /\.coffee$/ | ||
const CJSX = /\.cjsx$/ | ||
|
||
export function resolvableExtensions() { | ||
return [ `.coffee`, `.cjsx` ] | ||
} | ||
|
||
export function modifyWebpackConfig(ctx) { | ||
// we need to use Babel to get around the ES6 export issue | ||
const { config } = ctx.args | ||
config.loader('coffee', { | ||
test: COFFEE, | ||
loaders: [ 'babel', 'coffee' ] | ||
}) | ||
config.loader('cjsx', { | ||
test: CJSX, | ||
loaders: [ 'babel', 'coffee', 'cjsx' ] | ||
}) | ||
} | ||
|
||
export function preprocessSource(ctx) { | ||
const { args: { filename, contents }, pluginOptions } = ctx | ||
// don't need to account for ES6, Babylon can parse it | ||
if (CJSX.test(filename)) { | ||
return compile(transform(contents), pluginOptions) | ||
} else if (COFFEE.test(filename)) { | ||
return compile(contents, pluginOptions) | ||
} else return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// noop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
coffee-react-transform@^5.0.0: | ||
version "5.0.0" | ||
resolved "https://registry.yarnpkg.com/coffee-react-transform/-/coffee-react-transform-5.0.0.tgz#b62e8ae1b113fce9b1a5990b06c9bc44651ae9c5" | ||
|
||
coffeescript@next: | ||
version "2.0.0-alpha1" | ||
resolved "https://registry.yarnpkg.com/coffeescript/-/coffeescript-2.0.0-alpha1.tgz#abf8b54b5c20a9c9f707c8aff35b227d1d6b5e3b" | ||
dependencies: | ||
marked "~0.3.6" | ||
|
||
marked@~0.3.6: | ||
version "0.3.6" | ||
resolved "https://registry.yarnpkg.com/marked/-/marked-0.3.6.tgz#b2c6c618fccece4ef86c4fc6cb8a7cbf5aeda8d7" |