有没有一种方法可以让webpack知道是否没有声明全局
Is there a way for webpack to know if a global is not declared?
我正在使用一些外部语言(谷歌的js就是其中之一),并且我在文件的顶部有'use strict'
语句。我知道我可以用webpack的resolf-config参数伪造导入,但这只允许我导入不属于源代码的模块。
我想要一个不是"导入"的全局,或者类似的,在webpack处理时会抛出错误。
例如:
var foo = bar
我认为bar(未声明或导入)将是'use strict'
的错误。
我使用的是webpack 1.13.1
您可以在ESLint和no-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 的linting
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
},
// ...
};
这种配置会给你这种错误
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)
相关文章:
- 访问布局信息是否也会导致浏览器重排
- 如何使用url加载程序在webpack中导入多个图像
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 如何检测是否有溢出
- jQuery中是否内置了任何字符串格式化函数
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 测试索引值是否等于某个数字的倍数
- Fancybox是否将Click事件静音
- Webpack:是否可以在编译时评估javascript表达式
- 是否可以为聚合物组件实现Webpack HMR
- 是否可以在webpack构建期间记录进度?
- Webpack DllPlugin 中是否有任何“publicPath”选项或类似选项
- Webpack.是否可以进行依赖于入口点的导入?
- 是否有任何javascript模板库可以与webpack's i18n插件一起工作
- 我可以检测我的脚本是否正在被Webpack处理
- 有没有一种方法可以让webpack知道是否没有声明全局
- 是否可以像使用Browserify一样使用Webpack进行自定义导入
- 是否可以通过NPM和Webpack安装Reactrb和Opal
- 是否可以在webpack中禁用某些文件的源映射?
- 是否可以用webpack加载twilio.js库?