@openworkers/adapter-static 0.1.2 → 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.
- package/README.md +3 -3
- package/bin/cli.js +0 -0
- package/files/worker.js +1 -1
- package/index.js +28 -30
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,9 +48,9 @@ await adapt({
|
|
|
48
48
|
|
|
49
49
|
```
|
|
50
50
|
dist/
|
|
51
|
-
├──
|
|
52
|
-
├──
|
|
53
|
-
└──
|
|
51
|
+
├── _worker.js # Worker serving files via ASSETS binding
|
|
52
|
+
├── _routes.json # Route manifest for edge routing
|
|
53
|
+
└── assets/ # Static files
|
|
54
54
|
```
|
|
55
55
|
|
|
56
56
|
## Routing Modes
|
package/bin/cli.js
CHANGED
|
File without changes
|
package/files/worker.js
CHANGED
|
@@ -458,7 +458,7 @@ var Mime_default = Mime;
|
|
|
458
458
|
var index_lite_default = new Mime_default(standard_default)._freeze();
|
|
459
459
|
|
|
460
460
|
// src/worker.js
|
|
461
|
-
import
|
|
461
|
+
import routes from "ROUTES";
|
|
462
462
|
function isImmutable(pathname) {
|
|
463
463
|
for (const pattern of routes.immutable) {
|
|
464
464
|
if (pattern.endsWith("/*")) {
|
package/index.js
CHANGED
|
@@ -29,13 +29,10 @@ export async function adapt(options = {}) {
|
|
|
29
29
|
throw new Error(`Input directory not found: ${input}`);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
const tmp = resolve(out, '.tmp');
|
|
33
|
-
|
|
34
32
|
// Clean and create output directories
|
|
35
33
|
rmSync(out, { recursive: true, force: true });
|
|
36
34
|
mkdirSync(out, { recursive: true });
|
|
37
35
|
mkdirSync(join(out, 'assets'), { recursive: true });
|
|
38
|
-
mkdirSync(tmp, { recursive: true });
|
|
39
36
|
|
|
40
37
|
// Copy all files to assets/
|
|
41
38
|
cpSync(input, join(out, 'assets'), { recursive: true });
|
|
@@ -46,24 +43,45 @@ export async function adapt(options = {}) {
|
|
|
46
43
|
// Detect immutable patterns
|
|
47
44
|
const immutable = options.immutable ?? detectImmutable(join(out, 'assets'));
|
|
48
45
|
|
|
49
|
-
// Generate
|
|
50
|
-
const
|
|
46
|
+
// Generate _routes.json manifest for edge router
|
|
47
|
+
const allFiles = listFiles(join(out, 'assets'));
|
|
48
|
+
const staticFiles = allFiles.filter((f) => !isImmutablePath(f, immutable));
|
|
49
|
+
|
|
50
|
+
// Split HTML files and non-HTML files
|
|
51
|
+
const htmlFiles = staticFiles.filter((f) => f.endsWith('.html'));
|
|
52
|
+
const nonHtmlFiles = staticFiles.filter((f) => !f.endsWith('.html'));
|
|
53
|
+
|
|
54
|
+
// Generate prerendered routes (HTML files without .html extension)
|
|
55
|
+
const prerenderedRoutes = htmlFiles.map((f) => {
|
|
56
|
+
const path = '/' + f.replace(/\.html$/, '');
|
|
57
|
+
// Convert /index to / and /foo/index to /foo
|
|
58
|
+
return path.replace(/\/index$/, '/');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const manifest = {
|
|
51
62
|
mode,
|
|
52
63
|
fallback,
|
|
53
|
-
immutable
|
|
64
|
+
immutable,
|
|
65
|
+
// Static: all files with their original names (including .html)
|
|
66
|
+
static: staticFiles.map((f) => '/' + f),
|
|
67
|
+
// Prerendered: HTML files without .html extension
|
|
68
|
+
prerendered: prerenderedRoutes,
|
|
69
|
+
functions: [],
|
|
70
|
+
// If fallback is set, worker handles all unmatched routes (SPA mode)
|
|
71
|
+
ssr: fallback ? ['/*'] : []
|
|
54
72
|
};
|
|
55
73
|
|
|
56
|
-
writeFileSync(join(
|
|
74
|
+
writeFileSync(join(out, '_routes.json'), JSON.stringify(manifest, null, 2));
|
|
57
75
|
|
|
58
|
-
// Bundle worker with esbuild
|
|
76
|
+
// Bundle worker with esbuild (use _routes.json for ROUTES alias)
|
|
59
77
|
await build({
|
|
60
78
|
entryPoints: [fileURLToPath(new URL('./files/worker.js', import.meta.url).href)],
|
|
61
79
|
bundle: true,
|
|
62
80
|
format: 'esm',
|
|
63
81
|
platform: 'browser',
|
|
64
|
-
outfile: resolve(out, '
|
|
82
|
+
outfile: resolve(out, '_worker.js'),
|
|
65
83
|
alias: {
|
|
66
|
-
ROUTES: resolve(
|
|
84
|
+
ROUTES: resolve(out, '_routes.json')
|
|
67
85
|
},
|
|
68
86
|
minify: false,
|
|
69
87
|
banner: {
|
|
@@ -71,26 +89,6 @@ export async function adapt(options = {}) {
|
|
|
71
89
|
}
|
|
72
90
|
});
|
|
73
91
|
|
|
74
|
-
// Generate routes.js manifest for edge router
|
|
75
|
-
const allFiles = listFiles(join(out, 'assets'));
|
|
76
|
-
const staticFiles = allFiles.filter((f) => !isImmutablePath(f, immutable));
|
|
77
|
-
|
|
78
|
-
const manifest = {
|
|
79
|
-
immutable,
|
|
80
|
-
static: staticFiles.map((f) => '/' + f),
|
|
81
|
-
fallback
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
writeFileSync(
|
|
85
|
-
join(out, 'routes.js'),
|
|
86
|
-
`// Generated by ${name} v${version}\n` +
|
|
87
|
-
`// Used by OpenWorkers edge router for static content\n\n` +
|
|
88
|
-
`export default ${JSON.stringify(manifest, null, '\t')};\n`
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
// Cleanup tmp
|
|
92
|
-
rmSync(tmp, { recursive: true, force: true });
|
|
93
|
-
|
|
94
92
|
console.log(`Built static site for OpenWorkers:`);
|
|
95
93
|
console.log(` Input: ${input}`);
|
|
96
94
|
console.log(` Output: ${out}`);
|