2016-07-25 114 views
1

我正在使用一些外部(谷歌的js是一个),我有'use strict'声明在文件的顶部。我知道我可以使用webpack的resolve config参数伪造导入,但只允许导入不属于源的一部分的模块。有没有办法让webpack知道全局是否没有声明?

我想要一个没有“导入”或类似的全局,当webpack处理时会抛出一个错误。

举个例子:

var foo = bar 

我期望吧(未声明或进口)将是一个错误与'use strict'

我使用的WebPack 1.13.1

+1

你可以尝试用ESLint没有民主基金的规则:http://eslint.org/docs/rules/no-undef – Everettss

+0

可以很容易地实现与ESLint你问什么,但不''使用strict''条件(你不能指定ESLint规则取决于是否使用'strict strict')。 – Everettss

+0

完美。我已经在使用eslint,但由于某种原因,从来没有想过要检查eslint规则。 – chrisp

回答

1

您可以防止用的ESLintno-undef rule帮助不确定的变量构建包。

准备包该解决方案

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

您应该配置.eslintrc

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

在你webpack.config.js您必须添加此行来支持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 
    }, 
    // ... 
}; 

这CONFIGS给你这种错误

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) 
相关问题