99
1010import { build , createServer } from "vite" ;
1111import { svelte } from "@sveltejs/vite-plugin-svelte" ;
12- import {
13- cp ,
14- emptyBuildDir ,
15- sharedServerOptions ,
16- rootDir ,
17- SAFARI_EXT_RESOURCES ,
18- } from "./utils.js" ;
12+ import * as Utils from "./utils.js" ;
1913import https from "node:https" ;
2014
21- /**
22- * Define default vite config options
23- * Disable auto resolving {@link vite.config.js}
24- * @see {@link https://vitejs.dev/config/ Config }
25- * @see {@link https://vitejs.dev/guide/api-javascript.html#inlineconfig configFile }
26- * @type {import("vite").InlineConfig }
27- */
28- const defineConfig = {
29- configFile : false ,
30- envFile : false ,
31- root : await rootDir ( ) ,
32- base : "./" ,
15+ /** @type {import("vite").InlineConfig } */
16+ const sharedConfig = {
17+ ...Utils . baseConfig ,
3318 mode : "development" ,
3419 define : {
35- "import.meta.env.BROWSER" : JSON . stringify ( "Safari" ) ,
36- "import.meta.env.NATIVE_APP" : JSON . stringify ( "app" ) ,
20+ ...Utils . baseConfig . define ,
3721 "import.meta.env.SAFARI_VERSION" : JSON . stringify ( 16.4 ) ,
38- "import.meta.env.SAFARI_PLATFORM" : JSON . stringify (
39- process . env . SAFARI_PLATFORM ,
40- ) ,
4122 } ,
4223} ;
4324
@@ -51,19 +32,19 @@ async function buildResources(server, origin) {
5132 * empty resources directory
5233 * copy public static assets
5334 */
54- await emptyBuildDir ( "dist" ) ;
55- await emptyBuildDir ( SAFARI_EXT_RESOURCES ) ;
56- cp ( "public/ext/shared" , SAFARI_EXT_RESOURCES ) ;
57- cp ( "public/ext/shared-dev" , SAFARI_EXT_RESOURCES ) ;
35+ await Utils . emptyBuildDir ( "dist" ) ;
36+ await Utils . emptyBuildDir ( Utils . SAFARI_EXT_RESOURCES ) ;
37+ Utils . cp ( "public/ext/shared" , Utils . SAFARI_EXT_RESOURCES ) ;
38+ Utils . cp ( "public/ext/shared-dev" , Utils . SAFARI_EXT_RESOURCES ) ;
5839 if ( process . env . SAFARI_PLATFORM === "ios" ) {
59- cp (
40+ Utils . cp (
6041 "public/ext/safari-dev/manifest-ios.json" ,
61- `${ SAFARI_EXT_RESOURCES } /manifest.json` ,
42+ `${ Utils . SAFARI_EXT_RESOURCES } /manifest.json` ,
6243 ) ;
6344 } else {
64- cp (
45+ Utils . cp (
6546 "public/ext/safari-dev/manifest-mac.json" ,
66- `${ SAFARI_EXT_RESOURCES } /manifest.json` ,
47+ `${ Utils . SAFARI_EXT_RESOURCES } /manifest.json` ,
6748 ) ;
6849 }
6950
@@ -75,7 +56,7 @@ async function buildResources(server, origin) {
7556 ] . forEach ( ( input ) => {
7657 /** build proxy content scripts replace actual code */
7758 build ( {
78- ...defineConfig ,
59+ ...sharedConfig ,
7960 plugins : [
8061 {
8162 name : "generate-content-proxy" ,
@@ -100,7 +81,7 @@ async function buildResources(server, origin) {
10081 } ,
10182 ] ,
10283 build : {
103- outDir : `${ SAFARI_EXT_RESOURCES } /dist/content-scripts/` ,
84+ outDir : `${ Utils . SAFARI_EXT_RESOURCES } /dist/content-scripts/` ,
10485 emptyOutDir : false ,
10586 copyPublicDir : false ,
10687 rollupOptions : {
@@ -111,7 +92,7 @@ async function buildResources(server, origin) {
11192 } ) ;
11293 /** build content scripts for dev server and watch changes */
11394 build ( {
114- ...defineConfig ,
95+ ...sharedConfig ,
11596 build : {
11697 outDir : `dist/content-scripts/` ,
11798 emptyOutDir : false ,
@@ -128,7 +109,7 @@ async function buildResources(server, origin) {
128109
129110 /** generate entrance dist */
130111 build ( {
131- ...defineConfig ,
112+ ...sharedConfig ,
132113 publicDir : "public/ext/vendor/" ,
133114 plugins : [
134115 /**
@@ -147,7 +128,7 @@ async function buildResources(server, origin) {
147128 } ,
148129 ] ,
149130 build : {
150- outDir : `${ SAFARI_EXT_RESOURCES } /dist/` ,
131+ outDir : `${ Utils . SAFARI_EXT_RESOURCES } /dist/` ,
151132 emptyOutDir : false ,
152133 rollupOptions : {
153134 input : {
@@ -170,7 +151,7 @@ async function buildResources(server, origin) {
170151 * @see {@link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_Security_Policy }
171152 * @see {@link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_security_policy }
172153 */
173- const serverOptions = await sharedServerOptions ( ) ;
154+ const serverOptions = await Utils . sharedServerOptions ( ) ;
174155const SERVER_PORT = 55173 ;
175156const SERVER_ORIGIN = `https://userscripts.test:${ SERVER_PORT } ` ;
176157
@@ -204,7 +185,7 @@ async function serverCheck(url) {
204185( async ( ) => {
205186 /** run development server */
206187 const server = await createServer ( {
207- ...defineConfig ,
188+ ...sharedConfig ,
208189 plugins : [ svelte ( ) ] ,
209190 server : {
210191 host : true ,
0 commit comments