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

refactor(extractor/babel)!: sealed, non-fragile babel extractor options #1367

Merged
merged 5 commits into from
Feb 14, 2023

Conversation

timofei-iatsenko
Copy link
Collaborator

@timofei-iatsenko timofei-iatsenko commented Jan 25, 2023

Fixes:

#1314
#1305
#1206
#952

All current problems with babel extractor due to the fact user may have various babel configuration and transformation which potentially may break extraction process (and they do!)

Briefly:

  1. Extractor should not be affected by any of the babel plugins / preset user may or may not use on the project (hello nextjs with SWC!)
  2. Extractor should successfully parse files as long as it is valid ECMA syntax (up to stage3), with exceptions to typescript and flow
  3. Extractor should be self-contained, user should not install additional packages to parse a valid ECMAScript code
  4. Extractor should not transform code, only analyze. This brings speed for process and resolve issues when AST after transforming become not parsable by extractor

Supported projects:

  • Typescript / TSX -> supported out of the box, by matching a file extension
  • JS / JSX -> supported out of the box
  • Flow -> supported by enabling extractorParserOptions.flow: true
  • Custom projects with unsupported syntax, stage0 / stage1 proposals, vue, etc -> supported via custom extractor

Custom extractor interface was changed to supply code and sourcemaps, so simple custom extractor may look like:

import babel from "@lingui/cli/api/extractors/babel"

const extractor: ExtractorType = {
  match(filename: string) {
      return filename.endsWith(".custom");
  }

  extract(filename: string, code: string, onMessageExtracted) {
     const {code, sourcemaps} = transformMyCustomFileToJs(filename, code);

     // reuse extractor from cli
     return babel(filename, code, onMessageExtracted, {sourcemaps})
  }
}

BREAKING CHANGES:

  • @lingui/cli/api/extractors/typescript extractor was removed and should be deleted from lingui.config
  • extractorBabelOptions - was removed fron lingui config and no longer supported (todo: write a deperecation warning in configuration validation) and should be removed
  • Public interface of ExtractorType is changed

Tasks:

  • Update docs
    • Flow Setup
    • Describe settings
    • How to create a custom extractor
  • Add warning in validators for lingui config when extractorBabelOptions passed

@vercel
Copy link

vercel bot commented Jan 25, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 14, 2023 at 1:58PM (UTC)

@github-actions
Copy link

github-actions bot commented Jan 25, 2023

size-limit report 📦

Path Size
./packages/core/build/esm/index.js 1.76 KB (0%)
./packages/detect-locale/build/esm/index.js 812 B (0%)
./packages/react/build/esm/index.js 1.79 KB (0%)
./packages/remote-loader/build/esm/index.js 7.29 KB (0%)

@andrii-bodnar
Copy link
Contributor

@thekip anyway thanks for your contributions, but could you please do it in smaller reviewable parts? Such PRs are extremely hard to review and there is a big risk to miss some bugs

also, please do not push so hard with these major changes, rewriting the library it's something that we don't want at the moment. Please, let's do all of this work in smaller steps

@timofei-iatsenko
Copy link
Collaborator Author

but could you please do it in smaller reviewable parts?

No, i could not. This already an atomic change to only one part of functionality. Please review only this commit 5c162c2

The major changes concentrated around one file @lingui/cli/api/extractors/babel rest is tests updates or some fixes to make it work.

examples/js/package.json Outdated Show resolved Hide resolved
packages/babel-plugin-extract-messages/test/index.ts Outdated Show resolved Hide resolved
packages/cli/src/api/extractors/babel.ts Outdated Show resolved Hide resolved
packages/cli/src/api/extractors/babel.ts Outdated Show resolved Hide resolved
packages/cli/src/api/extractors/babel.ts Show resolved Hide resolved
packages/cli/src/api/extractors/typescript.ts Outdated Show resolved Hide resolved
packages/cli/src/lingui-extract.ts Show resolved Hide resolved
packages/macro/src/index.ts Show resolved Hide resolved
packages/macro/test/fixtures/js-t-var/js-t-var.expected.js Outdated Show resolved Hide resolved
@andrii-bodnar andrii-bodnar changed the title refactor(extractor/babel): make opinionated settings for babel extractor refactor(extractor/babel)!: make opinionated settings for babel extractor Jan 31, 2023
@timofei-iatsenko timofei-iatsenko force-pushed the refactor/extractor-babel branch from 3dae37b to 64bcc49 Compare January 31, 2023 20:13
@timofei-iatsenko timofei-iatsenko force-pushed the refactor/extractor-babel branch from 64bcc49 to 8a51f63 Compare February 2, 2023 17:40
@timofei-iatsenko timofei-iatsenko mentioned this pull request Feb 2, 2023
7 tasks
@timofei-iatsenko timofei-iatsenko force-pushed the refactor/extractor-babel branch from 8a51f63 to 2d43e87 Compare February 3, 2023 19:23
@timofei-iatsenko timofei-iatsenko changed the title refactor(extractor/babel)!: make opinionated settings for babel extractor refactor(extractor/babel)!: sealed, non-fragile babel extractor options Feb 14, 2023
packages/conf/src/makeConfig.ts Outdated Show resolved Hide resolved
packages/conf/src/__snapshots__/index.test.ts.snap Outdated Show resolved Hide resolved
website/docs/releases/migration-4.md Outdated Show resolved Hide resolved
website/docs/releases/migration-4.md Outdated Show resolved Hide resolved
website/docs/releases/migration-4.md Outdated Show resolved Hide resolved
website/docs/guides/custom-extractor.md Outdated Show resolved Hide resolved
website/docs/guides/custom-extractor.md Outdated Show resolved Hide resolved
website/docs/guides/custom-extractor.md Outdated Show resolved Hide resolved
website/docs/ref/conf.md Outdated Show resolved Hide resolved
website/docs/ref/conf.md Outdated Show resolved Hide resolved
@timofei-iatsenko
Copy link
Collaborator Author

done

@andrii-bodnar andrii-bodnar merged commit 77d1c08 into lingui:next Feb 14, 2023
@timofei-iatsenko timofei-iatsenko deleted the refactor/extractor-babel branch February 14, 2023 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants