question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

core js modules insertion breaks AMD exports

See original GitHub issue

I’m submitting a bug report

Webpack Version: 4.16.0

Babel Core Version: 7.0.0-rc.1

Babel Loader Version: 8.0.0-beta.4

Please tell us about your environment: macOS 10.13.6, node v8.9.4, npm 6.3.0

Current behavior: When babel found a method that probably need a polyfill it is inserting

/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var core_js_modules_es6_regexp_replace__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! core-js/modules/es6.regexp.replace */ "../node_modules/core-js/modules/es6.regexp.replace.js");
/* harmony import */ var core_js_modules_es6_regexp_replace__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(core_js_modules_es6_regexp_replace__WEBPACK_IMPORTED_MODULE_0__);

define(function (require, exports) {
  function getUppercase(featureName) {
    return featureName.replace(/-/g, '_');
  }

  exports.foo = function () {
    getUppercase();
  };

  exports.QUX = 'QUX';
});

/***/ }),

That is probably correct. But AMD code that follows it stops exporting it members.

Removing modules: false from .babelrc seems fix this issue, but is not desired, because of tree-shaking.

Console shows warnings:

WARNING in ./js/index.js 2:4-7
"export 'QUX' (imported as 'BAZ') was not found in './test'
 @ multi ./js/index.js

WARNING in ./js/index.js 2:0-3
"export 'foo' (imported as 'bar') was not found in './test'
 @ multi ./js/index.js

Expected/desired behavior: Without polyfills inserting, module exported correctly

/***/ (function(module, exports, __webpack_require__) {

var __WEBPACK_AMD_DEFINE_RESULT__;!(__WEBPACK_AMD_DEFINE_RESULT__ = (function (require, exports) {
  function getUppercase(featureName) {// return featureName.replace(/-/g, '_');
  }

  exports.foo = function () {
    getUppercase();
  };

  exports.QUX = 'QUX';
}).call(exports, __webpack_require__, exports, module),
				__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));

/***/ }),
  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem along with a gist/jsbin of your webpack configuration.
{
    "presets": [
        ["@babel/preset-env", {
            "modules": false,
            "useBuiltIns": "usage"
        }],
        "@babel/preset-react"
    ]
}
define(function(require, exports) {
    function getUppercase(featureName) {
        return featureName.replace(/-/g, '_');
    }


    exports.foo = function() {
        getUppercase();
    };

    exports.QUX = 'QUX';
});
  • What is the expected behavior? Module exports it members regardless of polyfills inserting

  • What is the motivation / use case for changing the behavior? Using webpack with multiple legacy modules

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
loganfsmythcommented, Aug 12, 2018

Oh right, for that you need to set sourceType: "unambiguous" so that Babel doesn’t just assume the file is an ES module.

0reactions
ogonkovcommented, Aug 13, 2018

Looks like sourceType fix AMD build

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Error: Can't resolve 'core-js/es6'
I found possible answer. You have core-js version 3.0, and this version doesn't have separate folders ...
Read more >
Understanding (all) JavaScript module formats and tools
By default, each .js file is a CommonJS module. A module variable and an exports variable are provided for a module (a file)...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
import declarations can only be present in modules, and only at the ... the export statement, this inserts myExport into the current scope....
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Modules are defined using a variety of import and export statements. ... Core modules provide named exports of their public API. A default...
Read more >
Upgrade to Babel 7 (API)
Support for Node.js 0.10 and 0.12 has been dropped as both of this versions ... Dropped use of add-module-exports plugin on Babel packages....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found