Skip to content

Commit

Permalink
fix: don't replace lodash/fp imports with lodash-es/fp
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
2 people authored and paul-vd committed Dec 1, 2020
1 parent c65eb60 commit e5e4f75
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"devDependencies": {
"@types/eslint": "^6.1.2",
"@types/fs-extra": "^9.0.1",
"@types/lodash": "^4.14.161",
"@types/node": "^14.11.1",
"@types/react": "^16.9.11",
"@types/rollup-plugin-json": "^3.0.2",
Expand Down
3 changes: 2 additions & 1 deletion src/babelPluginTsdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const isTruthy = (obj?: any) => {
return obj.constructor !== Object || Object.keys(obj).length > 0;
};

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

export const mergeConfigItems = (type: any, ...configItemsToMerge: any[]) => {
const mergedItems: any[] = [];
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/fixtures/build-default/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import './syntax/jsx-import/JSX-import-JSX';
import './syntax/async';
export { testGenerator } from './syntax/generator';

export { kebabCase } from 'lodash';
export { merge, mergeAll } from 'lodash/fp';

export { foo } from './foo';

export const sum = (a: number, b: number) => {
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/tsdx-build-default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ describe('tsdx build :: zero-config defaults', () => {
expect(matched).toBeFalsy();
});

it("shouldn't replace lodash/fp", () => {
const output = execWithCache('node ../dist/index.js build');
expect(output.code).toBe(0);

const matched = grep(/lodash\/fp/, ['dist/build-default.*.js']);
expect(matched).toBeTruthy();
});

it('should clean the dist directory before rebuilding', () => {
let output = execWithCache('node ../dist/index.js build');
expect(output.code).toBe(0);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,11 @@
dependencies:
"@types/node" "*"

"@types/lodash@^4.14.161":
version "4.14.161"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.161.tgz#a21ca0777dabc6e4f44f3d07f37b765f54188b18"
integrity sha512-EP6O3Jkr7bXvZZSZYlsgt5DIjiGr0dXP1/jVEwVLTFgg0d+3lWVQkRavYVQszV7dYUwvg0B8R0MBDpcmXg7XIA==

"@types/minimatch@*", "@types/minimatch@^3.0.3":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down

0 comments on commit e5e4f75

Please sign in to comment.