mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Don't use es6 features in compile-cache
APM requires it directly, and it uses an older version of node
This commit is contained in:
32
src/babel.js
32
src/babel.js
@@ -1,23 +1,33 @@
|
||||
'use strict'
|
||||
|
||||
const _ = require('underscore-plus')
|
||||
const crypto = require('crypto')
|
||||
const path = require('path')
|
||||
var _ = require('underscore-plus')
|
||||
var crypto = require('crypto')
|
||||
var path = require('path')
|
||||
var defaultOptions = require('../static/babelrc.json')
|
||||
|
||||
let babel = null
|
||||
let babelVersionDirectory = null
|
||||
var babel = null
|
||||
var babelVersionDirectory = null
|
||||
|
||||
const defaultOptions = require('../static/babelrc.json')
|
||||
var PREFIXES = [
|
||||
'/** @babel */',
|
||||
'"use babel"',
|
||||
'\'use babel\''
|
||||
]
|
||||
|
||||
var PREFIX_LENGTH = Math.max.apply(Math, PREFIXES.map(function (prefix) {
|
||||
return prefix.length
|
||||
}))
|
||||
|
||||
exports.shouldCompile = function(sourceCode) {
|
||||
return sourceCode.startsWith('/** @babel */') ||
|
||||
sourceCode.startsWith('"use babel"') ||
|
||||
sourceCode.startsWith("'use babel'")
|
||||
var start = sourceCode.substr(0, PREFIX_LENGTH)
|
||||
return PREFIXES.some(function (prefix) {
|
||||
return start.indexOf(prefix) === 0
|
||||
})
|
||||
}
|
||||
|
||||
exports.getCachePath = function(sourceCode) {
|
||||
if (babelVersionDirectory == null) {
|
||||
let babelVersion = require('babel-core/package.json').version
|
||||
var babelVersion = require('babel-core/package.json').version
|
||||
babelVersionDirectory = path.join('js', 'babel', createVersionAndOptionsDigest(babelVersion, defaultOptions))
|
||||
}
|
||||
|
||||
@@ -35,7 +45,7 @@ exports.compile = function(sourceCode, filePath) {
|
||||
babel = require('babel-core')
|
||||
}
|
||||
|
||||
let options = _.defaults({filename: filePath}, defaultOptions)
|
||||
var options = _.defaults({filename: filePath}, defaultOptions)
|
||||
return babel.transform(sourceCode, options).code
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const crypto = require('crypto')
|
||||
const path = require('path')
|
||||
var crypto = require('crypto')
|
||||
var path = require('path')
|
||||
|
||||
// The coffee-script compiler is required eagerly because:
|
||||
// 1. It is always used.
|
||||
// 2. It reassigns Error.prepareStackTrace, so we need to make sure that
|
||||
// the 'source-map-support' module is installed *after* it is loaded.
|
||||
const CoffeeScript = require('coffee-script')
|
||||
var CoffeeScript = require('coffee-script')
|
||||
|
||||
exports.shouldCompile = function() {
|
||||
return true
|
||||
@@ -24,13 +24,13 @@ exports.getCachePath = function(sourceCode) {
|
||||
}
|
||||
|
||||
exports.compile = function(sourceCode, filePath) {
|
||||
let output = CoffeeScript.compile(sourceCode, {
|
||||
var output = CoffeeScript.compile(sourceCode, {
|
||||
filename: filePath,
|
||||
sourceFiles: [filePath],
|
||||
sourceMap: true
|
||||
})
|
||||
|
||||
let js = output.js
|
||||
var js = output.js
|
||||
js += '\n'
|
||||
js += '//# sourceMappingURL=data:application/json;base64,'
|
||||
js += new Buffer(output.v3SourceMap).toString('base64')
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const CSON = require('season')
|
||||
const fs = require('fs-plus')
|
||||
var path = require('path')
|
||||
var CSON = require('season')
|
||||
var fs = require('fs-plus')
|
||||
var _ = require('underscore-plus')
|
||||
|
||||
const COMPILERS = {
|
||||
var COMPILERS = {
|
||||
'.js': require('./babel'),
|
||||
'.ts': require('./typescript'),
|
||||
'.coffee': require('./coffee-script')
|
||||
}
|
||||
|
||||
for (let extension in COMPILERS) {
|
||||
let compiler = COMPILERS[extension]
|
||||
var cacheDirectory = null
|
||||
|
||||
_.each(COMPILERS, function (compiler, extension) {
|
||||
Object.defineProperty(require.extensions, extension, {
|
||||
enumerable: true,
|
||||
writable: false,
|
||||
value: function (module, filePath) {
|
||||
let code = compileFileAtPath(compiler, filePath)
|
||||
var code = compileFileAtPath(compiler, filePath, extension)
|
||||
return module._compile(code, filePath)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let cacheDirectory = null
|
||||
})
|
||||
|
||||
exports.setAtomHomeDirectory = function (atomHome) {
|
||||
let cacheDir = path.join(atomHome, 'compile-cache')
|
||||
var cacheDir = path.join(atomHome, 'compile-cache')
|
||||
if (process.env.USER === 'root' && process.env.SUDO_USER && process.env.SUDO_USER !== process.env.USER) {
|
||||
cacheDir = path.join(cacheDirectory, 'root')
|
||||
}
|
||||
@@ -43,21 +43,23 @@ exports.getCacheDirectory = function () {
|
||||
|
||||
exports.addPathToCache = function (filePath, atomHome) {
|
||||
this.setAtomHomeDirectory(atomHome)
|
||||
extension = path.extname(filePath)
|
||||
if (extension === '.cson') {
|
||||
return CSON.readFileSync(filePath)
|
||||
}
|
||||
var extension = path.extname(filePath)
|
||||
|
||||
if (compiler = COMPILERS[extension]) {
|
||||
return compileFileAtPath(compiler, filePath)
|
||||
if (extension === '.cson') {
|
||||
CSON.readFileSync(filePath)
|
||||
} else {
|
||||
var compiler = COMPILERS[extension]
|
||||
if (compiler) {
|
||||
compileFileAtPath(compiler, filePath, extension)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function compileFileAtPath (compiler, filePath) {
|
||||
let sourceCode = fs.readFileSync(filePath, 'utf8')
|
||||
function compileFileAtPath (compiler, filePath, extension) {
|
||||
var sourceCode = fs.readFileSync(filePath, 'utf8')
|
||||
if (compiler.shouldCompile(sourceCode, filePath)) {
|
||||
let cachePath = compiler.getCachePath(sourceCode, filePath)
|
||||
let compiledCode = readCachedJavascript(cachePath)
|
||||
var cachePath = compiler.getCachePath(sourceCode, filePath)
|
||||
var compiledCode = readCachedJavascript(cachePath)
|
||||
if (compiledCode == null) {
|
||||
compiledCode = compiler.compile(sourceCode, filePath)
|
||||
writeCachedJavascript(cachePath, compiledCode)
|
||||
@@ -68,7 +70,7 @@ function compileFileAtPath (compiler, filePath) {
|
||||
}
|
||||
|
||||
function readCachedJavascript (relativeCachePath) {
|
||||
let cachePath = path.join(cacheDirectory, relativeCachePath)
|
||||
var cachePath = path.join(cacheDirectory, relativeCachePath)
|
||||
if (fs.isFileSync(cachePath)) {
|
||||
try {
|
||||
return fs.readFileSync(cachePath, 'utf8')
|
||||
@@ -78,11 +80,11 @@ function readCachedJavascript (relativeCachePath) {
|
||||
}
|
||||
|
||||
function writeCachedJavascript (relativeCachePath, code) {
|
||||
let cachePath = path.join(cacheDirectory, relativeCachePath)
|
||||
var cachePath = path.join(cacheDirectory, relativeCachePath)
|
||||
fs.writeFileSync(cachePath, code, 'utf8')
|
||||
}
|
||||
|
||||
const INLINE_SOURCE_MAP_REGEXP = /\/\/[#@]\s*sourceMappingURL=([^'"\n]+)\s*$/mg
|
||||
var INLINE_SOURCE_MAP_REGEXP = /\/\/[#@]\s*sourceMappingURL=([^'"\n]+)\s*$/mg
|
||||
|
||||
require('source-map-support').install({
|
||||
handleUncaughtExceptions: false,
|
||||
@@ -95,14 +97,14 @@ require('source-map-support').install({
|
||||
return null
|
||||
}
|
||||
|
||||
let sourceCode = fs.readFileSync(filePath, 'utf8')
|
||||
let compiler = COMPILERS[path.extname(filePath)]
|
||||
let fileData = readCachedJavascript(compiler.getCachePath(sourceCode, filePath))
|
||||
var sourceCode = fs.readFileSync(filePath, 'utf8')
|
||||
var compiler = COMPILERS[path.extname(filePath)]
|
||||
var fileData = readCachedJavascript(compiler.getCachePath(sourceCode, filePath))
|
||||
if (fileData == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
let match, lastMatch
|
||||
var match, lastMatch
|
||||
INLINE_SOURCE_MAP_REGEXP.lastIndex = 0
|
||||
while ((match = INLINE_SOURCE_MAP_REGEXP.exec(fileData))) {
|
||||
lastMatch = match
|
||||
@@ -111,9 +113,9 @@ require('source-map-support').install({
|
||||
return null
|
||||
}
|
||||
|
||||
let sourceMappingURL = lastMatch[1]
|
||||
let rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1)
|
||||
let sourceMap = JSON.parse(new Buffer(rawData, 'base64').toString())
|
||||
var sourceMappingURL = lastMatch[1]
|
||||
var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1)
|
||||
var sourceMap = JSON.parse(new Buffer(rawData, 'base64').toString())
|
||||
|
||||
return {
|
||||
map: sourceMap,
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
'use strict'
|
||||
|
||||
const _ = require('underscore-plus')
|
||||
const crypto = require('crypto')
|
||||
const path = require('path')
|
||||
var _ = require('underscore-plus')
|
||||
var crypto = require('crypto')
|
||||
var path = require('path')
|
||||
|
||||
let TypeScriptSimple = null
|
||||
let typescriptVersionDir = null
|
||||
|
||||
const defaultOptions = {
|
||||
var defaultOptions = {
|
||||
target: 1,
|
||||
module: 'commonjs',
|
||||
sourceMap: true
|
||||
}
|
||||
|
||||
var TypeScriptSimple = null
|
||||
var typescriptVersionDir = null
|
||||
|
||||
exports.shouldCompile = function() {
|
||||
return true
|
||||
}
|
||||
|
||||
exports.getCachePath = function(sourceCode) {
|
||||
if (typescriptVersionDir == null) {
|
||||
let version = require('typescript-simple/package.json').version
|
||||
var version = require('typescript-simple/package.json').version
|
||||
typescriptVersionDir = path.join('ts', createVersionAndOptionsDigest(version, defaultOptions))
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ exports.compile = function(sourceCode, filePath) {
|
||||
TypeScriptSimple = require('typescript-simple').TypeScriptSimple
|
||||
}
|
||||
|
||||
let options = _.defaults({filename: filePath}, defaultOptions)
|
||||
var options = _.defaults({filename: filePath}, defaultOptions)
|
||||
return new TypeScriptSimple(options, false).compile(sourceCode, filePath)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user