Rollup.js保持导入不变

问题描述

我的输入文件如下:

import * as chalk from 'chalk'

const chalkInstance = new chalk.Instance({
  level: 1
})

我的输出文件如下:

import { Instance } from 'chalk';

const chalkInstance = new Instance({
    level: 1
});

问题是粉笔是commonjs模块,并且我希望输出是es模块,因此当我执行文件时,出现以下错误The requested module 'chalk' is expected to be of type Commonjs,which does not support named exports等。有没有办法防止汇总从更改import * as something导入?即使禁用了树状握手,问题也不会消失。 预先谢谢你!

解决方法

import保留为键入内容将无济于事-您需要执行import chalk from 'chalk',因为CommonJS模块仅具有默认导出。如果您保留导入,您仍会收到错误,只是一个不同的错误:

const chalkInstance = new chalk.Instance({
                      ^

TypeError: chalk.Instance is not a constructor