Skip to content

fix(babel): Pass chunk filename to output plugin transform - #515

Closed
evocateur wants to merge 1 commit into
rollup:masterfrom
evocateur:fix-babel-output-plugin-filename
Closed

fix(babel): Pass chunk filename to output plugin transform#515
evocateur wants to merge 1 commit into
rollup:masterfrom
evocateur:fix-babel-output-plugin-filename

Conversation

@evocateur

Copy link
Copy Markdown

Rollup Plugin Name: @rollup/plugin-babel

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers: N/A

Description

This appears to be an oversight, as the filename is passed at the same place in the input plugin transform Babel options. Without this field, a Babel error including text "Preset /* your preset */ requires a filename to be set when babel is called directly" is thrown when using a custom preset.

[!] (plugin babel) Error: [BABEL] unknown: Preset /* your preset */ requires a filename to be set when babel is called directly,
```
babel.transform(code, { filename: 'file.ts', presets: [/* your preset */] });
```
See https://babeljs.io/docs/en/options#filename for more information.
Error: [BABEL] unknown: Preset /* your preset */ requires a filename to be set when babel is called directly,
```
babel.transform(code, { filename: 'file.ts', presets: [/* your preset */] });
```
See https://babeljs.io/docs/en/options#filename for more information.
    at validateIfOptionNeedsFilename ($PWD/node_modules/@babel/core/lib/config/full.js:274:11)
    at options.overrides.forEach.overrideOptions ($PWD/node_modules/@babel/core/lib/config/full.js:286:52)
    at Array.forEach (<anonymous>)
    at validatePreset ($PWD/node_modules/@babel/core/lib/config/full.js:286:25)
    at loadPresetDescriptor ($PWD/node_modules/@babel/core/lib/config/full.js:293:3)
    at loadPresetDescriptor.next (<anonymous>)
    at recurseDescriptors ($PWD/node_modules/@babel/core/lib/config/full.js:107:30)
    at recurseDescriptors.next (<anonymous>)
    at recurseDescriptors ($PWD/node_modules/@babel/core/lib/config/full.js:128:34)
    at recurseDescriptors.next (<anonymous>)

(stack trace is using @babel/core@7.10.5)

The test is not ideal, but without replicating a huge stack of other Rollup plugins and complicated Babel config, it's unclear how else to regress this in a focused, minimal way. A local edit of the Rollup Babel plugin in node_modules of the offending build does indicate the error is mitigated, however.

This appears to be an oversight, as the filename is passed at the same
place in the input plugin. Without this, a Babel error including text
"Preset /* your preset */ requires a filename to be set when babel is
called directly" is thrown when using a custom preset.

The test is not ideal, but without replicating a huge stack of other
Rollup plugins and complicated Babel config, it's unclear how else to
regress this. A local edit of the Rollup Babel plugin in node_modules
of the offending build does indicate the error is mitigated, however.
@evocateur

Copy link
Copy Markdown
Author

Ooh, I think I figured out the trigger, and it's much simpler than I thought: @babel/preset-typescript is configuring the test field, which is what triggers validateIfOptionNeedsFilename to explode. The workaround probably involves setting a preset option like allExtensions, but at least it promises to make the test here a bit more specific and less brittle.

@evocateur

Copy link
Copy Markdown
Author

Additionally, in a repo where @babel/preset-typescript is not enabled, it looks like a Babel config file with overrides will trip up getBabelOutputPlugin() too:

[!] (plugin babel) Error: Configuration contains string/RegExp pattern, but no filename was passed to Babel
Error: Configuration contains string/RegExp pattern, but no filename was passed to Babel
    at matchPattern ($PWD/node_modules/@babel/core/lib/config/config-chain.js:430:11)
    at patterns.some.pattern ($PWD/node_modules/@babel/core/lib/config/config-chain.js:417:35)
    at Array.some (<anonymous>)
    at matchesPatterns ($PWD/node_modules/@babel/core/lib/config/config-chain.js:417:19)
    at configFieldIsApplicable ($PWD/node_modules/@babel/core/lib/config/config-chain.js:399:10)
    at configIsApplicable ($PWD/node_modules/@babel/core/lib/config/config-chain.js:394:41)
    at forEach ($PWD/node_modules/@babel/core/lib/config/config-chain.js:255:13)
    at Array.forEach (<anonymous>)
    at $PWD/node_modules/@babel/core/lib/config/config-chain.js:252:42
    at Generator.next (<anonymous>)

The .babelrc.js in question:

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    ie: 11,
                },
            },
        ],
    ],
    overrides: [
        {
            test: [
                './src/stuff.js',
                './src/things/*.js',
            ],
            presets: [
                [
                    '@babel/preset-env',
                    {
                        exclude: ['@babel/plugin-transform-classes'],
                        targets: {
                            node: 'current',
                        },
                    },
                ],
            ],
        },
    ],
};

@Andarist

Copy link
Copy Markdown
Member

This was not implemented on purpose - see rollup/rollup-plugin-babel#333 (comment) . I'm too tired today to analyze this issue as a whole but maybe you could look at the linked comments to see how this applies to your case (or how it doesn't).

@evocateur

Copy link
Copy Markdown
Author

@Andarist Will do, thanks for the pointer!

@evocateur
evocateur marked this pull request as draft July 29, 2020 19:53
@evocateur

Copy link
Copy Markdown
Author

Well, turns out I was barking up the wrong tree entirely. Output plugins were not the solution, I needed to pass an array of configs from my rollup.config.js instead so the per-config input plugin could do the right thing w/r/t the supportsStaticESM caller property.

Thanks for your responsiveness!

@evocateur evocateur closed this Jul 30, 2020
@evocateur
evocateur deleted the fix-babel-output-plugin-filename branch October 14, 2020 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants