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

babel-plugin-transform-runtime insert code error #6979

Closed
magicdawn opened this Issue Dec 5, 2017 · 4 comments

Comments

Projects
None yet
5 participants
@magicdawn

magicdawn commented Dec 5, 2017

Choose one: is this a bug report or feature request?
Bug report

Input Code

module-sub.js

export default function test(arg) {
  return typeof arg
}

export const a = 1
export const b = 2

module.js

export * from './module-sub'

common.js

var x = typeof arg
module.exports = x

Babel/Babylon Configuration (.babelrc, package.json, cli command)

{
  "presets": [["es2015", { "modules": false }], "stage-2"],
  "plugins": ["transform-runtime"],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": ["istanbul"]
    }
  }
}

and

{
  "presets": ["es2015", "stage-2"],
  "plugins": ["transform-runtime"],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": ["istanbul"]
    }
  }
}

Expected Behavior

when modules not set, default commonjs

module.js become

'use strict';

import _Object$defineProperty from 'babel-runtime/core-js/object/define-property';
import _Object$keys from 'babel-runtime/core-js/object/keys';
Object.defineProperty(exports, "__esModule", {
  value: true
});

var _moduleSub = require('./module-sub');

_Object$keys(_moduleSub).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;

  _Object$defineProperty(exports, key, {
    enumerable: true,
    get: function get() {
      return _moduleSub[key];
    }
  });
});

when modules is set to false

common.js become

import _typeof from "babel-runtime/helpers/typeof";
var x = typeof arg === "undefined" ? "undefined" : _typeof(arg);
module.exports = x;

In a project mixed with commonjs files and ES module files with export *
babel generates import and exports, this will results webpack 2 rename exports to __webpack_exports__, final results exports is not defined

example code generated by webpack with content mixed with exports and import

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

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_babel_runtime_core_js_object_define_property__ = __webpack_require__("liLe");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_babel_runtime_core_js_object_define_property___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_babel_runtime_core_js_object_define_property__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_babel_runtime_core_js_object_keys__ = __webpack_require__("ZLEe");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_babel_runtime_core_js_object_keys___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1_babel_runtime_core_js_object_keys__);

Object.defineProperty(exports, "__esModule", {
    value: true
});

related issues
webpack/webpack#3974
https://www.google.com.hk/search?newwindow=1&safe=strict&ei=I1AmWtK9Doaj8QX86amoCw&q=exports+is+not+defined&oq=exp&gs_l=psy-ab.3.0.0i67k1j0j0i67k1l2j0j0i67k1l4j0.7698247.7698604.0.7699788.3.3.0.0.0.0.137.268.0j2.2.0....0...1c.1.64.psy-ab..1.2.267...0i131k1.0.WC-jJIZgzU8

Your Environment

software version(s)
Babel 6.26.0 (babel-core 6.26.0) babel-cli
Babylon
node v8.6.0
npm 5.5.1
Operating System macOS high Siera
@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Dec 5, 2017

Collaborator

Hey @magicdawn! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Collaborator

babel-bot commented Dec 5, 2017

Hey @magicdawn! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@BuptStEve

This comment has been minimized.

Show comment
Hide comment
@BuptStEve

BuptStEve Jan 9, 2018

@magicdawn

I encounter the same issue, maybe this issue could be helpful~

You can use passPerPreset~

passPerPreset: true will modify how babel traverses through plugins. Instead of a single traversal in which all plugins/presets are merged together, each preset will get their own traversal.

This allows users to have a specific order to how presets/plugins are applied and can help avoid potential collisions between plugins (and probably some known issues).

from https://github.com/babel/babel/blob/master/CHANGELOG.md#a-traversal-per-preset-experimental

{
  "passPerPreset": true,
  "presets": [
    { "plugins": [ "transform-runtime" ] },
    {
      "passPerPreset": false,
      "presets": [ "es2015", "stage-2" ]
    }
  ],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": ["istanbul"]
    }
  }
}

BuptStEve commented Jan 9, 2018

@magicdawn

I encounter the same issue, maybe this issue could be helpful~

You can use passPerPreset~

passPerPreset: true will modify how babel traverses through plugins. Instead of a single traversal in which all plugins/presets are merged together, each preset will get their own traversal.

This allows users to have a specific order to how presets/plugins are applied and can help avoid potential collisions between plugins (and probably some known issues).

from https://github.com/babel/babel/blob/master/CHANGELOG.md#a-traversal-per-preset-experimental

{
  "passPerPreset": true,
  "presets": [
    { "plugins": [ "transform-runtime" ] },
    {
      "passPerPreset": false,
      "presets": [ "es2015", "stage-2" ]
    }
  ],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": ["istanbul"]
    }
  }
}
@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Jan 9, 2018

Member

The fix for this bug is in Babel 7. You can try the beta if you want.

Member

nicolo-ribaudo commented Jan 9, 2018

The fix for this bug is in Babel 7. You can try the beta if you want.

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth Jan 9, 2018

Member

Yup, this is a duplicate of #2877 which has been fixed in Babel 7's beta releases.

Member

loganfsmyth commented Jan 9, 2018

Yup, this is a duplicate of #2877 which has been fixed in Babel 7's beta releases.

@loganfsmyth loganfsmyth closed this Jan 9, 2018

@lock lock bot added the outdated label May 3, 2018

@lock lock bot locked as resolved and limited conversation to collaborators May 3, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.