Comparing version 0.1.3 to 0.1.4
'use strict'; | ||
exports.__esModule = true; | ||
var _assign = require('babel-runtime/core-js/object/assign'); | ||
var _assign2 = _interopRequireDefault(_assign); | ||
exports.compile = compile; | ||
exports.compileFile = compileFile; | ||
exports.compileByBabel = compileByBabel; | ||
exports.getFiles = getFiles; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var fs = require('fs'); | ||
@@ -10,2 +21,6 @@ var path = require('path'); | ||
//babel not export default property | ||
//so can not use `import babel from 'babel-core'` | ||
var babel = require('babel-core'); | ||
var allowFileExt = ['.js']; | ||
@@ -26,60 +41,60 @@ | ||
/** | ||
* compile | ||
* compile files | ||
* @return {} [] | ||
*/ | ||
function compile(srcPath, outPath) { | ||
var files = getFiles(srcPath, true); | ||
var files = getFiles(srcPath, true); | ||
var changedFiles = []; | ||
var changedFiles = []; | ||
console.log(files); | ||
console.log(files); | ||
files.forEach(function (file) { | ||
var srcFullPath = path.join(srcPath, file); | ||
var saveOutFullPathpath = path.join(outPath, file); | ||
files.forEach(function (file) { | ||
var srcFullPath = path.join(srcPath, file); | ||
var saveOutFullPathpath = path.join(outPath, file); | ||
var extname = path.extname(file); | ||
var extname = path.extname(file); | ||
//if is not js file, only copy | ||
if (allowFileExt.indexOf(extname) === -1) { | ||
compileFile(srcFullPath, saveOutFullPathpath, true); | ||
return; | ||
} | ||
//if is not js file, only copy | ||
if (allowFileExt.indexOf(extname) === -1) { | ||
compileFile(srcFullPath, saveOutFullPathpath, true); | ||
return; | ||
} | ||
var mTime = fs.statSync(srcFullPath).mtime.getTime(); | ||
var mTime = fs.statSync(srcFullPath).mtime.getTime(); | ||
if (fileUtil.isFile(saveOutFullPathpath)) { | ||
var outmTime = fs.statSync(saveOutFullPathpath).mtime.getTime(); | ||
if (fileUtil.isFile(saveOutFullPathpath)) { | ||
var outmTime = fs.statSync(saveOutFullPathpath).mtime.getTime(); | ||
// if compiled file mtime is later than source file, | ||
// it means source file not modified, so there is no necessary to compile. | ||
if (outmTime >= mTime) { | ||
return; | ||
} | ||
} | ||
// if compiled file mtime is later than source file, | ||
// it means source file not modified, so there is no necessary to compile. | ||
if (outmTime >= mTime) { | ||
return; | ||
} | ||
} | ||
if (!compiledMtime[file] || mTime > compiledMtime[file]) { | ||
var ret = compileFile(srcFullPath, saveOutFullPathpath); | ||
if (!compiledMtime[file] || mTime > compiledMtime[file]) { | ||
var ret = compileFile(srcFullPath, saveOutFullPathpath); | ||
if (ret) { | ||
changedFiles.push(saveOutFullPathpath); | ||
} | ||
if (ret) { | ||
changedFiles.push(saveOutFullPathpath); | ||
} | ||
compiledMtime[file] = mTime; | ||
compiledMtime[file] = mTime; | ||
var index = compiledErrorFiles.indexOf(file); | ||
var index = compiledErrorFiles.indexOf(file); | ||
if (ret) { | ||
if (index > -1) { | ||
compiledErrorFiles.splice(index, 1); | ||
} | ||
} else if (ret === false) { | ||
if (index === -1) { | ||
compiledErrorFiles.push(file); | ||
} | ||
} | ||
if (ret) { | ||
if (index > -1) { | ||
compiledErrorFiles.splice(index, 1); | ||
} | ||
}); | ||
} else if (ret === false) { | ||
if (index === -1) { | ||
compiledErrorFiles.push(file); | ||
} | ||
} | ||
} | ||
}); | ||
// console.error(compiledErrorFiles) | ||
// console.error(compiledErrorFiles) | ||
} | ||
@@ -94,23 +109,36 @@ | ||
function compileFile(srcFullPath, saveOutFullPath, onlyCopy) { | ||
var content = fs.readFileSync(srcFullPath, 'utf8'); | ||
var content = fs.readFileSync(srcFullPath, 'utf8'); | ||
//when get file content empty, maybe file is locked | ||
if (!content) { | ||
return; | ||
} | ||
//when get file content empty, maybe file is locked | ||
if (!content) { | ||
return; | ||
} | ||
// only copy file content | ||
if (onlyCopy) { | ||
fse.copySync(srcFullPath, saveOutFullPath); | ||
return; | ||
} | ||
// only copy file content | ||
if (onlyCopy) { | ||
fse.copySync(srcFullPath, saveOutFullPath); | ||
return; | ||
} | ||
try { | ||
compileByBabel(content, srcFullPath, saveOutFullPath); | ||
return true; | ||
} catch (e) { | ||
console.error('compile file ' + srcFullPath + ' error', e); | ||
} | ||
try { | ||
var startTime = Date.now(); | ||
return false; | ||
var data = compileByBabel(content, { | ||
filename: srcFullPath | ||
// sourceMaps: true, | ||
// sourceFileName: relativePath | ||
}); | ||
var endTime = Date.now(); | ||
console.log('Compile file ' + srcFullPath, 'Babel cost ' + (endTime - startTime)); | ||
// save file | ||
fse.outputFileSync(saveOutFullPath, data.code); | ||
return true; | ||
} catch (e) { | ||
console.error('compile file ' + srcFullPath + ' error', e); | ||
} | ||
return false; | ||
} | ||
@@ -120,35 +148,26 @@ | ||
* babel compile | ||
* https://babeljs.io/docs/core-packages/#babeltransformcode-string-options-object | ||
* @return {} [] | ||
*/ | ||
function compileByBabel(content, srcFullPath, saveOutFullPath) { | ||
var startTime = Date.now(); | ||
//babel not export default property | ||
//so can not use `import babel from 'babel-core'` | ||
var babel = require('babel-core'); | ||
var data = babel.transform(content, { | ||
filename: srcFullPath, | ||
presets: ['es2015-loose', 'stage-1'], | ||
plugins: ['transform-runtime'] | ||
// sourceMaps: true, | ||
// sourceFileName: relativePath | ||
}); | ||
console.log('Compile file ' + srcFullPath, 'Babel', startTime); | ||
fse.outputFileSync(saveOutFullPath, data.code); | ||
function compileByBabel(content, options) { | ||
return babel.transform(content, (0, _assign2.default)({ | ||
presets: ['es2015-loose', 'stage-1'], | ||
plugins: ['transform-runtime'] | ||
}, options)); | ||
} | ||
/** | ||
* get all files | ||
* @param paths | ||
*/ | ||
function getFiles(paths) { | ||
var files = []; | ||
var result = fileUtil.getAllFiles(paths); | ||
var result = fileUtil.getAllFiles(paths); | ||
var files = result.map(function (item) { | ||
return item.relativePath; | ||
// return path.join(item.basePath, item.relativePath); | ||
}); | ||
files = result.map(function (item) { | ||
return item.relativePath; | ||
// return path.join(item.basePath, item.relativePath); | ||
}); | ||
return files; | ||
return files; | ||
} | ||
//# sourceMappingURL=babel-compile.js.map |
@@ -5,7 +5,11 @@ 'use strict'; | ||
module.exports = function (srcPath, outPath) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var babelCompile = _interopRequireWildcard(_babelCompile); | ||
(0, _babelCompile.compile)(srcPath, outPath, options); | ||
}; | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
var compileBabelD = babelCompile.compile; | ||
compileBabelD.babelCompile = babelCompile; | ||
module.exports = compileBabelD; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "babel-d", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "compile directories by babel using in node", | ||
@@ -33,3 +33,3 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"babel-core": "^6.25.0", | ||
"babel-core": "^6.26.0", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
@@ -39,15 +39,15 @@ "babel-preset-es2015": "^6.24.1", | ||
"babel-preset-stage-1": "^6.24.1", | ||
"babel-runtime": "^6.23.0", | ||
"babel-runtime": "^6.26.0", | ||
"fs-extra": "^3.0.1", | ||
"lodash": "^4.17.4", | ||
"lodash": "^4.17.5", | ||
"walk-sync": "^0.3.2" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-cli": "^6.26.0", | ||
"babel-eslint": "^7.2.3", | ||
"chai": "^4.0.2", | ||
"eslint": "^4.2.0", | ||
"mocha": "^3.4.2", | ||
"source-map": "^0.5.6" | ||
"chai": "^4.1.2", | ||
"eslint": "^4.19.1", | ||
"mocha": "^3.5.3", | ||
"source-map": "^0.5.7" | ||
} | ||
} |
@@ -6,2 +6,6 @@ const fs = require('fs'); | ||
//babel not export default property | ||
//so can not use `import babel from 'babel-core'` | ||
const babel = require('babel-core'); | ||
let allowFileExt = ['.js']; | ||
@@ -22,60 +26,60 @@ | ||
/** | ||
* compile | ||
* compile files | ||
* @return {} [] | ||
*/ | ||
export function compile(srcPath, outPath) { | ||
let files = getFiles(srcPath, true); | ||
let files = getFiles(srcPath, true); | ||
let changedFiles = []; | ||
let changedFiles = []; | ||
console.log(files); | ||
console.log(files); | ||
files.forEach(file => { | ||
let srcFullPath = path.join(srcPath, file); | ||
let saveOutFullPathpath = path.join(outPath, file); | ||
files.forEach(file => { | ||
let srcFullPath = path.join(srcPath, file); | ||
let saveOutFullPathpath = path.join(outPath, file); | ||
let extname = path.extname(file); | ||
let extname = path.extname(file); | ||
//if is not js file, only copy | ||
if (allowFileExt.indexOf(extname) === -1) { | ||
compileFile(srcFullPath, saveOutFullPathpath, true); | ||
return; | ||
} | ||
//if is not js file, only copy | ||
if (allowFileExt.indexOf(extname) === -1) { | ||
compileFile(srcFullPath, saveOutFullPathpath, true); | ||
return; | ||
} | ||
let mTime = fs.statSync(srcFullPath).mtime.getTime(); | ||
let mTime = fs.statSync(srcFullPath).mtime.getTime(); | ||
if (fileUtil.isFile(saveOutFullPathpath)) { | ||
let outmTime = fs.statSync(saveOutFullPathpath).mtime.getTime(); | ||
if (fileUtil.isFile(saveOutFullPathpath)) { | ||
let outmTime = fs.statSync(saveOutFullPathpath).mtime.getTime(); | ||
// if compiled file mtime is later than source file, | ||
// it means source file not modified, so there is no necessary to compile. | ||
if (outmTime >= mTime) { | ||
return; | ||
} | ||
} | ||
// if compiled file mtime is later than source file, | ||
// it means source file not modified, so there is no necessary to compile. | ||
if (outmTime >= mTime) { | ||
return; | ||
} | ||
} | ||
if (!compiledMtime[file] || mTime > compiledMtime[file]) { | ||
let ret = compileFile(srcFullPath, saveOutFullPathpath); | ||
if (!compiledMtime[file] || mTime > compiledMtime[file]) { | ||
let ret = compileFile(srcFullPath, saveOutFullPathpath); | ||
if (ret) { | ||
changedFiles.push(saveOutFullPathpath); | ||
} | ||
if (ret) { | ||
changedFiles.push(saveOutFullPathpath); | ||
} | ||
compiledMtime[file] = mTime; | ||
compiledMtime[file] = mTime; | ||
let index = compiledErrorFiles.indexOf(file); | ||
let index = compiledErrorFiles.indexOf(file); | ||
if (ret) { | ||
if (index > -1) { | ||
compiledErrorFiles.splice(index, 1); | ||
} | ||
} else if (ret === false) { | ||
if (index === -1) { | ||
compiledErrorFiles.push(file); | ||
} | ||
} | ||
if (ret) { | ||
if (index > -1) { | ||
compiledErrorFiles.splice(index, 1); | ||
} | ||
}); | ||
} else if (ret === false) { | ||
if (index === -1) { | ||
compiledErrorFiles.push(file); | ||
} | ||
} | ||
} | ||
}); | ||
// console.error(compiledErrorFiles) | ||
// console.error(compiledErrorFiles) | ||
} | ||
@@ -89,24 +93,37 @@ | ||
*/ | ||
function compileFile(srcFullPath, saveOutFullPath, onlyCopy) { | ||
let content = fs.readFileSync(srcFullPath, 'utf8'); | ||
export function compileFile(srcFullPath, saveOutFullPath, onlyCopy) { | ||
let content = fs.readFileSync(srcFullPath, 'utf8'); | ||
//when get file content empty, maybe file is locked | ||
if (!content) { | ||
return; | ||
} | ||
//when get file content empty, maybe file is locked | ||
if (!content) { | ||
return; | ||
} | ||
// only copy file content | ||
if (onlyCopy) { | ||
fse.copySync(srcFullPath, saveOutFullPath); | ||
return; | ||
} | ||
// only copy file content | ||
if (onlyCopy) { | ||
fse.copySync(srcFullPath, saveOutFullPath); | ||
return; | ||
} | ||
try { | ||
compileByBabel(content, srcFullPath, saveOutFullPath); | ||
return true; | ||
} catch (e) { | ||
console.error(`compile file ${srcFullPath} error`, e); | ||
} | ||
try { | ||
let startTime = Date.now(); | ||
return false; | ||
let data = compileByBabel(content, { | ||
filename: srcFullPath | ||
// sourceMaps: true, | ||
// sourceFileName: relativePath | ||
}); | ||
let endTime = Date.now(); | ||
console.log(`Compile file ${srcFullPath}`, `Babel cost ${endTime - startTime}`); | ||
// save file | ||
fse.outputFileSync(saveOutFullPath, data.code); | ||
return true; | ||
} catch (e) { | ||
console.error(`compile file ${srcFullPath} error`, e); | ||
} | ||
return false; | ||
} | ||
@@ -116,34 +133,25 @@ | ||
* babel compile | ||
* https://babeljs.io/docs/core-packages/#babeltransformcode-string-options-object | ||
* @return {} [] | ||
*/ | ||
function compileByBabel(content, srcFullPath, saveOutFullPath) { | ||
let startTime = Date.now(); | ||
//babel not export default property | ||
//so can not use `import babel from 'babel-core'` | ||
let babel = require('babel-core'); | ||
let data = babel.transform(content, { | ||
filename: srcFullPath, | ||
presets: ['es2015-loose', 'stage-1'], | ||
plugins: ['transform-runtime'], | ||
// sourceMaps: true, | ||
// sourceFileName: relativePath | ||
}); | ||
console.log(`Compile file ${srcFullPath}`, 'Babel', startTime); | ||
fse.outputFileSync(saveOutFullPath, data.code); | ||
export function compileByBabel(content, options) { | ||
return babel.transform(content, Object.assign({ | ||
presets: ['es2015-loose', 'stage-1'], | ||
plugins: ['transform-runtime'] | ||
}, options)); | ||
} | ||
function getFiles(paths) { | ||
let files = []; | ||
/** | ||
* get all files | ||
* @param paths | ||
*/ | ||
export function getFiles(paths) { | ||
let result = fileUtil.getAllFiles(paths); | ||
let result = fileUtil.getAllFiles(paths); | ||
let files = result.map((item) => { | ||
return item.relativePath; | ||
// return path.join(item.basePath, item.relativePath); | ||
}); | ||
files = result.map((item) => { | ||
return item.relativePath; | ||
// return path.join(item.basePath, item.relativePath); | ||
}); | ||
return files; | ||
return files; | ||
} |
@@ -1,5 +0,7 @@ | ||
import { compile } from './babel-compile'; | ||
import * as babelCompile from './babel-compile'; | ||
module.exports = (srcPath, outPath, options = {}) => { | ||
compile(srcPath, outPath, options); | ||
}; | ||
const compileBabelD = babelCompile.compile; | ||
compileBabelD.babelCompile = babelCompile; | ||
module.exports = compileBabelD; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
389
31718
23
Updatedbabel-core@^6.26.0
Updatedbabel-runtime@^6.26.0
Updatedlodash@^4.17.5