Skip to content

Commit

Permalink
Adds plugin support for CoffeeScript 2.x. (#745)
Browse files Browse the repository at this point in the history
- modifies Webpack config to add appropriate loaders
- adds '.coffee', '.cjsx' to resolvable extensions
- transpiles source before Babylon parsing
  • Loading branch information
noahlange authored and KyleAMathews committed Mar 21, 2017
1 parent bc8d810 commit 5e425f6
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/gatsby-plugin-coffeescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
coffee
/gatsby-node.js
/index.js
34 changes: 34 additions & 0 deletions packages/gatsby-plugin-coffeescript/.npmignore
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
40 changes: 40 additions & 0 deletions packages/gatsby-plugin-coffeescript/README.md
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");
```
27 changes: 27 additions & 0 deletions packages/gatsby-plugin-coffeescript/package.json
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 ."
}
}
32 changes: 32 additions & 0 deletions packages/gatsby-plugin-coffeescript/src/gatsby-node.js
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
}
1 change: 1 addition & 0 deletions packages/gatsby-plugin-coffeescript/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// noop
17 changes: 17 additions & 0 deletions packages/gatsby-plugin-coffeescript/yarn.lock
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"

0 comments on commit 5e425f6

Please sign in to comment.