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

commonjs: did not wrap minimized webpack output lib as format commonjs2 #935

Closed
githoniel opened this issue Jul 13, 2021 · 1 comment
Closed

Comments

@githoniel
Copy link
Contributor

githoniel commented Jul 13, 2021

Expected Behavior

webpack-commonjs2.min.js is minimize of webpack-commonjs2.js in the reproduction repo. so they should both be wrapped by getDefaultExportFromCjs

Actual Behavior

webpack-commonjs2.min.js is not wrap by getDefaultExportFromCjs and will throw error VM214:122 Uncaught TypeError: webpackCommonjs2_min is not a constructor when execute

Additional Information

webpackBootstrap in lib: commonjs2 is below

/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = function(exports) {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};

after minimization it will be

r.r = function (e) {
    "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e, Symbol.toStringTag, {
      value: "Module"
    }), Object.defineProperty(e, "__esModule", {
      value: !0
    })
  }

the problem is Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); will get true before minimization but false after minimization in

export function isDefineCompiledEsm(node) {
const definedProperty =
getDefinePropertyCallName(node, 'exports') || getDefinePropertyCallName(node, 'module.exports');
if (definedProperty && definedProperty.key === KEY_COMPILED_ESM) {
return isTruthy(definedProperty.value);
}
return false;
}

when transformCommonjs because of target is rename from exports to e

maybe we should revert #537 or just loose it by to not check targetName === 'module'

@githoniel githoniel changed the title commonjs: did not wrap webpack output lib as commonjs2 commonjs: did not wrap minimized webpack output lib as format commonjs2 Jul 13, 2021
@githoniel
Copy link
Contributor Author

webpack5 has change output like

/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it uses a non-standard name for the exports (exports).
(() => {
var exports = __webpack_exports__;

Object.defineProperty(exports, "__esModule", ({ value: true }));
var A = /** @class */ (function () {
    function A() {
        console.log('a');
    }
    return A;
}());
exports.default = A;

})();

module.exports.EventEmitter = __webpack_exports__;
/******/ })()
;

still face the same problem

githoniel added a commit to gem-mine/plugins that referenced this issue Jul 19, 2021
guybedford pushed a commit that referenced this issue Jul 24, 2021
#942)

* fix(commonjs): convert module.exports with `__esModule` property(#939)

* test(commonjs): add test sample for cjs with esm-property

* feat(commonjs): defaultIsModuleExports support module.exports

* fix(commonjs): do not check right node in module.exports reassgin when defaultIsModuleExports = false(#935)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant