React packaging optimization, CDN introduces JS, CSS

tags: javaScript  react  cdn  javassist  

Foreword

This article is based on ("React": "^17.0.2") version

The official scaffolding of the React scaffolding is to hide the webpack configuration. Before configuration, we need to expose the webpack configuration.

1. Enter the command NPM Run Eject

A command prompt will appear: This is a one -way operation. It is irreversible/returned after confirming the operation?

Inputy Return

After success, appear in the project root directoryconfig folder

2. Open the webpack.config.js file under the config folder

3. Find the New HTMLWEBPACKPLUGIN configuration item of Plugins, add content in the red box (for configuration CDN)

files: {
   js: [],
   css: []
}

4. Added the EXTERNALS configuration item, and the same level as the third step of Plugins (for modules that do not pack for packaging)

externals: {},


5. Configure build.js under the scripts folder

Note: Because the React development environment and the production environment use different files (you can view the scripts field of package.json), so you can directly configure the built.js file.

Note: The plum bidding here corresponds to the webpack.config.js file, and new htmlwebpackplugin () is corresponding to the corresponding position of the plugins configuration item.

// Configure cdn to introduce
config.plugins[0].options.files.js = [
  'https://lib.baomitu.com/react/17.0.2/umd/react.production.min.js',
  'https://lib.baomitu.com/react-dom/17.0.2/umd/react-dom.production.min.js',
  'https://lib.baomitu.com/redux/4.1.1/redux.min.js',
  'https://lib.baomitu.com/redux-thunk/2.3.0/redux-thunk.min.js',
  'https://lib.baomitu.com/react-redux/7.2.4/react-redux.min.js',
  'https://lib.baomitu.com/axios/0.21.1/axios.min.js',
  'https://lib.baomitu.com/lodash.js/4.17.21/lodash.min.js'
];
config.plugins[0].options.files.css = [];

// Configure files that are not packed in webpack
config.externals = {
  'react': 'React',
  'react-dom': 'ReactDOM',
  'redux': 'Redux',
  'redux-thunk': 'ReduxThunk',
  'react-redux': 'ReactRedux',
  'axios': 'axios',
  'lodash': '_'
};

6. Configure index.html, introduce the configuration CDN JS CSS link

<!-- require cdn assets css -->
<% for (var i in htmlWebpackPlugin.options.files && htmlWebpackPlugin.options.files.css) { %>
  <link rel="stylesheet" href="<%= htmlWebpackPlugin.options.files.css[i] %>" />
<% } %>
<!-- require cdn assets js -->
<% for (var i in htmlWebpackPlugin.options.files && htmlWebpackPlugin.options.files.js) { %>
  <script type="text/javascript" src="<%= htmlWebpackPlugin.options.files.js[i] %>"></script>
<% } %>


7. Effect chart

If this article is helpful to you, I am glad to help you.

Of course, if you think the article makes you feel unreasonable, or there is a simpler implementation method or you can't understand it. I hope you can point it out after seeing it. Reply to you.

Intelligent Recommendation

React project packaging optimization

Before optimization: Optimization:   Optimization:       Key points: 1. Route lazy loading 2. Introduced all the public components developed before the routing laziness loading 3. ...

React project packaging optimization ===

Table of contents Project packaging Project Local Preview Packing volume analysis Optimization of production environment Route lazy load Remove Console Configure CDN Project packaging Package the proj...

Three ways React introduces css

inReactIn, if you write the inline style directly as above, an error will be reported directly, becauseJSXGrammar is not supported,ReactSupport the following three types of writingcssMethods: 1. Inlin...

JS introduces external JS / CSS

JS introduces external JS / CSS Codes....

About webpack packaging js, css compression optimization summary

The project development is coming to an end, and optimization is not negligible. Let me talk about the optimization of the project: Development environment: installing projects using vue-cli A basic o...

More Recommendation

React introduces external JS files

React introduces external JS files Before the component is loaded, add an Script tag to the DOM to introduce....

Package optimization - CDN introduces a third-party library that does not want to be packaged

Purpose WebPack defaults all modules to be packaged in the place file, in addition to the code split optimization package, you can also use CDN, reduce the package, and enjoy the advantage of CDN cach...

Front end introduces JS, CSS

How to introduce CSS 1. Internal chain: Write directly in the html tag. 2, embedded: Use the selector to write it in <head>. 3. External type: Written in a CSS file, introduced with <link>...

Laravel introduces CSS JS files

If you find that the css file you imported sometimes works when you use laravel, sometimes it fails, then you have to modify the template file. Transfer from laravel collegehttp://laravelacademy.org/p...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top