Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rustflags = ["-C", "target-feature=-crt-static", "-C", "link-arg=-lgcc"]
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"

# We have not adapt this cfg for swc_ecma_minifier, which styled_jsx depends on.
# We have not adapt this cfg for swc_ecma_minifier, which styled_jsx and formatjs depends on.
# So we have to manually enable it for all crates except styled jsx.
# [target.'cfg(target_arch = "wasm32")']
# rustflags = [
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ jobs:
- name: Run cargo check
env:
RUSTFLAGS: "--cfg swc_ast_unknown"
run: |
run: >
cargo check --workspace --exclude swc_plugin_styled_jsx --exclude styled_jsx
--exclude swc_plugin_formatjs --exclude swc_formatjs_transform

cargo-test:
strategy:
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions packages/formatjs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ swc_core = { workspace = true, features = [
"ecma_plugin_transform",
"ecma_ast_serde",
] }
swc_ecma_minifier = { workspace = true }
swc_formatjs_transform = { path = "./transform" }
25 changes: 25 additions & 0 deletions packages/formatjs/__tests__/__snapshots__/wasm.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`formatjs swc plugin > should handle statically evaluate-able variables 1`] = `
"import { defineMessage, formatMessage, FormattedMessage } from 'react-intl';
var part1 = "Hello";
var part2 = "world";
var message = defineMessage({
id: "neWvNv",
defaultMessage: "Helloworld"
});
function Greeting() {
var message2 = formatMessage({
id: "emocAR",
defaultMessage: "Helloworld"
});
var templateMessage = formatMessage({
id: "cxaV5O",
defaultMessage: "~Hello, world!"
});
return /*#__PURE__*/ React.createElement(FormattedMessage, {
id: "mLV+F/",
defaultMessage: "Helloworld"
});
}
"
`;

exports[`formatjs swc plugin > should quote plural keys correctly when ast enabled 1`] = `
"import { formatMessage } from 'react-intl';
formatMessage({
Expand Down
45 changes: 45 additions & 0 deletions packages/formatjs/__tests__/wasm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,51 @@ describe("formatjs swc plugin", () => {
expect(output).not.toMatch(/description/);
});

it("should handle statically evaluate-able variables", async () => {
const input = `
import { defineMessage, formatMessage, FormattedMessage } from 'react-intl';

const part1 = "Hello";
const part2 = "world";

const message = defineMessage({
defaultMessage: part1 + part2,
description: "static vars"
});
function Greeting() {
const message2 = formatMessage({
defaultMessage: part1 + part2,
description: "static vars in function"
});
const templateMessage = formatMessage({
defaultMessage: \`~\${part1}, \${part2}!\`,
description: "static string"
});
return (<FormattedMessage defaultMessage={part1 + part2} />);
}
`;

const output = await transformCode(input);
expect(output).toMatchSnapshot();
});

it("should throw error on non-statically evaluate-able variables", async () => {
const input = `
import { defineMessage, formatMessage, FormattedMessage } from 'react-intl';

const part1 = "Hello, ";

const message = defineMessage({
defaultMessage: part1 + part2,
description: "static vars"
});
`;

expect(transformCode(input)).rejects.toThrow(
"[React Intl] Messages must be statically evaluate-able for extraction.",
);
});

it("should transform to ast when enabled", async () => {
const input = `
import { defineMessage, formatMessage, FormattedMessage } from 'react-intl';
Expand Down
4 changes: 2 additions & 2 deletions packages/formatjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"main": "swc_plugin_formatjs.wasm",
"scripts": {
"prepack": "pnpm run build",
"build": "RUSTFLAGS='--cfg swc_ast_unknown' cargo build --release -p swc_plugin_formatjs --target wasm32-wasip1 && cp ../../target/wasm32-wasip1/release/swc_plugin_formatjs.wasm .",
"build:debug": "RUSTFLAGS='--cfg swc_ast_unknown' cargo build -p swc_plugin_formatjs --target wasm32-wasip1 && cp ../../target/wasm32-wasip1/debug/swc_plugin_formatjs.wasm .",
"build": "cargo build --release -p swc_plugin_formatjs --target wasm32-wasip1 && cp ../../target/wasm32-wasip1/release/swc_plugin_formatjs.wasm .",
"build:debug": "cargo build -p swc_plugin_formatjs --target wasm32-wasip1 && cp ../../target/wasm32-wasip1/debug/swc_plugin_formatjs.wasm .",
"test": "pnpm run build:debug && vitest run --testTimeout=0"
},
"homepage": "https://swc.rs",
Expand Down
4 changes: 4 additions & 0 deletions packages/formatjs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use swc_core::{
proxies::TransformPluginProgramMetadata,
},
};
use swc_ecma_minifier::{eval::Evaluator, marks::Marks};
use swc_formatjs_transform::{create_formatjs_visitor, FormatJSPluginOptions};

#[plugin_transform]
Expand All @@ -23,11 +24,14 @@ pub fn process(mut program: Program, metadata: TransformPluginProgramMetadata) -
Default::default()
};

let module = program.as_module().unwrap();
let evaluator = &mut Evaluator::new(module.clone(), Marks::new());
let mut visitor = create_formatjs_visitor(
std::sync::Arc::new(metadata.source_map),
metadata.comments.as_ref(),
plugin_options,
filename,
evaluator,
);

program.visit_mut_with(&mut visitor);
Expand Down
23 changes: 12 additions & 11 deletions packages/formatjs/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ version = "18.0.0"
custom_transform = []

[dependencies]
base64ct = { workspace = true, features = ["alloc"] }
digest = { workspace = true }
hex = { workspace = true }
md-5 = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sha1 = { workspace = true }
sha2 = { workspace = true }
swc_core = { workspace = true, features = ["common", "ecma_visit", "ecma_ast"] }
base64ct = { workspace = true, features = ["alloc"] }
digest = { workspace = true }
hex = { workspace = true }
md-5 = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sha1 = { workspace = true }
sha2 = { workspace = true }
swc_core = { workspace = true, features = ["common", "ecma_visit", "ecma_ast"] }
swc_ecma_minifier = { workspace = true }

swc_icu_messageformat_parser = { features = [
"utf16",
Expand Down
Loading
Loading