@textadventures/vite-plugin-squiffy 0.1.4 → 6.0.0-beta.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.
- package/LICENSE +22 -0
- package/README.md +72 -0
- package/client.d.ts +1 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +55 -19
- package/package.json +30 -17
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2026 Alex Warren, textadventures.co.uk and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @textadventures/vite-plugin-squiffy
|
|
2
|
+
|
|
3
|
+
A [Vite](https://vite.dev) plugin that lets you import [Squiffy](https://squiffystory.com) stories into a web app.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install --save-dev @textadventures/vite-plugin-squiffy
|
|
7
|
+
npm install squiffy-runtime
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Add the plugin to `vite.config.ts`:
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { defineConfig } from "vite";
|
|
14
|
+
import squiffy from "@textadventures/vite-plugin-squiffy";
|
|
15
|
+
|
|
16
|
+
export default defineConfig({
|
|
17
|
+
plugins: [squiffy()],
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Then import a `.squiffy` file and run it with the Squiffy runtime:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { init } from "squiffy-runtime";
|
|
25
|
+
import "squiffy-runtime/squiffy.runtime.css";
|
|
26
|
+
import { story, title } from "./game.squiffy";
|
|
27
|
+
|
|
28
|
+
const squiffy = await init({
|
|
29
|
+
element: document.getElementById("squiffy")!,
|
|
30
|
+
story,
|
|
31
|
+
});
|
|
32
|
+
await squiffy.begin();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
A `.squiffy` import exports:
|
|
36
|
+
|
|
37
|
+
- `story` (also the default export): the compiled story, to pass to `init()`
|
|
38
|
+
- `title`: the story's `@title`
|
|
39
|
+
|
|
40
|
+
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
|
+
|
|
42
|
+
## Updating the story without reloading
|
|
43
|
+
|
|
44
|
+
By default, editing a `.squiffy` file reloads the page. To update the running story in place instead, keeping the player's progress, accept the update and pass the new story to the runtime's `update()`:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
if (import.meta.hot) {
|
|
48
|
+
import.meta.hot.accept("./game.squiffy", module => {
|
|
49
|
+
if (module) squiffy.update(module.story);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## TypeScript
|
|
55
|
+
|
|
56
|
+
To get types for `.squiffy` imports, add the plugin's client types to `tsconfig.json`:
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{
|
|
60
|
+
"compilerOptions": {
|
|
61
|
+
"types": ["vite/client", "@textadventures/vite-plugin-squiffy/client"]
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Options
|
|
67
|
+
|
|
68
|
+
| Option | Default | Description |
|
|
69
|
+
|-----------|---------|------------------------------------------|
|
|
70
|
+
| `verbose` | `false` | Log each `.squiffy` file as it compiles. |
|
|
71
|
+
|
|
72
|
+
See [`examples/vite-app`](https://github.com/textadventures/squiffy/tree/main/examples/vite-app) for a complete example.
|
package/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="@textadventures/squiffy-types" />
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import { type Plugin } from 'vite';
|
|
1
|
+
import type { Plugin } from "vite";
|
|
3
2
|
export interface SquiffyPluginOptions {
|
|
4
|
-
/** Log
|
|
3
|
+
/** Log each .squiffy file as it is compiled (defaults to false). */
|
|
5
4
|
verbose?: boolean;
|
|
6
5
|
}
|
|
7
6
|
export default function squiffyPlugin(options?: SquiffyPluginOptions): Plugin;
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,64 @@
|
|
|
1
|
-
import
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs/promises";
|
|
3
|
+
import { glob } from "glob";
|
|
4
|
+
import { compile } from "squiffy-compiler";
|
|
5
|
+
const toImportPath = (from, to) => {
|
|
6
|
+
const relative = path.relative(path.dirname(from), to).split(path.sep).join("/");
|
|
7
|
+
return relative.startsWith(".") ? relative : "./" + relative;
|
|
8
|
+
};
|
|
9
|
+
// Like the CLI's external-files.ts, but also records every file that was read,
|
|
10
|
+
// so that Vite can watch them and rebuild when an imported file changes.
|
|
11
|
+
const externalFiles = (inputFilename, readFiles) => {
|
|
12
|
+
const basePath = path.dirname(inputFilename);
|
|
13
|
+
return {
|
|
14
|
+
getMatchingFilenames: async (pattern) => {
|
|
15
|
+
const result = await glob(path.join(basePath, pattern).split(path.sep).join("/"), { absolute: true });
|
|
16
|
+
return result.filter(filename => !readFiles.includes(filename));
|
|
17
|
+
},
|
|
18
|
+
getContent: async (filename) => {
|
|
19
|
+
const resolvedPath = path.resolve(basePath, filename);
|
|
20
|
+
readFiles.push(resolvedPath);
|
|
21
|
+
return (await fs.readFile(resolvedPath)).toString();
|
|
22
|
+
},
|
|
23
|
+
getLocalFilename: (filename) => filename,
|
|
24
|
+
};
|
|
25
|
+
};
|
|
2
26
|
export default function squiffyPlugin(options = {}) {
|
|
3
27
|
const { verbose = false } = options;
|
|
4
28
|
return {
|
|
5
|
-
name:
|
|
6
|
-
enforce:
|
|
7
|
-
async
|
|
8
|
-
|
|
29
|
+
name: "vite-plugin-squiffy",
|
|
30
|
+
enforce: "pre",
|
|
31
|
+
async load(id) {
|
|
32
|
+
const filename = id.split("?")[0];
|
|
33
|
+
if (!filename.endsWith(".squiffy"))
|
|
9
34
|
return null;
|
|
10
35
|
if (verbose) {
|
|
11
|
-
this.
|
|
36
|
+
this.info(`compiling ${filename}`);
|
|
12
37
|
}
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
38
|
+
const readFiles = [filename];
|
|
39
|
+
const result = await compile({
|
|
40
|
+
scriptBaseFilename: path.basename(filename),
|
|
41
|
+
script: (await fs.readFile(filename)).toString(),
|
|
42
|
+
onWarning: message => this.warn(message),
|
|
43
|
+
externalFiles: externalFiles(filename, readFiles),
|
|
44
|
+
});
|
|
45
|
+
for (const file of readFiles.slice(1)) {
|
|
46
|
+
this.addWatchFile(file);
|
|
47
|
+
}
|
|
48
|
+
if (!result.success) {
|
|
49
|
+
this.error(`Failed to compile ${filename}:\n${result.errors.join("\n")}`);
|
|
50
|
+
}
|
|
51
|
+
// Scripts and stylesheets pulled in with @import are bundled by Vite
|
|
52
|
+
// instead of being added as <script> and <link> tags.
|
|
53
|
+
const uiInfo = result.getUiInfo();
|
|
54
|
+
const imports = [...uiInfo.externalStylesheets, ...uiInfo.externalScripts]
|
|
55
|
+
.map(file => `import ${JSON.stringify(toImportPath(filename, file))};`);
|
|
56
|
+
return [
|
|
57
|
+
...imports,
|
|
58
|
+
await result.getJs(true),
|
|
59
|
+
`export const title = ${JSON.stringify(uiInfo.title)};`,
|
|
60
|
+
"export default story;",
|
|
61
|
+
].join("\n");
|
|
26
62
|
},
|
|
27
63
|
};
|
|
28
64
|
}
|
package/package.json
CHANGED
|
@@ -1,41 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@textadventures/vite-plugin-squiffy",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Vite plugin
|
|
3
|
+
"version": "6.0.0-beta.4",
|
|
4
|
+
"description": "Vite plugin that lets you import .squiffy files into a web app",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/textadventures/squiffy.git",
|
|
8
|
+
"directory": "vite-plugin"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
5
13
|
"type": "module",
|
|
6
14
|
"main": "./dist/index.js",
|
|
7
|
-
"module": "./dist/index.js",
|
|
8
15
|
"types": "./dist/index.d.ts",
|
|
9
16
|
"exports": {
|
|
10
17
|
".": {
|
|
11
18
|
"types": "./dist/index.d.ts",
|
|
12
|
-
"
|
|
13
|
-
}
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./client": {
|
|
22
|
+
"types": "./client.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
14
25
|
},
|
|
15
26
|
"files": [
|
|
16
27
|
"dist",
|
|
28
|
+
"client.d.ts",
|
|
17
29
|
"LICENSE",
|
|
18
30
|
"README.md"
|
|
19
31
|
],
|
|
20
32
|
"scripts": {
|
|
21
|
-
"build": "tsc
|
|
22
|
-
"
|
|
23
|
-
"
|
|
33
|
+
"build": "tsc",
|
|
34
|
+
"test": "vitest --run",
|
|
35
|
+
"dev": "vitest"
|
|
24
36
|
},
|
|
25
37
|
"author": "Alex Warren",
|
|
26
38
|
"license": "MIT",
|
|
27
|
-
"engines": {
|
|
28
|
-
"node": ">=18.0.0"
|
|
29
|
-
},
|
|
30
39
|
"dependencies": {
|
|
31
|
-
"@textadventures/squiffy-types": "^0.
|
|
32
|
-
"
|
|
40
|
+
"@textadventures/squiffy-types": "^6.0.0-beta.4",
|
|
41
|
+
"glob": "^13.0.6",
|
|
42
|
+
"squiffy-compiler": "^6.0.0-beta.4"
|
|
33
43
|
},
|
|
34
44
|
"peerDependencies": {
|
|
35
|
-
"vite": "
|
|
45
|
+
"vite": ">=6.0.0"
|
|
36
46
|
},
|
|
37
47
|
"devDependencies": {
|
|
38
|
-
"
|
|
39
|
-
"typescript": "^
|
|
40
|
-
|
|
48
|
+
"@types/node": "^26.4.1",
|
|
49
|
+
"typescript": "^6.0.3",
|
|
50
|
+
"vite": "^8.2.2",
|
|
51
|
+
"vitest": "^5.0.3"
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "3c52f3730cf2a7f59f3a5ac76db42abd7024fb83"
|
|
41
54
|
}
|