config.ts
:
export const data = 'Config';
process.ts
:
import { data } from "./config.ts";
const calculated = data + ' is used.'
export {calculated}
main.ts
:
import { calculated } from "./process.ts";
console.log(calculated)
> deno run main.ts
Config is used.
现在,我希望能够通过命令行参数选择配置。那就是:
> deno run main.ts config1
Config1 is used.
> deno run main.ts config2
Config2 is used.
> deno run main.ts config3
Config3 is used.
这可能吗?
为了解决静态
import
和 Uncaught ReferenceError: Cannot access 'configToUse' before initialization
,我使用 动态导入 并将所有内容包装在函数内。代码变为:
process.ts
:
async function calculate(configToUse) {
const pathToConfig = './' + configToUse + '.ts'
const config = await import(pathToConfig)
return config.data + ' is used.'
}
export {calculate}
main.ts
:
import { calculate } from "./process.ts";
const configToUse = Deno.args[0]
calculate(configToUse).then((result) => {
console.log(result)
})