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

plugin-syntax-dynamic-import didnot re #11204

Closed
1 task
3cp opened this issue Mar 4, 2020 · 3 comments
Closed
1 task

plugin-syntax-dynamic-import didnot re #11204

3cp opened this issue Mar 4, 2020 · 3 comments
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@3cp
Copy link
Contributor

3cp commented Mar 4, 2020

Bug Report

  • I would like to work on a fix!

Current Behavior
Based on comments here in plugin-proposal-dynamic-import:

If you are using Webpack or Rollup and thus don't want
Babel to transpile your imports and exports, you can use
the @babel/plugin-syntax-dynamic-import plugin and let your
bundler handle dynamic imports.

plugin-syntax-dynamic-import should preserve dynamic import statement. But it did not.

Input Code

Demo repo https://github.com/3cp/babel-dynamic-import-issue

With config

{
  "presets": [["@babel/preset-env", {"loose": true}]],
  "plugins": ["@babel/plugin-syntax-dynamic-import"]
}

And source code

import('foo');

Babel compiled it into

"use strict";

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

Promise.resolve().then(function () {
  return _interopRequireWildcard(require('foo'));
});

The result didn't retain dynamic import statement.

Expected behavior/code
plugin-syntax-dynamic-import should help to retain dynamic import statement.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: . babelrc
{
  "presets": [["@babel/preset-env", {"loose": true}]],
  "plugins": ["@babel/plugin-syntax-dynamic-import"]
}

Environment

  System:
    OS: macOS 10.15.3
  Binaries:
    Node: 12.16.0 - ~/.nodenv/versions/12.16.0/bin/node
    Yarn: 1.22.0 - ~/.nodenv/versions/12.16.0/bin/yarn
    npm: 6.14.1 - ~/.nodenv/versions/12.16.0/bin/npm
  npmPackages:
    @babel/cli: ^7.8.4 => 7.8.4 
    @babel/core: ^7.8.6 => 7.8.6 
    @babel/plugin-syntax-dynamic-import: ^7.8.3 => 7.8.3 
    @babel/preset-env: ^7.8.6 => 7.8.6 

Possible Solution
Nil

Additional context/Screenshots
I recall it worked before. I don't remember what time I checked this, might be a year ago.

@babel-bot
Copy link
Collaborator

Hey @3cp! 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."

@nicolo-ribaudo
Copy link
Member

You have to exclude the plugin from preset-env. (Add exclude: ["proposal-dynamic-import"])

It's only automatically excluded when you are using a bundler.

@3cp
Copy link
Contributor Author

3cp commented Mar 4, 2020

Oh, thx you.

Might be a good idea to show preset-env config in doc of https://babeljs.io/docs/en/babel-plugin-syntax-dynamic-import

@3cp 3cp closed this as completed Mar 4, 2020
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 4, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants