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

fix(es/module): Do not determine module name for modules without exports in UMD #7718

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions crates/swc_ecma_transforms_module/src/umd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ where
fn visit_mut_module(&mut self, module: &mut Module) {
let import_interop = self.config.config.import_interop();

let filename = self.cm.span_to_filename(module.span);
let exported_name = self.config.determine_export_name(filename);

let module_items = &mut module.body;

let mut strip = ModuleDeclStrip::new(self.const_var_kind);
Expand Down Expand Up @@ -170,7 +167,7 @@ where
// Emit
// ====================

let (adapter_fn_expr, factory_params) = self.adapter(exported_name, is_export_assign);
let (adapter_fn_expr, factory_params) = self.adapter(module.span, is_export_assign);

let factory_fn_expr: Expr = Function {
params: factory_params,
Expand Down Expand Up @@ -341,7 +338,7 @@ where
/// });
/// ```
/// Return: adapter expr and factory params
fn adapter(&mut self, exported_name: Ident, is_export_assign: bool) -> (FnExpr, Vec<Param>) {
fn adapter(&mut self, module_span: Span, is_export_assign: bool) -> (FnExpr, Vec<Param>) {
macro_rules! js_typeof {
($test:expr =>! $type:expr) => {
Expr::Unary(UnaryExpr {
Expand Down Expand Up @@ -375,7 +372,6 @@ where
let factory = private_ident!("factory");

let module_exports = module.clone().make_member(quote_ident!("exports"));
let global_lib = global.clone().make_member(exported_name);
let define_amd = define.clone().make_member(quote_ident!("amd"));

let mut cjs_args = vec![];
Expand All @@ -385,6 +381,10 @@ where
let mut factory_params = vec![];

if !is_export_assign && self.exports.is_some() {
let filename = self.cm.span_to_filename(module_span);
let exported_name = self.config.determine_export_name(filename);
let global_lib = global.clone().make_member(exported_name);

cjs_args.push(quote_ident!("exports").as_arg());
amd_dep_list.push(Some(quote_str!("exports").as_arg()));
browser_args.push(
Expand Down
17 changes: 17 additions & 0 deletions node-swc/__tests__/module_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,20 @@ it("should work with amd and external helpers", () => {
expect(out.code).toContain(`_class_call_check._(this, Foo);`);
expect(out.code).toContain(`_inherits._(Bar, Foo);`);
});

it('should not require filename if no exports in umd', () => {
const code = `console.log('test')`;

const out = swc.transformSync(code, {
jsc: {
parser: {
syntax: 'ecmascript',
},
},
module: {
type: 'umd',
},
isModule: true,
});
expect(out.code).toContain(`function(global`);
});