从 CommonJS 动态导入 ES 模块

Import ES Modules from CommonJS dynamically

是否可以从 CommonJS 动态导入 ES 模块,而不必将文件扩展名更改为 mjs,并且如果可能的话使用旧的 Node 版本(早于 V13)?我正在创建一个 CLI 库,它将动态地从用户项目导入文件,以根据这些文件自动生成一些代码。

// bin.ts
// This file will be transpiled to CommonJS
const loadResourceFile = async (url: string) => {
  const resource = await import(url);
  return resource.default;
}
...
// rollup.config.js
import typescript from 'rollup-plugin-typescript2';
import pkg from './package.json';

const commonOutputOptions = {
  banner: '#!/usr/bin/env node',
  preferConst: true,
  sourcemap: true,
};

export default {
  input: 'src/bin.ts',
  output: [
    {
      ...commonOutputOptions,
      file: pkg.main,
      format: 'cjs',
    },
    {
      ...commonOutputOptions,
      file: pkg.module,
      format: 'esm',
    },
  ],
  external: [...Object.keys(pkg.dependencies || {})],
  plugins: [typescript()],
  inlineDynamicImports: true,
};

// resource.js
// This file will be a ES module
import a from './a';

export default {
   a,
   b: 'y',
}

提前致谢!

有可能,使用 vm (Specifically this) and fs 虽然我建议不要走这条路,因为如果你不小心,它很快就会变成无法维护的混乱。

Since your aim is to also support older nodejs versions, I would suggest you make two separate bundles, this way you do not mix CommonJS and ES modules.