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

formatters.mjs export issue #3744

Open
houd1ni opened this issue Mar 22, 2024 · 4 comments
Open

formatters.mjs export issue #3744

houd1ni opened this issue Mar 22, 2024 · 4 comments

Comments

@houd1ni
Copy link

houd1ni commented Mar 22, 2024

image

format(new Date(), 'yyyy') results in Uncaught TypeError: _formattersMjs.formatters is undefined

It's simple (almost sample) Parcel v 2.12.0 project with this config in package.json:

  "@parcel/resolver-default": {
    "packageExports": true
  }

image

@fturmel
Copy link
Member

fturmel commented Mar 22, 2024

That's really helpful, and related to #3670. It definitely looks like a Parcel bug (possibly 2?) and not specific to Plasmo's usage of Parcel.

I spent some time troubleshooting (I keep getting nerd-snipped here lol), and I got some additional info:

Re-exporting an import also used in the module that imports it breaks Parcel in dev/watch mode

Breaks:
The formatters get tree-shaken unless the re-exports are consumed by the app even though they are also used in the format function itself. If've described this in the other issue in greater details. This was introduced in date-fns 3.2.0.

import { formatters } from "../_lib/format/formatters/index.js";
import { longFormatters } from "../_lib/format/longFormatters/index.js";

// Rexports of internal for libraries to use.
// See: https://github.com/date-fns/date-fns/issues/3638#issuecomment-1877082874
export { formatters, longFormatters };

Possible workaround:
If we assign the imports to new constants and export those, it seems to be OK.

import { formatters as _formatters } from "./_lib/format/formatters.mjs";
import { longFormatters as _longFormatters } from "./_lib/format/longFormatters.mjs";
...
export const formatters = _formatters;
export const longFormatter = _longFormatters;

export {x as y} syntax breaks Parcel prod build

Breaks:
@parcel/core: node_modules/date-fns/index.mjs does not export 'format'
Introduced in date-fns 3.3.0

export { format as formatDate };

Possible workaround:
Similarly to the previous issue, assigning to a new constant that is exported seems to fix the problem.

export const formatDate = format;

Other bundlers don't have these problems AFAIK, so it should be reported to the Parcel team.

As demonstrated here, there appears to be workarounds that could be implemented in date-fns. They would not address the root cause though. @kossnocorp

date-fns v3.1.0 and earlier should work, so you can also try downgrading temporarily until this is resolved.

(@seaders this might be of interest to you)

@shepmaster
Copy link

I had this same problem, but was unable to reproduce it in a clean project. Eventually, I deleted my .parcel-cache in the original project that was experiencing the problem and could no longer reproduce it there either. 🤷

@Ziqi-Yang
Copy link

Another issue there ( it only occurs in production mode, i.e. parcel build):

bun parcel build 'templates/**/*.j2'
🚨 Build failed.

@parcel/core: node_modules/date-fns/index.mjs does not export 'format'

  /home/meowking/proj/class_proj/COMP3030J-SE-P2-G1/proj/static/js/chart/basic.ts:5:10
    4 | 
  > 5 | import { format } from 'date-fns';
  >   |          ^^^^^^
    6 | 
    7 | // temporary workaround for parcel tree-shaking error for date-fns

error: "parcel" exited with code 1
error: Recipe `parcel-build` failed on line 90 with exit code 1

@isakstarlander
Copy link

Hey! I ran into this same issue after upgrading date-fns from version 2.x to the latest version 3.6.0. Did some investigation and could make a reproduction of the issue in a bare parcel + date-fns repo. https://github.com/Milkywire/date-fns-parcel-reproduction

The real interesting part is that plain javascript works perfectly, but the typescript one fails.

Also noticed this PR related to a duplicate export which caused some issues, could be worth keeping an eye out on the effects of that PR getting merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants