Skip to content

Commit

Permalink
feat(es/codegen): Implement proper inline_script support (#9729)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #7602
  • Loading branch information
kdy1 authored Nov 11, 2024
1 parent 9dfa3f5 commit e732a36
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changeset/lemon-zebras-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
swc_core: patch
swc_ecma_codegen: patch
swc: patch
---

feat(es/codegen): Implement proper `inline_script` support
9 changes: 9 additions & 0 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@ impl Options {
}
});

// inline_script defaults to true, but it's case only if minify is enabled.
// This is because minifier API is compatible with Terser, and Terser
// defaults to true, while by default swc itself doesn't enable
// inline_script by default.
let codegen_inline_script = js_minify.as_ref().map_or(false, |v| v.format.inline_script);

let preamble = if !cfg.jsc.output.preamble.is_empty() {
cfg.jsc.output.preamble
} else {
Expand Down Expand Up @@ -800,6 +806,7 @@ impl Options {
emit_assert_for_import_attributes: experimental
.emit_assert_for_import_attributes
.into_bool(),
codegen_inline_script,
emit_isolated_dts: experimental.emit_isolated_dts.into_bool(),
resolver,
})
Expand Down Expand Up @@ -1110,6 +1117,7 @@ pub struct BuiltInput<P: Pass> {

pub output: JscOutputConfig,
pub emit_assert_for_import_attributes: bool,
pub codegen_inline_script: bool,

pub emit_isolated_dts: bool,
pub resolver: Option<(FileName, Arc<dyn ImportResolver>)>,
Expand Down Expand Up @@ -1142,6 +1150,7 @@ where
emit_source_map_columns: self.emit_source_map_columns,
output: self.output,
emit_assert_for_import_attributes: self.emit_assert_for_import_attributes,
codegen_inline_script: self.codegen_inline_script,
emit_isolated_dts: self.emit_isolated_dts,
resolver: self.resolver,
}
Expand Down
6 changes: 4 additions & 2 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,8 @@ impl Compiler {
.with_ascii_only(opts.format.ascii_only)
.with_emit_assert_for_import_attributes(
opts.format.emit_assert_for_import_attributes,
),
)
.with_inline_script(opts.format.inline_script),
output: None,
},
)
Expand Down Expand Up @@ -1047,7 +1048,8 @@ impl Compiler {
)
.with_emit_assert_for_import_attributes(
config.emit_assert_for_import_attributes,
),
)
.with_inline_script(config.codegen_inline_script),
output: if output.is_empty() {
None
} else {
Expand Down
9 changes: 9 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7602/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"jsc": {
"minify": {
"format": {
"inline_script": true
}
}
}
}
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-7xxx/7602/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<script></script>"
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-7xxx/7602/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<script><\/script>";
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
declare function a(a: number): number;
declare function a(a: string): string;
declare function a(a: any): any;
declare function b(a: number): number;
declare function b(a: string): string;
5 changes: 4 additions & 1 deletion crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ where
if &*node.value == "use strict"
&& node.raw.is_some()
&& node.raw.as_ref().unwrap().contains('\\')
&& (!self.cfg.inline_script || !node.raw.as_ref().unwrap().contains("script"))
{
self.wr
.write_str_lit(DUMMY_SP, node.raw.as_ref().unwrap())?;
Expand All @@ -670,7 +671,9 @@ where

if !self.cfg.minify {
if let Some(raw) = &node.raw {
if !self.cfg.ascii_only || raw.is_ascii() {
if (!self.cfg.ascii_only || raw.is_ascii())
&& (!self.cfg.inline_script || !node.raw.as_ref().unwrap().contains("script"))
{
self.wr.write_str_lit(DUMMY_SP, raw)?;
return Ok(());
}
Expand Down
8 changes: 4 additions & 4 deletions packages/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export interface JsFormatOptions {
* Currently noop.
* @alias inline_script
*/
inlineScript?: number;
inlineScript?: boolean;

/**
* Currently noop.
Expand Down Expand Up @@ -349,7 +349,7 @@ export interface TerserMangleOptions {
reserved?: string[];
}

export interface TerserManglePropertiesOptions {}
export interface TerserManglePropertiesOptions { }

/**
* Programmatic options.
Expand Down Expand Up @@ -1175,7 +1175,7 @@ export interface Output {
map?: string;
}

export interface MatchPattern {}
export interface MatchPattern { }

// -------------------------------
// ---------- Ast nodes ----------
Expand Down Expand Up @@ -1407,7 +1407,7 @@ export type Expression =
| OptionalChainingExpression
| Invalid;

interface ExpressionBase extends Node, HasSpan {}
interface ExpressionBase extends Node, HasSpan { }

export interface Identifier extends ExpressionBase {
type: "Identifier";
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@swc/types",
"packageManager": "yarn@4.0.2",
"version": "0.1.14",
"version": "0.1.15",
"description": "Typings for the swc project.",
"sideEffects": false,
"scripts": {
Expand Down

0 comments on commit e732a36

Please sign in to comment.