Skip to content

Commit

Permalink
ref(macro): split js (core) and jsx (react) macro
Browse files Browse the repository at this point in the history
  • Loading branch information
thekip committed Feb 23, 2023
1 parent 3107902 commit 931a531
Show file tree
Hide file tree
Showing 29 changed files with 1,078 additions and 1,013 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/api/extractors/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { ExtractPluginOpts } from "@lingui/babel-plugin-extract-messages"
import linguiExtractMessages from "@lingui/babel-plugin-extract-messages"

import type { ExtractorType } from "@lingui/conf"
import { ParserPlugin } from "@babel/parser"
import { LinguiMacroOpts } from "@lingui/macro/src"
import type { ParserPlugin } from "@babel/parser"
import type { LinguiMacroOpts } from "@lingui/macro-lib"

const babelRe = new RegExp(
"\\.(" +
Expand Down
19 changes: 18 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@
"dependencies": {
"@babel/runtime": "^7.20.13",
"@messageformat/parser": "^5.0.0",
"make-plural": "^6.2.2"
"make-plural": "^6.2.2",
"@lingui/conf": "3.17.1",
"@lingui/macro-lib": "3.17.1",
"@lingui/cli": "3.17.1"
},
"peerDependencies": {
"babel-plugin-macros": "2 || 3"
},
"peerDependenciesMeta": {
"babel-plugin-macros": {
"optional": true
}
},
"devDependencies": {
"@types/babel-plugin-macros": "^2.8.5",
"@babel/traverse":"^7.20.13",
"@babel/types": "^7.20.7",
"babel-plugin-macros": "^3.1.0"
}
}
94 changes: 94 additions & 0 deletions packages/core/src/macro/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { createMacro, MacroParams } from "babel-plugin-macros"
import { getConfig as loadConfig, LinguiConfigNormalized } from "@lingui/conf"

import { MacroJs } from "./macroJs"
import type { NodePath } from "@babel/traverse"
import {
addImport,
isRootPath,
throwNonMacroContextError,
} from "@lingui/macro-lib"

export type LinguiMacroOpts = {
// explicitly set by CLI when running extraction process
extract?: boolean
linguiConfig?: LinguiConfigNormalized
}

export const jsMacroTags = new Set([
"defineMessage",
"arg",
"t",
"plural",
"select",
"selectOrdinal",
])

let config: LinguiConfigNormalized

function getConfig(_config?: LinguiConfigNormalized) {
if (_config) {
config = _config
}
if (!config) {
config = loadConfig()
}
return config
}

export function macro({ references, state, babel, config }: MacroParams) {
const opts: LinguiMacroOpts = config as LinguiMacroOpts

const { i18nImportModule, i18nImportName } = getConfig(
opts.linguiConfig
).runtimeConfigModule

const jsNodes = new Set<NodePath>()
let needsI18nImport = false

Object.keys(references).forEach((tagName) => {
const nodes = references[tagName]

if (jsMacroTags.has(tagName)) {
nodes.forEach((node) => {
jsNodes.add(node.parentPath)
})
}
// else {
// throw nodes[0].buildCodeFrameError(`Unknown macro ${tagName}`)
// }
})

const stripNonEssentialProps =
process.env.NODE_ENV == "production" && !opts.extract

const jsNodesArray = Array.from(jsNodes)

jsNodesArray.filter(isRootPath(jsNodesArray)).forEach((path) => {
const macro = new MacroJs(babel, { i18nImportName, stripNonEssentialProps })
if (macro.replacePath(path)) needsI18nImport = true
})

if (needsI18nImport) {
addImport(
babel.types,
state.file.path.node,
i18nImportModule,
i18nImportName
)
}
}

;[...jsMacroTags].forEach((name) => {
Object.defineProperty(module.exports, name, {
get() {
throwNonMacroContextError("@lingui/core/macro")
},
})
})

export default createMacro(macro, {
configName: "lingui",
})

export * from "./types"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parseExpression } from "@babel/parser"
import * as types from "@babel/types"
import MacroJs from "./macroJs"
import { MacroJs } from "./macroJs"
import { CallExpression } from "@babel/types"

function createMacro() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import * as babelTypes from "@babel/types"
import {
import type * as babelTypes from "@babel/types"
import type {
CallExpression,
Expression,
Identifier,
isObjectProperty,
Node,
ObjectExpression,
ObjectProperty,
SourceLocation,
StringLiteral,
TemplateLiteral,
} from "@babel/types"
import { NodePath } from "@babel/traverse"
import type { NodePath } from "@babel/traverse"

import ICUMessageFormat, {
import {
COMMENT,
CONTEXT,
EXTRACT_MARK,
ID,
MESSAGE,
ICUMessageFormat,
ArgToken,
ParsedResult,
TextToken,
Token,
} from "./icu"
import { makeCounter } from "./utils"
import { COMMENT, CONTEXT, EXTRACT_MARK, ID, MESSAGE } from "./constants"
makeCounter,
} from "@lingui/macro-lib"

import { generateMessageId } from "@lingui/cli/api"

const keepSpaceRe = /(?:\\(?:\r\n|\r|\n))+\s+/g
Expand All @@ -35,7 +40,7 @@ export type MacroJsOpts = {
stripNonEssentialProps: boolean
}

export default class MacroJs {
export class MacroJs {
// Babel Types
types: typeof babelTypes

Expand Down Expand Up @@ -99,7 +104,7 @@ export default class MacroJs {
const i18nInstance = path.node.arguments[0]
const tokens = this.tokenizeNode(path.parentPath.node)

const messageFormat = new ICUMessageFormat()
const messageFormat = new ICUMessageFormat(this.types)
const { message: messageRaw, values } = messageFormat.fromTokens(tokens)
const message = normalizeWhitespace(messageRaw)

Expand Down Expand Up @@ -136,7 +141,7 @@ export default class MacroJs {

const tokens = this.tokenizeNode(path.node)

const messageFormat = new ICUMessageFormat()
const messageFormat = new ICUMessageFormat(this.types)
const { message: messageRaw, values } = messageFormat.fromTokens(tokens)
const message = normalizeWhitespace(messageRaw)

Expand Down Expand Up @@ -228,7 +233,7 @@ export default class MacroJs {
let messageNode = messageProperty.value as StringLiteral

if (tokens) {
const messageFormat = new ICUMessageFormat()
const messageFormat = new ICUMessageFormat(this.types)
const { message: messageRaw, values } = messageFormat.fromTokens(tokens)
const message = normalizeWhitespace(messageRaw)
messageNode = this.types.stringLiteral(message)
Expand Down Expand Up @@ -450,7 +455,8 @@ export default class MacroJs {
): ObjectProperty {
return objectExp.properties.find(
(property) =>
isObjectProperty(property) && this.isIdentifier(property.key, key)
this.types.isObjectProperty(property) &&
this.isIdentifier(property.key, key)
) as ObjectProperty
}

Expand Down
Loading

0 comments on commit 931a531

Please sign in to comment.