Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird import / bundling issue when using date-fns #398

Closed
vladjerca opened this issue Aug 28, 2020 · 4 comments · Fixed by #400 · 4 remaining pull requests
Closed

Weird import / bundling issue when using date-fns #398

vladjerca opened this issue Aug 28, 2020 · 4 comments · Fixed by #400 · 4 remaining pull requests
Labels

Comments

@vladjerca
Copy link
Contributor

vladjerca commented Aug 28, 2020

If I import a specific method from date-fns the module is incorrectly resolved.

eg:

import addDays from 'date-fns/addDays'

File.ts:

import addDays from 'date-fns/addDays';

export function useDateFns() {
    console.log(addDays(new Date(), 10));
}

File.spec.ts:

import { useDateFns } from './File';

fdescribe('File', () => {
    it('should return void', () => {
        expect(useDateFns()).toBeUndefined();
    });
});

The test fails with the following error:

SourceMap position not found for trace: TypeError: addDays_1.default is not a function

Here comes the juicy part, running the test with the debugger attached renders this:

image

This does not hold up when running the application in production, it can only be reproduced via karma-typescript 😱

Basically from what I can tell is that the bundler is somehow loading the entire date-fns library instead of targeting date-fns/addDays specifically.
image

LE:

karma-typescript: 5.1.0 (latest as of when this issue was opened)

The only workaround I've found for this ATM is to do a really gross hack in my test configuration script

import * as dateFns from 'date-fns';
import addDays from 'date-fns/addDays';

(addDays as any) = dateFns.addDays;

Let me know if there's any more information I can provide 👍

@erikbarke erikbarke added the bug label Aug 30, 2020
@vladjerca
Copy link
Contributor Author

vladjerca commented Aug 31, 2020

@erikbarke

If this helps, I've managed to find the source of the issue.

Within the Resolver.ts:166 at this point the moduleName is overwritten from date-fns/addDays to date-fns.

The cause of this issue is that the regex ends up getting applied on .../node_modules/date-fns/typings.d.ts for some reason.

LE: The real question is why is the filename resolving the typings, this may be potentially normal behavior, sadly I lack a proper understanding of the source to determine it at this point 😬

@erikbarke
Copy link
Collaborator

Are you using compiler paths?

@vladjerca
Copy link
Contributor Author

vladjerca commented Sep 1, 2020

@erikbarke No, this happens with out of the box configuration.

LE: if you want I can create a simple repro repository.

@vladjerca
Copy link
Contributor Author

@erikbarke Here's a repo that should reproduce the issue: https://github.com/vladjerca/karma-typescript-import-bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment