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

@babel/standalone transform failed when using preset-typescript #10154

Open
akira-cn opened this issue Jul 2, 2019 · 4 comments

Comments

Projects
None yet
4 participants
@akira-cn
Copy link

commented Jul 2, 2019

Bug Report

Current Behavior
@babel/standalone transform failed when using preset-typescript

Input Code

  • REPL or Repo link if applicable:
const _code = Babel.transform(code, { presets: ['typescript'] }).code;

Environment

  • Babel version(s): @babel/standalone@7.4.5
  • Node/npm version: node v8.9.3 / npm v6.9.0
  • OS: OSX 10.13.2
  • Monorepo: [no]
  • How you are using Babel: standalone

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Jul 2, 2019

Hey @akira-cn! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@JLHwung

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

Pass a filename option and it should work

Babel.transform(code, { filename: 'example.ts', presets: ['typescript'] });

@babel/preset-typescript uses filename to test if it is a TypeScript file and then it will apply plugin-transform-typescript by cases.

{
  test: /\.ts$/,
  plugins: [[transformTypeScript, { jsxPragma }]],
}

Per filename options document, although filename is optional, options like test rely on filename to match test rules. In your case, since the filename is not passed but the config builder has to test /\.ts$/, it bails out because it could not determine if it is a .ts file.

If the filename option seems artificial here as I guess you are transforming a code snippet instead of a file, you can also pass allExtensions: true to the typescript preset options. It will assume your code is .tsx and apply typescript transforms and JSX transforms.

Babel.transform(code, { presets: [['typescript', { allExtensions: true }]] })

@nicolo-ribaudo Although the behavior is expected, I think if we can have better documents here. For those presets, i.e. preset-typescript, using pattern match options, we could post a reminder to declare artificial filename option in transform API.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Jul 2, 2019

We definetly need a better error message. If the test option is used in a preset instead of a user-defined config, it should throw something like The PRESET_NAME preset needs the filename to be passed to Babel, but it was not provided.

@JLHwung

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

The PRESET_NAME preset needs the filename to be passed to Babel, but it was not provided.

Make sense, we could keep the issue open and I would spare some time to improve the error message.

JLHwung added a commit to JLHwung/babel that referenced this issue Jul 9, 2019

@JLHwung JLHwung referenced a pull request that will close this issue Jul 9, 2019

Open

feat(errors): validate preset when filename is absent #10181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.