Ich habe eine seltsame Warnung im Webpack gefunden. Beim Parsen von process.env
mit babel-loader
. In TypeError: result.setExpression is not a function
ist eine Warnung (webpack/lib/Parser.js
) aufgetreten. Ich bin in einem großen Projekt und möchte, dass mein Kern super solide ist. Ist es also sicher, diese Warnung zu ignorieren, da Webpack sie explizit abfängt und "Fehler ignorieren" kommentiert?
Die Warnung (TypeError: result.setExpression is not a function
) ist hier aufgetreten (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);
}
Ich habe diese result
gedruckt und festgestellt, dass es so ist
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 }
Ich habe auch die expression
gedruckt
{
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 }
Der process.env
kam von so etwas wie var isProd = process.env && process.env.NODE_ENV === 'production';
Hintergrund:
Die vollständige Warnmeldung:
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)
Meine babel configs sind:
"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'
]
};
Ich bin mit diesem Problem konfrontiert und habe mich einen halben Tag lang darum gekämpft. Diese Warnung wurde bei module.hot
expression angezeigt. Ich habe React-Hot-Loader verwendet.
Ich habe die Option --hot
in webpack-dev-server --hot --config ./webpack.dev.config.js --mode development
entfernt und die webpackHotModuleReplacementPlugin
in webpack config hinzugefügt. Das Problem wurde behoben.
Wo wird dieser process.env
verwendet? Wenn es sich in webpack config befindet und Ihre webpack config ein einfacher JS-Objektexport ist, wäre dies undefiniert.
Wenn es sich in einer Datei befindet, protokollieren Sie es und prüfen Sie, ob es undefiniert ist.