在Node和Webpack之间共享Babelified ECMAScript模块

时间:2018-09-07 11:02:07

标签: javascript webpack babel

我正在使用Babel编译我的JavaScript代码,并且为webpack(基础)和Node(覆盖)设置了单独的配置:

module.exports = {
    presets: [
        [
            '@babel/env',
            {
                modules: false,
                useBuiltIns: 'usage',
            },
        ],
        '@babel/react',
        '@babel/flow',
    ],
    overrides: [
        {
            include: ['src/scripts'],
            presets: [
                [
                    '@babel/env',
                    {
                        modules: 'commonjs',
                        useBuiltIns: 'usage',
                        targets: {
                            node: '6',
                        },
                    },
                ],
            ],
        },
    ],
};

所以scripts是我的Node代码,其余的是客户端(webpack)。我无法从任何其他文件夹中的模块导入scripts文件夹内的任何内容。

如果我使用默认导出:

export default function fn() {}

并像这样导入它:

import fn from '../scripts/fn'

我看到此错误:

"export 'default' (imported as 'fn') was not found in '../scripts/fn'

如果我使用命名导出:

export function fn() {}

并像这样导入它:

import {fn} from '../scripts/fn'

我看到此错误:

"'../scripts/fn' does not contain an export named 'fn'.

0 个答案:

没有答案