@textadventures/vite-plugin-squiffy 7.0.0-beta.1 → 7.0.0-beta.2
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.
- package/README.md +2 -0
- package/dist/index.js +52 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -37,6 +37,8 @@ A `.squiffy` import exports:
|
|
|
37
37
|
- `story` (also the default export): the compiled story, to pass to `init()`
|
|
38
38
|
- `title`: the story's `@title`
|
|
39
39
|
|
|
40
|
+
If the story's folder has a [`squiffy.json` project file](https://squiffystory.com/basic-concepts/#stories-in-several-files), importing its main file builds the whole story, with every `.squiffy` file in the folder, as the command-line tool does. Importing any other `.squiffy` file builds just that file. In dev mode, adding or removing a `.squiffy` file in the folder rebuilds the story.
|
|
41
|
+
|
|
40
42
|
Files pulled in with `@import` are handled too. Other `.squiffy` files are compiled into the story, and Vite watches them for changes. `.js` and `.css` files are bundled by Vite, so `.js` files are loaded as ES modules. If the story fails to compile, the build fails, and in dev mode the errors appear in Vite's error overlay.
|
|
41
43
|
|
|
42
44
|
## Updating the story without reloading
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import path from "path";
|
|
|
2
2
|
import fs from "fs/promises";
|
|
3
3
|
import { glob } from "glob";
|
|
4
4
|
import { compile } from "squiffy-compiler";
|
|
5
|
+
import { findStory, StoryNotFoundError } from "squiffy-compiler/node";
|
|
5
6
|
const toImportPath = (from, to) => {
|
|
6
7
|
const relative = path.relative(path.dirname(from), to).split(path.sep).join("/");
|
|
7
8
|
return relative.startsWith(".") ? relative : "./" + relative;
|
|
@@ -13,7 +14,14 @@ const externalFiles = (inputFilename, readFiles) => {
|
|
|
13
14
|
return {
|
|
14
15
|
getMatchingFilenames: async (pattern) => {
|
|
15
16
|
const result = await glob(path.join(basePath, pattern).split(path.sep).join("/"), { absolute: true });
|
|
16
|
-
|
|
17
|
+
// Sort the matches, as the editor and CLI do, since glob's order isn't
|
|
18
|
+
// guaranteed and decides which same-named section wins.
|
|
19
|
+
const relative = (filename) => path.relative(basePath, filename).split(path.sep).join("/");
|
|
20
|
+
return result
|
|
21
|
+
.filter(filename => !readFiles.includes(filename))
|
|
22
|
+
.map(filename => ({ filename, key: relative(filename) }))
|
|
23
|
+
.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0)
|
|
24
|
+
.map(f => f.filename);
|
|
17
25
|
},
|
|
18
26
|
getContent: async (filename) => {
|
|
19
27
|
const resolvedPath = path.resolve(basePath, filename);
|
|
@@ -25,9 +33,30 @@ const externalFiles = (inputFilename, readFiles) => {
|
|
|
25
33
|
};
|
|
26
34
|
export default function squiffyPlugin(options = {}) {
|
|
27
35
|
const { verbose = false } = options;
|
|
36
|
+
// Each loaded project's main file (the module id), by the folder its squiffy.json is in. A
|
|
37
|
+
// .squiffy file added to or removed from that folder changes the story, but Vite only watches
|
|
38
|
+
// files the story has already read, so the dev server is told to rebuild it.
|
|
39
|
+
const projectFolders = new Map();
|
|
40
|
+
let server;
|
|
41
|
+
const onFileAddedOrRemoved = (file) => {
|
|
42
|
+
if (!server || !file.endsWith(".squiffy"))
|
|
43
|
+
return;
|
|
44
|
+
for (const [id, folder] of projectFolders) {
|
|
45
|
+
if (!path.relative(folder, file).startsWith("..")) {
|
|
46
|
+
const module = server.moduleGraph.getModuleById(id);
|
|
47
|
+
if (module)
|
|
48
|
+
void server.reloadModule(module);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
28
52
|
return {
|
|
29
53
|
name: "vite-plugin-squiffy",
|
|
30
54
|
enforce: "pre",
|
|
55
|
+
configureServer(devServer) {
|
|
56
|
+
server = devServer;
|
|
57
|
+
devServer.watcher.on("add", onFileAddedOrRemoved);
|
|
58
|
+
devServer.watcher.on("unlink", onFileAddedOrRemoved);
|
|
59
|
+
},
|
|
31
60
|
async load(id) {
|
|
32
61
|
const filename = id.split("?")[0];
|
|
33
62
|
if (!filename.endsWith(".squiffy"))
|
|
@@ -35,16 +64,38 @@ export default function squiffyPlugin(options = {}) {
|
|
|
35
64
|
if (verbose) {
|
|
36
65
|
this.info(`compiling ${filename}`);
|
|
37
66
|
}
|
|
67
|
+
// The same rule as the CLI: importing a project's main file builds the whole project in
|
|
68
|
+
// its squiffy.json, and importing any other file builds just that file.
|
|
69
|
+
let story;
|
|
70
|
+
try {
|
|
71
|
+
story = await findStory(filename);
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
if (e instanceof StoryNotFoundError)
|
|
75
|
+
this.error(e.message);
|
|
76
|
+
throw e;
|
|
77
|
+
}
|
|
78
|
+
if (story.note && verbose) {
|
|
79
|
+
this.info(story.note);
|
|
80
|
+
}
|
|
38
81
|
const readFiles = [filename];
|
|
39
82
|
const result = await compile({
|
|
40
83
|
scriptBaseFilename: path.basename(filename),
|
|
41
84
|
script: (await fs.readFile(filename)).toString(),
|
|
42
85
|
onWarning: message => this.warn(message),
|
|
43
86
|
externalFiles: externalFiles(filename, readFiles),
|
|
87
|
+
storyFiles: story.storyFiles,
|
|
44
88
|
});
|
|
45
89
|
for (const file of readFiles.slice(1)) {
|
|
46
90
|
this.addWatchFile(file);
|
|
47
91
|
}
|
|
92
|
+
if (story.projectFile) {
|
|
93
|
+
this.addWatchFile(story.projectFile);
|
|
94
|
+
projectFolders.set(id, path.dirname(story.projectFile));
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
projectFolders.delete(id);
|
|
98
|
+
}
|
|
48
99
|
if (!result.success) {
|
|
49
100
|
this.error(`Failed to compile ${filename}:\n${result.errors.join("\n")}`);
|
|
50
101
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@textadventures/vite-plugin-squiffy",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.2",
|
|
4
4
|
"description": "Vite plugin that lets you import .squiffy files into a web app",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -37,9 +37,9 @@
|
|
|
37
37
|
"author": "Alex Warren",
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@textadventures/squiffy-types": "^7.0.0-beta.
|
|
40
|
+
"@textadventures/squiffy-types": "^7.0.0-beta.2",
|
|
41
41
|
"glob": "^14.0.2",
|
|
42
|
-
"squiffy-compiler": "^7.0.0-beta.
|
|
42
|
+
"squiffy-compiler": "^7.0.0-beta.2"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"vite": ">=6.0.0"
|
|
@@ -50,5 +50,5 @@
|
|
|
50
50
|
"vite": "^8.3.5",
|
|
51
51
|
"vitest": "^5.0.5"
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "2913cee0b823e2fadceb48d89c7029f2365974d9"
|
|
54
54
|
}
|