Skip to content

Commit f604fee

Browse files
authored
feat: Add outLodashPackage feature to allow for lodash-es, fix .cjs and .mjs files. (#2)
* feat: add outLodashPackage feature to allow for lodash-es * fix: Fix loader for cjs and mjs extension files
1 parent 173a8d4 commit f604fee

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@ isEmpty(something);
6262

6363
## Options
6464

65-
You can specify your own `filter` as per according to esbuild docs [here](https://esbuild.github.io/plugins/#concepts).
65+
You can specify your own `filter` as per according to esbuild docs [here](https://esbuild.github.io/plugins/#concepts).
66+
67+
You can specify `outLodashPackage` which by default is `lodash`. An example
68+
of this would be specifying calls to be rewritten to use `lodash-es`.

__tests__/fixtures/es/input.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { isEmpty, chunk } from 'lodash';
2+
3+
const empty = {};
4+
const notEmpty = {
5+
a: 1,
6+
};
7+
8+
checkIsEmpty(obj);
9+
checkIsEmpty(notEmpty);
10+
11+
chunk(['a', 'b', 'c', 'd'], 2);

__tests__/fixtures/es/output.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import isEmpty from "lodash-es/isEmpty";
2+
import chunk from "lodash-es/chunk";
3+
const empty = {};
4+
const notEmpty = {
5+
a: 1
6+
};
7+
checkIsEmpty(obj);
8+
checkIsEmpty(notEmpty);
9+
chunk(["a", "b", "c", "d"], 2);

__tests__/index.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ describe('Lodash plugin tests', () => {
2222
expect(res.outputFiles[0].text).toStrictEqual(output);
2323
});
2424

25+
it('should honor outLodashPackage option', async () => {
26+
const output = fs.readFileSync(resolvePath('fixtures/es/output.js'), 'utf-8');
27+
28+
const res = await esbuild.build({
29+
entryPoints: [resolvePath('fixtures/es/input.mjs')],
30+
bundle: false,
31+
plugins: [lodashPlugin({ outLodashPackage: 'lodash-es' })],
32+
write: false,
33+
});
34+
35+
expect(res.outputFiles[0].text).toStrictEqual(output);
36+
});
37+
2538
it('should handle destructured import transformation', async () => {
2639
const output = fs.readFileSync(resolvePath('fixtures/destructured/output.js'), 'utf-8');
2740

index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
const fs = require('fs');
22
const path = require('path');
33

4+
// All extensions included here: https://esbuild.github.io/content-types/#javascript
5+
const JS_EXTENSIONS = new Set(['js', 'cjs', 'mjs']);
6+
47
function pluginLodashImport(options = {}) {
5-
const { filter = /.*/ } = options;
8+
const { filter = /.*/, outLodashPackage = 'lodash' } = options;
69

710
return {
811
name: 'lodash',
912
setup(build) {
1013
build.onLoad({ filter }, async args => {
1114
const contents = await fs.promises.readFile(args.path, 'utf8');
1215
const extension = path.extname(args.path).replace('.', '');
13-
const loader = extension === 'js' ? 'jsx' : extension;
16+
const loader = JS_EXTENSIONS.has(extension) ? 'jsx' : extension;
1417

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

@@ -49,9 +52,9 @@ function pluginLodashImport(options = {}) {
4952
const previousResult = `${result ? `${result}\n` : ''}`;
5053
if (name.includes(' as ')) {
5154
const [realName, alias] = name.split(' as ');
52-
result = `${previousResult}import ${alias} from 'lodash/${realName}';`;
55+
result = `${previousResult}import ${alias} from '${outLodashPackage}/${realName}';`;
5356
} else {
54-
result = `${previousResult}import ${name} from 'lodash/${name}';`;
57+
result = `${previousResult}import ${name} from '${outLodashPackage}/${name}';`;
5558
}
5659
});
5760

0 commit comments

Comments
 (0)