Skip to content

Commit 053f7d6

Browse files
authored
fix: respect to jsc.output.charset in swc loader (#11568)
* Add test * Fix * Update test * Fix clippy
1 parent 0fe3eea commit 053f7d6

File tree

8 files changed

+104
-31
lines changed

8 files changed

+104
-31
lines changed

crates/rspack_javascript_compiler/src/compiler/minify.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl JavaScriptCompiler {
170170

171171
minify_file_comments(
172172
&comments,
173-
opts
173+
&opts
174174
.format
175175
.comments
176176
.clone()
@@ -192,7 +192,9 @@ impl JavaScriptCompiler {
192192
input_source_map: None,
193193
minify: opts.minify,
194194
comments: Some(&comments),
195-
format: &opts.format,
195+
preamble: &opts.format.preamble,
196+
ascii_only: opts.format.ascii_only,
197+
inline_script: opts.format.inline_script,
196198
};
197199

198200
self.print(&program, print_options).map_err(|e| e.into())

crates/rspack_javascript_compiler/src/compiler/stringify.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ pub struct PrintOptions<'a> {
7474
pub input_source_map: Option<&'a sourcemap::SourceMap>,
7575
pub minify: bool,
7676
pub comments: Option<&'a dyn Comments>,
77-
pub format: &'a JsMinifyFormatOptions,
77+
pub preamble: &'a str,
78+
pub ascii_only: bool,
79+
pub inline_script: bool,
7880
}
7981

8082
impl JavaScriptCompiler {
@@ -105,7 +107,9 @@ impl JavaScriptCompiler {
105107
.unwrap_or_default()
106108
.then(|| program.comments.as_ref().map(|c| c as &dyn Comments))
107109
.flatten(),
108-
format: &format_opt,
110+
preamble: &format_opt.preamble,
111+
ascii_only: format_opt.ascii_only,
112+
inline_script: format_opt.inline_script,
109113
};
110114
self.print(program.get_inner_program(), print_options)
111115
})
@@ -124,7 +128,9 @@ impl JavaScriptCompiler {
124128
input_source_map,
125129
minify,
126130
comments,
127-
format,
131+
preamble,
132+
ascii_only,
133+
inline_script,
128134
} = options;
129135
let mut src_map_buf = vec![];
130136

@@ -148,7 +154,7 @@ impl JavaScriptCompiler {
148154
source_map_config.enable.then_some(&mut src_map_buf),
149155
);
150156

151-
w.preamble(&format.preamble).into_diagnostic()?;
157+
w.preamble(preamble).into_diagnostic()?;
152158
let mut wr = Box::new(w) as Box<dyn WriteJs>;
153159

154160
if minify {
@@ -159,8 +165,8 @@ impl JavaScriptCompiler {
159165
cfg: codegen::Config::default()
160166
.with_minify(minify)
161167
.with_target(target)
162-
.with_ascii_only(format.ascii_only)
163-
.with_inline_script(format.inline_script),
168+
.with_ascii_only(ascii_only)
169+
.with_inline_script(inline_script),
164170
comments,
165171
cm: source_map.clone(),
166172
wr,

crates/rspack_javascript_compiler/src/compiler/transform.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use swc_core::{
2525
base::{
2626
BoolOr,
2727
config::{
28-
BuiltInput, Config, ConfigFile, InputSourceMap, JsMinifyCommentOption, JsMinifyFormatOptions,
29-
Rc, RootMode, SourceMapsConfig,
28+
BuiltInput, Config, ConfigFile, InputSourceMap, JsMinifyCommentOption, OutputCharset, Rc,
29+
RootMode, SourceMapsConfig,
3030
},
3131
sourcemap,
3232
},
@@ -301,7 +301,7 @@ impl<'a> JavaScriptTransformer<'a> {
301301
where
302302
P: Pass + 'a,
303303
{
304-
let built_input = self.parse_built_input(before_pass)?;
304+
let mut built_input = self.parse_built_input(before_pass)?;
305305

306306
let target = built_input.target;
307307
let source_map_kind: SourceMapKind = match self.options.config.source_maps {
@@ -322,12 +322,12 @@ impl<'a> JavaScriptTransformer<'a> {
322322

323323
inspect_parsed_ast(&built_input.program);
324324

325-
let (program, diagnostics) = self.transform_with_built_input(built_input)?;
326-
let format_opt = JsMinifyFormatOptions {
327-
inline_script: false,
328-
ascii_only: true,
329-
..Default::default()
330-
};
325+
let diagnostics = self.transform_with_built_input(&mut built_input)?;
326+
let ascii_only = built_input
327+
.output
328+
.charset
329+
.as_ref()
330+
.is_some_and(|v| matches!(v, OutputCharset::Ascii));
331331

332332
let print_options = PrintOptions {
333333
source_len: self.fm.byte_length(),
@@ -337,12 +337,14 @@ impl<'a> JavaScriptTransformer<'a> {
337337
input_source_map: input_source_map.as_ref(),
338338
minify,
339339
comments: Some(&self.comments as &dyn Comments),
340-
format: &format_opt,
340+
preamble: &built_input.output.preamble,
341+
ascii_only,
342+
inline_script: built_input.codegen_inline_script,
341343
};
342344

343345
self
344346
.javascript_compiler
345-
.print(&program, print_options)
347+
.print(&built_input.program, print_options)
346348
.map(|o| o.with_diagnostics(diagnostics))
347349
}
348350

@@ -435,21 +437,19 @@ impl<'a> JavaScriptTransformer<'a> {
435437

436438
fn transform_with_built_input(
437439
&self,
438-
built_input: BuiltInput<impl Pass>,
439-
) -> Result<(Program, Vec<String>), Error> {
440-
let program = built_input.program;
441-
let mut pass = built_input.pass;
440+
built_input: &mut BuiltInput<impl Pass>,
441+
) -> Result<Vec<String>, Error> {
442442
let mut diagnostics = vec![];
443-
let program = self.run(|| {
443+
let result = self.run(|| {
444444
helpers::HELPERS.set(&self.helpers, || {
445445
let result = try_with_handler(self.cm.clone(), Default::default(), |handler| {
446446
// Apply external plugin passes to the Program AST.
447447
// External plugins may emit warnings or inject helpers,
448448
// so we need a handler to properly process them.
449-
let program = program.apply(&mut pass);
449+
built_input.pass.process(&mut built_input.program);
450450
diagnostics.extend(handler.take_diagnostics());
451451

452-
Ok(program)
452+
Ok(())
453453
});
454454

455455
result.map_err(|err| {
@@ -495,14 +495,12 @@ impl<'a> JavaScriptTransformer<'a> {
495495

496496
minify_file_comments(
497497
comments,
498-
built_input.preserve_comments,
498+
&built_input.preserve_comments,
499499
preserve_annotations,
500500
);
501501
}
502502

503-
program
504-
.map(|program| (program, diagnostics))
505-
.map_err(|e| e.into())
503+
result.map(|_| diagnostics).map_err(|e| e.into())
506504
}
507505

508506
pub fn input_source_map(

crates/rspack_util/src/swc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn test_normalize_custom_filename() {
4848
*/
4949
pub fn minify_file_comments(
5050
comments: &SingleThreadedComments,
51-
preserve_comments: BoolOr<JsMinifyCommentOption>,
51+
preserve_comments: &BoolOr<JsMinifyCommentOption>,
5252
preserve_annotations: bool,
5353
) {
5454
match preserve_comments {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
```js title=a.js
2+
(self["webpackChunkwebpack"] = self["webpackChunkwebpack"] || []).push([["a"], {
3+
"./a.js": (function () {
4+
console.log("你好");
5+
6+
7+
}),
8+
9+
},function(__webpack_require__) {
10+
var __webpack_exec__ = function(moduleId) { return __webpack_require__(__webpack_require__.s = moduleId) }
11+
var __webpack_exports__ = (__webpack_exec__("./a.js"));
12+
13+
}
14+
]);
15+
```
16+
17+
```js title=b.js
18+
(self["webpackChunkwebpack"] = self["webpackChunkwebpack"] || []).push([["b"], {
19+
"./b.js": (function () {
20+
console.log("\u4F60\u597D");
21+
22+
23+
}),
24+
25+
},function(__webpack_require__) {
26+
var __webpack_exec__ = function(moduleId) { return __webpack_require__(__webpack_require__.s = moduleId) }
27+
var __webpack_exports__ = (__webpack_exec__("./b.js"));
28+
29+
}
30+
]);
31+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("你好")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("你好")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/** @type {import("@rspack/core").Configuration} */
2+
module.exports = {
3+
entry: {
4+
a: "./a.js",
5+
b: "./b.js"
6+
},
7+
module: {
8+
rules: [
9+
{
10+
test: /a\.js/,
11+
use: [
12+
{
13+
loader: "builtin:swc-loader"
14+
}
15+
]
16+
},
17+
{
18+
test: /b\.js/,
19+
use: [
20+
{
21+
loader: "builtin:swc-loader",
22+
options: {
23+
jsc: {
24+
output: {
25+
charset: "ascii"
26+
}
27+
}
28+
}
29+
}
30+
]
31+
}
32+
]
33+
}
34+
};

0 commit comments

Comments
 (0)