Skip to content

Commit 4dae46c

Browse files
committed
feat: supports method shorthand
1 parent 3875176 commit 4dae46c

File tree

368 files changed

+2819
-2751
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

368 files changed

+2819
-2751
lines changed

crates/node_binding/napi-binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,7 @@ export interface RawEntryDynamicResult {
20792079

20802080
export interface RawEnvironment {
20812081
const?: boolean
2082+
methodShorthand?: boolean
20822083
arrowFunction?: boolean
20832084
nodePrefixForCoreModules?: boolean
20842085
asyncFunction?: boolean

crates/rspack/tests/snapshots/defaults__default_options.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ CompilerOptions {
100100
const: Some(
101101
false,
102102
),
103+
method_shorthand: None,
103104
arrow_function: Some(
104105
false,
105106
),

crates/rspack_binding_api/src/raw_options/raw_output.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl From<RawCrossOriginLoading> for CrossOriginLoading {
4444
#[napi(object)]
4545
pub struct RawEnvironment {
4646
pub r#const: Option<bool>,
47+
pub method_shorthand: Option<bool>,
4748
pub arrow_function: Option<bool>,
4849
pub node_prefix_for_core_modules: Option<bool>,
4950
pub async_function: Option<bool>,
@@ -63,6 +64,7 @@ impl From<RawEnvironment> for Environment {
6364
fn from(value: RawEnvironment) -> Self {
6465
Self {
6566
r#const: value.r#const,
67+
method_shorthand: value.method_shorthand,
6668
arrow_function: value.arrow_function,
6769
node_prefix_for_core_modules: value.node_prefix_for_core_modules,
6870
async_function: value.async_function,

crates/rspack_core/src/options/output.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ pub struct LibraryCustomUmdObject {
503503
#[derive(Debug, Default, Copy, Clone)]
504504
pub struct Environment {
505505
pub r#const: Option<bool>,
506+
pub method_shorthand: Option<bool>,
506507
pub arrow_function: Option<bool>,
507508
pub node_prefix_for_core_modules: Option<bool>,
508509
pub async_function: Option<bool>,
@@ -523,6 +524,10 @@ impl Environment {
523524
self.r#const.unwrap_or_default()
524525
}
525526

527+
pub fn supports_method_shorthand(&self) -> bool {
528+
self.method_shorthand.unwrap_or_default()
529+
}
530+
526531
pub fn supports_arrow_function(&self) -> bool {
527532
self.arrow_function.unwrap_or_default()
528533
}

crates/rspack_plugin_javascript/src/runtime.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ pub async fn render_module(
154154
}
155155
};
156156

157+
/*
158+
If supports method shorthand, render function factory as:
159+
"./module.js"(module) { code }
160+
Otherwise render as:
161+
"./module.js": (function(module) { code })
162+
*/
163+
let use_method_shorthand = compilation
164+
.options
165+
.output
166+
.environment
167+
.supports_method_shorthand();
168+
157169
hooks
158170
.render_module_content
159171
.call(
@@ -173,7 +185,6 @@ pub async fn render_module(
173185
sources.add(RawStringSource::from(
174186
serde_json::to_string(&module_id).to_rspack_result()?,
175187
));
176-
sources.add(RawStringSource::from_static(": "));
177188

178189
let mut post_module_container = {
179190
let runtime_requirements = ChunkGraph::get_module_runtime_requirements(
@@ -212,16 +223,24 @@ pub async fn render_module(
212223

213224
let mut container_sources = ConcatSource::default();
214225

215-
container_sources.add(RawStringSource::from(format!(
216-
"(function ({}) {{\n",
217-
args.join(", ")
218-
)));
226+
if use_method_shorthand {
227+
container_sources.add(RawStringSource::from(format!("({}) {{\n", args.join(", "))));
228+
} else {
229+
container_sources.add(RawStringSource::from(format!(
230+
": (function ({}) {{\n",
231+
args.join(", ")
232+
)));
233+
}
219234
if module.build_info().strict && !all_strict {
220235
container_sources.add(RawStringSource::from_static("\"use strict\";\n"));
221236
}
222237
container_sources.add(render_source.source);
223-
container_sources.add(RawStringSource::from_static("\n\n})"));
224-
container_sources.add(RawStringSource::from_static(",\n"));
238+
239+
if use_method_shorthand {
240+
container_sources.add(RawStringSource::from_static("\n\n},\n"));
241+
} else {
242+
container_sources.add(RawStringSource::from_static("\n\n}),\n"));
243+
}
225244

226245
RenderSource {
227246
source: container_sources.boxed(),

packages/rspack/etc/core.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,7 @@ interface EnvConfig {
22112211

22122212
// @public
22132213
export type Environment = {
2214+
methodShorthand?: boolean;
22142215
arrowFunction?: boolean;
22152216
asyncFunction?: boolean;
22162217
bigIntLiteral?: boolean;

packages/rspack/src/config/adapter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ function getRawOutputEnvironment(
123123
): RawEnvironment {
124124
return {
125125
const: Boolean(environment.const),
126+
methodShorthand: Boolean(environment.methodShorthand),
126127
arrowFunction: Boolean(environment.arrowFunction),
127128
nodePrefixForCoreModules: Boolean(environment.nodePrefixForCoreModules),
128129
asyncFunction: Boolean(environment.asyncFunction),

packages/rspack/src/config/browserslistTargetHandler.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ export const resolve = (
9999
kaios: [2, 5],
100100
node: [6, 0]
101101
}),
102+
shorthandMethod: rawChecker({
103+
chrome: 47,
104+
and_chr: 47,
105+
edge: 12,
106+
firefox: 34,
107+
and_ff: 34,
108+
// ie: Not supported,
109+
opera: 34,
110+
op_mob: 34,
111+
safari: 9,
112+
ios_saf: 9,
113+
samsung: 5,
114+
android: 47,
115+
// baidu: Not tracked,
116+
and_qq: [14, 9],
117+
and_uc: [15, 5],
118+
kaios: [2, 5],
119+
node: [4, 9]
120+
}),
102121
arrowFunction: rawChecker({
103122
chrome: 45,
104123
and_chr: 45,

packages/rspack/src/config/defaults.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ const applyOutputDefaults = (
650650
F(environment, "globalThis", () => tp?.globalThis);
651651
F(environment, "bigIntLiteral", () => tp && optimistic(tp.bigIntLiteral));
652652
F(environment, "const", () => tp && optimistic(tp.const));
653+
// IGNORE(output.environment.methodShorthand): will align method shorthand optimization for webpack soon
654+
F(environment, "methodShorthand", () => tp && optimistic(tp.methodShorthand));
653655
F(environment, "arrowFunction", () => tp && optimistic(tp.arrowFunction));
654656
F(environment, "asyncFunction", () => tp && optimistic(tp.asyncFunction));
655657
F(environment, "forOf", () => tp && optimistic(tp.forOf));

packages/rspack/src/config/target.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export type EcmaTargetProperties = {
7676
bigIntLiteral: boolean | null;
7777
/** const and let variable declarations are available */
7878
const: boolean | null;
79+
/** method shorthand in object is available */
80+
shorthandMethod: boolean | null;
7981
/** arrow functions are available */
8082
arrowFunction: boolean | null;
8183
/** for of iteration is available */
@@ -243,6 +245,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
243245
const: v(6),
244246
templateLiteral: v(4),
245247
optionalChaining: v(14),
248+
methodShorthand: v(4),
246249
arrowFunction: v(6),
247250
asyncFunction: v(7, 6),
248251
forOf: v(5),
@@ -289,6 +292,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
289292
const: v(1, 1),
290293
templateLiteral: v(1, 1),
291294
optionalChaining: v(8),
295+
methodShorthand: v(1, 1),
292296
arrowFunction: v(1, 1),
293297
asyncFunction: v(1, 7),
294298
forOf: v(0, 36),
@@ -327,6 +331,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
327331
const: v(0, 15),
328332
templateLiteral: v(0, 13),
329333
optionalChaining: v(0, 44),
334+
methodShorthand: v(0, 15),
330335
arrowFunction: v(0, 15),
331336
asyncFunction: v(0, 21),
332337
forOf: v(0, 13),
@@ -349,6 +354,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
349354
const: v >= 2015,
350355
templateLiteral: v >= 2015,
351356
optionalChaining: v >= 2020,
357+
methodShorthand: v >= 2015,
352358
arrowFunction: v >= 2015,
353359
forOf: v >= 2015,
354360
destructuring: v >= 2015,

0 commit comments

Comments
 (0)