Skip to content

Commit

Permalink
fix: loader & function type
Browse files Browse the repository at this point in the history
  • Loading branch information
josteph committed Mar 6, 2021
1 parent a6ab30f commit e710105
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
const fs = require('fs');
const path = require('path');

async function pluginLodashImport(options = {}) {
function pluginLodashImport(options = {}) {
const { filter = /.*/, namespace = '' } = options;

return {
name: 'lodash',
setup(build) {
build.onLoad({ filter, namespace }, async args => {
const contents = await fs.promises.readFile(args.path, 'utf8');
const extension = path.extname(args.path).replace('.', '');
const loader = extension === 'js' ? 'jsx' : extension;

const lodashImportRegex = /import\s+?(?:(?:(?:[\w*\s{},]*)\s+from\s+?)|)(?:(?:'lodash\/?.*?'))[\s]*?(?:;|$|)/g;

const lodashImports = contents.match(lodashImportRegex);

if (!lodashImports) {
return contents;
return {
loader,
contents
};
}

const destructuredImportRegex = /\{\s?(((\w+),?\s?)+)\}/g;
Expand Down Expand Up @@ -58,7 +64,10 @@ async function pluginLodashImport(options = {}) {
finalContents = contents.replace(line, result);
});

return finalContents;
return {
loader,
contents: finalContents
};
});
},
};
Expand Down

0 comments on commit e710105

Please sign in to comment.