Skip to content

Commit b8ee32f

Browse files
committed
build: Build extension using Vite
1 parent d86a8c3 commit b8ee32f

File tree

12 files changed

+612
-92
lines changed

12 files changed

+612
-92
lines changed

.eslintrc.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ module.exports = {
4040
},
4141
},
4242

43+
{
44+
files: ['*.mjs'],
45+
parserOptions: {
46+
sourceType: 'module',
47+
ecmaVersion: '2020',
48+
},
49+
},
50+
4351
{
4452
files: ['*.ts'],
4553
extends: ['@metamask/eslint-config-typescript'],
@@ -50,20 +58,6 @@ module.exports = {
5058
rules: {
5159
// Enable rules that are disabled in `@metamask/eslint-config-typescript`
5260
'@typescript-eslint/no-explicit-any': 'error',
53-
54-
// TODO: auto-fix breaks stuff
55-
'@typescript-eslint/promise-function-async': 'off',
56-
57-
// Without the `allowAny` option, this rule causes a lot of false
58-
// positives.
59-
'@typescript-eslint/restrict-template-expressions': [
60-
'error',
61-
{
62-
allowAny: true,
63-
allowBoolean: true,
64-
allowNumber: true,
65-
},
66-
],
6761
},
6862
},
6963

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
extends: ['../../.eslintrc.js'],
3+
4+
overrides: [
5+
{
6+
files: ['src/extension/**/*.js'],
7+
globals: { chrome: 'readonly', clients: 'readonly' },
8+
rules: {
9+
'jsdoc/require-jsdoc': 'off',
10+
},
11+
},
12+
],
13+
};

packages/ocap-skunkworks/package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
"dist/"
1212
],
1313
"scripts": {
14-
"build": "ts-bridge --project tsconfig.build.json --clean",
15-
"build:docs": "typedoc",
14+
"build:extension": "vite build -c vite.config.extension.mjs",
15+
"build:library": "ts-bridge --project tsconfig.build.json --clean",
1616
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/ocap-skunkworks",
1717
"publish:preview": "yarn npm publish --tag preview",
18+
"start": "vite -c vite.config.extension.mjs",
1819
"test": "jest --reporters=jest-silent-reporter",
1920
"posttest": "jest-it-up",
2021
"test:clean": "jest --clearCache",
2122
"test:verbose": "jest --verbose",
22-
"test:watch": "jest --watch"
23+
"test:watch": "jest --watch",
24+
"build": "ts-bridge --project tsconfig.build.json --clean",
25+
"build:docs": "typedoc"
2326
},
2427
"devDependencies": {
2528
"@arethetypeswrong/cli": "^0.15.3",
@@ -33,7 +36,9 @@
3336
"ts-jest": "^28.0.7",
3437
"typedoc": "^0.24.8",
3538
"typedoc-plugin-missing-exports": "^2.0.0",
36-
"typescript": "~4.9.5"
39+
"typescript": "~4.9.5",
40+
"vite": "^5.3.4",
41+
"vite-plugin-static-copy": "^1.0.6"
3742
},
3843
"engines": {
3944
"node": "^18.18 || >=20"

packages/ocap-skunkworks/src/extension/background.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ const OFFSCREEN_DOCUMENT_PATH = '/offscreen.html';
22

33
// Send
44
chrome.action.onClicked.addListener(async () => {
5-
sendMessage(
6-
'greetings',
7-
{ name: 'Kernel' },
8-
);
5+
sendMessage('greetings', { name: 'Kernel' });
96
});
107

118
async function sendMessage(type, data) {
@@ -14,12 +11,12 @@ async function sendMessage(type, data) {
1411
chrome.runtime.sendMessage({
1512
type,
1613
target: 'offscreen',
17-
data
14+
data,
1815
});
1916
}
2017

2118
async function provideOffScreenDocument() {
22-
if (!(await hasDocument())) {
19+
if (!(await chrome.offscreen.hasDocument())) {
2320
await chrome.offscreen.createDocument({
2421
url: OFFSCREEN_DOCUMENT_PATH,
2522
reasons: [chrome.offscreen.Reason.IFRAME_SCRIPTING],
@@ -47,18 +44,8 @@ async function handleMessage(message) {
4744
}
4845

4946
async function closeOffscreenDocument() {
50-
if (!(await hasDocument())) {
47+
if (!(await chrome.offscreen.hasDocument())) {
5148
return;
5249
}
5350
await chrome.offscreen.closeDocument();
5451
}
55-
56-
async function hasDocument() {
57-
const allClients = await clients.matchAll();
58-
for (const client of allClients) {
59-
if (client.url.endsWith(OFFSCREEN_DOCUMENT_PATH)) {
60-
return true;
61-
}
62-
}
63-
return false;
64-
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<!DOCTYPE html>
2-
<script src="offscreen.js"></script>
2+
<script src="offscreen.js" type="module"></script>

packages/ocap-skunkworks/src/extension/offscreen.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ async function handleMessage(message) {
77

88
switch (message.type) {
99
case 'greetings':
10-
reply('salutations', `Good day to you, ${message.data.name}!`)
10+
reply('salutations', `Good day to you, ${message.data.name}!`);
1111
break;
1212
default:
1313
console.error(`Received unexpected message type: "${message.type}"`);
14-
return;
1514
}
1615
}
1716

@@ -20,5 +19,5 @@ function reply(type, data) {
2019
data,
2120
target: 'background',
2221
type,
23-
})
22+
});
2423
}

packages/ocap-skunkworks/src/index.test.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/ocap-skunkworks/src/index.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/ocap-skunkworks/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"baseUrl": "./"
55
},
66
"references": [],
7-
"include": ["./src"]
7+
"include": []
88
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import path from 'path';
2+
import { defineConfig } from 'vite'
3+
import { viteStaticCopy } from 'vite-plugin-static-copy';
4+
5+
const root = './src/extension';
6+
7+
// https://vitejs.dev/config/
8+
export default defineConfig({
9+
root,
10+
assetsInclude: ['**/*.json'],
11+
build: {
12+
emptyOutDir: true,
13+
outDir: path.resolve(root, '../../dist'),
14+
rollupOptions: {
15+
input: {
16+
background: path.resolve(root, 'background.js'),
17+
offscreen: path.resolve(root, 'offscreen.html'),
18+
},
19+
output: {
20+
entryFileNames: '[name].js',
21+
chunkFileNames: '[name].js',
22+
assetFileNames: '[name].[ext]'
23+
}
24+
}
25+
},
26+
plugins: [
27+
viteStaticCopy({ targets: [{ src: 'manifest.json', dest: './' }]})
28+
]
29+
});

0 commit comments

Comments
 (0)