bun
bun copied to clipboard
Can't import package that starts with @
Version
0.1.13
Platform
Darwin 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:46:32 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T6000 arm64
What steps will reproduce the bug?
npm i ts-morph
// test.js
// ts morph requires a package called '@ts-morph/common', and when bun imports it,
// it throws a syntax error that is not present when run with node.
// Sorry to talk about a competing project, I just wanted to share that
// it's a problem with bun, not real syntax issues. THANK YOU!
let morph = require("ts-morph")
bun test.js
How often does it reproduce? Is there a required condition?
every time
What is the expected behavior?
Should import correctly
What do you see instead?
1 | // 'use strict';
2 |
3 | // Object.defineProperty(exports, '__esModule', { value: true });
4 |
5 | var common = require('@ts-morph/common');
^
TypeError: Unexpected string literal ":string;rightContext:string;". Expected '}' to end an object literal. while parsing module "/Users/me/dev/lonestar/node_modules/@ts-morph/common/dist/ts-morph-common.js"
at /Users/me/dev/lonestar/node_modules/ts-morph/dist/ts-morph.js:5:21
at bun:wrap:1:16360
at bun:main:3:0
Additional information
I troubleshooted, and it's because the required library starts with '@'. If I change it to not start with '@', it doesn't give a syntax error anymore. The problem code in the libary is this:
var common = require('@ts-morph/common');
But if I just change it like this, it doesn't say syntax error, it just can't find it.
var common = require('t@s-morph/common');
How interesting
This is probably a printing bug?
Packages with "@" definitely work in other cases
Can you run "bun build node_modules/ts-morph/dist/ts-morph.js" and paste the output
Found this issue as well, its actually a build error with the underlying @ts-morph/common library and a long string literal definition for the es declaration files, the one for es5 specifically.
the type in the literal that seem to cause it to throw is the regexp interface, specifically special characters in the interface
interface RegExpConstructor {
...
"$+": string;
leftContext: string;
"$`": string;
rightContext: string;
"$'": string;
}
"bun build node_modules/@ts-morph/common/dist/ts-morph-common.js" works fine and seems to build, but if i call "bun run index.ts"
import '@ts-morph/common';
console.log("Hello via Bun!");
in a brand new project it will fail with the error:
SyntaxError: Unexpected string literal ":string;rightContext:string;". Expected '}' to end an object literal.
at ... /node_modules/@ts-morph/common/dist/ts-morph-common.js:1034:14
removing this snippet from the literal in the file:
"$+":string;leftContext:string;"$`":string;rightContext:string;"$'":string;
fixes the issue
Short of editing a peer dependency's source code is there a proper path forward? Does ts-morph-common have to fix something?
@ethandaya the issue you described is fixed in https://github.com/oven-sh/bun/pull/3169