简体   繁体   English

通过 React 延迟导入和 Webpack 不会发生代码拆分

[英]Code splitting not happening via React lazy import ,and Webpack

 //webpack dev const common = require("./webpack.common"); const merge = require("webpack-merge"); const globImporter = require('node-sass-glob-importer'); module.exports = merge(common, { mode: "development", module: { rules: [ { test: /\.(sa|sc|c)ss$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader' }, { loader: 'postcss-loader', options: { plugins: () => [require('autoprefixer')({ 'overrideBrowserslist': ['> 1%', 'last 2 versions'] })], } }, { loader: 'sass-loader', options: { sassOptions: { importer: globImporter() } } }] }, ] }, devServer: { // contentBase: path.join(__dirname, 'dist'), historyApiFallback: true, port: 8081 } });

 //webpack prod const common = require("./webpack.common"); const path = require("path"); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const merge = require("webpack-merge"); const MiniCssExtractPlugin = require("mini-css-extract-plugin"); module.exports = merge(common, { mode: "production", output: { filename: "main.[contenthash].js", path: path.resolve(__dirname, "dist") }, module: { rules: [ { test: /\.(sa|sc|c)ss$/, use: [ { loader: MiniCssExtractPlugin.loader }, { loader: 'css-loader' }, { loader: 'postcss-loader', options: { plugins: () => [require('autoprefixer')({ 'overrideBrowserslist': ['> 1%', 'last 2 versions'] })], } }, { loader: 'sass-loader', options: { sassOptions: { importer: globImporter() } } }] } ] }, plugins: [new MiniCssExtractPlugin({ filename: "./src/css/[name].[contentHash].css" }, ), new CleanWebpackPlugin()] })

 // webpack common const HTMLWebpackPlugin = require('html-webpack-plugin'); module.exports={ entry:"./src/index.tsx", resolve: { // Add '.ts' and '.tsx' as resolvable extensions. extensions: [".ts", ".tsx",".js", ".jsx"] }, module:{ rules:[ { test: /\.ts(x?)$/, exclude: /node_modules/, use: [ { loader: "ts-loader" } ] }, { test:/\html$/, use:["html-loader"] }, { test:/\.(svg|png|jpe?g|gif)$/i, use:{ loader:"file-loader", options:{ name:"[name].[hash].[ext]", outputPath:"images" } } } ] }, // externals: { // "react": "React", // "react-dom": "ReactDOM" // }, plugins:[new HTMLWebpackPlugin({ template:"./src/index.html" })] }

Code splitting not happening via React lazy import,and Webpack通过 React 延迟导入和 Webpack 不会发生代码拆分

I want to do code splitting with React Suspense and Lazy imports, I don't know what is missing because separate chunk is not getting created for my dynamically import component我想用 React Suspense 和 Lazy 导入进行代码拆分,我不知道缺少什么,因为没有为我的动态导入组件创建单独的块

Please anyone help I am using webpack 4 and React version 16.9请任何人帮助我使用 webpack 4 和 React 版本 16.9

Getting below message warning console获取以下消息警告控制台


WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB).入口点大小限制中的警告:以下入口点组合资产大小超过了建议的限制 (244 KiB)。 This can impact web performance.这会影响 web 的性能。 Entrypoints: main (533 KiB)./src/css/main.000e3971ce67d214e0d7.css main.5877aa0d0c3e45fb034f.js入口点:main (533 KiB)./src/css/main.000e3971ce67d214e0d7.css main.5877aa0d0c3e45fb034f.js

WARNING in webpack performance recommendations: You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application. webpack 性能建议中的警告:您可以使用 import() 或 require.ensure 来限制捆绑包的大小,以延迟加载应用程序的某些部分。
For more info visit https://webpack.js.org/guides/code-splitting/欲了解更多信息,请访问https://webpack.js.org/guides/code-splitting/


In tsconfig.json set "module": "esnext"tsconfig.json设置"module": "esnext"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用 React Route 和 Webpack 实现 React “Lazy” 路由以进行代码拆分 - Trying to implement a React “Lazy” route for code splitting with React Route and Webpack 使用webpack设置块路径a并响应延迟代码拆分 - set chunk path a with webpack and react lazy code splitting Webpack 2 + React - 使用System.import进行代码拆分时的嵌套路由 - Webpack 2 + React - nested routes when code splitting with System.import 使用Webpack和ocLazyLoad进行代码拆分/延迟加载 - Code splitting/Lazy load with Webpack & ocLazyLoad Webpack导入代码拆分渲染<undefined> - Webpack Import Code Splitting rendering <undefined> 如何使用 react-router 实现延迟加载和代码拆分? - How to implement lazy loading and code splitting with react-router? 代码拆分如何与导入/导出以及babel和webpack一起使用? - how code splitting works with import/export and babel and webpack? Webpack代码拆分打破了使用vueJs组件的导入 - Webpack code splitting breaks jest import with vueJs components 使用System.import进行Webpack 2代码拆分:依赖关系的依赖关系 - Webpack 2 code splitting with System.import: Dependencies of dependencies Typescript + react 17 + webpack 5. 使用 react HashRouter 进行代码拆分不起作用 - Typescript + react 17 + webpack 5. Code splitting with react HashRouter not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM