`module` undefined when using `:module-type :commonjs`

Description

The Google Closure Compiler support for CommonJS modules rewrites `exports` and `module.exports`, but not `module`. Many libraries try to detect the module type (CommonJS) by checking the type of `module`, e.g. this is taken from D3.

```
typeof exports === 'object' && typeof module !== 'undefined'
```

This becomes

```
goog.provide('module$resource$d3')
typeof module$resource$d3 === 'object' && typeof module !== 'undefined'
```

Because `module` is undefined this fails, and nothing gets exported.

This seems like something Google Closure should address.

Alternatives would include injecting some code that defines `module` (`var module={}`) or munging `typeof module` to `"object"`.

Environment

None

Activity

Show:
Arne Brasseur
September 14, 2016, 2:32 PM

Seems this exact case was already documented on Maria Geller's blog: http://mneise.github.io/posts/2015-07-08-week-6.html

Arne Brasseur
September 14, 2016, 3:04 PM

Did some more digging, the issue is that thanks to http://dev.clojure.org/jira/browse/CLJS-1312 Closure Compiler tries to deal with UMD syntax, but there's no single definition of what UMD really looks like. Two popular tools (rollup and webpack) generate code that is not correctly recognized. This is what rollup generates

This is what webpack generates

This will require changes to ProcessCommonJSModulesTest, similar to https://github.com/google/closure-compiler/commit/aa0a99cf380b05b2185156735d023b6fa78ec4ac

Juho Teperi
August 4, 2017, 10:41 PM

I'm working on improving UMD wrapper support on Closure (https://github.com/google/closure-compiler/pull/2597), and I think the d3 wrapper is in fact the same as Leaflet wrapper, which I have already made some progress with.

Juho Teperi
February 10, 2018, 10:23 PM

D3 from NPM still doesn't work due to https://github.com/google/closure-compiler/issues/2005, but the UMD module as foreign-lib with :module-type :commonjs (like in the first comment) works with the latest changes (updated Closure with better UMD wrapper detection).

Juho Teperi
February 10, 2018, 10:23 PM

Assignee

Juho Teperi

Reporter

Arne Brasseur

Labels

None

Approval

None

Patch

None

Priority

Major
Configure