You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

babel-plugin-transform-dynamic-imports-to-static-imports

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-dynamic-imports-to-static-imports

transform dynamic import to static imports

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
705
-33.36%
Maintainers
2
Weekly downloads
 
Created
Source

babel-plugin-transform-dynamic-imports-to-static-imports

npm build

A transform to translate dynamic imports to static imports

Installation:

npm install babel-plugin-transform-dynamic-imports-to-static-imports

Usage:

with configuration file:

{
  "plugins": ["babel-plugin-transform-dynamic-imports-to-static-imports"]
}

Via CLI

babel --plugins babel-plugin-transform-dynamic-imports-to-static-imports script.js

via node API

require("@babel/core").transform("code", {
  plugins: ["babel-plugin-transform-dynamic-imports-to-static-imports"],
});

Example of Transform

input:

import * as zero from "./zero";
import "./one";

async function test() {
  import("./two");
  await import("./three");
  import("./four")
    .then((test) => {
      /* do something */
    })
    .then();
  await import("./five").then((test) => {
    /* do something */
  });
}

output:

import * as $$1 from "./five";
import * as $$0 from "./four";
import "./three";
import "./two";
import * as zero from "./zero";
import "./one";

async function test() {
  Promise.resolve($$0)
    .then((test) => {
      /* do something */
    })
    .then();
  await Promise.resolve($$1).then((test) => {
    /* do something */
  });
}

for more examples see test.js

Caveat

dynamic imports with dynamic paths do not have a static equivalent.

async function test() {
  import(`dynamic${test}`);
  await import(`dynamic${test}`);
  import(`dynamic${test}`)
    .then((test) => {
      /* do something */
    })
    .then();
  await import(`dynamic${test}`).then((test) => {
    /* do something */
  });
}

will output:

async function test() {
  import(`dynamic${test}`);
  await import(`dynamic${test}`);
  import(`dynamic${test}`)
    .then((test) => {
      /* do something */
    })
    .then();
  await import(`dynamic${test}`).then((test) => {
    /* do something */
  });
}

for more examples see test.js

Keywords

babel-plugin

FAQs

Package last updated on 31 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts