Skip to content

Commit 8b91c74

Browse files
Altrim Beqiriagilgur5
andcommitted
fix: don't replace lodash/fp imports with lodash-es/fp
- previously, when importing modules from `lodash/fp`, the imports broke since `babel-plugin-transform-rename-import` renames all `lodash` imports to `lodash-es` for the ESM build - e.g `import mergeAll from 'lodash/fp/mergeAll';` ends up being imported as `import mergeAll from 'lodash-es/fp/mergeAll'` and we got an error since `lodash-es/fp` doesn't exist - with this fix we use a regex instead of bare string for replacement and add a negative lookahead to it so that it doesn't replace `lodash/fp` imports Co-authored-by: Anton Gilgur <agilgur5@gmail.com>
1 parent da53ea8 commit 8b91c74

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"devDependencies": {
102102
"@types/eslint": "^6.1.2",
103103
"@types/fs-extra": "^9.0.1",
104+
"@types/lodash": "^4.14.161",
104105
"@types/node": "^14.11.1",
105106
"@types/react": "^16.9.11",
106107
"@types/rollup-plugin-json": "^3.0.2",

src/babelPluginTsdx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export const isTruthy = (obj?: any) => {
1010
return obj.constructor !== Object || Object.keys(obj).length > 0;
1111
};
1212

13-
const replacements = [{ original: 'lodash', replacement: 'lodash-es' }];
13+
// replace lodash with lodash-es, but not lodash/fp
14+
const replacements = [{ original: 'lodash(?!/fp)', replacement: 'lodash-es' }];
1415

1516
export const mergeConfigItems = (type: any, ...configItemsToMerge: any[]) => {
1617
const mergedItems: any[] = [];

test/e2e/fixtures/build-default/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import './syntax/jsx-import/JSX-import-JSX';
66
import './syntax/async';
77
export { testGenerator } from './syntax/generator';
88

9+
export { kebabCase } from 'lodash';
10+
export { merge, mergeAll } from 'lodash/fp';
11+
912
export { foo } from './foo';
1013

1114
export const sum = (a: number, b: number) => {

test/e2e/tsdx-build-default.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ describe('tsdx build :: zero-config defaults', () => {
6767
expect(matched).toBeFalsy();
6868
});
6969

70+
it("shouldn't replace lodash/fp", () => {
71+
const output = execWithCache('node ../dist/index.js build');
72+
expect(output.code).toBe(0);
73+
74+
const matched = grep(/lodash\/fp/, ['dist/build-default.*.js']);
75+
expect(matched).toBeTruthy();
76+
});
77+
7078
it('should clean the dist directory before rebuilding', () => {
7179
let output = execWithCache('node ../dist/index.js build');
7280
expect(output.code).toBe(0);

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,11 @@
13141314
dependencies:
13151315
"@types/node" "*"
13161316

1317+
"@types/lodash@^4.14.161":
1318+
version "4.14.161"
1319+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.161.tgz#a21ca0777dabc6e4f44f3d07f37b765f54188b18"
1320+
integrity sha512-EP6O3Jkr7bXvZZSZYlsgt5DIjiGr0dXP1/jVEwVLTFgg0d+3lWVQkRavYVQszV7dYUwvg0B8R0MBDpcmXg7XIA==
1321+
13171322
"@types/minimatch@*", "@types/minimatch@^3.0.3":
13181323
version "3.0.3"
13191324
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"

0 commit comments

Comments
 (0)