@@ -4,12 +4,7 @@ import { Program, Identifier } from "@babel/types"
44import { MacroJSX } from "./macroJsx"
55import type { NodePath } from "@babel/traverse"
66import { MacroJs } from "./macroJs"
7- import {
8- MACRO_CORE_PACKAGE ,
9- MACRO_REACT_PACKAGE ,
10- MACRO_LEGACY_PACKAGE ,
11- JsMacroName ,
12- } from "./constants"
7+ import { JsMacroName } from "./constants"
138import {
149 type LinguiConfigNormalized ,
1510 getConfig as loadConfig ,
@@ -18,9 +13,24 @@ import {
1813let config : LinguiConfigNormalized
1914
2015export type LinguiPluginOpts = {
21- // explicitly set by CLI when running extraction process
16+ /*
17+ * When set `true` all auxiliary data such as `comment`, `context`,
18+ * and default message would be kept regardless of the current environment
19+ *
20+ * This flag explicitly set by Lingui CLI when running extraction process
21+ */
2222 extract ?: boolean
23+ /**
24+ * Setting `stripMessageField` to `true` will strip messages and comments from both development and production bundles.
25+ * Alternatively, set it to `false` to keep the original messages in both environments.
26+ *
27+ * If not set value would be determined based on `process.env.NODE_ENV === "production"`
28+ */
2329 stripMessageField ?: boolean
30+
31+ /**
32+ * Resolved and normalized Lingui Configuration
33+ */
2434 linguiConfig ?: LinguiConfigNormalized
2535}
2636
@@ -61,14 +71,17 @@ type LinguiSymbol = "Trans" | "useLingui" | "i18n"
6171const getIdentifierPath = ( ( path : NodePath , node : Identifier ) => {
6272 let foundPath : NodePath
6373
64- path . traverse ( {
65- Identifier : ( path ) => {
66- if ( path . node === node ) {
67- foundPath = path
68- path . stop ( )
69- }
74+ path . traverse (
75+ {
76+ Identifier : ( path ) => {
77+ if ( path . node === node ) {
78+ foundPath = path
79+ path . stop ( )
80+ }
81+ } ,
7082 } ,
71- } )
83+ path . state
84+ )
7285
7386 return foundPath
7487} ) as any
@@ -110,14 +123,15 @@ export default function ({
110123 }
111124
112125 function getMacroImports ( path : NodePath < Program > ) {
126+ const linguiPackages = new Set ( [
127+ ...config . macro . corePackage ,
128+ ...config . macro . jsxPackage ,
129+ ] )
130+
113131 return path . get ( "body" ) . filter ( ( statement ) => {
114132 return (
115133 statement . isImportDeclaration ( ) &&
116- [
117- MACRO_CORE_PACKAGE ,
118- MACRO_REACT_PACKAGE ,
119- MACRO_LEGACY_PACKAGE ,
120- ] . includes ( statement . get ( "source" ) . node . value )
134+ linguiPackages . has ( statement . get ( "source" ) . node . value )
121135 )
122136 } )
123137 }
@@ -138,11 +152,9 @@ export default function ({
138152
139153 if ( macro === JsMacroName . useLingui ) {
140154 if (
141- identPath . referencesImport (
142- MACRO_REACT_PACKAGE ,
143- JsMacroName . useLingui
144- ) ||
145- identPath . referencesImport ( MACRO_LEGACY_PACKAGE , JsMacroName . useLingui )
155+ config . macro . jsxPackage . some ( ( moduleSource ) =>
156+ identPath . referencesImport ( moduleSource , JsMacroName . useLingui )
157+ )
146158 ) {
147159 return true
148160 }
@@ -151,8 +163,9 @@ export default function ({
151163 identPath = identPath || getIdentifierPath ( path . getFunctionParent ( ) , node )
152164
153165 if (
154- identPath . referencesImport ( MACRO_CORE_PACKAGE , macro ) ||
155- identPath . referencesImport ( MACRO_LEGACY_PACKAGE , macro )
166+ config . macro . corePackage . some ( ( moduleSource ) =>
167+ identPath . referencesImport ( moduleSource , macro )
168+ )
156169 ) {
157170 return true
158171 }
@@ -164,6 +177,11 @@ export default function ({
164177 visitor : {
165178 Program : {
166179 enter ( path , state ) {
180+ state . set (
181+ "linguiConfig" ,
182+ getConfig ( ( state . opts as LinguiPluginOpts ) . linguiConfig )
183+ )
184+
167185 const macroImports = getMacroImports ( path )
168186
169187 if ( ! macroImports . length ) {
@@ -172,11 +190,6 @@ export default function ({
172190
173191 state . set ( "macroImport" , macroImports [ 0 ] )
174192
175- state . set (
176- "linguiConfig" ,
177- getConfig ( ( state . opts as LinguiPluginOpts ) . linguiConfig )
178- )
179-
180193 state . set ( "linguiIdentifiers" , {
181194 i18n : path . scope . generateUidIdentifier ( "i18n" ) ,
182195 Trans : path . scope . generateUidIdentifier ( "Trans" ) ,
0 commit comments