Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes committed Jun 23, 2017
0 parents commit fe2e68d
Show file tree
Hide file tree
Showing 13 changed files with 581 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto
*.js text eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- '8'
- '6'
- '4'
52 changes: 52 additions & 0 deletions cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

const path = require('path');
const fs = require('fs');
const makeDir = require('make-dir').sync;
const findCacheDir = require('find-cache-dir');
const homeOrTmp = require('home-or-tmp');
const buble = require('buble');

const DEFAULT_CACHE_DIR = findCacheDir({name: 'buble-register'}) || homeOrTmp;
const DEFAULT_FILENAME = path.join(DEFAULT_CACHE_DIR, `.buble.${buble.VERSION}.json`);
const FILENAME = process.env.BUBLE_CACHE_PATH || DEFAULT_FILENAME;
let data = {};

const save = () => {
let serialized = '{}';

try {
serialized = JSON.stringify(data, null, ' ');
} catch (err) {
if (err.message === 'Invalid string length') {
err.message = 'Cache too large so it\'s been cleared';
console.error(err.stack);
} else {
throw err;
}
}

makeDir(path.dirname(FILENAME));
fs.writeFileSync(FILENAME, serialized);
};

exports.save = save;

exports.load = () => {
if (process.env.BUBLE_DISABLE_CACHE) {
return;
}

process.on('exit', save);
process.nextTick(save);

if (!fs.existsSync(FILENAME)) {
return;
}

try {
data = JSON.parse(fs.readFileSync(FILENAME));
} catch (err) {}
};

exports.get = () => data;
193 changes: 193 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
'use strict';

const path = require('path');
const fs = require('fs');
const sourceMapSupport = require('source-map-support');
const escapeRegExp = require('lodash.escaperegexp');
const isRegExp = require('lodash.isregexp');
const minimatch = require('minimatch');
const addHook = require('pirates').addHook;
const arrify = require('arrify');
const slash = require('slash');
const buble = require('buble');
const registerCache = require('./cache');

const startsWith = (str, match) => str.indexOf(match) === 0;

const regexify = val => {
if (!val) {
return new RegExp(/.^/);
}

if (Array.isArray(val)) {
val = new RegExp(val.map(escapeRegExp).join('|'), 'i');
}

if (typeof val === 'string') {
val = slash(val);

if (startsWith(val, './') || startsWith(val, '*/')) {
val = val.slice(2);
}

if (startsWith(val, '**/')) {
val = val.slice(3);
}

const regex = minimatch.makeRe(val, {nocase: true});

return new RegExp(regex.source.slice(1, -1), 'i');
}

if (isRegExp(val)) {
return val;
}

return new TypeError('Illegial type for regexify');
};

const maps = {};
const transformOpts = {};
let piratesRevert;
let ignore;
let only;

sourceMapSupport.install({
handleUncaughtExtensions: false,
environment: 'node',
retrieveSourceMap: source => {
const map = maps[source];

if (map) {
return {
url: null,
map
};
}

return null;
}
});

registerCache.load();
let cache = registerCache.get();

const cwd = process.cwd();
const getRelativePath = filename => path.relative(cwd, filename);
const mtime = filename => Number(fs.statSync(filename).mtime);

const _shouldIgnore = (pattern, filename) => {
return typeof pattern === 'function' ? pattern(filename) : pattern.test(filename);
};

const shouldIgnore = filename => {
if (!ignore && !only) {
return getRelativePath(filename).split(path.sep).indexOf('node_modules') >= 0;
}

filename = filename.replace(/\\/g, '/');

if (only) {
for (const pattern of only) {
if (_shouldIgnore(pattern, filename)) {
return false;
}
}

return true;
}

if (ignore.length > 0) {
for (const pattern of ignore) {
if (_shouldIgnore(pattern, filename)) {
return true;
}
}
}

return false;
};

const compile = (code, filename) => {
if (shouldIgnore(filename)) {
return code;
}

const cacheKey = `${filename}:${JSON.stringify(transformOpts)}:${buble.VERSION}`;

if (cache) {
const cached = cache[cacheKey];

if (cached && cached.mtime === mtime(filename)) {
return cached.code;
}
}

const opts = Object.assign({}, transformOpts, {
source: filename
});

const result = buble.transform(code, opts);

if (cache) {
result.mtime = mtime(filename);
cache[cacheKey] = result;
}

maps[filename] = result.map;

return result.code;
};

const hookExtensions = exts => {
if (piratesRevert) {
piratesRevert();
}

piratesRevert = addHook(compile, {
exts,
ignoreNodeModules: false
});
};

const revert = () => {
if (piratesRevert) {
piratesRevert();
}

delete require.cache[require.resolve(__filename)];
};

const register = opts => {
opts = opts || {};

if (opts.extensions) {
hookExtensions(opts.extensions);
}

if (opts.cache === false) {
cache = null;
}

if (opts.ignore) {
ignore = arrify(opts.ignore).map(regexify);
}

if (opts.only) {
only = arrify(opts.only).map(regexify);
}

delete opts.extensions;
delete opts.cache;
delete opts.ignore;
delete opts.only;

Object.assign(transformOpts, opts);
};

register({
extensions: ['.js', '.jsx', '.es6', '.es']
});

module.exports = register;
module.exports.revert = revert;
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Vadim Demedes <vdemedes@gmail.com> (github.com/vadimdemedes)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "buble-register",
"version": "0.0.0",
"description": "Buble require hook",
"license": "MIT",
"repository": "vadimdemedes/buble-register",
"author": {
"name": "Vadim Demedes",
"email": "vdemedes@gmail.com",
"url": "github.com/vadimdemedes"
},
"engines": {
"node": ">= 4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"buble"
],
"dependencies": {
"arrify": "^1.0.1",
"buble": "^0.15.2",
"find-cache-dir": "^1.0.0",
"home-or-tmp": "^3.0.0",
"lodash.escaperegexp": "^4.1.2",
"lodash.isregexp": "^4.0.1",
"make-dir": "^1.0.0",
"minimatch": "^3.0.4",
"pirates": "^3.0.1",
"slash": "^1.0.0",
"source-map-support": "^0.4.15"
},
"devDependencies": {
"ava": "^0.19.1",
"decache": "^4.1.0",
"tempfile": "^2.0.0",
"xo": "^0.18.2"
},
"ava": {
"serial": true
}
}
Loading

0 comments on commit fe2e68d

Please sign in to comment.