From d4795ab5621905ffada3341a60f17cc099cd2070 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 27 Jul 2016 16:09:49 +0200 Subject: [PATCH] Transpile assets in-place --- build/build.js | 4 +-- .../{copy-static-assets.js => copy-assets.js} | 14 ++++++---- build/lib/transpile-babel-paths.js | 22 ++++----------- build/lib/transpile-coffee-script-paths.js | 28 +++++++------------ 4 files changed, 27 insertions(+), 41 deletions(-) rename build/lib/{copy-static-assets.js => copy-assets.js} (59%) diff --git a/build/build.js b/build/build.js index 0688033e6..60777a70c 100644 --- a/build/build.js +++ b/build/build.js @@ -3,11 +3,11 @@ 'use strict' const cleanOutputDirectory = require('./lib/clean-output-directory') +const copyAssets = require('./lib/copy-assets') const transpileBabelPaths = require('./lib/transpile-babel-paths') const transpileCoffeeScriptPaths = require('./lib/transpile-coffee-script-paths') -const copyStaticAssets = require('./lib/copy-static-assets') cleanOutputDirectory() +copyAssets() transpileBabelPaths() transpileCoffeeScriptPaths() -copyStaticAssets() diff --git a/build/lib/copy-static-assets.js b/build/lib/copy-assets.js similarity index 59% rename from build/lib/copy-static-assets.js rename to build/lib/copy-assets.js index b5ef54983..3b15f17f7 100644 --- a/build/lib/copy-static-assets.js +++ b/build/lib/copy-assets.js @@ -7,16 +7,20 @@ const path = require('path') const fs = require('fs-extra') const computeDestinationPath = require('./compute-destination-path') const CONFIG = require('../config') +const glob = require('glob') module.exports = function () { - console.log('Copying static assets...'); - const sourcePaths = [ - path.join(CONFIG.repositoryRootPath, 'static'), + console.log('Copying assets...'); + const srcPaths = [ path.join(CONFIG.repositoryRootPath, 'dot-atom'), + path.join(CONFIG.repositoryRootPath, 'exports'), + path.join(CONFIG.repositoryRootPath, 'node_modules'), + path.join(CONFIG.repositoryRootPath, 'static'), + path.join(CONFIG.repositoryRootPath, 'src'), path.join(CONFIG.repositoryRootPath, 'vendor') ] - - for (let srcPath of sourcePaths) { + srcPaths.concat(glob.sync(path.join(CONFIG.repositoryRootPath, 'spec', '*.*'), {ignore: '**/*-spec.*'})) + for (let srcPath of srcPaths) { fs.copySync(srcPath, computeDestinationPath(srcPath)) } } diff --git a/build/lib/transpile-babel-paths.js b/build/lib/transpile-babel-paths.js index b1dfa754d..3fbff56c9 100644 --- a/build/lib/transpile-babel-paths.js +++ b/build/lib/transpile-babel-paths.js @@ -8,9 +8,7 @@ module.exports = transpileBabelPaths const babel = require('babel-core') const fs = require('fs') const glob = require('glob') -const mkdirp = require('mkdirp') const path = require('path') -const computeDestinationPath = require('./compute-destination-path') const CONFIG = require('../config') const BABEL_OPTIONS = require('../../static/babelrc.json') @@ -25,9 +23,9 @@ const BUFFER = Buffer(PREFIX_LENGTH) function transpileBabelPaths () { console.log('Transpiling Babel paths...'); - for (let srcPath of glob.sync(`${CONFIG.repositoryRootPath}/src/**/*.js`)) { - if (usesBabel(srcPath)) { - transpileBabelPath(srcPath, computeDestinationPath(srcPath)) + for (let path of glob.sync(`${CONFIG.electronAppPath}/src/**/*.js`)) { + if (usesBabel(path)) { + transpileBabelPath(path) } } } @@ -40,16 +38,8 @@ function usesBabel (path) { return BABEL_PREFIXES.indexOf(filePrefix) !== -1 } -function transpileBabelPath (srcPath, destPath) { +function transpileBabelPath (path) { const options = Object.assign({}, BABEL_OPTIONS) - options.sourceFileName = path.relative(path.dirname(destPath), srcPath) - if (process.platform === 'win32') { - options.sourceFileName = options.sourceFileName.replace(/\\/g, '/') - } - options.sourceMapTarget = path.basename(destPath) - - let result = babel.transformFileSync(srcPath, options) - - mkdirp.sync(path.dirname(destPath)) - fs.writeFileSync(destPath, result.code) + options.sourceMap = null + fs.writeFileSync(path, babel.transformFileSync(path, options).code) } diff --git a/build/lib/transpile-coffee-script-paths.js b/build/lib/transpile-coffee-script-paths.js index 80a92d41a..a53993a52 100644 --- a/build/lib/transpile-coffee-script-paths.js +++ b/build/lib/transpile-coffee-script-paths.js @@ -10,34 +10,26 @@ const mkdirp = require('mkdirp') const path = require('path') const CONFIG = require('../config') -const computeDestinationPath = require('./compute-destination-path') - -const GLOBS = [ - 'src/**/*.coffee,spec/*.coffee', - '!spec/*-spec.coffee', - 'exports/**/*.coffee', - 'static/**/*.coffee' -] module.exports = function transpileCoffeeScriptPaths () { console.log('Transpiling CoffeeScript paths...'); - for (let srcPath of getPathsToTranspile()) { - transpileCoffeeScriptPath(srcPath, computeDestinationPath(srcPath).replace(/coffee$/, 'js')) + for (let path of getPathsToTranspile()) { + transpileCoffeeScriptPath(path) } } function getPathsToTranspile () { let paths = [] - paths = paths.concat(glob.sync(`${CONFIG.repositoryRootPath}/src/**/*.coffee`)) - paths = paths.concat(glob.sync(`${CONFIG.repositoryRootPath}/spec/*.coffee`, {ignore: '**/*-spec.coffee'})) - paths = paths.concat(glob.sync(`${CONFIG.repositoryRootPath}/exports/**/*.coffee`)) + paths = paths.concat(glob.sync(`${CONFIG.electronAppPath}/src/**/*.coffee`)) + paths = paths.concat(glob.sync(`${CONFIG.electronAppPath}/spec/*.coffee`, {ignore: '**/*-spec.coffee'})) + paths = paths.concat(glob.sync(`${CONFIG.electronAppPath}/exports/**/*.coffee`)) return paths } -function transpileCoffeeScriptPath (srcPath, destPath) { - const inputCode = fs.readFileSync(srcPath, 'utf8') - let outputCode = coffee.compile(inputCode) - mkdirp.sync(path.dirname(destPath)) - fs.writeFileSync(destPath, outputCode) +function transpileCoffeeScriptPath (coffeePath) { + const inputCode = fs.readFileSync(coffeePath, 'utf8') + const jsPath = coffeePath.replace(/coffee$/g, 'js') + fs.writeFileSync(jsPath, coffee.compile(inputCode)) + fs.unlinkSync(coffeePath) }