2016-07-25 19 views
1

私はいくつかの外部(googleのjsは1つ)を使用していますが、ファイルの先頭に'use strict'というステートメントがあります。私はwebpackのresolve configパラメータでインポートを偽装することができますが、ソースの一部ではないモジュールだけをインポートすることができます。グローバルが宣言されていないかどうかをwebpackが知る方法はありますか?

「インポートされた」などのグローバルではなく、webpackプロセスでエラーが発生します。

例として:

var foo = bar 

私はバー(宣言またはインポートされませんが)'use strict'とエラーになります期待したいです。

私はあなたがESLintno-undef ruleの助けを借りて、未定義の変数を持つ建物のパッケージを防ぐことができますWebPACKの1.13.1

+1

あなたは無undefをルールにESLintを試すことができます。http://eslint.org/docs/rules/no-undef – Everettss

+0

それはあなたが求めるものをESLintで達成するのは簡単ですが、「strict''条件を使用せずに ' (ESLintルールは '' use strict''を指定しても指定できません)。 – Everettss

+0

パーフェクト。私はすでにeslintを使っていましたが、何らかの理由でeslintルールをチェックすることは考えていませんでした。 – chrisp

答えて

1

を使用しています。

あなたのwebpack.config.jsあなたはこれのconfigsが与えるESLint

module.exports = { 
    loaders: [ 
     { 
      test: /\.js$/, 
      loader: 'eslint-loader', 
      exclude: /(node_modules|bower_components)/ 
     }, 
     // ... 
    ], 
    eslint: { 
     failOnError: true, // block build on non passed linting 
     configFile: '.eslintrc', // path to your .eslintrc 
     formatter: require('eslint-friendly-formatter') // better error report 
    }, 
    // ... 
}; 

とリンティングをサポートするために、この行を追加する必要がありますで.eslintrc

{ 
    "env": { 
     "browser": true, 
     "node": true, 
     "es6": true 
    }, 
    "parserOptions": { 
     "ecmaVersion": 6, 
     "sourceType": "module" 
    }, 
    "rules": { 
     "no-undef": 2 // <-- ERROR on not defined variables 
    } 
} 

を設定する必要があります。このソリューション

npm install eslint eslint-loader eslint-friendly-formatter --save-dev 

のパッケージを準備この種のエラー

ERROR in ./src/example.js 
Module build failed: Error: Module failed because of a eslint error. 

    ✘ http://eslint.org/docs/rules/no-undef 'bar' is not defined 
    /yourpath/src/example.js:2:11 
    var foo = bar; 
      ^


✘ 1 problem (1 error, 0 warnings) 
関連する問題