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

Caching and logging #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ will attempt to look for a `.babelrc-browser` file. If neither of those files ex
the `package.json` for a `babel` property.

You can specify different file extensions with the `extensions` option in the transform's config (shown below).
Files are cached in memory by default. You can use `memoryCachedExtensions` to remove caching on some files if you prefer.
To save memory the cache is flushed after `idleCacheFlushTimeout` (default: 10 minutes).

```javascript
require('lasso').configure({
Expand All @@ -34,7 +36,9 @@ require('lasso').configure({
{
transform: 'lasso-babel-transform',
config: {
extensions: ['.js', '.es6'] // Enabled file extensions. Default: ['.js', '.es6']
extensions: ['.marko', '.js', '.es6'], // Enabled file extensions. Default: ['.js', '.es6']
memoryCachedExtensions: ['.js', '.es6'], // Enabled memory caching for these file extensions. Default: same of 'extensions'
idleCacheFlushTimeout: 20 * 60 * 1000, // FLushese the cache to save memory after 20 mins. Default 10 minutes.
}
}
]
Expand Down Expand Up @@ -73,7 +77,7 @@ be transpiled by Babel. For example:

_my-module/.babelrc:_

```
```javascript
{
"exclude": ["excluded/**"],
"presets": [ "@babel/preset-env" ]
Expand All @@ -83,7 +87,7 @@ _my-module/.babelrc:_
As mentioned above, you can also opt to use the `babel` property to the `package.json`.

_my-module/package.json:_
```
```javascript
{
"name": "my-module",
...
Expand All @@ -99,3 +103,15 @@ You will need to install any Babel plugins enabled in your babel config. For exa
npm install @babel/preset-env --save
```

## Debugging

Add this at the beginning of your main file:

```javascript
require('raptor-logging').configure({
loggers: {
'lasso-babel-transform': 'DEBUG', // or Info
'lasso-babel-transform/cache': 'DEBUG'
}
});
```
9 changes: 3 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lasso-babel-transform",
"version": "3.0.1",
"version": "3.1.0",
"description": "Lasso.js transform that uses Babel to transpile ES6 code to ES5.",
"main": "src/index.js",
"scripts": {
Expand All @@ -23,6 +23,7 @@
"dependencies": {
"lasso-caching-fs": "^1.0.1",
"lasso-package-root": "^1.0.0",
"raptor-logging": "^1.0.0",
"strip-json-comments": "^2.0.1"
},
"devDependencies": {
Expand Down
87 changes: 83 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const stripJsonComments = require('strip-json-comments');
const lassoPackageRoot = require('lasso-package-root');
const readOptions = { encoding: 'utf8' };

const logger = require('raptor-logging').logger('lasso-babel-transform');
const loggerCache = require('raptor-logging').logger('lasso-babel-transform/cache');
const caches = new WeakMap();

let babel;

function getBabel() {
Expand All @@ -20,17 +24,37 @@ function readAndParse(path) {
fs.readFileSync(path, readOptions)));
}

let cacheNumber = 0;
module.exports = {
id: __filename,
stream: false,
createTransform(transformConfig) {

const logInfoEnabled = logger.isInfoEnabled();
const logDebugEnabled = logger.isDebugEnabled();
const loggerCacheDebugEnabled = loggerCache.isDebugEnabled();

let extensions = transformConfig.extensions;

if (!extensions) {
extensions = ['.js', '.es6'];
}

logger.info('These extensions will be transformed: ' + JSON.stringify(extensions));

let memoryCachedExtensions = transformConfig.memoryCachedExtensions === undefined && extensions || [];

logger.info('These extensions will be CACHED: ' + JSON.stringify(memoryCachedExtensions));


const idleCacheFlushTimeout = transformConfig.idleCacheFlushTimeout || 10 * 60 * 1000;
if(!idleCacheFlushTimeout) {
logger.info('The cache will NOT be flushed');
} else {
logger.info('The cache will be flush after ' + idleCacheFlushTimeout + ' ms of inactivity.');
}


extensions = extensions.reduce((lookup, ext) => {
if (ext.charAt(0) !== '.') {
ext = '.' + ext;
Expand All @@ -39,14 +63,57 @@ module.exports = {
return lookup;
}, {});

memoryCachedExtensions = memoryCachedExtensions.reduce((lookup, ext) => {
if (ext.charAt(0) !== '.') {
ext = '.' + ext;
}
lookup[ext] = true;
return lookup;
}, {});

return function lassoBabelTransform(code, lassoContext) {
let filename = lassoContext.filename;
const ext = path.extname(filename);

if (!filename || !extensions.hasOwnProperty(path.extname(filename))) {
let relativeFilename = filename;
if (loggerCacheDebugEnabled || logInfoEnabled) relativeFilename = path.relative(process.cwd(), filename);

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

const shouldMemoryCache = memoryCachedExtensions.hasOwnProperty(ext);

let lasso = lassoContext.lasso;
let cache;
if (shouldMemoryCache) {
cache = caches.get(lasso);
if (!cache) {
logger.debug('Creating a new cache');
cacheNumber++;
cache = { cacheNumber }; // Save the number of caches being created
caches.set(lasso, cache);
}

if (idleCacheFlushTimeout) {
if (cache.timerId) clearTimeout(cache.timerId);
cache.timerId = setTimeout(() => {
loggerCache.debug('CACHE (#' + cache.cacheNumber + ') was FLUSHED after ' + idleCacheFlushTimeout + ' ms of inactivity.');
caches.delete(lasso);
}, idleCacheFlushTimeout);
}

if (cache) {
let cachedCode = cache[filename];

if (cachedCode) {
if (loggerCacheDebugEnabled) loggerCache.debug('CACHE HIT (#' + cache.cacheNumber + '): ' + relativeFilename);
return cachedCode;
}
}
}

let babelOptions = transformConfig.babelOptions;

if (!babelOptions) {
Expand Down Expand Up @@ -104,15 +171,27 @@ module.exports = {
babelOptions.filename = filename;
babelOptions.babelrc = false;
let babel = getBabel();
let resultCode;

let result = babel.transformSync(code, babelOptions);
const start = Date.now();
const result = babel.transformSync(code, babelOptions);
const ms = Date.now() - start;
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
return code;
if (logDebugEnabled) logger.debug('File "' + relativeFilename + '" was NOT compiled ( ' + ms + ' ms)');
resultCode = code;
} else {
if (logInfoEnabled) logger.info('File "' + relativeFilename + '" was COMPILED ( ' + ms + ' ms)');
resultCode = result.code;
}

if (cache) {
cache[filename] = resultCode;
if (loggerCacheDebugEnabled) loggerCache.debug('File "' + relativeFilename + '" was CACHED(#' + cache.cacheNumber + ')');
}
return result.code;
return resultCode;
};
}
};