汇总动态模块解析
Rollup dynamic module resolution
我正在尝试使用 Rollup 将一些 JS 捆绑到一个文件中。然而,我想要 import
的其中一个文件的路径仅在编译时已知,因此我需要告诉 Rollup 如何找到它。
这是我的主文件:
import * as commands from 'command-loader';
console.log(commands);
您可以看到它正在从 command-loader
导入所有内容。文件的名称和内容并不重要,我只需要将其内容捆绑到主文件中即可。该文件看起来像:
export function exampleCommand() {
console.log('Running command...');
}
Rollup 错误很明显,它不知道如何找到 command-loader
: 'command-loader' is imported by ../index.js, but could not be resolved – treating it as an external dependency
.
我知道我可能只读取命令文件的内容并将其添加到主文件中,但这感觉很尴尬,因为我必须从命令文件中删除 export
并定义一个 "commands" 对象,这可能违背了 Rollup 的目的。
我试过使用 rollup-plugin-node-resolve
告诉 Rollup 如何找到 command-loader
,但它似乎没有用(当然,我不太了解这个插件)。
const rollup = require('rollup');
const resolve = require('rollup-plugin-node-resolve');
await rollup.rollup({
input: mainFilePath,
plugins: [
resolve({
'command-loader': dynamicFilePath
})
]
});
我也尝试过使用 rollup-plugin-bundle-imports
无济于事。
const rollup = require('rollup');
const { bundleImports } = require('rollup-plugin-bundle-imports');
await rollup.rollup({
input: mainFilePath,
plugins: [
bundleImports({
include: [dynamicFilePath],
importAs: 'command-loader',
})
]
});
也许我采取了错误的方法。如果有更好的方法在编译时动态 import
文件,我很乐意了解它。
A Rollup plugin for defining aliases when bundling packages.
这是我最终使用的代码:
const rollup = require('rollup');
const alias = require('@rollup/plugin-alias');
await rollup.rollup({
input: mainFilePath,
plugins: [
alias({
entries: {
'command-loader': dynamicFilePath
}
})
]
});
结合 CJS 输出格式,解析上述示例代码并产生:
'use strict';
function exampleCommand() {
console.log('Running command...');
}
var commands = /*#__PURE__*/Object.freeze({
__proto__: null,
exampleCommand: exampleCommand
});
console.log(commands);
成功!
我正在尝试使用 Rollup 将一些 JS 捆绑到一个文件中。然而,我想要 import
的其中一个文件的路径仅在编译时已知,因此我需要告诉 Rollup 如何找到它。
这是我的主文件:
import * as commands from 'command-loader';
console.log(commands);
您可以看到它正在从 command-loader
导入所有内容。文件的名称和内容并不重要,我只需要将其内容捆绑到主文件中即可。该文件看起来像:
export function exampleCommand() {
console.log('Running command...');
}
Rollup 错误很明显,它不知道如何找到 command-loader
: 'command-loader' is imported by ../index.js, but could not be resolved – treating it as an external dependency
.
我知道我可能只读取命令文件的内容并将其添加到主文件中,但这感觉很尴尬,因为我必须从命令文件中删除 export
并定义一个 "commands" 对象,这可能违背了 Rollup 的目的。
我试过使用 rollup-plugin-node-resolve
告诉 Rollup 如何找到 command-loader
,但它似乎没有用(当然,我不太了解这个插件)。
const rollup = require('rollup');
const resolve = require('rollup-plugin-node-resolve');
await rollup.rollup({
input: mainFilePath,
plugins: [
resolve({
'command-loader': dynamicFilePath
})
]
});
我也尝试过使用 rollup-plugin-bundle-imports
无济于事。
const rollup = require('rollup');
const { bundleImports } = require('rollup-plugin-bundle-imports');
await rollup.rollup({
input: mainFilePath,
plugins: [
bundleImports({
include: [dynamicFilePath],
importAs: 'command-loader',
})
]
});
也许我采取了错误的方法。如果有更好的方法在编译时动态 import
文件,我很乐意了解它。
A Rollup plugin for defining aliases when bundling packages.
这是我最终使用的代码:
const rollup = require('rollup');
const alias = require('@rollup/plugin-alias');
await rollup.rollup({
input: mainFilePath,
plugins: [
alias({
entries: {
'command-loader': dynamicFilePath
}
})
]
});
结合 CJS 输出格式,解析上述示例代码并产生:
'use strict';
function exampleCommand() {
console.log('Running command...');
}
var commands = /*#__PURE__*/Object.freeze({
__proto__: null,
exampleCommand: exampleCommand
});
console.log(commands);
成功!