user7536780
user7536780

Reputation:

babel-core 'transform' function cannot find plugin

I have a global node module that uses babel-core transform function. I have no .babelrc at the root of my module. It takes a file and basically just use transform to 'compile' it.

const result = transformSync(content, {
    filename: src,
});

There is a .babelrc file along with the said file and I'm indeed able to find it

{
  "presets": ["@babel/preset-env"]
}

but it complains about not finding '@babel/preset-env' which is right, because the module is installed with mine and not the file/.babelrc being transpiled.

I've tried many options in https://babeljs.io/docs/en/options but still can't make it work.

How can I configure transform so it get plugins from my module while loading babel configuration from the folder of the transpiled file ?

Upvotes: 4

Views: 464

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161457

By design, Babel's plugin loader searches for plugins relative to the config file that references them, or uses the cwd for plugins passed directly in the transformSync options. Control of that isn't exposed to utilities calling Babel.

Changing those sematics would mean that a Babel config file would vary in behavior based on the tool that was loading it, which would be very inconsistent for users, especiall considering that one of the main benefits of having the config file format is so that config can easy be shared across multiple tools calling Babel, for instance one for tests and one for bundling.

If you want users to be able to customize your tool, it sounds like what you may actually want is your own entirely separate config file for your tool, so you can define whatever semantics you want for that.

Upvotes: 1

Related Questions