Skip to content

Commit

Permalink
fix(es/minifier): Respect module: false (#8925)
Browse files Browse the repository at this point in the history
**Description:**

Tests are modified because I enabled script detection for the fixture test suite.

**Related issue:**

 - Closes #8909
  • Loading branch information
kdy1 authored May 7, 2024
1 parent 3180a86 commit aca6a77
Show file tree
Hide file tree
Showing 207 changed files with 805 additions and 849 deletions.
2 changes: 1 addition & 1 deletion crates/swc/benches/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn bench_minify(b: &mut Bencher, filename: &str) {
&JsMinifyOptions {
compress: BoolOrDataConfig::from_bool(true),
mangle: BoolOrDataConfig::from_bool(true),
toplevel: true,
toplevel: Some(true),
source_map: BoolOrDataConfig::from_bool(true),
output_path: Default::default(),
inline_sources_content: true,
Expand Down
2 changes: 2 additions & 0 deletions crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ impl VisitMut for MinifierPass<'_> {
v.const_to_let = Some(true);
}

v.module = false;

v.into_config(self.cm.clone())
}),
mangle: options
Expand Down
90 changes: 47 additions & 43 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,54 +302,58 @@ impl Options {
syntax.typescript(),
));

if program.is_module() {
js_minify = js_minify.map(|c| {
let compress = c
.compress
.unwrap_as_option(|default| match default {
Some(true) => Some(Default::default()),
_ => None,
})
.map(|mut c| {
if c.toplevel.is_none() {
c.toplevel = Some(TerserTopLevelOptions::Bool(true));
}
let default_top_level = program.is_module();

js_minify = js_minify.map(|mut c| {
let compress = c
.compress
.unwrap_as_option(|default| match default {
Some(true) => Some(Default::default()),
_ => None,
})
.map(|mut c| {
if c.toplevel.is_none() {
c.toplevel = Some(TerserTopLevelOptions::Bool(default_top_level));
}

if matches!(
cfg.module,
None | Some(ModuleConfig::Es6(..) | ModuleConfig::NodeNext(..))
) {
c.module = true;
}
if matches!(
cfg.module,
None | Some(ModuleConfig::Es6(..) | ModuleConfig::NodeNext(..))
) {
c.module = true;
}

c
})
.map(BoolOrDataConfig::from_obj)
.unwrap_or_else(|| BoolOrDataConfig::from_bool(false));
c
})
.map(BoolOrDataConfig::from_obj)
.unwrap_or_else(|| BoolOrDataConfig::from_bool(false));

let mangle = c
.mangle
.unwrap_as_option(|default| match default {
Some(true) => Some(Default::default()),
_ => None,
})
.map(|mut c| {
if c.top_level.is_none() {
c.top_level = Some(default_top_level);
}

let mangle = c
.mangle
.unwrap_as_option(|default| match default {
Some(true) => Some(Default::default()),
_ => None,
})
.map(|mut c| {
if c.top_level.is_none() {
c.top_level = Some(true);
}
c
})
.map(BoolOrDataConfig::from_obj)
.unwrap_or_else(|| BoolOrDataConfig::from_bool(false));

c
})
.map(BoolOrDataConfig::from_obj)
.unwrap_or_else(|| BoolOrDataConfig::from_bool(false));
if c.toplevel.is_none() {
c.toplevel = Some(default_top_level);
}

JsMinifyOptions {
compress,
mangle,
..c
}
});
}
JsMinifyOptions {
compress,
mangle,
..c
}
});

if js_minify.is_some() && js_minify.as_ref().unwrap().keep_fnames {
js_minify = js_minify.map(|c| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
var _ts_generator = require("@swc/helpers/_/_ts_generator");
function gen() {
return _ts_generator(this, function(_state) {
return _ts_generator._(this, function(_state) {
switch(_state.label){
case 0:
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
"use strict";
console.log("src/dep.ts");
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
"use strict";
console.log("src2/dep-2.ts");
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

console.log(displayA());
})();

export { }
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { _ as _type_of } from "@swc/helpers/_/_type_of";
var _type_of = require("@swc/helpers/_/_type_of");
var window = "foo";
console.log(typeof window === "undefined" ? "undefined" : _type_of(window));
console.log(typeof window === "undefined" ? "undefined" : _type_of._(window));
32 changes: 17 additions & 15 deletions crates/swc/tests/fixture/issues-0xxx/846/case1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _decorate } from "@swc/helpers/_/_decorate";
import { _ as _get } from "@swc/helpers/_/_get";
import { _ as _get_prototype_of } from "@swc/helpers/_/_get_prototype_of";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
import { _ as _create_super } from "@swc/helpers/_/_create_super";
var SomeClass = _decorate([], function(_initialize) {
var _assert_this_initialized = require("@swc/helpers/_/_assert_this_initialized");
var _class_call_check = require("@swc/helpers/_/_class_call_check");
var _decorate = require("@swc/helpers/_/_decorate");
var _get = require("@swc/helpers/_/_get");
var _get_prototype_of = require("@swc/helpers/_/_get_prototype_of");
var _inherits = require("@swc/helpers/_/_inherits");
var _create_super = require("@swc/helpers/_/_create_super");
var SomeClass = _decorate._([], function(_initialize) {
"use strict";
var SomeClass = function SomeClass() {
"use strict";
_class_call_check(this, SomeClass);
_class_call_check._(this, SomeClass);
_initialize(this);
};
return {
Expand All @@ -25,16 +26,17 @@ var SomeClass = _decorate([], function(_initialize) {
]
};
});
var OtherClass = _decorate([], function(_initialize, _SomeClass) {
var OtherClass = _decorate._([], function(_initialize, _SomeClass) {
"use strict";
var OtherClass = /*#__PURE__*/ function(_SomeClass) {
"use strict";
_inherits(OtherClass, _SomeClass);
var _super = _create_super(OtherClass);
_inherits._(OtherClass, _SomeClass);
var _super = _create_super._(OtherClass);
function OtherClass() {
_class_call_check(this, OtherClass);
_class_call_check._(this, OtherClass);
var _this;
_this = _super.apply(this, arguments);
_initialize(_assert_this_initialized(_this));
_initialize(_assert_this_initialized._(_this));
return _this;
}
return OtherClass;
Expand All @@ -49,7 +51,7 @@ var OtherClass = _decorate([], function(_initialize, _SomeClass) {
],
key: "anotherMethod",
value: function anotherMethod() {
_get(_get_prototype_of(OtherClass.prototype), "someMethod", this).call(this);
_get._(_get_prototype_of._(OtherClass.prototype), "someMethod", this).call(this);
}
}
]
Expand Down
10 changes: 3 additions & 7 deletions crates/swc/tests/fixture/issues-1xxx/1160/output/entry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _define_property = require("@swc/helpers/_/_define_property");
const _ts_decorate = require("@swc/helpers/_/_ts_decorate");
const _ts_metadata = require("@swc/helpers/_/_ts_metadata");
var _define_property = require("@swc/helpers/_/_define_property");
var _ts_decorate = require("@swc/helpers/_/_ts_decorate");
var _ts_metadata = require("@swc/helpers/_/_ts_metadata");
var MyEnum;
(function(MyEnum) {
MyEnum["x"] = "xxx";
Expand Down
12 changes: 6 additions & 6 deletions crates/swc/tests/fixture/issues-1xxx/1216/case-1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
var _ts_generator = require("@swc/helpers/_/_ts_generator");
var source = Math.random() < 2 ? "matilda" : "fred";
var details = {
_id: "1"
Expand All @@ -8,8 +8,8 @@ function request(path) {
return _request.apply(this, arguments);
}
function _request() {
_request = _async_to_generator(function(path) {
return _ts_generator(this, function(_state) {
_request = _async_to_generator._(function(path) {
return _ts_generator._(this, function(_state) {
return [
2,
"success:".concat(path)
Expand All @@ -18,9 +18,9 @@ function _request() {
});
return _request.apply(this, arguments);
}
_async_to_generator(function() {
_async_to_generator._(function() {
var obj, _tmp;
return _ts_generator(this, function(_state) {
return _ts_generator._(this, function(_state) {
switch(_state.label){
case 0:
if (!(source === "matilda")) return [
Expand Down
12 changes: 6 additions & 6 deletions crates/swc/tests/fixture/issues-1xxx/1216/case-2/output/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
var _ts_generator = require("@swc/helpers/_/_ts_generator");
var source = Math.random() < 2 ? "matilda" : "fred";
var details = {
_id: "1"
Expand All @@ -8,8 +8,8 @@ function request(path) {
return _request.apply(this, arguments);
}
function _request() {
_request = _async_to_generator(function(path) {
return _ts_generator(this, function(_state) {
_request = _async_to_generator._(function(path) {
return _ts_generator._(this, function(_state) {
return [
2,
"success:".concat(path)
Expand All @@ -18,9 +18,9 @@ function _request() {
});
return _request.apply(this, arguments);
}
_async_to_generator(function() {
_async_to_generator._(function() {
var obj, _tmp;
return _ts_generator(this, function(_state) {
return _ts_generator._(this, function(_state) {
switch(_state.label){
case 0:
if (!(source === "matilda")) return [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
var _ts_generator = require("@swc/helpers/_/_ts_generator");
function foo() {
var val, _tmp;
return _ts_generator(this, function(_state) {
return _ts_generator._(this, function(_state) {
switch(_state.label){
case 0:
if (!true) return [
Expand Down
6 changes: 3 additions & 3 deletions crates/swc/tests/fixture/issues-1xxx/1219/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { _ as _define_property } from "@swc/helpers/_/_define_property";
var _define_property = require("@swc/helpers/_/_define_property");
class Foo {
constructor(){
_define_property(this, "static", 5);
_define_property(this, "declare", 5);
_define_property._(this, "static", 5);
_define_property._(this, "declare", 5);
}
}
3 changes: 0 additions & 3 deletions crates/swc/tests/fixture/issues-1xxx/1259/output/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
var _tagged_template_literal = require("@swc/helpers/_/_tagged_template_literal");
function _templateObject() {
var data = _tagged_template_literal._([
Expand Down
8 changes: 2 additions & 6 deletions crates/swc/tests/fixture/issues-1xxx/1278/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _ts_decorate = require("@swc/helpers/_/_ts_decorate");
const _ts_metadata = require("@swc/helpers/_/_ts_metadata");
var _ts_decorate = require("@swc/helpers/_/_ts_decorate");
var _ts_metadata = require("@swc/helpers/_/_ts_metadata");
function MyDecorator(klass) {
return ()=>{
// do something
Expand Down
20 changes: 10 additions & 10 deletions crates/swc/tests/fixture/issues-1xxx/1306/case1/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _class_private_field_get } from "@swc/helpers/_/_class_private_field_get";
import { _ as _class_private_field_init } from "@swc/helpers/_/_class_private_field_init";
import { _ as _class_private_field_set } from "@swc/helpers/_/_class_private_field_set";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
var _class_call_check = require("@swc/helpers/_/_class_call_check");
var _class_private_field_get = require("@swc/helpers/_/_class_private_field_get");
var _class_private_field_init = require("@swc/helpers/_/_class_private_field_init");
var _class_private_field_set = require("@swc/helpers/_/_class_private_field_set");
var _create_class = require("@swc/helpers/_/_create_class");
var _name = /*#__PURE__*/ new WeakMap();
var Animal = /*#__PURE__*/ function() {
"use strict";
function Animal(name) {
_class_call_check(this, Animal);
_class_private_field_init(this, _name, {
_class_call_check._(this, Animal);
_class_private_field_init._(this, _name, {
writable: true,
value: void 0
});
_class_private_field_set(this, _name, name);
_class_private_field_set._(this, _name, name);
}
_create_class(Animal, [
_create_class._(Animal, [
{
key: "noise",
value: function noise() {
return _class_private_field_get(this, _name).toUpperCase();
return _class_private_field_get._(this, _name).toUpperCase();
}
}
]);
Expand Down
20 changes: 10 additions & 10 deletions crates/swc/tests/fixture/issues-1xxx/1306/case2/output/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _class_private_field_get } from "@swc/helpers/_/_class_private_field_get";
import { _ as _class_private_field_init } from "@swc/helpers/_/_class_private_field_init";
import { _ as _class_private_field_set } from "@swc/helpers/_/_class_private_field_set";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
var _class_call_check = require("@swc/helpers/_/_class_call_check");
var _class_private_field_get = require("@swc/helpers/_/_class_private_field_get");
var _class_private_field_init = require("@swc/helpers/_/_class_private_field_init");
var _class_private_field_set = require("@swc/helpers/_/_class_private_field_set");
var _create_class = require("@swc/helpers/_/_create_class");
var _name = /*#__PURE__*/ new WeakMap();
var Animal = /*#__PURE__*/ function() {
"use strict";
function Animal(name) {
_class_call_check(this, Animal);
_class_private_field_init(this, _name, {
_class_call_check._(this, Animal);
_class_private_field_init._(this, _name, {
writable: true,
value: void 0
});
_class_private_field_set(this, _name, name);
_class_private_field_set._(this, _name, name);
}
_create_class(Animal, [
_create_class._(Animal, [
{
key: "noise",
value: function noise() {
return _class_private_field_get(this, _name).toUpperCase();
return _class_private_field_get._(this, _name).toUpperCase();
}
}
]);
Expand Down
Loading

0 comments on commit aca6a77

Please sign in to comment.