是否可以安全忽略此webpack警告?

时间:2018-11-08 10:01:41

标签: webpack babel babel-loader

我在webpack中发现了一个奇怪的警告。使用process.env解析babel-loader时。 TypeError: result.setExpression is not a function中发生了警告(webpack/lib/Parser.js)。我正在做一个大项目,希望我的核心超级坚实。那么,由于webpack明确捕获了此警告并注释了“忽略错误”,因此忽略此警告是否安全?

警告(TypeError: result.setExpression is not a function)发生在此处(webpack / lib / Parser.js https://github.com/webpack/webpack/blob/2361995a429794d372d61e3fb628dea3929e0ca4/lib/Parser.js#L1987

evaluateExpression(expression) {
    try {
        const hook = this.hooks.evaluate.get(expression.type);
        if (hook !== undefined) {
            const result = hook.call(expression);
            if (result !== undefined) {
                if (result) {
                    result.setExpression(expression);
                }
                return result;
            }
        }
    } catch (e) {
        console.warn(e);
        // ignore error
    }
    return new BasicEvaluatedExpression()
        .setRange(expression.range)
        .setExpression(expression);
}

我打印了这个result,发现它是

BasicEvaluatedExpression {
  type: 0,
  range: [ 945, 956 ],
  falsy: false,
  truthy: true,
  bool: null,
  number: null,
  regExp: null,
  string: null,
  quasis: null,
  array: null,
  items: null,
  options: null,
  prefix: null,
  postfix: null } 

我还打印了expression

{
  type: 'MemberExpression',
  start: 945,
  end: 956,
  loc: 
   SourceLocation {
     start: Position { line: 16, column: 13 },
     end: Position { line: 16, column: 24 } },
  range: [ 945, 956 ],
  object: 
   Node {
     type: 'Identifier',
     start: 945,
     end: 952,
     loc: SourceLocation { start: [Object], end: [Object] },
     range: [ 945, 952 ],
     name: 'process' },
  property: 
   Node {
     type: 'Identifier',
     start: 953,
     end: 956,
     loc: SourceLocation { start: [Object], end: [Object] },
     range: [ 953, 956 ],
     name: 'env' },
  computed: false }

process.env来自类似 var isProd = process.env && process.env.NODE_ENV === 'production';

的事物

背景:

完整的警告消息:

TypeError: result.setExpression is not a function
    at Parser.evaluateExpression (node_modules/webpack/lib/Parser.js:1991:14)
    at hooks.evaluate.for.tap.expr (node_modules/webpack/lib/Parser.js:368:17)
    at SyncBailHook.eval [as call] (eval at create (node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:16)
    at Parser.evaluateExpression (node_modules/webpack/lib/Parser.js:1984:25)
    at parser.hooks.statementIf.tap.statement (node_modules/webpack/lib/ConstPlugin.js:122:28)
    at SyncBailHook.eval [as call] (eval at create (node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:16)
    at Parser.walkIfStatement (node_modules/webpack/lib/Parser.js:1011:41)
    at Parser.walkStatement (node_modules/webpack/lib/Parser.js:961:10)
    at Parser.walkStatements (node_modules/webpack/lib/Parser.js:864:9)
    at Parser.walkBlockStatement (node_modules/webpack/lib/Parser.js:996:8)
    at Parser.walkStatement (node_modules/webpack/lib/Parser.js:931:10)
    at inScope (node_modules/webpack/lib/Parser.js:1603:10)
    at Parser.inScope (node_modules/webpack/lib/Parser.js:1910:3)
    at Parser.walkFunctionExpression (node_modules/webpack/lib/Parser.js:1596:8)
    at Parser.walkExpression (node_modules/webpack/lib/Parser.js:1509:10)
    at Parser.walkAssignmentExpression (node_modules/webpack/lib/Parser.js:1694:8)
    at Parser.walkExpression (node_modules/webpack/lib/Parser.js:1491:10)
    at Parser.walkExpressionStatement (node_modules/webpack/lib/Parser.js:1000:8)
    at Parser.walkStatement (node_modules/webpack/lib/Parser.js:946:10)
    at Parser.walkStatements (node_modules/webpack/lib/Parser.js:864:9)
    at Parser.walkBlockStatement (node_modules/webpack/lib/Parser.js:996:8)
    at Parser.walkStatement (node_modules/webpack/lib/Parser.js:931:10)
    at inScope (node_modules/webpack/lib/Parser.js:1792:10)
    at Parser.inScope (node_modules/webpack/lib/Parser.js:1910:3)
    at Parser._walkIIFE (node_modules/webpack/lib/Parser.js:1780:8)
    at Parser.walkCallExpression (node_modules/webpack/lib/Parser.js:1817:9)
    at Parser.walkExpression (node_modules/webpack/lib/Parser.js:1500:10)
    at Parser.walkVariableDeclaration (node_modules/webpack/lib/Parser.js:1381:32)
    at Parser.walkStatement (node_modules/webpack/lib/Parser.js:979:10)
    at Parser.walkStatements (node_modules/webpack/lib/Parser.js:864:9)
    at Parser.parse (node_modules/webpack/lib/Parser.js:2127:9)
    at doBuild.err (node_modules/webpack/lib/NormalModule.js:446:32)
    at runLoaders (node_modules/webpack/lib/NormalModule.js:327:12)
    at node_modules/loader-runner/lib/LoaderRunner.js:370:3
    at iterateNormalLoaders (node_modules/loader-runner/lib/LoaderRunner.js:211:10)
    at iterateNormalLoaders (node_modules/loader-runner/lib/LoaderRunner.js:218:10)

我的babel配置是:

"webpack": "4.25.1",
"babel-core": "6.26.3",
"babel-eslint": "8.2.6",
"babel-loader": "7.1.5",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-object-rest-spread": "6.26.0",
"babel-plugin-transform-runtime": "6.23.0",
"babel-preset-env": "1.7.0",
"babel-preset-react": "6.24.1",
"babel-register": "6.26.0",


const babelCoreConfig = {
    babelrc: false,
    presets: [
       'babel-preset-env',
       'babel-preset-react'
    ],
    plugins: [
       'babel-plugin-transform-class-properties',
        ['babel-plugin-transform-runtime', {
            helpers: true,
            polyfill: false,
            regenerator: false,
        }],
       'babel-plugin-transform-object-rest-spread'
    ]
};

1 个答案:

答案 0 :(得分:3)

我已经遇到了同样的问题,并且在解决问题上苦苦挣扎了大约半天。我在module.hot表达式中收到此警告。我正在使用react-hot-loader。

我在--hot中删除了选项webpack-dev-server --hot --config ./webpack.dev.config.js --mode development,并在webpack配置中添加了webpackHotModuleReplacementPlugin。这解决了这个问题。

process.env在哪里使用?如果它在webpack配置中,并且您的webpack配置是简单的JS对象导出,则它将是未定义的。

如果它在文件中,请尝试对其进行记录并检查其是否未定义。