Skip to content

Commit

Permalink
Merge runtime plugin into babel-plugin-fbtee
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandernanberg committed Dec 21, 2024
1 parent f3fde5b commit ffbfecb
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 180 deletions.
4 changes: 0 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ export default {
],
},
},
{
displayName: '@nkzw/babel-plugin-fbtee-runtime',
roots: ['<rootDir>/packages/babel-plugin-fbtee-runtime'],
},
{
displayName: 'fbtee',
modulePaths: ['<rootDir>/packages/fbtee/src'],
Expand Down
1 change: 0 additions & 1 deletion packages/babel-plugin-fbtee-runtime/.npmignore

This file was deleted.

39 changes: 0 additions & 39 deletions packages/babel-plugin-fbtee-runtime/package.json

This file was deleted.

94 changes: 0 additions & 94 deletions packages/babel-plugin-fbtee-runtime/src/index.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { transformSync } from '@babel/core';
import presetReact from '@babel/preset-react';
import { describe, it } from '@jest/globals';
import fbtee from '@nkzw/babel-plugin-fbtee';
import fbtAutoImport from '@nkzw/babel-plugin-fbtee-auto-import';
import fbtee from '../index.tsx';
import {
assertSourceAstEqual,
withFbtRequireStatement,
} from '@nkzw/babel-plugin-fbtee/src/__tests__/FbtTestUtil.tsx';
import fbteeRuntime from '../index.tsx';
} from './FbtTestUtil.tsx';

const transform = (source: string) =>
transformSync(source, {
ast: false,
plugins: [fbtAutoImport, fbtee, fbteeRuntime],
plugins: [fbtAutoImport, fbtee],
presets: [presetReact],
sourceType: 'module',
})?.code || '';
Expand Down
83 changes: 82 additions & 1 deletion packages/babel-plugin-fbtee/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import type { NodePath } from '@babel/core';
import {
CallExpression,
identifier,
ImportDeclaration,
isCallExpression,
isObjectExpression,
JSXElement,
Node,
nullLiteral,
objectExpression,
objectProperty,
stringLiteral,
StringLiteral,
} from '@babel/types';
import invariant from 'invariant';
import { parse as parseDocblock } from 'jest-docblock';
import FbtCommonFunctionCallProcessor from './babel-processors/FbtCommonFunctionCallProcessor.tsx';
import type { MetaPhrase } from './babel-processors/FbtFunctionCallProcessor.tsx';
Expand All @@ -16,11 +25,14 @@ import { toPlainFbtNodeTree } from './fbt-nodes/FbtNodeUtil.tsx';
import type { FbtCommonMap } from './FbtCommon.tsx';
import { init } from './FbtCommon.tsx';
import type { FbtCallSiteOptions, FbtOptionConfig } from './FbtConstants.tsx';
import { ValidFbtOptions } from './FbtConstants.tsx';
import { SENTINEL, ValidFbtOptions } from './FbtConstants.tsx';
import type { EnumManifest } from './FbtEnumRegistrar.tsx';
import FbtEnumRegistrar from './FbtEnumRegistrar.tsx';
import fbtHashKey from './fbtHashKey.tsx';
import FbtNodeChecker from './FbtNodeChecker.tsx';
import { checkOption, errorAt } from './FbtUtil.tsx';
import { mapLeaves } from './JSFbtUtil.tsx';
import replaceClearTokensWithTokenAliases from './replaceClearTokensWithTokenAliases.tsx';
import { FbtVariationType } from './translate/IntlVariations.tsx';
import type { FbtTableKey, PatternHash, PatternString } from './Types.d.ts';

Expand Down Expand Up @@ -71,6 +83,7 @@ export type PluginOptions = {
filename?: string | null;
// If true, generate the `outerTokenName` property on the JSFbt tree leaves.
generateOuterTokenName?: boolean;
runtime?: boolean;
};
/**
* Token alias (aka mangled token name)
Expand Down Expand Up @@ -316,6 +329,74 @@ export default function transform() {
});
},
},

/**
* Transform the following:
* fbt._(
* SENTINEL +
* JSON.strinfigy({
* type: "text",
* jsfbt: "jsfbt test" | {
* "t": {... jsfbt table}
* ...
* },
* desc: "desc",
* project: "project",
* }) +
* SENTINEL
* );
* to:
* fbt._("jsfbt test") or fbt._({... jsfbt table})
*/
StringLiteral(path: NodePath<StringLiteral>) {
const sentinelLength = SENTINEL.length;
const phrase = path.node.value;
if (
!phrase.startsWith(SENTINEL) ||
!phrase.endsWith(SENTINEL) ||
phrase.length <= sentinelLength * 2
) {
return;
}

const parsedPhrase = JSON.parse(
phrase.slice(sentinelLength, phrase.length - sentinelLength),
) as ObjectWithJSFBT;

const runtimeInput = mapLeaves(parsedPhrase.jsfbt.t, (leaf) =>
replaceClearTokensWithTokenAliases(leaf.text, leaf.tokenAliases),
);
path.replaceWithSourceString(JSON.stringify(runtimeInput));

const parentNode = path.parentPath && path.parentPath.node;
invariant(
isCallExpression(parentNode),
`Expected parent node to be a 'CallExpression'`,
);

// Append runtime options - key for runtime dictionary lookup
if (parentNode.arguments.length === 1) {
// Second param 'args' could be omitted sometimes. Use null here.
parentNode.arguments.push(nullLiteral());
}

// Append hash key to the options argument
const optionsNode = parentNode.arguments[2];
invariant(
optionsNode == null || isObjectExpression(optionsNode),
'Expect options node to be either null or an object expression but got %s (%s)',
optionsNode,
typeof optionsNode,
);

parentNode.arguments[2] = objectExpression([
...(optionsNode == null ? [] : [...optionsNode.properties]),
objectProperty(
identifier('hk'),
stringLiteral(fbtHashKey(parsedPhrase.jsfbt.t)),
),
]);
},
},
};
}
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-preset-fbtee/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
},
"dependencies": {
"@nkzw/babel-plugin-fbtee": "workspace:^",
"@nkzw/babel-plugin-fbtee-auto-import": "workspace:^",
"@nkzw/babel-plugin-fbtee-runtime": "workspace:^"
"@nkzw/babel-plugin-fbtee-auto-import": "workspace:^"
}
}
2 changes: 0 additions & 2 deletions packages/babel-preset-fbtee/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fbt, { PluginOptions } from '@nkzw/babel-plugin-fbtee';
import autoImport from '@nkzw/babel-plugin-fbtee-auto-import';
import fbtRuntime from '@nkzw/babel-plugin-fbtee-runtime';

export default function preset(
_: unknown,
Expand All @@ -10,7 +9,6 @@ export default function preset(
plugins: [
...(options?.disableAutoImport ? [] : [autoImport]),
[fbt, options],
fbtRuntime,
],
};
}
1 change: 0 additions & 1 deletion packages/fbtee/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"devDependencies": {},
"peerDependencies": {
"@nkzw/babel-plugin-fbtee": "workspace:^",
"@nkzw/babel-plugin-fbtee-runtime": "workspace:^",
"react": "^19.0.0"
}
}
31 changes: 0 additions & 31 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
packages:
- 'example'
- 'packages/babel-plugin-fbtee-auto-import'
- 'packages/babel-plugin-fbtee-runtime'
- 'packages/babel-plugin-fbtee'
- 'packages/babel-preset-fbtee'
- 'packages/fbtee'
Expand Down

0 comments on commit ffbfecb

Please sign in to comment.