Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/module): support node interop #5069

Merged
merged 8 commits into from Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

16 changes: 10 additions & 6 deletions crates/swc/src/builder.rs
Expand Up @@ -182,14 +182,18 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
where
P: 'cmt,
{
let (need_analyzer, no_interop, ignore_dynamic) = match module {
Some(ModuleConfig::CommonJs(ref c)) => (true, c.no_interop, c.ignore_dynamic),
Some(ModuleConfig::Amd(ref c)) => (true, c.config.no_interop, c.config.ignore_dynamic),
Some(ModuleConfig::Umd(ref c)) => (true, c.config.no_interop, c.config.ignore_dynamic),
let (need_analyzer, import_interop, ignore_dynamic) = match module {
Some(ModuleConfig::CommonJs(ref c)) => (true, c.import_interop(), c.ignore_dynamic),
Some(ModuleConfig::Amd(ref c)) => {
(true, c.config.import_interop(), c.config.ignore_dynamic)
}
Some(ModuleConfig::Umd(ref c)) => {
(true, c.config.import_interop(), c.config.ignore_dynamic)
}
Some(ModuleConfig::SystemJs(_))
| Some(ModuleConfig::Es6)
| Some(ModuleConfig::NodeNext)
| None => (false, true, true),
| None => (false, true.into(), true),
};

let mut feature_flag = FeatureFlag::empty();
Expand Down Expand Up @@ -320,7 +324,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
compat_pass,
// module / helper
Optional::new(
modules::import_analysis::import_analyzer(no_interop, ignore_dynamic),
modules::import_analysis::import_analyzer(import_interop, ignore_dynamic),
need_analyzer
),
compat::reserved_words::reserved_words(),
Expand Down
12 changes: 12 additions & 0 deletions crates/swc/tests/fixture/interop/babel/input/.swcrc
@@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"target": "es2020"
},
"module": {
"type": "commonjs",
"importInterop": "babel"
}
}
9 changes: 9 additions & 0 deletions crates/swc/tests/fixture/interop/babel/input/index.js
@@ -0,0 +1,9 @@
import foo from "foo";
import bar, { baz } from "bar";

export const x = { foo, bar };
export const y = baz;
export default {
x,
y,
};
28 changes: 28 additions & 0 deletions crates/swc/tests/fixture/interop/babel/output/index.js
@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
get: all[name],
enumerable: true
});
}
_export(exports, {
default: ()=>_default,
x: ()=>x,
y: ()=>y
});
const _interopRequireDefault = require("@swc/helpers/lib/_interop_require_default.js").default;
const _interopRequireWildcard = require("@swc/helpers/lib/_interop_require_wildcard.js").default;
const _foo = _interopRequireDefault(require("foo"));
const _bar = _interopRequireWildcard(require("bar"));
const x = {
foo: _foo.default,
bar: _bar.default
};
const y = _bar.baz;
var _default = {
x,
y
};
12 changes: 12 additions & 0 deletions crates/swc/tests/fixture/interop/node/input/.swcrc
@@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"target": "es2020"
},
"module": {
"type": "commonjs",
"importInterop": "node"
}
}
9 changes: 9 additions & 0 deletions crates/swc/tests/fixture/interop/node/input/index.js
@@ -0,0 +1,9 @@
import foo from "foo";
import bar, { baz } from "bar";

export const x = { foo, bar };
export const y = baz;
export default {
x,
y,
};
28 changes: 28 additions & 0 deletions crates/swc/tests/fixture/interop/node/output/index.js
@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.y = exports.x = exports.default = void 0;
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
get: all[name],
enumerable: true
});
}
_export(exports, {
default: ()=>_default,
x: ()=>x,
y: ()=>y
});
const _interopRequireWildcard = require("@swc/helpers/lib/_interop_require_wildcard.js").default;
const _foo = require("foo");
const _bar = _interopRequireWildcard(require("bar"), true);
const x = {
foo: _foo,
bar: _bar.default
};
const y = _bar.baz;
var _default = {
x,
y
};
12 changes: 12 additions & 0 deletions crates/swc/tests/fixture/interop/none/input/.swcrc
@@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"target": "es2020"
},
"module": {
"type": "commonjs",
"importInterop": "none"
}
}
9 changes: 9 additions & 0 deletions crates/swc/tests/fixture/interop/none/input/index.js
@@ -0,0 +1,9 @@
import foo from "foo";
import bar, { baz } from "bar";

export const x = { foo, bar };
export const y = baz;
export default {
x,
y,
};
23 changes: 23 additions & 0 deletions crates/swc/tests/fixture/interop/none/output/index.js
@@ -0,0 +1,23 @@
"use strict";
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
get: all[name],
enumerable: true
});
}
_export(exports, {
default: ()=>_default,
x: ()=>x,
y: ()=>y
});
const _foo = require("foo");
const _bar = require("bar");
const x = {
foo: _foo.default,
bar: _bar.default
};
const y = _bar.baz;
var _default = {
x,
y
};
12 changes: 12 additions & 0 deletions crates/swc/tests/fixture/interop/swc/input/.swcrc
@@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"target": "es2020"
},
"module": {
"type": "commonjs",
"importInterop": "swc"
}
}
9 changes: 9 additions & 0 deletions crates/swc/tests/fixture/interop/swc/input/index.js
@@ -0,0 +1,9 @@
import foo from "foo";
import bar, { baz } from "bar";

export const x = { foo, bar };
export const y = baz;
export default {
x,
y,
};
28 changes: 28 additions & 0 deletions crates/swc/tests/fixture/interop/swc/output/index.js
@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
get: all[name],
enumerable: true
});
}
_export(exports, {
default: ()=>_default,
x: ()=>x,
y: ()=>y
});
const _interopRequireDefault = require("@swc/helpers/lib/_interop_require_default.js").default;
const _interopRequireWildcard = require("@swc/helpers/lib/_interop_require_wildcard.js").default;
const _foo = _interopRequireDefault(require("foo"));
const _bar = _interopRequireWildcard(require("bar"));
const x = {
foo: _foo.default,
bar: _bar.default
};
const y = _bar.baz;
var _default = {
x,
y
};
@@ -1,28 +1,31 @@
function _getRequireWildcardCache() {
function _getRequireWildcardCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
var cache = new WeakMap();
_getRequireWildcardCache = function () { return cache; };
return cache;

var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireWildcardCache = function (nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {

function _interopRequireWildcard(obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}

if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) {
return { default: obj }
}
var cache = _getRequireWildcardCache();

var cache = _getRequireWildcardCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}

var newObj = {};

var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_module/Cargo.toml
Expand Up @@ -18,6 +18,7 @@ ahash = "0.7.4"
anyhow = "1.0.41"
bitflags = "1"
indexmap = "1.8.0"
is-macro = "0.2.0"
path-clean = "0.1.0"
pathdiff = "0.2.0"
serde = { version = "1.0.118", features = ["derive"] }
Expand Down