fix(es/modules): Use an indirect call for a tagged template (#5382)

This commit is contained in:
magic-akari 2022-08-04 12:59:42 +08:00 committed by GitHub
parent de1332088a
commit cdb6164937
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 89 additions and 3 deletions

View File

@ -7,8 +7,8 @@ Object.defineProperty(exports, "Button", {
get: ()=>Button
});
const _linaria = require("linaria");
const Button = _linaria.css`
const Button = (0, _linaria.css)`
color: red;
`;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL2lucHV0L2luZGV4LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IGNzcyB9IGZyb20gXCJsaW5hcmlhXCI7XG5cbmV4cG9ydCBjb25zdCBCdXR0b24gPSBjc3NgXG4gICAgY29sb3I6IHJlZDtcbmA7XG4iXSwibmFtZXMiOlsiQnV0dG9uIiwiY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTs7OzsrQkFFYUEsUUFBTTs7YUFBTkEsTUFBTTs7eUJBRkMsU0FBUztBQUV0QixNQUFNQSxNQUFNLEdBQUdDLFFBQUcsSUFBQSxDQUFDOztBQUUxQixDQUFDLEFBQUMifQ==
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL2lucHV0L2luZGV4LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IGNzcyB9IGZyb20gXCJsaW5hcmlhXCI7XG5cbmV4cG9ydCBjb25zdCBCdXR0b24gPSBjc3NgXG4gICAgY29sb3I6IHJlZDtcbmA7XG4iXSwibmFtZXMiOlsiQnV0dG9uIiwiY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTs7OzsrQkFFYUEsUUFBTTs7YUFBTkEsTUFBTTs7eUJBRkMsU0FBUztBQUV0QixNQUFNQSxNQUFNLEdBQUdDLElBQUFBLFFBQUcsSUFBQSxDQUFBLENBQUM7O0FBRTFCLENBQUMsQUFBQyJ9

View File

@ -98,6 +98,21 @@ impl VisitMut for ModuleRefRewriter {
}
}
fn visit_mut_tagged_tpl(&mut self, n: &mut TaggedTpl) {
let is_indirect = n
.tag
.as_ident()
.and_then(|ident| self.import_map.get(&ident.to_id()))
.map(|(_, prop)| prop.is_some())
.unwrap_or_default();
n.visit_mut_children_with(self);
if is_indirect {
*n = n.take().into_indirect()
}
}
fn visit_mut_function(&mut self, n: &mut Function) {
self.visit_mut_with_non_global_this(n);
}

View File

@ -0,0 +1,8 @@
import foo from "foo";
import { bar } from "bar";
foo("foo");
foo`foo`;
bar("bar");
bar`bar`;

View File

@ -0,0 +1,16 @@
define([
"require",
"exports",
"foo",
"bar"
], function(require, exports, _foo, _bar) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
_foo = /*#__PURE__*/ _interopRequireDefault(_foo);
(0, _foo.default)("foo");
(0, _foo.default)`foo`;
(0, _bar.bar)("bar");
(0, _bar.bar)`bar`;
});

View File

@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _foo = /*#__PURE__*/ _interopRequireDefault(require("foo"));
const _bar = require("bar");
(0, _foo.default)("foo");
(0, _foo.default)`foo`;
(0, _bar.bar)("bar");
(0, _bar.bar)`bar`;

View File

@ -0,0 +1,19 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("foo"), require("bar"));
else if (typeof define === "function" && define.amd) define([
"exports",
"foo",
"bar"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {}, global.foo, global.bar);
})(this, function(exports, _foo, _bar) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
_foo = /*#__PURE__*/ _interopRequireDefault(_foo);
(0, _foo.default)("foo");
(0, _foo.default)`foo`;
(0, _bar.bar)("bar");
(0, _bar.bar)`bar`;
});

View File

@ -1,7 +1,7 @@
use std::iter;
use swc_atoms::js_word;
use swc_common::{Span, Spanned, DUMMY_SP};
use swc_common::{util::take::Take, Span, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
/// Extension methods for [Expr].
@ -326,6 +326,24 @@ impl IntoIndirectCall for Callee {
}
}
impl IntoIndirectCall for TaggedTpl {
type Item = TaggedTpl;
#[cfg_attr(not(debug_assertions), inline(always))]
fn into_indirect(mut self) -> Self {
Self {
tag: Box::new(
SeqExpr {
span: DUMMY_SP,
exprs: vec![0f64.into(), self.tag.take()],
}
.into(),
),
..self
}
}
}
pub trait FunctionFactory: Into<Function> {
#[cfg_attr(not(debug_assertions), inline(always))]
fn into_fn_expr(self, ident: Option<Ident>) -> FnExpr {