Skip to content

Commit

Permalink
feat(es/minifier): Support preserve_annotations of terser (#9775)
Browse files Browse the repository at this point in the history
**Description:**

 - `jsc.output.preserveAnnotations` (`transform()`/`.swcrc`)
 - `format.preserve_annotations` (`minify()`)

are added to support stripping out PURE comments while preserving license comments.

**Related issue:**

 - #9674
  • Loading branch information
kdy1 authored Dec 2, 2024
1 parent 6b36ffd commit 6e1c9fd
Show file tree
Hide file tree
Showing 89 changed files with 168 additions and 148 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-seahorses-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_compiler_base: major
---

feat(es/minifier): Support `preserve_annotations` of terser
11 changes: 9 additions & 2 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,11 @@ impl Options {
comments: comments.cloned(),
preserve_comments,
emit_source_map_columns: cfg.emit_source_map_columns.into_bool(),
output: JscOutputConfig { charset, preamble },
output: JscOutputConfig {
charset,
preamble,
preserve_annotations: cfg.jsc.output.preserve_annotations,
},
emit_assert_for_import_attributes: experimental
.emit_assert_for_import_attributes
.into_bool(),
Expand Down Expand Up @@ -1144,9 +1148,9 @@ where
output_path: self.output_path,
source_root: self.source_root,
source_file_name: self.source_file_name,
comments: self.comments,
preserve_comments: self.preserve_comments,
inline_sources_content: self.inline_sources_content,
comments: self.comments,
emit_source_map_columns: self.emit_source_map_columns,
output: self.output,
emit_assert_for_import_attributes: self.emit_assert_for_import_attributes,
Expand Down Expand Up @@ -1212,6 +1216,9 @@ pub struct JscOutputConfig {

#[serde(default)]
pub preamble: String,

#[serde(default)]
pub preserve_annotations: BoolConfig<false>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
Expand Down
12 changes: 10 additions & 2 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,11 @@ impl Compiler {
.clone()
.into_inner()
.unwrap_or(BoolOr::Data(JsMinifyCommentOption::PreserveSomeComments));
swc_compiler_base::minify_file_comments(&comments, preserve_comments);
swc_compiler_base::minify_file_comments(
&comments,
preserve_comments,
opts.format.preserve_annotations,
);

self.print(
&program,
Expand Down Expand Up @@ -1018,7 +1022,11 @@ impl Compiler {
});

if let Some(comments) = &config.comments {
swc_compiler_base::minify_file_comments(comments, config.preserve_comments);
swc_compiler_base::minify_file_comments(
comments,
config.preserve_comments,
config.output.preserve_annotations.into_bool(),
);
}

self.print(
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-2xxx/2854/1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export function App() {
return /*#__PURE__*/ React.createElement(Form, null);
return React.createElement(Form, null);
}
export function Form(param) {
var _param_onChange = param.onChange, onChange = _param_onChange === void 0 ? function() {} : _param_onChange;
return /*#__PURE__*/ React.createElement("input", {
return React.createElement("input", {
onChange: function onChange1() {
onChange();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var App = /*#__PURE__*/ function(_React_Component) {
key: "render",
value: function render() {
console.log(this.props);
return /*#__PURE__*/ React.createElement("div", null, "134");
return React.createElement("div", null, "134");
}
}
]);
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7653/output/1.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function Home(){return /*#__PURE__*/React.createElement("div",{dangerouslySetInnerHTML:{__html:"Hello World"}})}
export default function Home(){return React.createElement("div",{dangerouslySetInnerHTML:{__html:"Hello World"}})}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ let positions = {
};
export function PositionRender({ isRtl, position }) {
let display = ('fe-fe-fe' === isRtl ? rtlPositions : positions)[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export function PositionRender({ isRtl, position }) {
] : {
positions
})[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export function PositionRender({ isRtl, position }) {
] : {
positions
})[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/4.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export function PositionRender({ isRtl, position }) {
let display = ('fe-fe-fe' === isRtl ? rtlPositions : {
a: positions
})[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/5.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export function PositionRender({ isRtl, position }) {
positions
]
})[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7783/output/1.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function e(){return /*#__PURE__*/React.createElement("div",null,foo.a)}let foo={get a(){return`a ${this.b}`},get b(){return"b"}};
export default function e(){return React.createElement("div",null,foo.a)}let foo={get a(){return`a ${this.b}`},get b(){return"b"}};
6 changes: 3 additions & 3 deletions crates/swc/tests/fixture/issues-7xxx/7821/output/1.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var Blocks = {
Block1: function() {
return /*#__PURE__*/ React.createElement(React.Fragment, null, "'Block1xx'");
return React.createElement(React.Fragment, null, "'Block1xx'");
},
Block2: function() {
return /*#__PURE__*/ React.createElement(React.Fragment, null, "'Block2xx'");
return React.createElement(React.Fragment, null, "'Block2xx'");
},
Layout1: function() {
return [
Expand All @@ -14,5 +14,5 @@ var Blocks = {
}
};
export function render() {
return /*#__PURE__*/ React.createElement(Blocks.Layout1, null);
return React.createElement(Blocks.Layout1, null);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-8xxx/8199/output/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jsx as _jsx } from "react/jsx-runtime";
export const IconSpecHotkey = (param)=>{
let { icon } = param;
return /*#__PURE__*/ _jsx("div", {
return _jsx("div", {
children: icon
});
};
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-8xxx/8210/output/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { jsxDEV: _jsxDEV } = require("react/jsx-dev-runtime");
const Component = ()=>{
return /*#__PURE__*/ _jsxDEV("p", {
thing: /*#__PURE__*/ _jsxDEV("a", {}, void 0, false, {
return _jsxDEV("p", {
thing: _jsxDEV("a", {}, void 0, false, {
fileName: "$DIR/tests/fixture/issues-8xxx/8210/input/1.js",
lineNumber: 2,
columnNumber: 23
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-8xxx/8243/output/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jsx as _jsx } from "react/jsx-runtime";
export const Foo = (props)=>{
return originalMessage ? /*#__PURE__*/ _jsx(Bar, {
children: /*#__PURE__*/ _jsx(Baz, {
return originalMessage ? _jsx(Bar, {
children: _jsx(Baz, {
children: ()=>null
})
}) : null;
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-8xxx/8243/output/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jsx as _jsx } from "react/jsx-runtime";
export const Foo = (props)=>{
return originalMessage ? /*#__PURE__*/ _jsx(Bar, {
children: /*#__PURE__*/ _jsx(Baz, {
return originalMessage ? _jsx(Bar, {
children: _jsx(Baz, {
children: ()=>null
})
}) : null;
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-8xxx/8243/output/3.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jsx as _jsx } from "react/jsx-runtime";
export const Foo = (props)=>{
return originalMessage ? /*#__PURE__*/ _jsx(Bar, {
children: /*#__PURE__*/ _jsx(Baz, {
return originalMessage ? _jsx(Bar, {
children: _jsx(Baz, {
children: ()=>null
})
}) : null;
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-8xxx/8243/output/4.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jsx as _jsx } from "react/jsx-runtime";
export const Foo = (props)=>{
return originalMessage ? /*#__PURE__*/ _jsx(Bar, {
children: /*#__PURE__*/ _jsx(Baz, {
return originalMessage ? _jsx(Bar, {
children: _jsx(Baz, {
children: ()=>null
})
}) : null;
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-8xxx/8275/output/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*#__PURE__*/ export const c = (function(strings, ...values) {
export const c = (function(strings, ...values) {
return {
strings,
values
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-8xxx/8594/output/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
(function(FooNs) {
FooNs.Shared = ()=>'I\'m shared component';
FooNs.Main = ()=>/*#__PURE__*/ React.createElement(FooNs.Shared, null);
FooNs.Main = ()=>React.createElement(FooNs.Shared, null);
})(FooNs || (FooNs = {}));
export var FooNs;
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-8xxx/8640/output/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
(function(Ns) {
Ns.Context = /*#__PURE__*/ React.createContext();
Ns.Component = ()=>/*#__PURE__*/ React.createElement(Ns.Context.Provider, null);
Ns.Context = React.createContext();
Ns.Component = ()=>React.createElement(Ns.Context.Provider, null);
})(Ns || (Ns = {}));
export var Ns;
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-9xxx/9204/output/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let Foo = createFoo();
export function App() {
return /*#__PURE__*/ React.createElement("view", null, /*#__PURE__*/ React.createElement(Foo, null));
return React.createElement("view", null, React.createElement(Foo, null));
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/sourcemap/003/output/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/sourcemap/003/output/index.map
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": "uGAAA,gBAAe,SAASA,EAAW,CAAQ,MAAR,AAAEC,EAAF,EAAEA,KACjC,oBAAO,oBAACC,WAAKD,EAAKE,GAAG,CACzB,CAEA,gBAAsBC,wBAAAA,iCAAAA,WAAAA,EAAf,EAAA,qCACH,SAAO,CACHC,MAAO,CACHJ,KAAM,CACFE,IAAK,KACT,CACJ,CACJ,IACJ",
"mappings": "uGAAA,gBAAe,SAASA,EAAW,CAAQ,MAAR,AAAEC,EAAF,EAAEA,KACjC,OAAO,oBAACC,WAAKD,EAAKE,GAAG,CACzB,CAEA,gBAAsBC,wBAAAA,iCAAAA,WAAAA,EAAf,EAAA,qCACH,SAAO,CACHC,MAAO,CACHJ,KAAM,CACFE,IAAK,KACT,CACJ,CACJ,IACJ",
"names": [
"StaticPage",
"data",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [file.tsx]
import * as React from "react";
/*#__PURE__*/ GenericComponent, /*#__PURE__*/ GenericComponent, /*#__PURE__*/ GenericComponent, /*#__PURE__*/ GenericComponent;
GenericComponent, GenericComponent, GenericComponent, GenericComponent;
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//// [checkJsxUnionSFXContextualTypeInferredCorrectly.tsx]
import React from 'react';
export function ComponentWithUnion(props) {
return /*#__PURE__*/ React.createElement("h1", null);
return React.createElement("h1", null);
}
export function HereIsTheError() {
return /*#__PURE__*/ React.createElement(ComponentWithUnion, {
return React.createElement(ComponentWithUnion, {
multi: !1,
value: 's',
onChange: function(val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
//// [0.tsx]
import * as cx from 'classnames';
import * as React from "react";
/*#__PURE__*/ cx('class1', {
cx('class1', {
class2: !0
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
//// [0.tsx]
import * as cx from 'classnames';
import * as React from "react";
/*#__PURE__*/ cx('class1', {
cx('class1', {
class2: !0
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
//// [0.tsx]
import * as cx from 'classnames';
import * as React from "react";
/*#__PURE__*/ cx('class1', {
cx('class1', {
class2: !0
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import * as cx from 'classnames';
import * as React from "react";
/*#__PURE__*/ _object_spread_props(_object_spread({}, buttonProps), {
_object_spread_props(_object_spread({}, buttonProps), {
className: cx('class1', {
class2: !0
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ export { };
import { h, Fragment } from "./renderer";
//// [snabbdomy.tsx]
import { jsx } from "./renderer";
/*#__PURE__*/ React.Fragment;
React.Fragment;
//// [preacty-only-fragment.tsx]
import { h, Fragment } from "./renderer";
//// [snabbdomy-only-fragment.tsx]
import { jsx } from "./renderer";
/*#__PURE__*/ React.Fragment;
React.Fragment;
//// [preacty-only-fragment-no-jsx.tsx]
import { Fragment } from "./renderer";
//// [snabbdomy-only-fragment-no-jsx.tsx]
import "./renderer";
/*#__PURE__*/ React.Fragment;
React.Fragment;
//// [preacty-no-fragment.tsx]
import { h } from "./renderer";
//// [snabbdomy-no-fragment.tsx]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export { };
//// [snabbdom.d.ts]
export { };
//// [reacty.tsx]
/*#__PURE__*/ React.Fragment;
React.Fragment;
export { };
//// [preacty.tsx]
import { h, Frag } from "./preact";
//// [snabbdomy.tsx]
import { h } from "./snabbdom";
/*#__PURE__*/ React.Fragment;
React.Fragment;
//// [mix-n-match.tsx]
import { h } from "./preact";
import { Fragment } from "./react";
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ export { dom as default };
import * as React from "./renderer";
//// [other.tsx]
import { dom as h } from "./renderer";
export var prerendered = /*#__PURE__*/ h("h", null);
export var prerendered = h("h", null);
//// [othernoalias.tsx]
import { otherdom } from "./renderer";
export var prerendered2 = /*#__PURE__*/ otherdom("h", null);
export var prerendered2 = otherdom("h", null);
//// [reacty.tsx]
import React from "./renderer";
export var prerendered3 = /*#__PURE__*/ React.createElement("h", null);
export var prerendered3 = React.createElement("h", null);
//// [index.tsx]
//! x Expression expected
//! ,-[3:1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
var _this = this;
import { predom } from "./renderer2";
export var MySFC = function(props) {
return /*#__PURE__*/ predom.apply(void 0, [
return predom.apply(void 0, [
"p",
null,
props.x,
Expand All @@ -23,7 +23,7 @@ export var MyClass = /*#__PURE__*/ function() {
_class_call_check(this, MyClass), this.props = props;
}
return MyClass.prototype.render = function() {
return /*#__PURE__*/ predom.apply(void 0, [
return predom.apply(void 0, [
"p",
null,
this.props.x,
Expand All @@ -34,17 +34,17 @@ export var MyClass = /*#__PURE__*/ function() {
].concat(_to_consumable_array(this.props.children)));
}, MyClass;
}();
export var tree = /*#__PURE__*/ predom(MySFC, {
export var tree = predom(MySFC, {
x: 1,
y: 2
}, /*#__PURE__*/ predom(MyClass, {
}, predom(MyClass, {
x: 3,
y: 4
}), /*#__PURE__*/ predom(MyClass, {
}), predom(MyClass, {
x: 5,
y: 6
}));
export default /*#__PURE__*/ predom("h", null);
export default predom("h", null);
//// [index.tsx]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { };
export { };
//// [component.tsx]
import { predom } from "./renderer2";
export default /*#__PURE__*/ predom("h", null);
export default predom("h", null);
//// [index.tsx]
import { dom } from "./renderer";
import prerendered from "./component";
Loading

0 comments on commit 6e1c9fd

Please sign in to comment.