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

bun build transforms modules.exports.now to exports.now - breaks date-format and log4js packages #11175

Open
GeoffWilliams opened this issue May 19, 2024 · 0 comments
Labels
bug Something isn't working transpiler parser || printer

Comments

@GeoffWilliams
Copy link

What version of Bun is running?

1.1.8+89d25807f

What platform is your computer?

Linux 6.1.0-17-amd64 x86_64 unknown

What steps can reproduce the bug?

When using bun build with date-format, modules.exports is getting transpiled to exports which causes runtime errors.

This testcase demonstrates the error - I was also getting a similar error from log4js

create project

bun init
bun add date-format

index.ts

import dateFormat from "date-format";
console.log(dateFormat.asString(new Date().toISOString()));

build

bun build --outdir build index.ts --target=bun

run testcase

bun ./build/index.js

What is the expected behavior?

Should print the current date to console, no errors. Eg:

2024-05-19T03:47:19.008Z

What do you see instead?

Bun reports error because exports.now is undefined:

40 |     if (typeof format !== "string") {
41 |       date = format;
42 |       format = exports.ISO8601_FORMAT;
43 |     }
44 |     if (!date) {
45 |       date = exports.now();
                  ^
TypeError: exports.now is not a function. (In 'exports.now()', 'exports.now' is undefined)
      at asString (/home/geoff/tmp/expr/testcase/build/index.js:45:14)
      at /home/geoff/tmp/expr/testcase/build/index.js:184:13

Bun v1.1.8 (Linux x64)

If we look in the source codes of date-format package, we see module.exports.now() - bun removed the module. qualification which breaks the library:

// from node_modules/date-format/lib/index.js
function asString(format, date) {
  if (typeof format !== "string") {
    date = format;
    format = module.exports.ISO8601_FORMAT;
  }
  if (!date) {
    date = module.exports.now();
  }

If I edit build/index.js and change back to module.exports.now the codes works as expected.

Additional information

I originally found this because log4js was erroring when using bun build and bun build --compile:

914 |     }
915 |     var vDay = addZero(date.getDate());
916 |     var vMonth = addZero(date.getMonth() + 1);
917 |     var vYearLong = addZero(date.getFullYear());
918 |     var vYearShort = addZero(vYearLong.substring(2, 4));
919 |     var vYear = format.indexOf("yyyy") > -1 ? vYearLong : vYearShort;
                      ^
TypeError: undefined is not an object (evaluating 'format.indexOf')
      at asString (/home/geoff/tmp/expr/testcase/build/index.js:919:17)
      at timestampLevelAndCategory (/home/geoff/tmp/expr/testcase/build/index.js:1062:52)
      at colouredLayout (/home/geoff/tmp/expr/testcase/build/index.js:1068:12)
      at /home/geoff/tmp/expr/testcase/build/index.js:1813:31
      at /home/geoff/tmp/expr/testcase/build/index.js:6044:7
      at forEach (:1:21)
      at sendLogEventToAppender (/home/geoff/tmp/expr/testcase/build/index.js:6043:5)
      at forEach (:1:21)
      at sendToListeners (/home/geoff/tmp/expr/testcase/build/index.js:1680:5)
      at send (/home/geoff/tmp/expr/testcase/build/index.js:1731:9)

Bun v1.1.8 (Linux x64)

Workaround

  1. Build with --external=date-format
  2. node_modules/date-format resolvable

Found via: #7024

@GeoffWilliams GeoffWilliams added the bug Something isn't working label May 19, 2024
@Electroid Electroid added the transpiler parser || printer label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working transpiler parser || printer
Projects
None yet
Development

No branches or pull requests

2 participants