2017-08-10 7 views
0

問題:カルマ、WebPACKの、バベル奇妙な輸入行動

私が最初にエクスポートされた関数が定義されていないモジュールをインポートします。 例外として、モジュールが定義されています。

なぜそれが未定義ですか?


SCENARIO:

メインファイル

import { UserClass } from './user'; 
import { query, Client } from 'faunadb'; 
import { FaunaClass } from '../class'; 

console.log('init', typeof UserClass, typeof FaunaClass, typeof query); 

export function initialise (client : Client) { 

    return new Promise((resolve, reject) => { 
    const CLASSES : (typeof FaunaClass)[] = [ 
     UserClass 
    ]; 

    let count = 0; 
    const total = CLASSES.length; 

    console.log('classes', CLASSES); 

    CLASSES.forEach(cl => 
     client.query(query.CreateClass({ name: cl.className })) 
     .then(checkDone) 
     .catch(reject)); 

    function checkDone() { 
     count += 1; 

     if (total === count) { 
     resolve(); 
     } 

    } 

    }) 
    .catch(e => { 
     console.log('on catch', UserClass); 
     console.log(e.message); 
    }); 

} 

あなたはこの関数内でいくつかのコンソールログがある見ることができるように。 karma startの出力は次のようになります。

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'init', 'undefined', 'undefined', 'object' 

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'classes', [undefined] 

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'on catch', function UserClass() { ... } 

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'undefined is not an object (evaluating 'cl.className')' 

PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 SUCCESS (0.933 secs/0.928 secs) 

UserClass.ts:

import { FaunaClass } from '../class'; 

export class UserClass extends FaunaClass { 

    static className = 'users'; 

} 

**コンフィギュレーションファイル:**

karma.conf.js

const webpackConfig = require('./webpack.config'); 
const webpack = require('webpack'); 

module.exports = function(config) { 
    config.set({ 
    basePath: '', 
    frameworks: ['mocha', 'chai', 'sinon'], 

    files: [ 
     'src/**/*.spec.ts' 
    ], 

    preprocessors: { 
     '**/*.ts': ['webpack', 'sourcemap'] 
    }, 

    webpack: { 
     module: webpackConfig.module, 
     resolve: webpackConfig.resolve, 
     devtool: 'inline-source-map' 
    }, 

    // Webpack please don't spam the console when running in karma! 
    webpackServer: { noInfo: true }, 

    reporters: ['progress'], 
    colors: true, 
    autoWatch: true, 
    logLevel: config.LOG_INFO, 
    browsers: ['PhantomJS'], 
    singleRun: false, 
    concurrency: 'Infinity' 
    }); 
}; 

WebPACKの設定:

var path = require('path'); 

module.exports = { 
    entry: './handler.ts', 
    target: 'node', 
    module: { 
    loaders: [ 
     { 
     test: /\.tsx?$/, 
     loaders: ['babel-loader', 'ts-loader'], 
     exclude: [/node_modules/] 
     }, 
     { test: /\.json$/, loader: 'json-loader' }, 
    ] 
    }, 
    resolve: { 
    extensions: ['.ts', '.js', '.tsx', '.jsx'] 
    }, 
    output: { 
    libraryTarget: 'commonjs', 
    path: path.join(__dirname, '.webpack'), 
    filename: 'handler.js' 
    } 
}; 

だから、あなたが見ることができるようUserClassのロギングが開始時に定義されていない、と例外が約束の中にスローされたとき、クラスが定義されてしまいます。

UserClassがエクスポートされる前に何らかの理由でコードが実行されていて、その瞬間に未定義になっているような気がします。

設定ファイルが完全に壊れています。

答えて

0

この理由は、循環インポートループが原因でした。 コードの実行元を変更するだけでした。 (または輸入の順)