Jervis2049 / vite-plugin-crx-mv3

Build a Chrome Extension with Vite⚡
201 stars 30 forks source link

content_scripts配置js引入jquery时,经过编译报错。 #6

Closed winson52u closed 1 year ago

winson52u commented 1 year ago

在content_scripts引入jquery,应该是jquery经过vite编译后,导致content.ts使用jquery时,出现以下错误: jquery-2.2.4.min.js:3 Uncaught SyntaxError: Unexpected token 'export' (at jquery-2.2.4.min.js:3:6594) content.js:1 Uncaught ReferenceError: $ is not defined at content.js:1:37 at content.js:2:3 能否自定义哪些文件不进行编译直接复制? 配置如下。 "content_scripts": [{ "matches": [ "" ], "js": ["assets/js/jquery-2.2.4.min.js","content.ts"] }]

Jervis2049 commented 1 year ago

个人不推荐使用jquery,因为现在原生的api操作元素也很方便了。如果还是希望使用,可以这样做: 1、安装jquery依赖

  "dependencies": {
    "jquery": "2.2.4",
  },

2、在content.js里面引入jquery

import $ from 'jquery'

window.onload = function(){
  console.log($('body'));
}

content_scripts只需引入content.js