[英]An error happens in webpack production mode, but but works well at development mode
An Uncaught TypeError
occurred when I use webpack's production mode, but it works well in webpack's development mode. 当我使用webpack的生产模式时发生了
Uncaught TypeError
,但是在webpack的开发模式下效果很好。
After I compiled my frontend code into static resources with webpack, I served these resources on nodejs server with 3002 port. 使用Webpack将前端代码编译成静态资源后,我将这些资源提供给具有3002端口的nodejs服务器。 Now,when I visit http://localhost:3002/simple with the development mode resources,i can get right response,but i get error with the production mode resources.
现在,当我使用开发模式资源访问http:// localhost:3002 / simple时,我可以获得正确的响应,但是生产模式资源却出现了错误。 An 'Uncaught TypeError' occurred when i use webpack's production mode, but it can works well when i use webpack's development mode to generate the static resources.
当我使用webpack的生产模式时发生“未捕获的TypeError”,但是当我使用webpack的开发模式生成静态资源时,它可以很好地工作。 Both the two modes use a same webpack.config.js.
两种模式都使用相同的webpack.config.js。
webpack's build result in production mode: webpack在生产模式下的构建结果:
ℹ 「atl」: Using typescript@3.5.3 from typescript
ℹ 「atl」: Using tsconfig.json from /Users/liuchangyu/workspace/yid0708/src/web/tsconfig.json
ℹ 「atl」: Checking started in a separate process...
ℹ 「atl」: Time: 2710ms
Hash: b5a201513cf282946d8c
Version: webpack 4.35.3
Time: 26364ms
Built at: 2019-07-17 6:43:43 PM
Asset Size Chunks Chunk Names
scripts/common.b5a20.bundles.js 10.8 KiB 0 [emitted] common
scripts/index.b5a20.bundles.js 74 bytes 1 [emitted] index
scripts/runtime.b5a20.bundles.js 2.19 KiB 2 [emitted] runtime
scripts/simple.b5a20.bundles.js 187 bytes 3 [emitted] simple
scripts/vendor.b5a20.bundles.js 117 KiB 4 [emitted] vendor
views/index.html 678 bytes [emitted]
Entrypoint index = scripts/runtime.b5a20.bundles.js scripts/vendor.b5a20.bundles.js scripts/common.b5a20.bundles.js scripts/index.b5a20.bundles.js
[5] ./node_modules/_react-router@5.0.1@react-router/esm/react-router.js + 1 modules 27.7 KiB {4} [built]
| 2 modules
[13] (webpack)/buildin/global.js 472 bytes {0} [built]
[26] ./index.tsx + 2 modules 1.64 KiB {0} [built]
| ./index.tsx 661 bytes [built]
| ./pages/App.tsx 237 bytes [built]
| ./routes/index.tsx 761 bytes [built]
+ 27 hidden modules
Child html-webpack-plugin for "views/index.html":
1 asset
Entrypoint undefined = views/index.html
[0] ./node_modules/_html-webpack-plugin@3.2.0@html-webpack-plugin/lib/loader.js!./templates/template.html 596 bytes {0} [built]
[2] (webpack)/buildin/global.js 472 bytes {0} [built]
[3] (webpack)/buildin/module.js 497 bytes {0} [built]
+ 1 hidden module
and in development mode: 并处于开发模式:
ℹ 「atl」: Using typescript@3.5.3 from typescript
ℹ 「atl」: Using tsconfig.json from /Users/liuchangyu/workspace/yid0708/src/web/tsconfig.json
ℹ 「atl」: Checking started in a separate process...
ℹ 「atl」: Time: 2733ms
Hash: a7dc899dea7d21139095
Version: webpack 4.35.3
Time: 26759ms
Built at: 2019-07-17 6:37:49 PM
Asset Size Chunks Chunk Names
scripts/common.a7dc8.bundles.js 129 KiB common [emitted] common
scripts/index.a7dc8.bundles.js 123 bytes index [emitted] index
scripts/runtime.a7dc8.bundles.js 8.92 KiB runtime [emitted] runtime
scripts/simple.a7dc8.bundles.js 990 bytes simple [emitted] simple
scripts/vendor.a7dc8.bundles.js 926 KiB vendor [emitted] vendor
views/index.html 678 bytes [emitted]
Entrypoint index = scripts/runtime.a7dc8.bundles.js scripts/vendor.a7dc8.bundles.js scripts/common.a7dc8.bundles.js scripts/index.a7dc8.bundles.js
[./index.tsx] 661 bytes {common} [built]
[./node_modules/_webpack@4.35.3@webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {common} [built]
[./pages/App.tsx] 237 bytes {common} [built]
[./routes/index.tsx] 761 bytes {common} [built]
+ 32 hidden modules
Child html-webpack-plugin for "views/index.html":
1 asset
Entrypoint undefined = views/index.html
[./node_modules/_html-webpack-plugin@3.2.0@html-webpack-plugin/lib/loader.js!./templates/template.html] 596 bytes {0} [built]
[./node_modules/_webpack@4.35.3@webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {0} [built]
[./node_modules/_webpack@4.35.3@webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {0} [built]
+ 1 hidden module
index.tsx: index.tsx:
import * as React from 'react';
import * as ReactDom from "react-dom"
import App from "./pages/App";
ReactDom.render(<App/>, document.getElementById("main"));
App.tsx: App.tsx:
import * as React from 'react';
import Routes from "../routes";
import {BrowserRouter} from "react-router-dom"
const App = () => {
return <BrowserRouter basename="/">{Routes()}</BrowserRouter>
};
export default App;
routes.tsx: route.tsx:
import * as React from 'react';
const {Suspense, lazy} = React;
import {Switch, RouteProps, Route} from "react-router-dom"
const Simple = lazy(() =>
import(
/* webpackChunkName:"simple" */
'../components/Simple')
);
const routes: RouteProps[] = [
{
path: "/simple",
exact: true,
component: Simple
},
];
const Routes = () => (
<Suspense fallback={<i>loading...</i>}>
<Switch>
{
routes.map(r => {
const {path, exact, component} = r;
return (
<Route key={"23123"} exact={exact} path={path} component={component}/>
)
})
}
</Switch>
</Suspense>
);
export default Routes;
Simple.tsx: Simple.tsx:
import * as React from 'react';
const Simple = () => {
return <div>Simple Component</div>
};
export default Simple;
The browser's error log is: 浏览器的错误日志为:
runtime.b5a20.bundles.js:1 Uncaught TypeError: Cannot read property 'call' of undefined at i (runtime.b5a20.bundles.js:1) at Object.
runtime.b5a20.bundles.js:1未捕获的TypeError:无法读取对象i处undefined的属性“ call”(runtime.b5a20.bundles.js:1)。 (vendor.b5a20.bundles.js:1) at i (runtime.b5a20.bundles.js:1) at Module.
(i)(ventime.b5a20.bundles.js:1)在模块处(vendor.b5a20.bundles.js:1)。 (common.b5a20.bundles.js:15) at i (runtime.b5a20.bundles.js:1) at t (runtime.b5a20.bundles.js:1) at Array.r [as push] (runtime.b5a20.bundles.js:1) at index.b5a20.bundles.js:1 i @ runtime.b5a20.bundles.js:1 (anonymous) @ vendor.b5a20.bundles.js:1 i @ runtime.b5a20.bundles.js:1 (anonymous) @ common.b5a20.bundles.js:15 i @ runtime.b5a20.bundles.js:1 t @ runtime.b5a20.bundles.js:1 r @ runtime.b5a20.bundles.js:1 (anonymous) @ index.b5a20.bundles.js:1
(common.b5a20.bundles.js:15)在Array.r的t(runtime.b5a20.bundles.js:1)在i(runtime.b5a20.bundles.js:1)在Array.r [按推送](runtime.b5a20。 bundles.js:1)位于index.b5a20.bundles.js:1 i @ runtime.b5a20.bundles.js:1(匿名)@ vendor.b5a20.bundles.js:1 i @ runtime.b5a20.bundles.js: 1(匿名)@ common.b5a20.bundles.js:15 i @ runtime.b5a20.bundles.js:1 t @ runtime.b5a20.bundles.js:1 r @ runtime.b5a20.bundles.js:1(匿名) @ index.b5a20.bundles.js:1
It seems like some thing wrong with react or react-router ? react或react-router似乎有些问题? because it can work well when I change
BrowserRouter
into a simple div
. 因为当我将
BrowserRouter
更改为简单的div
时它可以很好地工作。
Today I find the reason , I used webpack-deep-scope-plugin
for tree shaking ,because of optimization.usedExports
is enabled in production
mode and disabled elsewise. 今天,我找到了原因,由于进行了
optimization.usedExports
,我使用了webpack-deep-scope-plugin
进行树状摇动。usedExports在production
模式下已启用,在其他情况下已禁用。 They clashed! This will be work: 他们发生了冲突!这将是有效的:
module.exports = {
//...
optimization: {
usedExports: false
}
};
I suggest no longer to use webpack-deep-scope-plugin
, just do webpack's default production
optimization. 我建议不再使用
webpack-deep-scope-plugin
,而只需执行webpack的默认production
优化即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.