i18n-extract icon indicating copy to clipboard operation
i18n-extract copied to clipboard

[BABEL] unknown: Preset /* your preset */ requires a filename to be set when babel is called directly,

Open sbycrosz opened this issue 5 years ago • 2 comments

Hello,

I've been getting this error in my React Native project

Code:

    usedKeys = extractor.extractFromFiles(['./src/**/*.js'], {
      marker: 't',
    });

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 (node_modules/i18n-extract/node_modules/@babel/core/lib/config/full.js:274:11)
      at node_modules/i18n-extract/node_modules/@babel/core/lib/config/full.js:286:52
          at Array.forEach (<anonymous>)
      at validatePreset (node_modules/i18n-extract/node_modules/@babel/core/lib/config/full.js:286:25)
      at loadPresetDescriptor (node_modules/i18n-extract/node_modules/@babel/core/lib/config/full.js:293:3)
          at loadPresetDescriptor.next (<anonymous>)
      at recurseDescriptors (node_modules/i18n-extract/node_modules/@babel/core/lib/config/full.js:107:30)
          at recurseDescriptors.next (<anonymous>)
      at loadFullConfig (node_modules/i18n-extract/node_modules/@babel/core/lib/config/full.js:142:6)
          at loadFullConfig.next (<anonymous>)

babel.config.js:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
};

Downgrading i18n-extract to 0.6.3 fixes it so this isn't really urgent, but it would be extremely nice to get this fixed :)

Thanks again for the great library!

sbycrosz avatar Feb 03 '20 07:02 sbycrosz

The above problem we faced was caused by module:metro-react-native-babel-preset, which expects filename to be provided to transformSync().

We prevented loading module:metro-react-native-babel-preset by disabling reading from the project root's Babel config file using the configFile: false option.

// Taken from i18n-extract's TYPESCRIPT_PARSER_OPTIONS
const BABEL_OPTIONS = {
  configFile: false,  // disable reading from project root's Babel config
  ast: true,
  parserOpts: {
    sourceType: 'module',
    plugins: [
      'jsx',
      'asyncFunctions',
      'classConstructorCall',
      'doExpressions',
      'trailingFunctionCommas',
      'objectRestSpread',
      'decoratorsLegacy',
      'classProperties',
      'exportExtensions',
      'exponentiationOperator',
      'asyncGenerators',
      'functionBind',
      'functionSent',
      'dynamicImport',
      'optionalChaining',
      'typescript',
    ],
  },
};

const ... = extractor.extractFromFiles(['./src/**/*.ts', './src/**/*.tsx'], {
    marker: 't',
    babelOptions: BABEL_OPTIONS,
  },
);

joshchoo avatar Jun 14 '21 07:06 joshchoo

Adding configFile: false to https://github.com/oliviertassinari/i18n-extract/blob/9110ba51362b8739a9590d85801cde4b9ce3347c/src/extractFromCode.js#L98 should fix this issue that was introduced by release 0.6.4 without users having to copy over babel config like in the above solution.

As the moment the library is assuming you want to used your root directory babel config file even if you are not passing in anything for babelOptions e.g. using a value for parser instead.

I would think that the default when using a parser should be to not read the root directory babel config unless specified by using the babelOptions configuration option?

Bronsequences avatar Nov 15 '21 00:11 Bronsequences