Skip to content

Commit

Permalink
Merge pull request #5 from tcrossland/master
Browse files Browse the repository at this point in the history
Add support for JSX files
  • Loading branch information
Philipp Eschenbach committed Nov 11, 2015
2 parents 9b0620b + 5d0457b commit f323c21
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ maven { url "http://dl.bintray.com/errbuddy/plugins" }
```

to you repositories in build.gradle.
The plugin will *ONLY process .es6* files if not otherwise told.
The plugin will *ONLY process .es6 and .jsx* files if not otherwise told.

# configuration
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'maven-publish'
apply plugin: 'maven'

group = 'net.errbuddy.plugins'
version = '1.4.0'
version = '1.4.1'
ext.isReleaseVersion = !version.endsWith("SNAPSHOT")

buildscript {
Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/asset/pipeline/babel/BabelProcessor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class BabelProcessor extends AbstractProcessor {
String process(String input, AssetFile assetFile) {
// only process if
// processing is enabled AND
// the given AssetFile is a Es6File OR processJsFiles is enabled
if (enabled && (processJsFiles || assetFile in Es6AssetFile)) {
// the given AssetFile is a Es6File OR JsxFile OR processJsFiles is enabled
if (enabled && (processJsFiles || assetFile in Es6AssetFile || assetFile in JsxAssetFile)) {
try {
String localBabelOptions = globalBabelOptions
Map config = getConfig()?.options?.clone()
Expand Down
13 changes: 13 additions & 0 deletions src/main/groovy/asset/pipeline/babel/JsxAssetFile.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package asset.pipeline.babel

import asset.pipeline.AbstractAssetFile

import java.util.regex.Pattern

class JsxAssetFile extends AbstractAssetFile {
static final List<String> contentType = ['text/jsx', 'application/javascript','application/x-javascript','text/javascript']
static List<String> extensions = ['jsx']
static String compiledExtension = 'js'
static processors = [BabelProcessor]
Pattern directivePattern = ~/(?m)^\/\/=(.*)/
}
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/asset-pipeline/asset.specs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
asset.pipeline.babel.Es6AssetFile
asset.pipeline.babel.Es6AssetFile
asset.pipeline.babel.JsxAssetFile
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/asset-pipeline/processor.specs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
asset.pipeline.babel.BabelProcessor=asset.pipeline.JsAssetFile,asset.pipeline.babel.Es6AssetFile
asset.pipeline.babel.BabelProcessor=asset.pipeline.JsAssetFile,asset.pipeline.babel.Es6AssetFile,asset.pipeline.babel.JsxAssetFile
16 changes: 14 additions & 2 deletions src/test/groovy/asset/pipeline/babel/BabelProcessorSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import spock.lang.Unroll
@Stepwise
class BabelProcessorSpec extends Specification {

private static final JSX_INPUT = "var foo = (<h1>Hello</h1>);"
private static final JSX_OUTPUT = "var foo = React.createElement(\n \"h1\",\n null,\n \"Hello\"\n);"
private static final ES6_INPUT = """var a = ["Hydrogen","Helium","Lithium","Beryl­lium"];var result = a.map(s => s.length);"""

private static final ES5_OUTPUT = '''var a = ["Hydrogen", "Helium", "Lithium", "Beryl­lium"];var result = a.map(function (s) {
Expand Down Expand Up @@ -123,7 +125,18 @@ try {
false | false
}

def "JsAssetFiles are only processed if enabled option is #processJs"() {
def "process should transform JSX"() {
given:
BabelProcessor processor = new BabelProcessor(new AssetCompiler())
Es6AssetFile file = new Es6AssetFile()
file.path = "test.jsx"
when:
def result = processor.process(JSX_INPUT, file)
then:
result == JSX_OUTPUT
}

def "JsAssetFiles are only processed if enabled option is #processJs"() {
given:
AssetPipelineConfigHolder.config = [babel: [processJsFiles: processJs, enabled: true, options: [blacklist: ['useStrict']]]]
BabelProcessor processor = new BabelProcessor(new AssetCompiler())
Expand All @@ -138,7 +151,6 @@ try {
processJs | isProcessed
true | true
false | false

}

@Unroll
Expand Down

0 comments on commit f323c21

Please sign in to comment.