我正在尝试使用jQuery和laravel 9 + vite。它在开发中运行良好,但在构建时,我发现jQuery不是一个函数
- 资料库. ts**
import * as jQuery from 'jquery';
declare global {
interface Window {
jQuery: typeof jQuery;
$: typeof jQuery;
}
}
window.$ = window.jQuery = jQuery;
- 主文件. ts**
jQuery(function(){
console.log(jQuery(".datepicker"));
});
- 维生素配置ts**
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import { esbuildCommonjs } from '@originjs/vite-plugin-commonjs'
export default defineConfig({
plugins: [
laravel({
input: [
'resources/scss/libs.scss',
'resources/scss/main.scss',
'resources/css/app.css',
'resources/js/libs.ts',
'resources/js/main.ts',
],
refresh: true,
}),
],
optimizeDeps: {
include: ['jquery/dist/jquery'],
esbuildOptions: {
plugins: [
esbuildCommonjs(['jquery/dist/jquery'])
]
}
},
build: {
rollupOptions: {
plugins: [
{
name: 'no-tree',
transform(_, id) {
if (id.includes('jquery/dist/jquery')) {
return { moduleSideEffects: 'no-tree' }
}
}
}
],
output: {
globals: {
jquery: 'window.jQuery',
}
}
}
}
});
- npm运行开发的输出**
- npm运行构建的输出**
1条答案
按热度按时间jhdbpxl91#
使用@rollup/plugin-inject注入jQuery: