JSPM

  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q67783F
  • License MIT

es module polyfill

Package Exports

  • virtual-es-module
  • virtual-es-module/dist/virtual-esm.esm-bundler.js
  • virtual-es-module/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (virtual-es-module) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Virtual es module

NPM version

Based on the es module implemented by acorn, easy to operate in the sandbox.

Demo

<!DOCTYPE html>
<html lang="en">
<body>
  <script src="dist/virtual-esm.umd.js"></script>
  <script type="virtual-module">
    import * as _ from 'https://unpkg.com/lodash-es';
    console.log(_);

    import * as m from './m.js';
    console.log(m);

    import('./m.js').then(mm => {
      console.log(m === mm); // true
    })
  </script>
</body>
</html>

API

Import by url

import { Runtime } from 'virtual-es-module';
const runtime = new Runtime();

const module = await runtime.importByUrl('./a.mjs');
console.log(module);

Import by code

import { Runtime } from 'virtual-es-module';
const runtime = new Runtime();

const module = await runtime.importByCode(`
  import * as m from './a.mjs';
  export default 1;
`);
console.log(module);

Not support

The code executed by eval cannot be converted by this scheme.

import m from './m.js';

eval('console.log(m);') // throw error 'm is not defined'