vite-plugin-fvtt 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +136 -126
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -95,26 +95,36 @@ function createPartialViteConfig(config) {
95
95
  const base = config.base ?? `/${context.manifest?.manifestType}s/${context.manifest?.id}/`;
96
96
  const useEsModules = context.manifest?.esmodules.length === 1;
97
97
  const formats = useEsModules ? ["es"] : ["umd"];
98
- const fileName = (useEsModules ? context.manifest?.esmodules[0] : context.manifest?.scripts?.[0]) ?? "bundle";
99
- if (fileName === "bundle") logger_default.warn("No output file specified in manifest, using default \"bundle\"");
98
+ const fileName = (useEsModules ? context.manifest?.esmodules[0] : context.manifest?.scripts?.[0]) ?? "scripts/bundle.js";
99
+ if (!(useEsModules || context.manifest?.scripts?.[0])) logger_default.warn("No output file specified in manifest, using default \"bundle\" in the \"scripts/\" folder");
100
100
  if (!context.manifest?.styles?.length) logger_default.warn("No CSS file found in manifest");
101
- const cssFileName = posix.parse(context.manifest?.styles[0] ?? "bundle.css").name;
101
+ const cssFileName = context.manifest?.styles[0] ?? "styles/bundle.css";
102
+ if (!context.manifest?.styles[0]) logger_default.warn("No output css file specified in manifest, using default \"bundle\" in the \"styles/\" folder");
102
103
  const foundryPort = context.env?.foundryPort ?? 3e4;
103
104
  const foundryUrl = context.env?.foundryUrl ?? "localhost";
104
105
  const entry = (config.build?.lib)?.entry;
105
106
  if (!entry) logger_default.fail("Entry must be specified in lib");
106
107
  if (typeof entry !== "string") logger_default.fail("Only a singular string entry is supported for build.lib.entry");
108
+ const isWatch = process.argv.includes("--watch") || !!config.build?.watch;
107
109
  return {
108
110
  base,
109
111
  build: {
110
- minify: "esbuild",
112
+ emptyOutDir: config.build?.emptyOutDir ?? !isWatch,
111
113
  lib: {
112
- cssFileName,
113
114
  entry,
114
- fileName,
115
115
  formats,
116
- name: context.manifest?.id
117
- }
116
+ name: context.manifest?.id ?? "bundle",
117
+ cssFileName: "bundle"
118
+ },
119
+ minify: "esbuild",
120
+ rollupOptions: { output: {
121
+ entryFileNames: fileName,
122
+ assetFileNames: (assetInfo) => {
123
+ const names = assetInfo.names ?? [];
124
+ if (names.some((n) => n.endsWith(".css"))) return cssFileName;
125
+ return "[name][extname]";
126
+ }
127
+ } }
118
128
  },
119
129
  define: { __FVTT_PLUGIN__: {
120
130
  id: context.manifest?.id,
@@ -133,85 +143,6 @@ function createPartialViteConfig(config) {
133
143
  };
134
144
  }
135
145
 
136
- //#endregion
137
- //#region src/server/hmr-client.ts
138
- var hmr_client_default = `
139
- if (import.meta.hot) {
140
- const FVTT_PLUGIN = __FVTT_PLUGIN__
141
-
142
- function refreshApplications(path = null) {
143
- // AppV1 refresh
144
- Object.values(foundry.ui.windows).forEach(app => app.render(true))
145
- // AppV2 refresh
146
- if (path)
147
- foundry.applications.instances.forEach(appV2 => {
148
- Object.values(appV2.constructor.PARTS ?? {}).forEach(part => {
149
- const templates = Array.isArray(part.templates) ? part.templates : []
150
- if (part.template === path || templates.includes(path)) appV2.render(true)
151
- })
152
- })
153
- else foundry.applications.instances.forEach(appV2 => appV2.render(true))
154
- }
155
-
156
- import.meta.hot.on('foundryvtt-template-update', ({ path }) => {
157
- game.socket.emit('template', path, response => {
158
- if (response.error) new Error(response.error)
159
- let template = undefined
160
- try {
161
- template = Handlebars.compile(response.html)
162
- } catch (error) {
163
- console.error(error)
164
- return
165
- }
166
- Handlebars.registerPartial(path, template)
167
- console.log(\`Vite | Retrieved and compiled template \${path}\`)
168
- refreshApplications(path)
169
- })
170
- })
171
-
172
- async function hmrLanguage(lang, targetObject = game.i18n.translations) {
173
- try {
174
- const languages = FVTT_PLUGIN.isSystem
175
- ? game.system.languages
176
- : game.modules.get(FVTT_PLUGIN.id)?.languages
177
- if (!languages) {
178
- console.warn(
179
- 'Vite | Got a HMR request to reload languages, however no languages were found.',
180
- )
181
- return
182
- }
183
- const langEntry = languages.find(l => l.lang === lang)
184
- if (!langEntry) {
185
- console.warn('Vite | Got an HMR request for an undefined language')
186
- return
187
- }
188
-
189
- const url = langEntry.path
190
- const resp = await fetch(url)
191
- if (!resp.ok) throw new Error('Failed to fetch language file!')
192
-
193
- const json = await resp.json()
194
-
195
- foundry.utils.mergeObject(targetObject, json)
196
- console.log(\`Vite | HMR: Reloaded language '\${lang}'\`)
197
- } catch (error) {
198
- console.error(\`Vite | HMR: Error reloading language '\${lang}' for \${FVTT_PLUGIN.id}\`, error);
199
- }
200
- }
201
-
202
- import.meta.hot.on('foundryvtt-language-update', async () => {
203
- const currentLang = game.i18n.lang
204
- const promises = []
205
- if (currentLang !== 'en') {
206
- promises.push(hmrLanguage('en', game.i18n._fallback))
207
- }
208
- promises.push(hmrLanguage(currentLang))
209
- await Promise.all(promises)
210
- refreshApplications()
211
- })
212
- } else console.error('Vite | HMR is disabled')
213
- //`;
214
-
215
146
  //#endregion
216
147
  //#region src/server/trackers/abstract-file-tracker.ts
217
148
  var AbstractFileTracker = class {
@@ -247,19 +178,6 @@ var AbstractFileTracker = class {
247
178
  }
248
179
  };
249
180
 
250
- //#endregion
251
- //#region src/server/trackers/handlebars-tracker.ts
252
- var HandlebarsTracker = class extends AbstractFileTracker {
253
- updateEvent = "foundryvtt-template-update";
254
- constructor() {
255
- super(context.config);
256
- }
257
- getEventData(changedPath, value) {
258
- return { path: value };
259
- }
260
- };
261
- const handlebarsTracker = new HandlebarsTracker();
262
-
263
181
  //#endregion
264
182
  //#region src/server/trackers/language-tracker.ts
265
183
  var LanguageTracker = class extends AbstractFileTracker {
@@ -445,6 +363,46 @@ function transform(dataMap) {
445
363
  return mergedData;
446
364
  }
447
365
 
366
+ //#endregion
367
+ //#region src/language/validator.ts
368
+ function validator() {
369
+ const manifest = context.manifest;
370
+ const baseLanguageData = loadLanguage("en", true);
371
+ if (baseLanguageData.size === 0) {
372
+ logger_default.error("Base language \"en\" not found or could not be loaded.");
373
+ return;
374
+ }
375
+ const base = flattenKeys(baseLanguageData.values().next().value);
376
+ for (const lang of manifest.languages) {
377
+ if (lang.lang === "en") continue;
378
+ const currentLanguageData = loadLanguage(lang.lang, true);
379
+ if (currentLanguageData.size === 0) {
380
+ console.warn(`Summary for language [${lang.lang}]: Could not be loaded.`);
381
+ continue;
382
+ }
383
+ const current = flattenKeys(currentLanguageData.values().next().value);
384
+ const missing = Object.keys(base).filter((key) => !(key in current));
385
+ const extra = Object.keys(current).filter((key) => !(key in base));
386
+ console.log(`Summary for language [${lang.lang}]:`);
387
+ if (missing.length) console.warn(`\tMissing keys: ${missing.length}`, missing.slice(0, 5));
388
+ if (extra.length) console.warn(`\tExtra keys: ${extra.length}`, extra.slice(0, 5));
389
+ if (!missing.length && !extra.length) console.log(" ✅ All keys match.");
390
+ }
391
+ }
392
+
393
+ //#endregion
394
+ //#region src/server/trackers/handlebars-tracker.ts
395
+ var HandlebarsTracker = class extends AbstractFileTracker {
396
+ updateEvent = "foundryvtt-template-update";
397
+ constructor() {
398
+ super(context.config);
399
+ }
400
+ getEventData(changedPath, value) {
401
+ return { path: value };
402
+ }
403
+ };
404
+ const handlebarsTracker = new HandlebarsTracker();
405
+
448
406
  //#endregion
449
407
  //#region src/server/http-middleware.ts
450
408
  function httpMiddlewareHook(server) {
@@ -523,31 +481,83 @@ function setupDevServer(server) {
523
481
  }
524
482
 
525
483
  //#endregion
526
- //#region src/language/validator.ts
527
- function validator() {
528
- const manifest = context.manifest;
529
- const baseLanguageData = loadLanguage("en", true);
530
- if (baseLanguageData.size === 0) {
531
- logger_default.error("Base language \"en\" not found or could not be loaded.");
532
- return;
533
- }
534
- const base = flattenKeys(baseLanguageData.values().next().value);
535
- for (const lang of manifest.languages) {
536
- if (lang.lang === "en") continue;
537
- const currentLanguageData = loadLanguage(lang.lang, true);
538
- if (currentLanguageData.size === 0) {
539
- console.warn(`Summary for language [${lang.lang}]: Could not be loaded.`);
540
- continue;
541
- }
542
- const current = flattenKeys(currentLanguageData.values().next().value);
543
- const missing = Object.keys(base).filter((key) => !(key in current));
544
- const extra = Object.keys(current).filter((key) => !(key in base));
545
- console.log(`Summary for language [${lang.lang}]:`);
546
- if (missing.length) console.warn(`\tMissing keys: ${missing.length}`, missing.slice(0, 5));
547
- if (extra.length) console.warn(`\tExtra keys: ${extra.length}`, extra.slice(0, 5));
548
- if (!missing.length && !extra.length) console.log(" ✅ All keys match.");
549
- }
550
- }
484
+ //#region src/server/hmr-client.ts
485
+ var hmr_client_default = `
486
+ if (import.meta.hot) {
487
+ const FVTT_PLUGIN = __FVTT_PLUGIN__
488
+
489
+ function refreshApplications(path = null) {
490
+ // AppV1 refresh
491
+ Object.values(foundry.ui.windows).forEach(app => app.render(true))
492
+ // AppV2 refresh
493
+ if (path)
494
+ foundry.applications.instances.forEach(appV2 => {
495
+ Object.values(appV2.constructor.PARTS ?? {}).forEach(part => {
496
+ const templates = Array.isArray(part.templates) ? part.templates : []
497
+ if (part.template === path || templates.includes(path)) appV2.render(true)
498
+ })
499
+ })
500
+ else foundry.applications.instances.forEach(appV2 => appV2.render(true))
501
+ }
502
+
503
+ import.meta.hot.on('foundryvtt-template-update', ({ path }) => {
504
+ game.socket.emit('template', path, response => {
505
+ if (response.error) new Error(response.error)
506
+ let template = undefined
507
+ try {
508
+ template = Handlebars.compile(response.html)
509
+ } catch (error) {
510
+ console.error(error)
511
+ return
512
+ }
513
+ Handlebars.registerPartial(path, template)
514
+ console.log(\`Vite | Retrieved and compiled template \${path}\`)
515
+ refreshApplications(path)
516
+ })
517
+ })
518
+
519
+ async function hmrLanguage(lang, targetObject = game.i18n.translations) {
520
+ try {
521
+ const languages = FVTT_PLUGIN.isSystem
522
+ ? game.system.languages
523
+ : game.modules.get(FVTT_PLUGIN.id)?.languages
524
+ if (!languages) {
525
+ console.warn(
526
+ 'Vite | Got a HMR request to reload languages, however no languages were found.',
527
+ )
528
+ return
529
+ }
530
+ const langEntry = languages.find(l => l.lang === lang)
531
+ if (!langEntry) {
532
+ console.warn('Vite | Got an HMR request for an undefined language')
533
+ return
534
+ }
535
+
536
+ const url = langEntry.path
537
+ const resp = await fetch(url)
538
+ if (!resp.ok) throw new Error('Failed to fetch language file!')
539
+
540
+ const json = await resp.json()
541
+
542
+ foundry.utils.mergeObject(targetObject, json)
543
+ console.log(\`Vite | HMR: Reloaded language '\${lang}'\`)
544
+ } catch (error) {
545
+ console.error(\`Vite | HMR: Error reloading language '\${lang}' for \${FVTT_PLUGIN.id}\`, error);
546
+ }
547
+ }
548
+
549
+ import.meta.hot.on('foundryvtt-language-update', async () => {
550
+ const currentLang = game.i18n.lang
551
+ const promises = []
552
+ if (currentLang !== 'en') {
553
+ promises.push(hmrLanguage('en', game.i18n._fallback))
554
+ }
555
+ promises.push(hmrLanguage(currentLang))
556
+ await Promise.all(promises)
557
+ refreshApplications()
558
+ })
559
+ } else console.error('Vite | HMR is disabled')
560
+ //`;
551
561
 
552
562
  //#endregion
553
563
  //#region src/index.ts
@@ -590,7 +600,7 @@ function foundryVTTPlugin() {
590
600
  },
591
601
  load(id) {
592
602
  const config = context.config;
593
- const jsFileName = config.build.lib.fileName;
603
+ const jsFileName = (config.build.rollupOptions?.output).entryFileNames;
594
604
  if (id === jsFileName || id === `/${jsFileName}`) {
595
605
  const entryPath = posix.resolve(config.build.lib.entry);
596
606
  const viteId = `/@fs/${entryPath}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-fvtt",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "A Vite plugin for module and system development for Foundry VTT",
5
5
  "keywords": [
6
6
  "vite",