简体   繁体   English

使用ES6和let / const进行export / import和Karma / webpack-顶部导出

[英]Using ES6 and let/const with export / import and Karma/webpack - export at the top

export { test };

const test = (str) => {
    return str;
};

import { test } from './func';

describe('func', () => {
    describe('func', () => {
        it('should return the same string', () => {
            expect(test('hello world')).to.equal('hello world');
        });
    });
});

test-function is undefined because of hoisting, I suppose. 我想,由于吊装,测试功能未定义。 Because if I do: 因为如果我这样做:

const test = (str) => {
    return str;
};

export { test };

The test works. 测试工作正常。

But, I would like to keep my exports in the top of the file for easy reference. 但是,我想将导出内容保留在文件的顶部,以方便参考。

Any way to achieve that? 有什么办法做到这一点?

My karma.conf.js: 我的karma.conf.js:

const webpackConfig = require('./webpack.config');
const fileGlob = 'src/**/*.test.js';

module.exports = (config) => {
    config.set({
        basePath: '',
        frameworks: ['mocha', 'chai'],
        files: [fileGlob],
        preprocessors: {
            [fileGlob]: ['webpack']
        },
        webpack: webpackConfig,
        webpackMiddleware: {noInfo: true},
        reporters: ['progress', 'mocha'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: false,
        browsers: ['Firefox'],
        singleRun: true,
        concurrency: Infinity,
    });
};

And relevant parts of package.json: 和package.json的相关部分:

"devDependencies": {
    "webpack": "^3.5.5",

    "babel-core": "^6.26.0",

    "babel-loader": "^7.1.2",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-preset-es2015": "^6.24.1",

    "chai": "^4.1.1",
    "mocha": "^3.5.0",
    "karma": "^1.7.0",
    "karma-chai": "^0.1.0",
    "karma-mocha": "^1.3.0",

    "karma-webpack": "^2.0.4",

  },

ES module import reflects the state of module export. ES模块导入反映了模块导出的状态。 Even though const decaration is not hoisted, it is in temporal dead zone when export { test } is evaluated, but when the module is imported export already reflects test actual value. 即使不进行const decaration,在评估export { test }时它也处于时间盲区,但是在导入模块时,export已经反映了test实际值。

The problem likely results from incorrect behaviour caused by module transpilation. 该问题很可能是由模块移植引起的不正确行为引起的。 Babel doesn't implement module exports correctly , this results in undefined export. Babel 没有正确实现模块导出 ,这导致undefined导出。

As it can be seen here (available in browsers that support ES modules, ie latest Chrome), module export works as intended natively. 正如可以看到这里 (在支持ES模块,即最新的Chrome浏览器,可用),作为本地打算模块的出口工作。

TypeScript handles exports as intended , too. TypeScript也按预期处理导出

To make code compatible with existing implementations, it should be: 为了使代码与现有的实现兼容,它应该是:

export const test = (str) => {
    return str;
};

Or: 要么:

const test = (str) => {
    return str;
};

export { test };

Both are conventional ways to do exports, particularly because of this issue. 两者都是常规的出口方式,尤其是因为这个问题。 Exports at the end of the module conform to coding habits that result from the use of CommonJS modules. 模块末尾的导出符合使用CommonJS模块导致的编码习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM