Replies: 13 comments 13 replies
-
Vanilla js and HTML exampledefine an entry named
// import $ from 'jquery/dist/jquery'
import $ from 'jquery'
window.jQuery = window.$ = $ or with build: {
rollupOptions: {
output: {
globals: {
jquery: 'window.jQuery',
jquery: 'window.$'
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I have the same problem. |
Beta Was this translation helpful? Give feedback.
-
Vanilla js and HTML example
https://code.jquery.com/jquery-3.6.0.min.js (umd) no need to wait for page load to access build: {
rollupOptions: {
external: ['jquery'],
.
.
output: {
jquery: "$"
}
}
}
jquery.js: import jQuery from "jquery";
Object.assign(window, { $: jQuery, jQuery })
//or
window.jQuery = window.$ = $ <html>
<head>
</head>
<body>
<script type="module" src="/@vite/client"></script>
<script type="module" src="/src/jquery.js"></script>
<!-- normal script inside html file -->
<script>
console.log($) // Uncaught ReferenceError: $ is not defined
window.addEventListener("load", function() {
console.log($) // works
})
</script>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
-
使用 @rollup/plugin-inject @rollup/plugin-node-resolve 两个 rollup 插件完成依赖注入的功能
|
Beta Was this translation helpful? Give feedback.
-
resole(),
inject({
$: 'jquery',
jQuery: 'jquery',
}), run config: |
Beta Was this translation helpful? Give feedback.
-
optimizeDeps: {
include: ["jquery"],
}, it's work |
Beta Was this translation helpful? Give feedback.
-
I'm having issues with some jQuery code that's in the header already (after the entrypoint script tag) but because jQuery is being imported as a seperate request, $ isn't available by the time the head script code is evaluated. Is there a way to force jQuery into the initial application entrypoint response? |
Beta Was this translation helpful? Give feedback.
-
I had some trouble setting up vitest to correctly inject jQuery. I was facing the error
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
This thread was helpful in resolving my issue. This is what worked for me with Vite 4.5 In vite.config.js I have the following: plugins: [
inject({
jQuery: 'jquery',
}),
],
optimizeDeps: {
include: ['jquery'],
}, In the main index.js import jQuery from 'jquery';
Object.assign(window, { $: jQuery, jQuery });
// or if you don't want to pollute the entire window scope you can use the following to set $ as a global variable in your script
globalThis.$ = jQuery;
// As of 2023-11-27 using syntax Object.assign(globalThis, { $: jQuery, jQuery }); will export the variable into window scope with Vite 5.0, the globalThis.$ will set it only to the module/file scope. |
Beta Was this translation helpful? Give feedback.
-
Bellow is the only way I got any luck in making vite use external window.jQuery, it just works god knows why, tried everyting above but was unable to get anything to work either because I am stupid, or because of bad weather. Funny thing as soon as I add jquery package into my package.json file, vite automatically opts in to import local jquery instead of global from window object. I use bootstrap.tsimport * as jQuery from 'jquery';
declare global {
interface Window {
jQuery: typeof jQuery;
$: typeof jQuery;
}
}
window.$ = window.jQuery
window.jQuery = window.jQuery nothing special in my config just some boilerplate vite.config.ts:import { defineConfig } from "vite"
import laravel from "laravel-vite-plugin"
import tsconfigPaths from 'vite-tsconfig-paths'
import path from "path"
export default defineConfig({
plugins: [
laravel({
input: [
"src/ts/entry.ts",
],
publicDirectory: "./static/build",
}),
tsconfigPaths()
],
resolve: {
alias: {
"~": path.resolve("."),
},
},
build: {
outDir: "./static/build",
},
server: {
https: {
key: "./ssl/localhost.key",
cert: "./ssl/localhost.crt",
},
hmr: { host: "localhost" },
},
}) package.json"devDependencies": {
"@types/jquery": "^3.5.29",
}
than in my entry file. import "./bootstrap"
$(async function () {
console.log("Hello World");
}); Sometimes its sad that such basic features are not available out of the box. |
Beta Was this translation helpful? Give feedback.
-
My complicated use case, problem and o1-preview's recommended solutionHybrid MPA that bundles a vite/typescript front end for use in aspnet core 8.0 pages, but the pages also have inline scripts that need $() etc right away. Including jquery from a static lib folder (not installed in node_modules) and marking it as external worked for everything both sides as long as I didn't import Jquery explicitly anywhere (it was there though). Until I wanted to import 'datatables.net-dt' into a new entry point and use it conditionally. Since that app uses import JQuery from 'JQuery' it immediately failed with Uncaught TypeError: Failed to resolve module specifier "jquery". Solution:
Works perfectly. Configuration
|
Beta Was this translation helpful? Give feedback.
-
In webpack,We can use
ProviderPlugin
to import jquery in webpackage.config file. Like this:Now use vite2,how to do that.(I don't want to import jquery file in html,i want build all file in a single js file);
Beta Was this translation helpful? Give feedback.
All reactions