Skip to content

Commit 54719e0

Browse files
authored
feat: support to disable camel2DashComponentName of transformImport (#1773)
* feat: support to disable camel2DashComponentName of transformImport * fix: typings
1 parent 5f2a065 commit 54719e0

File tree

10 files changed

+60
-4
lines changed

10 files changed

+60
-4
lines changed

crates/binding/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub struct BuildParams {
120120
umd?: false | string | { name: string, export?: string[] };
121121
cjs?: boolean;
122122
writeToDisk?: boolean;
123-
transformImport?: { libraryName: string; libraryDirectory?: string; style?: boolean | string }[];
123+
transformImport?: { libraryName: string; libraryDirectory?: string; style?: boolean | string, camel2DashComponentName?: boolean }[];
124124
clean?: boolean;
125125
nodePolyfill?: boolean;
126126
ignores?: string[];

crates/mako/src/config/transform_import.rs

+6
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ pub struct TransformImportConfig {
1313
pub library_name: String,
1414
pub library_directory: Option<String>,
1515
pub style: Option<TransformImportStyle>,
16+
#[serde(default = "default_as_true")]
17+
pub camel2_dash_component_name: bool,
18+
}
19+
20+
fn default_as_true() -> bool {
21+
true
1622
}

crates/mako/src/plugins/import.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ impl<'a> VisitMut for ImportVisitor<'a> {
8484
.to_string()
8585
.chars()
8686
.fold(String::new(), |mut acc, c| {
87-
if c.is_uppercase() {
87+
if c.is_uppercase() && import_config.camel2_dash_component_name
88+
{
8889
if acc.len() > 1 {
8990
acc.push('-');
9091
}
@@ -207,6 +208,7 @@ import { Button, DatePicker } from "antd";
207208
library_name: "antd".to_string(),
208209
library_directory: None,
209210
style: None,
211+
camel2_dash_component_name: true,
210212
}],
211213
);
212214
assert_eq!(
@@ -231,6 +233,7 @@ import { Button, DatePicker } from "antd";
231233
library_name: "antd".to_string(),
232234
library_directory: None,
233235
style: Some(TransformImportStyle::Source(true)),
236+
camel2_dash_component_name: true,
234237
}],
235238
);
236239
assert_eq!(
@@ -257,6 +260,7 @@ import { Button, DatePicker } from "antd";
257260
library_name: "antd".to_string(),
258261
library_directory: None,
259262
style: Some(TransformImportStyle::Built("css".to_string())),
263+
camel2_dash_component_name: true,
260264
}],
261265
);
262266
assert_eq!(
@@ -283,6 +287,7 @@ import { Button, DatePicker } from "antd";
283287
library_name: "antd".to_string(),
284288
library_directory: Some("es".to_string()),
285289
style: None,
290+
camel2_dash_component_name: true,
286291
}],
287292
);
288293
assert_eq!(
@@ -307,6 +312,7 @@ import { Button as MyButton } from "antd";
307312
library_name: "antd".to_string(),
308313
library_directory: None,
309314
style: None,
315+
camel2_dash_component_name: true,
310316
}],
311317
);
312318
assert_eq!(
@@ -333,6 +339,7 @@ import { Button, DatePicker } from "antd";
333339
library_name: "antd".to_string(),
334340
library_directory: None,
335341
style: None,
342+
camel2_dash_component_name: true,
336343
}],
337344
);
338345
assert_eq!(
@@ -359,4 +366,28 @@ import DatePicker from "antd/lib/date-picker";
359366
ast.ast.visit_mut_with(&mut ImportVisitor { config });
360367
ast.generate(context.clone()).unwrap().code
361368
}
369+
370+
#[test]
371+
fn test_disable_camel2_dash_component_name() {
372+
let code = generate(
373+
r#"
374+
import { MyButton } from "antd";
375+
"#,
376+
&vec![TransformImportConfig {
377+
library_name: "antd".to_string(),
378+
library_directory: None,
379+
style: None,
380+
camel2_dash_component_name: false,
381+
}],
382+
);
383+
assert_eq!(
384+
code,
385+
r#"
386+
import MyButton from "antd/lib/MyButton";
387+
388+
//# sourceMappingURL=/test/path.map
389+
"#
390+
.trim(),
391+
);
392+
}
362393
}

e2e/fixtures/config.transformImport/expect.js

+6
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ assert.match(
1111
"js should have foo/es/button"
1212
);
1313
assert.match(cssContent, /\.foo-btn/, "css should have `.foo-btn`");
14+
15+
assert.match(
16+
content,
17+
/"node_modules\/bar\/es\/barBar\/index.js"/,
18+
"js should have bar/es/barBar"
19+
);

e2e/fixtures/config.transformImport/mako.config.json

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"libraryName": "foo",
55
"libraryDirectory": "es",
66
"style": true
7+
},
8+
{
9+
"libraryName": "bar",
10+
"libraryDirectory": "es",
11+
"camel2DashComponentName": false
712
}
813
],
914
"optimization": {

e2e/fixtures/config.transformImport/node_modules/bar/es/barBar/index.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/fixtures/config.transformImport/node_modules/bar/package.json

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/fixtures/config.transformImport/node_modules/foo/es/button/button.js

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
import { Button } from "foo";
2+
import { barBar } from "bar";
23
console.log(Button);
4+
console.log(barBar);

packages/mako/binding.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ export interface BuildParams {
197197
libraryName: string;
198198
libraryDirectory?: string;
199199
style?: boolean | string;
200+
camel2DashComponentName?: boolean;
200201
}[];
201202
clean?: boolean;
202203
nodePolyfill?: boolean;

0 commit comments

Comments
 (0)