Skip to content

Commit

Permalink
fix(es/module): Do not determine module name for modules without expo…
Browse files Browse the repository at this point in the history
…rts in UMD (swc-project#7718)

**Related issue:**

 - Closes swc-project#6263.
  • Loading branch information
magic-akari authored and kdy1 committed Aug 15, 2023
1 parent e79b485 commit 2998ed9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
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`);
});

0 comments on commit 2998ed9

Please sign in to comment.