Skip to content

Commit e732a36

Browse files
authored
feat(es/codegen): Implement proper inline_script support (#9729)
**Related issue:** - Closes #7602
1 parent 9dfa3f5 commit e732a36

File tree

10 files changed

+40
-9
lines changed

10 files changed

+40
-9
lines changed

Diff for: .changeset/lemon-zebras-sort.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_codegen: patch
4+
swc: patch
5+
---
6+
7+
feat(es/codegen): Implement proper `inline_script` support

Diff for: crates/swc/src/config/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,12 @@ impl Options {
532532
}
533533
});
534534

535+
// inline_script defaults to true, but it's case only if minify is enabled.
536+
// This is because minifier API is compatible with Terser, and Terser
537+
// defaults to true, while by default swc itself doesn't enable
538+
// inline_script by default.
539+
let codegen_inline_script = js_minify.as_ref().map_or(false, |v| v.format.inline_script);
540+
535541
let preamble = if !cfg.jsc.output.preamble.is_empty() {
536542
cfg.jsc.output.preamble
537543
} else {
@@ -800,6 +806,7 @@ impl Options {
800806
emit_assert_for_import_attributes: experimental
801807
.emit_assert_for_import_attributes
802808
.into_bool(),
809+
codegen_inline_script,
803810
emit_isolated_dts: experimental.emit_isolated_dts.into_bool(),
804811
resolver,
805812
})
@@ -1110,6 +1117,7 @@ pub struct BuiltInput<P: Pass> {
11101117

11111118
pub output: JscOutputConfig,
11121119
pub emit_assert_for_import_attributes: bool,
1120+
pub codegen_inline_script: bool,
11131121

11141122
pub emit_isolated_dts: bool,
11151123
pub resolver: Option<(FileName, Arc<dyn ImportResolver>)>,
@@ -1142,6 +1150,7 @@ where
11421150
emit_source_map_columns: self.emit_source_map_columns,
11431151
output: self.output,
11441152
emit_assert_for_import_attributes: self.emit_assert_for_import_attributes,
1153+
codegen_inline_script: self.codegen_inline_script,
11451154
emit_isolated_dts: self.emit_isolated_dts,
11461155
resolver: self.resolver,
11471156
}

Diff for: crates/swc/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,8 @@ impl Compiler {
909909
.with_ascii_only(opts.format.ascii_only)
910910
.with_emit_assert_for_import_attributes(
911911
opts.format.emit_assert_for_import_attributes,
912-
),
912+
)
913+
.with_inline_script(opts.format.inline_script),
913914
output: None,
914915
},
915916
)
@@ -1047,7 +1048,8 @@ impl Compiler {
10471048
)
10481049
.with_emit_assert_for_import_attributes(
10491050
config.emit_assert_for_import_attributes,
1050-
),
1051+
)
1052+
.with_inline_script(config.codegen_inline_script),
10511053
output: if output.is_empty() {
10521054
None
10531055
} else {
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"jsc": {
3+
"minify": {
4+
"format": {
5+
"inline_script": true
6+
}
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"<script></script>"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"<script><\/script>";
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
declare function a(a: number): number;
22
declare function a(a: string): string;
3-
declare function a(a: any): any;
43
declare function b(a: number): number;
54
declare function b(a: string): string;

Diff for: crates/swc_ecma_codegen/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ where
657657
if &*node.value == "use strict"
658658
&& node.raw.is_some()
659659
&& node.raw.as_ref().unwrap().contains('\\')
660+
&& (!self.cfg.inline_script || !node.raw.as_ref().unwrap().contains("script"))
660661
{
661662
self.wr
662663
.write_str_lit(DUMMY_SP, node.raw.as_ref().unwrap())?;
@@ -670,7 +671,9 @@ where
670671

671672
if !self.cfg.minify {
672673
if let Some(raw) = &node.raw {
673-
if !self.cfg.ascii_only || raw.is_ascii() {
674+
if (!self.cfg.ascii_only || raw.is_ascii())
675+
&& (!self.cfg.inline_script || !node.raw.as_ref().unwrap().contains("script"))
676+
{
674677
self.wr.write_str_lit(DUMMY_SP, raw)?;
675678
return Ok(());
676679
}

Diff for: packages/types/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export interface JsFormatOptions {
109109
* Currently noop.
110110
* @alias inline_script
111111
*/
112-
inlineScript?: number;
112+
inlineScript?: boolean;
113113

114114
/**
115115
* Currently noop.
@@ -349,7 +349,7 @@ export interface TerserMangleOptions {
349349
reserved?: string[];
350350
}
351351

352-
export interface TerserManglePropertiesOptions {}
352+
export interface TerserManglePropertiesOptions { }
353353

354354
/**
355355
* Programmatic options.
@@ -1175,7 +1175,7 @@ export interface Output {
11751175
map?: string;
11761176
}
11771177

1178-
export interface MatchPattern {}
1178+
export interface MatchPattern { }
11791179

11801180
// -------------------------------
11811181
// ---------- Ast nodes ----------
@@ -1407,7 +1407,7 @@ export type Expression =
14071407
| OptionalChainingExpression
14081408
| Invalid;
14091409

1410-
interface ExpressionBase extends Node, HasSpan {}
1410+
interface ExpressionBase extends Node, HasSpan { }
14111411

14121412
export interface Identifier extends ExpressionBase {
14131413
type: "Identifier";

Diff for: packages/types/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@swc/types",
33
"packageManager": "yarn@4.0.2",
4-
"version": "0.1.14",
4+
"version": "0.1.15",
55
"description": "Typings for the swc project.",
66
"sideEffects": false,
77
"scripts": {

0 commit comments

Comments
 (0)