Write in front
During a project scaffolding upgrade, the previous basiswebpack
The built project is ported toVite
Build.
Some component libraries depend onjQuery
, such as:Bootstrap
; When introducing these components, the project needs to already existjQuery
environment.
For example: When we areIntroduced in the following way
Bootstrap
hour:
// /* bootstarp */ import '@/assets/css/' import '@/assets/js/'
We must ensure that we have a globaljQuery
environment. Otherwise, inBootstrap
The missing report is reportedjQuery
Error.
In the original project,There is a paragraph about
jQuery
The configuration is as follows:
= { ... plugins: [ new ({ $: "jquery", jQuery: "jquery", "": "jquery" }) ], ... }
useGlobal plugin injection
jQuery
, so that after the project construction starts and runs, there will be a global projectjQuery
environment.
So, how to configure or implement this function in Vite projects?
Method 1. Global static introduction
The meaning of global static introduction is to the main page file of the projectin, use original import
js
Files are introducedjquery
。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="icon" type="image/svg+xml" href="/" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Vite-Vue3</title> <script src="/src/"></script> </head> <body> <div ></div> <script type="module" src="/src/"></script> </body> </html>
Method 2: Inject jquery using plugin @rollup/plugin-inject
First, installjquery
、@rollup/plugin-inject
.
npm i jquery @rollup/plugin-inject -S
Configuration file in the projectmiddle:
import inject from '@rollup/plugin-inject' // /config/ export default defineConfig({ plugins: [ vue(), inject({ $: "jquery", // jquery in node_modules will be automatically loaded here jQuery: "jquery", "": "jquery" }) ], resolve: { alias: { '@': resolve(__dirname, './src') } } })
This allows the functions implemented in the Vite project.
Extracurricular knowledge:
1. About
learnwebpack
How to use plug-in:
// const HtmlWebpackPlugin = require('html-webpack-plugin') = { ... plugins: [ new ({}), new ('/^\.\/locale$/, /moment$/'), // or (or) new HtmlWebpackPlugin({ template: './src/' }) ] ... }
There are two types ofwebpack
How to use plug-in:new ()
andnew HtmlWebpackPlugin()
;
The former iswebpack
Built-in module, the latter is notwebpack
Built-in modules need to be installed first using npm.
-
ProvidePlugin
,yeswebpack
built-in module. - use
ProvidePlugin
The loaded module will no longer be needed when usedimport
andrequire
Introduce. - by
jquery
As an example,ProvidePlugin
After instance initialization,jquery
It will be automatically loaded and imported into the correspondingnode
In the module.
new ({ $: 'jquery', jQuery: 'jquery' }) // Then we can use it directly in the code $('#item'); // <= just works jQuery('#item'); // <= just works // $ is automatically set to the exports of module "jquery"
2. Use @rollup/plugin-node-resolve to solve the reference problem between modules
use@rollup/plugin-node-resolve
Solve the problem of reference between modules.
This is the article about using plug-in @rollup/plugin-inject to inject global jQuery in the Vite project. For more related @rollup/plugin-inject to inject global jQuery content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!