jervis2049 / vite-plugin-crx-mv3 Goto Github PK
View Code? Open in Web Editor NEWBuild a Chrome Extension with Vite⚡
Build a Chrome Extension with Vite⚡
Hi I'm running into the following issue using yarn 3.6.3. I'm using workspaces in yarn not sure if that could be causing part of the issues?
failed to load config from /my_project/extension/vite.config.ts
error during build:
file:///my_project/node_modules/vite-plugin-crx-mv3/dist/index.mjs:16
import acorn from "acorn";
^^^^^
SyntaxError: The requested module 'acorn' does not provide an export named 'default'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
at async loadConfigFromBundledFile (file:///my_project/node_modules/vite/dist/node/chunks/dep-df561101.js:66235:21)
at async loadConfigFromFile (file:///my_project/node_modules/vite/dist/node/chunks/dep-df561101.js:66086:28)
at async resolveConfig (file:///my_project/node_modules/vite/dist/node/chunks/dep-df561101.js:65682:28)
at async build (file:///my_project/node_modules/vite/dist/node/chunks/dep-df561101.js:47852:20)
at async CAC.<anonymous> (file:///my_project/node_modules/vite/dist/node/cli.js:822:9)
打印出来是这样的 { default: [Getter] }
Hola!
"vite-plugin-crx-mv3": "^1.1.0"
vite build never ends. Me helps only CTRL + C or install 0.0.7 version
> vite build --mode production
command: build
mode: production
isProd: true
vite v4.0.4 building for production...
✓ 5 modules transformed.
✓ 90 modules transformed.
rendering chunks (4)...
dist/contentscript-loader-content-fc7b9f3b.js
dist/popup.html 0.30 kB
dist/manifest.json 0.98 kB
dist/assets/icons/icon_48.png 1.80 kB
dist/assets/icons/icon_128.png 2.94 kB
dist/content.js 4.46 kB
dist/assets/montserrat-f35fdf51.ttf 394.26 kB
dist/assets/popup-dcffa69a.css 12.38 kB │ gzip: 4.99 kB
dist/assets/background-d7f5b2ce.js 1.99 kB │ gzip: 1.01 kB
dist/assets/content-fc7b9f3b.js 4.46 kB │ gzip: 1.56 kB
dist/assets/popup-f3bf849f.js 115.08 kB │ gzip: 42.41 kB
example with crx-vue
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: vite@undefined
npm ERR! node_modules/vite
npm ERR! dev vite@"^3.2.9" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer vite@"^2.5.10" from @vitejs/[email protected]
npm ERR! node_modules/@vitejs/plugin-vue
npm ERR! dev @vitejs/plugin-vue@"^2.3.0" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution
如何既支持覆盖浏览器起始页也支持内嵌页面,目前打包不支持,newtab.html 不能用vue构建
vue2 在content.js 中无法挂载
大佬 我这build的时候 Sourcemap is likely to be incorrect: a plugin (vite-plugin-crx-mv3) was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help
出这个提示 然后打包后的content.js还是原先的代码 然后content里面的vue文件中的css 并没有打包到最后的文件中 打包后的manifest文件也没有注入css
It looks that it isn't compatible with last version of vite.
It happens when I try to start project. It occurs inside vite.config.ts
I took look the examples for react but I have got error message below:
failed to load config from /Users/vkovaliov/temp/github-exts/vite.config.ts
error during build:
file:///Users/vkovaliov/temp/github-exts/node_modules/vite-plugin-crx-mv3/dist/index.mjs:16
import acorn from "acorn";
^^^^^
SyntaxError: The requested module 'acorn' does not provide an export named 'default'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:122:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:188:5)
Configs:
node: v20.2.0
vite: ^5.0.0
typescript: ^5.2.2
Vite config:
import { defineConfig } from "vite";
import { resolve } from "path";
import react from "@vitejs/plugin-react";
import crx from 'vite-plugin-crx-mv3'
// TODO: makes dedicated enrties for popup and content script
// https://vitejs.dev/config/
export default defineConfig({
build: {
rollupOptions: {
input: {
contentScript: resolve(__dirname, "contentScript.html"),
popup: resolve(__dirname, "popup.html"),
main: "src/main.tsx",
mainPopup: "src/popup/mainPopup.tsx",
},
},
},
plugins: [react(), crx({
manifest: './src/manifest.json',
}),],
});
Typescript config:
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": false,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
Any advices or way to manage it, please?
Might be it possible to update for repo according to ne versions of vite and typescript?
浏览器插件a想写一个配置页面,但是路径每次都被变成加上了随机数,我写代码跳转页面也就失效了。 那还有什么办法自定义一个页面并获取到路径吗
我的目录层级如下:
{
...
"action": {
"default_popup": "./popup/index.html"
},
...
}
此时生成的dist的目录如下,符合预期:
但是dist中生成的 manifest.json代码如下,不符合预期:
{
...
"action": {
"default_popup": "index.html"
},
...
}
期望生成如下路径
{
...
"action": {
"default_popup": "./src/crx/popup/index.html"
},
...
}
阅读源码时发现问题来源自下方 src/processors/manifest.ts 中的 generateManifest 函数的代码:
...
generateManifest (){
...
if (manifest.action?.default_popup) {
manifest.action.default_popup = basename(manifest.action.default_popup)
}
...
}
...
如果可以的话,希望其中使用 basename() 函数生成 popup 这里可以加一点适配不同的目录结构的逻辑,比如:
...
generateManifest (){
...
if (manifest.action?.default_popup) {
manifest.action.default_popup = manifest.action.default_popup.replace( /^\./, this.srcDir);
}
...
}
...
或者把popup的index.html文件生成到根目录。
When I use import
to invoke other api
, I got this error below:
Uncaught SyntaxError: Cannot use import statement outside a module
Is there any way to solve this?
复现github demo: https://github.com/zangguojun/customizable-tomato-clock
当进行工程化的开发时,很多工具只对package.json 有着较好的支持。
比如 standard-version 进行版本发布,这个工具只会自动修改packages.json 的中的版本,而manifest 中需要手动修改。
建议当manifest version 设置为auto,或者未设置,或者通过vite.config 中的配置来进行控制。
`import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import crx from 'vite-plugin-crx-mv3'
export default defineConfig({
plugins: [
vue(),
crx({
manifest: './manifest.json'
}),
],
})`
报错
error when starting dev server:
TypeError: crx is not a function
ref https://developer.chrome.com/docs/webstore/i18n/
related rollup plugin snippet:
const pathManifestAbs = path.normalize(pathManifest);
const pathParent = path.dirname(pathManifestAbs);
const pathRoot = path.dirname(path.dirname(pathManifestAbs));
const manifest = fs.readFileSync(pathManifestAbs, { encoding: "utf8" })
const manifestContent: chrome.runtime.ManifestV3 = JSON.parse(manifest);
// ....
async generateBundle(options: NormalizedOutputOptions, bundle: OutputBundle, isWrite: boolean) {
console.log({ default_locale: manifestContent.default_locale })
if (manifestContent.default_locale) {
const src = path.resolve(pathParent, "_locales")
const pathLocaleMessagesJson = path.resolve(src, manifestContent.default_locale, "messages.json")
if (!fs.existsSync(pathLocaleMessagesJson)) {
console.warn(`locale file ${pathLocaleMessagesJson} not found`)
}
const dest = path.resolve(config.build.outDir, "_locales")
console.info(`copy _locales ${src} => ${dest}`)
fs.cpSync(src, dest, { recursive: true });
}
// ....
}
目前的情况是:需要 插入 injected.js ,能插入。但是这个文件只能手写js,因为他没有经过vite的编译。
因为injected 的资源一般也需要在 web_accessible_resources 中配置,所以希望能够读取mainfest.json 中 web_accessible_resources 字段中的文件,也进行一下编译。
目前的做法是需要单独对这个文件编译一下,起两个vite ,不优雅。
要么就只能纯手写js, 但是文件的需要手动移动到dist文件,或者直接在dist里面编写。但这样对工程化很不友好。
您好!fork了您的项目,感觉很不错!最近需要做chrome插件,但是对于前端技术,本人属于小白“菜”!
不知道可不可以有时间,增加一个相对全一些的demo!方便我这种“菜”上手,如果打扰您,真是不好意思!
content-scripts 的文件路径和名称是固定的吗,能不能支持manifest可以用js,转换成json
或者相对路径转绝对路径
例如这样
import content from './content.js'
export default {
...
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
content
],
"run_at": "document_idle"
}
],
...
}
起因是我在background引入第三方模块,报错Uncaught SyntaxError: Cannot use import statement outside a module。
然后尝试在background {
"service_worker": "background.js",
"type": "module" // 增加 module
}
但是打包出来的manifest中 type:"module" 被自动去除,请问大佬这个可以解决吗,不然每次都得自己手动补上
非常感谢提供了这个插件,有一个问题可以探讨一下:
在
,似乎只是处理了 content script 的 import 的资源,但是没有处理动态引入的资源,比如通过chrome.runtime.getURL
引入的 injected.js(当然,我看到处理编译的时候,是有的。在这里似乎漏掉了?)在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": [ "<all_urls>" ],
"js": ["assets/js/jquery-2.2.4.min.js","content.ts"]
}]
请问项目里如何在content_scripts 注入一个js文件, 放在public目录下?
manifest 中 content_scripts 在"world" = "MAIN" 时 没有chrome.runtime对象,引发报错
您好 再content_scripts中页面的样式是和谷歌插件公用的,这样会导致某些页面会出现偏移情况,请问如何能做的样式隔离?
manifest v3 不能安装vue-i18n吗,unsafe-eval?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.