In the Vite project, use the plugin @rollup/plugin-inject to inject the global jQuery environment
written in front
In the process of a project scaffolding upgrade, the previously webpack
built-based project is migrated to Vite
the build.
Some component libraries are jQuery
dependent , such as: Bootstrap
; When introducing these components, jQuery
the environment .
For example: when we import main.js
in as follows Bootstrap
:
// main.js
/* bootstarp */
import '@/assets/css/bootstrap.min.css'
import '@/assets/js/bootstrap.min.js'
We must ensure that we have the global jQuery
environment . Otherwise, a missing will be reported Bootstrap
in jQuery
the error.
In the original project, webpack.base.conf.js
in , there is a section jQuery
about configuration like this:
module.exports = {
...
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery"
})
],
...
}
Use webpack.ProvidePlugin
plug- in global injection jQuery
, so that after the project is built and started running, the project has a global jQuery
environment .
So, how to configure or realize this function in the Vite project?
Method 1. Global static import
The meaning of global static import is to use the original import file index.html
in the main page file of the project to import .js
jquery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite-Vue3</title>
<script src="/src/jquery.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
Method 2. Use the plugin @rollup/plugin-inject to inject jquery
First, install jquery
, @rollup/plugin-inject
.
npm i jquery @rollup/plugin-inject -S
vite.config.js
In the project's configuration file :
import inject from '@rollup/plugin-inject'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
inject({
$: "jquery", // 这里会自动载入 node_modules 中的 jquery
jQuery: "jquery",
"windows.jQuery": "jquery"
})
],
resolve: {
alias: {
'@': resolve(__dirname, './src')
}
}
})
In this way, the function of webpack.ProvidePlugin can be realized in the Vite project.
Extracurricular Knowledge:
1. About webpack.ProvidePlugin
webpack
Learn how to use plugins for :
// webpack.base.conf.js
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
...
plugins: [
new webpack.ProvidePlugin({
}),
new webpack.IgnorePlugin('/^\.\/locale$/, /moment$/'),
// or(或者)
new HtmlWebpackPlugin({
template: './src/index.html'
})
]
...
}
There are webpack
two ways to use the plugin: new webpack.ProvidePlugin()
and new HtmlWebpackPlugin()
;
The former is webpack
a built-in module, while the latter is not webpack
a built-in module and needs to be installed using npm before use.
ProvidePlugin
, is a built-in modulewebpack
of .ProvidePlugin
Modules loaded with will no longer needimport
andrequire
be imported when used.- Take
jquery
for example ,ProvidePlugin
after the instance is initialized with ,jquery
it will be automatically loaded and imported into the correspondingnode
module .
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
// 然后我们可以在代码中直接使用
$('#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 problem of referencing between modules
Use to @rollup/plugin-node-resolve
solve reference problems between modules.
—————————— [End of text] ——————————
Front-end learning exchange group, if you want to come in face-to-face, you can join the group: 832485817 , 685486827 ;
Written at the end: convention is better than configuration - the principle of simplicity in software development
—————————— 【End】 ——————————
My:
Personal website: https://neveryu.github.io/neveryu/
Github: https://github.com/Neveryu
Wechat: miracle421354532
For more learning resources, please pay attention to my WeChat... okay?