Gutenberg: eslint extend: '__' is not defined

Created on 30 Mar 2019  路  4Comments  路  Source: WordPress/gutenberg

Describe the bug
'__' is not defined although eslint has been extended with @wordpress/eslint-plugin/[...].
recommended, react and jsx-a11y don't declare __ a global.

To Reproduce
Steps to reproduce the behavior:

  1. Extend eslint with e.g. @wordpress/eslint-plugin/recommended
  2. Add some code that uses __, e.g. adding a new Gutenberg block style.
  3. Lint will complain that '__' is not defined.

Expected behavior
__ and other i18n methods are found by eslint.

Additional context
https://github.com/WordPress/gutenberg/issues/14724

[Package] i18n [Status] Not Applicable [Type] Help Request

Most helpful comment

__ is not a global function exposed by the Gutenberg packages.

It is part of wp.i18n, so you'd need to use wp.i18n.__().

If you have a Babel/webpack setup and use import { __ } from '@wordpress/i18n', then ESLint should not complain either.

All 4 comments

__ is not a global function exposed by the Gutenberg packages.

It is part of wp.i18n, so you'd need to use wp.i18n.__().

If you have a Babel/webpack setup and use import { __ } from '@wordpress/i18n', then ESLint should not complain either.

@swissspidy: Importing __ makes sense (import { __ } from '@wordpress/i18n';).
However, during build webpack tries to use a @wordpress/i18n package:
````
This dependency was not found:

  • @wordpress/i18n in ./resources/assets/scripts/editor.js
    To install it, you can run: npm install --save @wordpress/i18
    ````
    Shouldn't these modules be available in Gutenberg editor page scope?
    How can webpack be configured to use it from the page/global scope?

Shouldn't these modules be available in Gutenberg editor page scope?
How can webpack be configured to use it from the page/global scope?

Check out the default webpack config from the wp-scripts package. Basically, you need to set the externals configuration option in webpack, which tells webpack that @wordpress/i18n actually should point to wp.i18n.

This way, you get all the benefits from import without duplicating any code.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ellatrix picture ellatrix  路  3Comments

davidsword picture davidsword  路  3Comments

youknowriad picture youknowriad  路  3Comments

aduth picture aduth  路  3Comments

jasmussen picture jasmussen  路  3Comments