Skip to content

Commit

Permalink
fix: perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 12, 2022
1 parent cf75fcc commit 40d89af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
*.orig
.DS_Store
coverage
/.cache
/.cache
.vscode
41 changes: 23 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const cachingFS = require('lasso-caching-fs');
const stripJsonComments = require('strip-json-comments');
const lassoPackageRoot = require('lasso-package-root');
const readOptions = { encoding: 'utf8' };
const markoRegistryFile = /\.marko\.(?:register|init)\.js$/;

let babel;

Expand All @@ -24,28 +25,27 @@ module.exports = {
id: __filename,
stream: false,
createTransform(transformConfig) {

let extensions = transformConfig.extensions;

if (!extensions) {
extensions = ['.js', '.es6'];
const requestedExtensions = transformConfig.extensions;
const extensions = new Set();

if (!requestedExtensions) {
extensions.add('.js');
extensions.add('.es6');
} else {
for (const ext of requestedExtensions) {
extensions.add(ext.charAt(0) !== '.' ? '.' + ext : ext);
}
}

extensions = extensions.reduce((lookup, ext) => {
if (ext.charAt(0) !== '.') {
ext = '.' + ext;
}
lookup[ext] = true;
return lookup;
}, {});
const hasMarko = extensions.has(".marko");

return function lassoBabelTransform(code, lassoContext) {
let filename = lassoContext.filename;

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

const ext = path.extname(filename);
if (!extensions.has(ext)) return code;
if (hasMarko && ext === ".js" && markoRegistryFile.test(filename)) return code;

let babelOptions = transformConfig.babelOptions;

Expand Down Expand Up @@ -107,7 +107,12 @@ module.exports = {
// "ignore" and "only" disable ALL babel processing of a file
// e.g. => .babelrc = { "only": ["included/**"] }
// transform('excluded/foo.js') will return null
return getBabel().transformAsync(code, babelOptions).then(r => r ? r.code : code);
try {
const r = getBabel().transformSync(code, babelOptions);
return r ? r.code : code;
} catch (err) {
return Promise.reject(err);
}
};
}
};

0 comments on commit 40d89af

Please sign in to comment.