从 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',
}
提前致谢!
是否可以从 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',
}
提前致谢!