Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding caching #15

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const cachingFS = require('lasso-caching-fs');
const stripJsonComments = require('strip-json-comments');
const lassoPackageRoot = require('lasso-package-root');
const readOptions = { encoding: 'utf8' };
const isDev = !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
const caches = new WeakMap();

let babel;

Expand Down Expand Up @@ -41,12 +43,25 @@ module.exports = {

return function lassoBabelTransform(code, lassoContext) {
let filename = lassoContext.filename;
let lasso = lassoContext.lasso;
let cache = !isDev && (
caches.get(lasso) || (() => {
const cache = {};
caches.set(lasso, cache);
return cache
})()
);
let cachedCode;

if (!filename || !extensions.hasOwnProperty(path.extname(filename))) {
// This shouldn't be the case
return code;
}

if (cache && (cachedCode = cache[filename])) {
return cachedCode;
}

let babelOptions = transformConfig.babelOptions;

let curDir = path.dirname(filename);
Expand Down Expand Up @@ -103,14 +118,17 @@ module.exports = {
babelOptions.filename = path.relative(curDir, filename);
babelOptions.babelrc = false;
let babel = getBabel();

// console.log("babel file ==> ", curDir, babelOptions.filename);
let result = babel.transformSync(code, babelOptions);
if(result == null) {
// "ignore" and "only" disable ALL babel processing of a file
// e.g. => .babelrc = { "only": ["included/**"] }
// transform('excluded/foo.js') will return null
cache && (cache[filename] = code);
return code;
}

cache && (cache[filename] = result.code);
return result.code;
};
}
Expand Down