@zikojs/notebook-vite-resolver 0.0.0-stage → 0.1.1
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 +21 -0
- package/package.json +24 -3
- package/src/index.js +156 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ZAKARIA ELALAOUI
|
|
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.
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zikojs/notebook-vite-resolver",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"zikojs",
|
|
11
|
+
"resolver",
|
|
12
|
+
"vite"
|
|
13
|
+
],
|
|
14
|
+
"author": "zakaria elalaoui",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/zikojs/notebook.git"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"ziko": "2.0.0-beta.17.1"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
26
|
+
}
|
|
6
27
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
const MANIFEST_FILE = "ziko-notebook-imports.json";
|
|
5
|
+
const ENDPOINT = "/__notebook_resolve";
|
|
6
|
+
|
|
7
|
+
function safeName(specifier) {
|
|
8
|
+
return specifier.replace(/[^a-zA-Z0-9_-]+/g, "-");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default function notebookViteResolver({
|
|
12
|
+
modules = [],
|
|
13
|
+
manifestFileName = MANIFEST_FILE,
|
|
14
|
+
} = {}) {
|
|
15
|
+
let command;
|
|
16
|
+
let root;
|
|
17
|
+
let base = "/";
|
|
18
|
+
const entries = new Map();
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
name: "ziko-notebook-vite-resolver",
|
|
22
|
+
|
|
23
|
+
configResolved(config) {
|
|
24
|
+
command = config.command;
|
|
25
|
+
root = config.root;
|
|
26
|
+
base = config.base;
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
configureServer(server) {
|
|
30
|
+
server.middlewares.use(ENDPOINT, async (req, res) => {
|
|
31
|
+
res.setHeader("Content-Type", "application/json");
|
|
32
|
+
res.setHeader("Cache-Control", "no-store");
|
|
33
|
+
|
|
34
|
+
if (req.method !== "GET") {
|
|
35
|
+
res.statusCode = 405;
|
|
36
|
+
res.end(JSON.stringify({ error: "Method not allowed" }));
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const requestUrl = new URL(
|
|
42
|
+
req.url || "/",
|
|
43
|
+
"http://localhost",
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const specifier = requestUrl.searchParams.get("specifier");
|
|
47
|
+
|
|
48
|
+
if (
|
|
49
|
+
!specifier ||
|
|
50
|
+
specifier.startsWith(".") ||
|
|
51
|
+
specifier.startsWith("/") ||
|
|
52
|
+
specifier.startsWith("#") ||
|
|
53
|
+
/^(?:https?:|data:|blob:)/i.test(specifier)
|
|
54
|
+
) {
|
|
55
|
+
res.statusCode = 400;
|
|
56
|
+
res.end(JSON.stringify({ error: "Invalid package specifier" }));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Resolve relative to the Vite project, not the notebook package.
|
|
61
|
+
const importer = path.join(
|
|
62
|
+
server.config.root,
|
|
63
|
+
"__ziko_notebook_resolver__.js",
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const resolved = await server.pluginContainer.resolveId(
|
|
67
|
+
specifier,
|
|
68
|
+
importer,
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
if (
|
|
72
|
+
!resolved ||
|
|
73
|
+
resolved.external ||
|
|
74
|
+
resolved.id.startsWith("\0") ||
|
|
75
|
+
resolved.id.startsWith("virtual:")
|
|
76
|
+
) {
|
|
77
|
+
res.statusCode = 404;
|
|
78
|
+
res.end(JSON.stringify({ error: "Local package not found" }));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Let Vite serve and transform the resolved source file.
|
|
83
|
+
const [file, ...queryParts] = resolved.id.split("?");
|
|
84
|
+
const query = queryParts.length
|
|
85
|
+
? `?${queryParts.join("?")}`
|
|
86
|
+
: "";
|
|
87
|
+
|
|
88
|
+
if (!path.isAbsolute(file)) {
|
|
89
|
+
res.statusCode = 404;
|
|
90
|
+
res.end(JSON.stringify({ error: "Not a local file module" }));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const url = server.resolvedUrls
|
|
95
|
+
? `/@fs/${file.replace(/\\/g, "/")}${query}`
|
|
96
|
+
: `/@fs/${file.replace(/\\/g, "/")}${query}`;
|
|
97
|
+
|
|
98
|
+
res.statusCode = 200;
|
|
99
|
+
res.end(JSON.stringify({ url }));
|
|
100
|
+
} catch {
|
|
101
|
+
res.statusCode = 404;
|
|
102
|
+
res.end(JSON.stringify({ error: "Local resolution failed" }));
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
async buildStart() {
|
|
108
|
+
if (command !== "build") return;
|
|
109
|
+
|
|
110
|
+
entries.clear();
|
|
111
|
+
|
|
112
|
+
for (const specifier of new Set(modules)) {
|
|
113
|
+
const importer = path.join(root, "__ziko_notebook_resolver__.js");
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const resolved = await this.resolve(specifier, importer);
|
|
117
|
+
|
|
118
|
+
if (!resolved || resolved.external) {
|
|
119
|
+
this.warn(`Notebook package could not be bundled: ${specifier}`);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const referenceId = this.emitFile({
|
|
124
|
+
type: "chunk",
|
|
125
|
+
id: resolved.id,
|
|
126
|
+
name: `notebook-${safeName(specifier)}`,
|
|
127
|
+
preserveSignature: "strict",
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
entries.set(specifier, referenceId);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
this.warn(
|
|
133
|
+
`Failed to bundle notebook package "${specifier}": ${error.message}`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
generateBundle() {
|
|
140
|
+
if (command !== "build") return;
|
|
141
|
+
|
|
142
|
+
const imports = {};
|
|
143
|
+
|
|
144
|
+
for (const [specifier, referenceId] of entries) {
|
|
145
|
+
const fileName = this.getFileName(referenceId);
|
|
146
|
+
imports[specifier] = `${base}${fileName}`.replace(/([^:]\/)\/+/g, "$1");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this.emitFile({
|
|
150
|
+
type: "asset",
|
|
151
|
+
fileName: manifestFileName,
|
|
152
|
+
source: JSON.stringify(imports, null, 2),
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
package/README.md
DELETED