Merge pull request #5579 from sebmck/babel

Rename 6to5 to Babel
This commit is contained in:
Kevin Sawicki
2015-02-20 15:04:26 -08:00
11 changed files with 51 additions and 35 deletions

View File

@@ -19,10 +19,10 @@
],
"atomShellVersion": "0.21.0",
"dependencies": {
"6to5-core": "^3.6.0",
"async": "0.2.6",
"atom-keymap": "^3.1.2",
"atom-space-pen-views": "^2.0.4",
"babel-core": "^4.0.2",
"bootstrap": "git+https://github.com/atom/bootstrap.git#6af81906189f1747fd6c93479e3d998ebe041372",
"clear-cut": "0.4.0",
"coffee-cash": "0.7.0",

View File

@@ -1,8 +1,8 @@
to5 = require '../src/6to5'
babel = require '../src/babel'
crypto = require 'crypto'
describe "6to5 transpiler support", ->
describe "::create6to5VersionAndOptionsDigest", ->
describe "Babel transpiler support", ->
describe "::createBabelVersionAndOptionsDigest", ->
it "returns a digest for the library version and specified options", ->
defaultOptions =
blacklist: [
@@ -16,26 +16,36 @@ describe "6to5 transpiler support", ->
sourceMap: 'inline'
version = '3.0.14'
shasum = crypto.createHash('sha1')
shasum.update('6to5-core', 'utf8')
shasum.update('babel-core', 'utf8')
shasum.update('\0', 'utf8')
shasum.update(version, 'utf8')
shasum.update('\0', 'utf8')
shasum.update('{"blacklist": ["useStrict",],"experimental": true,"optional": ["asyncToGenerator",],"reactCompat": true,"sourceMap": "inline",}')
expectedDigest = shasum.digest('hex')
observedDigest = to5.create6to5VersionAndOptionsDigest(version, defaultOptions)
observedDigest = babel.createBabelVersionAndOptionsDigest(version, defaultOptions)
expect(observedDigest).toEqual expectedDigest
describe "when a .js file starts with 'use babel';", ->
it "transpiles it using babel", ->
transpiled = require('./fixtures/babel/babel-single-quotes.js')
expect(transpiled(3)).toBe 4
describe "when a .js file starts with 'use 6to5';", ->
it "transpiles it using 6to5", ->
transpiled = require('./fixtures/6to5/single-quotes.js')
transpiled = require('./fixtures/babel/6to5-single-quotes.js')
expect(transpiled(3)).toBe 4
describe 'when a .js file starts with "use babel";', ->
it "transpiles it using babel", ->
transpiled = require('./fixtures/babel/babel-double-quotes.js')
expect(transpiled(3)).toBe 4
describe 'when a .js file starts with "use 6to5";', ->
it "transpiles it using 6to5", ->
transpiled = require('./fixtures/6to5/double-quotes.js')
it "transpiles it using babel", ->
transpiled = require('./fixtures/babel/6to5-double-quotes.js')
expect(transpiled(3)).toBe 4
describe "when a .js file does not start with 'use 6to6';", ->
it "does not transpile it using 6to5", ->
expect(-> require('./fixtures/6to5/invalid.js')).toThrow()
it "does not transpile it using babel", ->
expect(-> require('./fixtures/babel/invalid.js')).toThrow()

View File

@@ -2,12 +2,12 @@ path = require 'path'
CSON = require 'season'
CoffeeCache = require 'coffee-cash'
to5 = require '../src/6to5'
babel = require '../src/babel'
CompileCache = require '../src/compile-cache'
describe "Compile Cache", ->
describe ".addPathToCache(filePath)", ->
it "adds the path to the correct CSON, CoffeeScript, or 6to5 cache", ->
it "adds the path to the correct CSON, CoffeeScript, or babel cache", ->
spyOn(CSON, 'readFileSync').andCallThrough()
spyOn(CoffeeCache, 'addPathToCache').andCallThrough()
spyOn(to5, 'addPathToCache').andCallThrough()
@@ -22,7 +22,7 @@ describe "Compile Cache", ->
expect(CoffeeCache.addPathToCache.callCount).toBe 1
expect(to5.addPathToCache.callCount).toBe 0
CompileCache.addPathToCache(path.join(__dirname, 'fixtures', '6to5', 'double-quotes.js'))
CompileCache.addPathToCache(path.join(__dirname, 'fixtures', 'babel', 'double-quotes.js'))
expect(CSON.readFileSync.callCount).toBe 1
expect(CoffeeCache.addPathToCache.callCount).toBe 1
expect(to5.addPathToCache.callCount).toBe 1

View File

@@ -0,0 +1,3 @@
"use babel";
module.exports = v => v + 1

View File

@@ -0,0 +1,3 @@
'use babel';
module.exports = v => v + 1

View File

@@ -1,5 +1,5 @@
###
Cache for source code transpiled by 6to5.
Cache for source code transpiled by Babel.
Inspired by https://github.com/atom/atom/blob/6b963a562f8d495fbebe6abdbafbc7caf705f2c3/src/coffee-cache.coffee.
###
@@ -7,7 +7,7 @@ Inspired by https://github.com/atom/atom/blob/6b963a562f8d495fbebe6abdbafbc7caf7
crypto = require 'crypto'
fs = require 'fs-plus'
path = require 'path'
to5 = null # Defer until used
babel = null # Defer until used
stats =
hits: 0
@@ -28,7 +28,7 @@ defaultOptions =
]
# Includes support for es7 features listed at:
# http://6to5.org/docs/usage/transformers/#es7-experimental-.
# http://babeljs.io/docs/usage/transformers/#es7-experimental-.
experimental: true
optional: [
@@ -79,10 +79,10 @@ updateDigestForJsonValue = (shasum, value) ->
shasum.update(',', 'utf8')
shasum.update('}', 'utf8')
create6to5VersionAndOptionsDigest = (version, options) ->
createBabelVersionAndOptionsDigest = (version, options) ->
shasum = crypto.createHash('sha1')
# Include the version of 6to5 in the hash.
shasum.update('6to5-core', 'utf8')
# Include the version of babel in the hash.
shasum.update('babel-core', 'utf8')
shasum.update('\0', 'utf8')
shasum.update(version, 'utf8')
shasum.update('\0', 'utf8')
@@ -96,8 +96,8 @@ getCachePath = (sourceCode) ->
digest = crypto.createHash('sha1').update(sourceCode, 'utf8').digest('hex')
unless jsCacheDir?
to5Version = require('6to5-core/package.json').version
jsCacheDir = path.join(cacheDir, create6to5VersionAndOptionsDigest(to5Version, defaultOptions))
to5Version = require('babel-core/package.json').version
jsCacheDir = path.join(cacheDir, createBabelVersionAndOptionsDigest(to5Version, defaultOptions))
path.join(jsCacheDir, "#{digest}.js")
@@ -109,7 +109,7 @@ getCachedJavaScript = (cachePath) ->
return cachedJavaScript
null
# Returns the 6to5 options that should be used to transpile filePath.
# Returns the babel options that should be used to transpile filePath.
createOptions = (filePath) ->
options = filename: filePath
for key, value of defaultOptions
@@ -118,8 +118,8 @@ createOptions = (filePath) ->
transpile = (sourceCode, filePath, cachePath) ->
options = createOptions(filePath)
to5 ?= require '6to5-core'
js = to5.transform(sourceCode, options).code
babel ?= require 'babel-core'
js = babel.transform(sourceCode, options).code
stats.misses++
try
@@ -132,7 +132,7 @@ transpile = (sourceCode, filePath, cachePath) ->
# either generated on the fly or pulled from cache.
loadFile = (module, filePath) ->
sourceCode = fs.readFileSync(filePath, 'utf8')
unless sourceCode.startsWith('"use 6to5"') or sourceCode.startsWith("'use 6to5'")
unless /^("use 6to5"|'use 6to5'|"use babel"|'use babel')/.test(sourceCode)
return module._compile(sourceCode, filePath)
cachePath = getCachePath(sourceCode)
@@ -158,7 +158,7 @@ module.exports =
getCacheHits: -> stats.hits
# Visible for testing.
create6to5VersionAndOptionsDigest: create6to5VersionAndOptionsDigest
createBabelVersionAndOptionsDigest: createBabelVersionAndOptionsDigest
addPathToCache: (filePath) ->
return if path.extname(filePath) isnt '.js'

View File

@@ -1,7 +1,7 @@
path = require 'path'
CSON = require 'season'
CoffeeCache = require 'coffee-cash'
to5 = require './6to5'
babel = require './babel'
# This file is required directly by apm so that files can be cached during
# package install so that the first package load in Atom doesn't have to
@@ -15,7 +15,7 @@ exports.addPathToCache = (filePath, atomHome) ->
CoffeeCache.setCacheDirectory(path.join(cacheDir, 'coffee'))
CSON.setCacheDir(path.join(cacheDir, 'cson'))
to5.setCacheDirectory(path.join(cacheDir, 'js', '6to5'))
babel.setCacheDirectory(path.join(cacheDir, 'js', 'babel'))
switch path.extname(filePath)
when '.coffee'
@@ -23,4 +23,4 @@ exports.addPathToCache = (filePath, atomHome) ->
when '.cson'
CSON.readFileSync(filePath)
when '.js'
to5.addPathToCache(filePath)
babel.addPathToCache(filePath)

View File

@@ -47,7 +47,7 @@ window.onload = function() {
setupVmCompatibility();
setupCsonCache(cacheDir);
setupSourceMapCache(cacheDir);
setup6to5(cacheDir);
setupBabel(cacheDir);
require(loadSettings.bootstrapScript);
require('ipc').sendChannel('window-command', 'window:loaded');
@@ -90,10 +90,10 @@ var setupAtomHome = function() {
}
}
var setup6to5 = function(cacheDir) {
var to5 = require('../src/6to5');
to5.setCacheDirectory(path.join(cacheDir, 'js', '6to5'));
to5.register();
var setupBabel = function(cacheDir) {
var babel = require('../src/babel');
babel.setCacheDirectory(path.join(cacheDir, 'js', 'babel'));
babel.register();
}
var setupCsonCache = function(cacheDir) {