slicejs-cli 4.5.0 → 4.6.0
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/client.js +162 -83
- package/commands/Print.js +158 -164
- package/commands/build/build.js +27 -1
- package/commands/buildProduction/buildProduction.js +42 -71
- package/commands/bundle/bundle.js +65 -60
- package/commands/docs/agents.js +260 -0
- package/commands/docs/events.js +365 -0
- package/commands/docs/index.js +123 -0
- package/commands/doctor/doctor.js +43 -9
- package/commands/export/checkExport.js +163 -0
- package/commands/export/clientRedirects.js +72 -0
- package/commands/export/copyDist.js +180 -0
- package/commands/export/export.js +236 -0
- package/commands/export/files.js +33 -0
- package/commands/export/hosts/cloudflare.js +48 -0
- package/commands/export/hosts/github.js +31 -0
- package/commands/export/hosts/index.js +69 -0
- package/commands/export/hosts/netlify.js +42 -0
- package/commands/export/hosts/nginx.js +73 -0
- package/commands/export/hosts/none.js +23 -0
- package/commands/export/hosts/s3.js +24 -0
- package/commands/export/hosts/shared.js +62 -0
- package/commands/export/hosts/vercel.js +69 -0
- package/commands/export/outDir.js +91 -0
- package/commands/export/prerender.js +121 -0
- package/commands/getComponent/getComponent.js +39 -34
- package/commands/init/init.js +32 -22
- package/commands/lint/lint.js +2 -2
- package/commands/lint/types.js +1 -1
- package/commands/listComponents/listComponents.js +205 -172
- package/commands/seo/check.js +1 -1
- package/commands/seo/images.js +1 -1
- package/commands/seo/sitemap.js +1 -1
- package/commands/setup/lint.js +1 -1
- package/commands/skill/skill.js +2 -2
- package/commands/startServer/startServer.js +60 -21
- package/commands/startServer/watchServer.js +3 -3
- package/commands/translator/check.js +1 -1
- package/commands/translator/init.js +1 -1
- package/commands/typecheck/typecheck.js +2 -2
- package/commands/types/types.js +22 -3
- package/commands/utils/VersionChecker.js +14 -14
- package/commands/utils/bundling/BundleGenerator.js +45 -27
- package/commands/utils/bundling/DependencyAnalyzer.js +38 -17
- package/commands/utils/componentCatalog.js +120 -0
- package/commands/utils/publicEnv.js +74 -0
- package/commands/utils/updateManager.js +12 -12
- package/package.json +22 -23
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// commands/export/export.js
|
|
2
|
+
//
|
|
3
|
+
// `slice export`: turn the production build into a folder a static host can serve
|
|
4
|
+
// without Express. This file is orchestration only; copy rules, prerender, the
|
|
5
|
+
// check and each host live in their own module.
|
|
6
|
+
import fs from 'fs-extra';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import Print from '../Print.js';
|
|
9
|
+
import { getProjectRoot, getDistPath } from '../utils/PathHelper.js';
|
|
10
|
+
import build from '../build/build.js';
|
|
11
|
+
import { resolveSafeOutDir } from './outDir.js';
|
|
12
|
+
import { copyDist, ensureEnv, flattenPublic } from './copyDist.js';
|
|
13
|
+
import { buildPrerender } from './prerender.js';
|
|
14
|
+
import { checkExport } from './checkExport.js';
|
|
15
|
+
import { listFiles, writePage } from './files.js';
|
|
16
|
+
import { writeClientRedirects, writeClientSpa404 } from './clientRedirects.js';
|
|
17
|
+
import { detectHost, getHost } from './hosts/index.js';
|
|
18
|
+
|
|
19
|
+
function spaLabel(preset, spa) {
|
|
20
|
+
if (!spa) return 'no (unknown routes -> 404.html)';
|
|
21
|
+
if (preset.spaMode === 'omit-404') return 'yes (Cloudflare auto-SPA; no 404.html)';
|
|
22
|
+
if (preset.spaMode === 'client-404') return 'via 404.html (soft 404; host-dependent)';
|
|
23
|
+
return 'yes (unknown routes -> index.html)';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default async function exportStatic(options = {}) {
|
|
27
|
+
const projectRoot = getProjectRoot(import.meta.url);
|
|
28
|
+
const distDir = getDistPath(import.meta.url);
|
|
29
|
+
const checkOnly = Boolean(options.check);
|
|
30
|
+
const dryRun = Boolean(options.dryRun);
|
|
31
|
+
const keepComponents = Boolean(options.keepComponents);
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
Print.warning('slice export is experimental; flags and output may change between releases.');
|
|
35
|
+
|
|
36
|
+
// `--check` only reads, so it can inspect any existing folder. Every write
|
|
37
|
+
// path validates the target before touching it (a typo must not delete the
|
|
38
|
+
// project).
|
|
39
|
+
if (checkOnly) {
|
|
40
|
+
const outDir = path.resolve(projectRoot, options.out || 'dist-static');
|
|
41
|
+
const preset = options.host ? getHost(options.host) : await detectHost(outDir);
|
|
42
|
+
const explicitHost = options.host ? preset.id : null;
|
|
43
|
+
|
|
44
|
+
Print.title('Static export check');
|
|
45
|
+
const { issues, warnings } = await checkExport(outDir, {
|
|
46
|
+
keepComponents: options.keepComponents,
|
|
47
|
+
preset,
|
|
48
|
+
explicitHost
|
|
49
|
+
});
|
|
50
|
+
for (const warning of warnings) Print.warning(warning);
|
|
51
|
+
if (issues.length > 0) {
|
|
52
|
+
for (const issue of issues) Print.error(issue);
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
Print.success(`Export looks valid: ${path.relative(projectRoot, outDir)}`);
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const outDir = await resolveSafeOutDir(projectRoot, options.out);
|
|
60
|
+
const preset = getHost(options.host);
|
|
61
|
+
|
|
62
|
+
if (options.sourcemap) {
|
|
63
|
+
Print.warning('--sourcemap builds maps that the export filters out; they will not be published.');
|
|
64
|
+
}
|
|
65
|
+
if (options.minify === false) {
|
|
66
|
+
Print.warning('--no-minify ships readable bundles; use it only for debugging.');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!options.fromDist) {
|
|
70
|
+
const built = await build({
|
|
71
|
+
minify: options.minify,
|
|
72
|
+
obfuscate: options.obfuscate,
|
|
73
|
+
sourcemap: options.sourcemap,
|
|
74
|
+
hashFilenames: options.hashFilenames,
|
|
75
|
+
compress: options.compress
|
|
76
|
+
});
|
|
77
|
+
if (built === false) return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (!await fs.pathExists(distDir)) {
|
|
81
|
+
throw new Error('No dist/ found. Run "slice build" first, or drop --from-dist.');
|
|
82
|
+
}
|
|
83
|
+
if (!await fs.pathExists(path.join(distDir, 'App', 'index.html'))) {
|
|
84
|
+
throw new Error('dist/App/index.html is missing. Run "slice build" again.');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// A custom host rule inside src/public would be flattened and then silently
|
|
88
|
+
// replaced by the generated preset. Fail instead of losing CSP/redirects.
|
|
89
|
+
const customRuleFiles = (preset.ruleFiles || [])
|
|
90
|
+
.filter((name) => fs.existsSync(path.join(distDir, 'public', name)));
|
|
91
|
+
if (customRuleFiles.length > 0) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`dist/public/${customRuleFiles.join(', ')} would be replaced by --host ${preset.id}. Move custom host rules out of public/ or merge them into the generated files.`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (fs.existsSync(path.join(distDir, 'public', '404.html'))) {
|
|
97
|
+
Print.warning('dist/public/404.html will be replaced by the generated 404.html.');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const prerender = await buildPrerender(projectRoot, distDir, options);
|
|
101
|
+
|
|
102
|
+
if (dryRun) {
|
|
103
|
+
Print.title('Static export (dry run)');
|
|
104
|
+
Print.info(`Would write ${path.relative(projectRoot, outDir)} from ${path.relative(projectRoot, distDir)}.`);
|
|
105
|
+
Print.info(`Host rules: ${preset.id}`);
|
|
106
|
+
if (prerender) {
|
|
107
|
+
Print.info(`${prerender.pages.size} route(s) would be prerendered`);
|
|
108
|
+
if (prerender.seo.redirects.length) {
|
|
109
|
+
Print.info(`${prerender.seo.redirects.length} locale redirect(s)`);
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
Print.info('No SEO manifest; the SPA shell would be the only page.');
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const skippedEntries = [];
|
|
118
|
+
await fs.remove(outDir);
|
|
119
|
+
await copyDist(outDir, distDir, { keepComponents, skipped: skippedEntries });
|
|
120
|
+
await flattenPublic(outDir);
|
|
121
|
+
|
|
122
|
+
// In Express production `/Components` answers 404. Match that in the static
|
|
123
|
+
// export: component source is minified but still source, and every component
|
|
124
|
+
// used at runtime is in a bundle. `--keep-components` is a debugging escape.
|
|
125
|
+
let componentsState = 'none';
|
|
126
|
+
if (keepComponents && await fs.pathExists(path.join(outDir, 'Components'))) {
|
|
127
|
+
componentsState = 'kept';
|
|
128
|
+
} else if (skippedEntries.includes('Components/')) {
|
|
129
|
+
componentsState = 'pruned';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const rootIndex = path.join(outDir, 'index.html');
|
|
133
|
+
let pagesWritten = 0;
|
|
134
|
+
|
|
135
|
+
if (prerender) {
|
|
136
|
+
for (const [url, page] of prerender.pages) {
|
|
137
|
+
await writePage(outDir, url, page);
|
|
138
|
+
pagesWritten += 1;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (!await fs.pathExists(rootIndex)) {
|
|
143
|
+
await fs.copy(path.join(outDir, 'App', 'index.html'), rootIndex);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const redirects = prerender?.seo.redirects || [];
|
|
147
|
+
const spa = options.spa === true || !prerender;
|
|
148
|
+
const cloudflareSpa = preset.spaMode === 'omit-404' && spa;
|
|
149
|
+
const clientSpa = preset.spaMode === 'client-404' && spa;
|
|
150
|
+
const notFoundPage = prerender
|
|
151
|
+
? (prerender.seo.notFound.get(null) || [...prerender.seo.notFound.values()][0])
|
|
152
|
+
: null;
|
|
153
|
+
|
|
154
|
+
if (cloudflareSpa) {
|
|
155
|
+
// Cloudflare Pages only enables its own SPA mode when there is no
|
|
156
|
+
// top-level 404.html. A `_redirects` catch-all is rejected as an infinite
|
|
157
|
+
// loop, and `/* / 200` would hijack every asset.
|
|
158
|
+
await fs.remove(path.join(outDir, '404.html'));
|
|
159
|
+
} else if (clientSpa) {
|
|
160
|
+
// GitHub/S3/none cannot return 200 for unknown routes. The portable
|
|
161
|
+
// fallback is a 404.html that renders the app shell (soft 404).
|
|
162
|
+
await writeClientSpa404(outDir);
|
|
163
|
+
} else if (notFoundPage) {
|
|
164
|
+
await fs.writeFile(path.join(outDir, '404.html'), notFoundPage.html);
|
|
165
|
+
} else {
|
|
166
|
+
await fs.copy(rootIndex, path.join(outDir, '404.html'));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const envKeys = await ensureEnv(outDir, projectRoot);
|
|
170
|
+
const hostFiles = await preset.write({ outDir, redirects, spa });
|
|
171
|
+
|
|
172
|
+
// Hosts without server-side redirect syntax get portable HTML redirect files.
|
|
173
|
+
let clientRedirects = { written: 0, skipped: [] };
|
|
174
|
+
if (preset.clientRedirectPages && redirects.length) {
|
|
175
|
+
clientRedirects = await writeClientRedirects(
|
|
176
|
+
outDir,
|
|
177
|
+
redirects,
|
|
178
|
+
prerender?.seo.origin || '',
|
|
179
|
+
{ allowRoot: !prerender?.pages.has('/') }
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const files = await listFiles(outDir);
|
|
184
|
+
const totalBytes = files.reduce((sum, file) => sum + file.size, 0);
|
|
185
|
+
|
|
186
|
+
Print.title('Static export');
|
|
187
|
+
Print.success(`Exported to ${path.relative(projectRoot, outDir) || '.'}`);
|
|
188
|
+
Print.info(`${files.length} files, ${(totalBytes / 1024 / 1024).toFixed(2)} MB`);
|
|
189
|
+
|
|
190
|
+
if (prerender) {
|
|
191
|
+
const localeLabel = prerender.localeCount ? ` (${prerender.localeCount} locales)` : '';
|
|
192
|
+
Print.info(`${pagesWritten} route(s) prerendered${localeLabel}`);
|
|
193
|
+
if (cloudflareSpa) {
|
|
194
|
+
Print.info('404.html: omitted (Cloudflare auto-SPA)');
|
|
195
|
+
} else {
|
|
196
|
+
Print.info(`404.html${notFoundPage && !clientSpa ? ' (noindex)' : ''}`);
|
|
197
|
+
}
|
|
198
|
+
if (redirects.length) Print.info(`${redirects.length} locale redirect(s)`);
|
|
199
|
+
if (prerender.patternsSkipped) {
|
|
200
|
+
Print.warning(`${prerender.patternsSkipped} dynamic route(s) not prerendered; add sitemap.paths or pass --spa.`);
|
|
201
|
+
}
|
|
202
|
+
} else if (options.prerender === false) {
|
|
203
|
+
Print.warning('Prerender disabled; using the SPA shell (soft 404).');
|
|
204
|
+
} else {
|
|
205
|
+
Print.warning('No seo.json found; using the SPA shell (soft 404).');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
Print.info(`Public env: ${envKeys} key(s) (JSON + ESM)`);
|
|
209
|
+
Print.info(`Host rules: ${hostFiles.length ? hostFiles.join(', ') : 'none'}`);
|
|
210
|
+
Print.info(`SPA fallback: ${spaLabel(preset, spa)}`);
|
|
211
|
+
if (componentsState === 'pruned') {
|
|
212
|
+
Print.info('Components: pruned (source not shipped)');
|
|
213
|
+
} else if (componentsState === 'kept') {
|
|
214
|
+
Print.warning('Components: kept; component source is fetchable.');
|
|
215
|
+
}
|
|
216
|
+
if (skippedEntries.length > 0 && process.env.SLICE_DEBUG) {
|
|
217
|
+
Print.verbose(`Skipped from dist: ${skippedEntries.join(', ')}`);
|
|
218
|
+
}
|
|
219
|
+
if (clientRedirects.written) {
|
|
220
|
+
Print.info(`${clientRedirects.written} client redirect page(s)`);
|
|
221
|
+
}
|
|
222
|
+
for (const skipped of clientRedirects.skipped) {
|
|
223
|
+
Print.warning(`Redirect path already has a page; kept the page: ${skipped}`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
Print.newLine();
|
|
227
|
+
Print.subtitle('Next step');
|
|
228
|
+
Print.commandExample('Serve the export locally', `npx serve ${path.relative(projectRoot, outDir) || '.'}`);
|
|
229
|
+
Print.info('Use "npx serve -s" only to test the SPA fallback: it hides the real 404.html.');
|
|
230
|
+
return true;
|
|
231
|
+
} catch (error) {
|
|
232
|
+
Print.error(`Static export failed: ${error.message}`);
|
|
233
|
+
Print.stack(error);
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// commands/export/files.js
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
export async function listFiles(dir, base = dir) {
|
|
6
|
+
const files = [];
|
|
7
|
+
for (const entry of await fs.readdir(dir)) {
|
|
8
|
+
const full = path.join(dir, entry);
|
|
9
|
+
const stat = await fs.stat(full);
|
|
10
|
+
if (stat.isDirectory()) {
|
|
11
|
+
files.push(...await listFiles(full, base));
|
|
12
|
+
} else {
|
|
13
|
+
files.push({ path: path.relative(base, full).replace(/\\/g, '/'), size: stat.size });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return files;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function pagePathForUrl(url) {
|
|
20
|
+
const clean = String(url).split(/[?#]/)[0].replace(/^\/+|\/+$/g, '');
|
|
21
|
+
if (!clean) return 'index.html';
|
|
22
|
+
const normalized = path.normalize(clean);
|
|
23
|
+
if (normalized.startsWith('..') || path.isAbsolute(normalized)) {
|
|
24
|
+
throw new Error(`Refusing to export an unsafe route: ${url}`);
|
|
25
|
+
}
|
|
26
|
+
return path.join(normalized, 'index.html');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function writePage(outDir, url, page) {
|
|
30
|
+
const file = path.join(outDir, pagePathForUrl(url));
|
|
31
|
+
await fs.ensureDir(path.dirname(file));
|
|
32
|
+
await fs.writeFile(file, page.html);
|
|
33
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// commands/export/hosts/cloudflare.js
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { HEADER_RULES, redirectCatchAll, redirectLines } from './shared.js';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
id: 'cloudflare',
|
|
8
|
+
// Cloudflare Pages only enables its automatic SPA mode when there is no
|
|
9
|
+
// top-level 404.html. A `_redirects` catch-all is rejected as an infinite
|
|
10
|
+
// loop, and `/* / 200` would intercept every asset.
|
|
11
|
+
spaMode: 'omit-404',
|
|
12
|
+
clientRedirectPages: false,
|
|
13
|
+
ruleFiles: ['_redirects', '_headers'],
|
|
14
|
+
|
|
15
|
+
async write({ outDir, redirects }) {
|
|
16
|
+
const redirectBlock = redirects.length ? `${redirectLines(redirects)}\n` : '';
|
|
17
|
+
await fs.writeFile(path.join(outDir, '_redirects'), redirectBlock, 'utf8');
|
|
18
|
+
await fs.writeFile(path.join(outDir, '_headers'), HEADER_RULES, 'utf8');
|
|
19
|
+
return ['_redirects', '_headers'];
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
async check({ outDir, explicitHost }) {
|
|
23
|
+
const issues = [];
|
|
24
|
+
const warnings = [];
|
|
25
|
+
const hasRedirects = await fs.pathExists(path.join(outDir, '_redirects'));
|
|
26
|
+
const hasHeaders = await fs.pathExists(path.join(outDir, '_headers'));
|
|
27
|
+
|
|
28
|
+
if (hasRedirects !== hasHeaders) {
|
|
29
|
+
issues.push('_redirects and _headers must be written together (Cloudflare/Netlify).');
|
|
30
|
+
}
|
|
31
|
+
if (explicitHost && !hasRedirects) {
|
|
32
|
+
issues.push('Cloudflare export is missing _redirects.');
|
|
33
|
+
}
|
|
34
|
+
return { issues, warnings };
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
async spaFallback({ outDir }) {
|
|
38
|
+
const has404 = await fs.pathExists(path.join(outDir, '404.html'));
|
|
39
|
+
if (!has404 && await fs.pathExists(path.join(outDir, '_redirects'))) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
const redirectsFile = path.join(outDir, '_redirects');
|
|
43
|
+
if (await fs.pathExists(redirectsFile)) {
|
|
44
|
+
return redirectCatchAll(await fs.readFile(redirectsFile, 'utf8'));
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// commands/export/hosts/github.js
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { clientSpaFallback } from './shared.js';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
id: 'github',
|
|
8
|
+
// GitHub Pages has no server redirects/headers. `--spa` writes the shell as
|
|
9
|
+
// 404.html; unknown routes render the app with a 404 status.
|
|
10
|
+
spaMode: 'client-404',
|
|
11
|
+
clientRedirectPages: true,
|
|
12
|
+
ruleFiles: [],
|
|
13
|
+
|
|
14
|
+
async write({ outDir }) {
|
|
15
|
+
await fs.writeFile(path.join(outDir, '.nojekyll'), '', 'utf8');
|
|
16
|
+
return ['.nojekyll'];
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
async check({ outDir, explicitHost }) {
|
|
20
|
+
const issues = [];
|
|
21
|
+
const warnings = [];
|
|
22
|
+
if (explicitHost && !await fs.pathExists(path.join(outDir, '.nojekyll'))) {
|
|
23
|
+
issues.push('GitHub Pages export is missing .nojekyll.');
|
|
24
|
+
}
|
|
25
|
+
return { issues, warnings };
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
spaFallback({ outDir }) {
|
|
29
|
+
return clientSpaFallback(outDir);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// commands/export/hosts/index.js
|
|
2
|
+
//
|
|
3
|
+
// Host preset registry. Every preset is a plain object with one job:
|
|
4
|
+
//
|
|
5
|
+
// id stable name used by --host
|
|
6
|
+
// spaMode how --spa is implemented on this host
|
|
7
|
+
// clientRedirectPages whether locale redirects become HTML pages
|
|
8
|
+
// write(ctx) write the host files, return their names
|
|
9
|
+
// check(ctx) validate the host files
|
|
10
|
+
// spaFallback(ctx) whether the export declares an SPA fallback
|
|
11
|
+
//
|
|
12
|
+
// `spaMode` values:
|
|
13
|
+
// omit-404 Cloudflare Pages: no top-level 404.html, automatic SPA
|
|
14
|
+
// redirects Netlify: `/* /index.html 200`
|
|
15
|
+
// rewrite Vercel: `/(.*)` rewrite
|
|
16
|
+
// try-files nginx: `try_files ... /index.html`
|
|
17
|
+
// client-404 GitHub/S3/none: shell as 404.html, status stays 404
|
|
18
|
+
import fs from 'fs-extra';
|
|
19
|
+
import path from 'path';
|
|
20
|
+
import cloudflare from './cloudflare.js';
|
|
21
|
+
import netlify from './netlify.js';
|
|
22
|
+
import vercel from './vercel.js';
|
|
23
|
+
import nginx from './nginx.js';
|
|
24
|
+
import github from './github.js';
|
|
25
|
+
import s3 from './s3.js';
|
|
26
|
+
import none from './none.js';
|
|
27
|
+
import { redirectCatchAll } from './shared.js';
|
|
28
|
+
|
|
29
|
+
const PRESETS = new Map([
|
|
30
|
+
[cloudflare.id, cloudflare],
|
|
31
|
+
[netlify.id, netlify],
|
|
32
|
+
[vercel.id, vercel],
|
|
33
|
+
[nginx.id, nginx],
|
|
34
|
+
[github.id, github],
|
|
35
|
+
[s3.id, s3],
|
|
36
|
+
[none.id, none]
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
export const HOST_IDS = [...PRESETS.keys()];
|
|
40
|
+
|
|
41
|
+
export function getHost(value) {
|
|
42
|
+
const id = String(value || 'cloudflare').toLowerCase();
|
|
43
|
+
const preset = PRESETS.get(id);
|
|
44
|
+
if (!preset) {
|
|
45
|
+
throw new Error(`Unknown host "${value}". Use one of: ${HOST_IDS.join(', ')}.`);
|
|
46
|
+
}
|
|
47
|
+
return preset;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Pick the preset for `--check` when the user did not pass `--host`.
|
|
52
|
+
* The files are the only evidence: Vercel/nginx/GitHub have unique files;
|
|
53
|
+
* Cloudflare and Netlify share `_redirects`/`_headers`, so the catch-all
|
|
54
|
+
* distinguishes them.
|
|
55
|
+
*/
|
|
56
|
+
export async function detectHost(outDir) {
|
|
57
|
+
if (await fs.pathExists(path.join(outDir, 'vercel.json'))) return vercel;
|
|
58
|
+
if (await fs.pathExists(path.join(outDir, 'nginx.conf'))) return nginx;
|
|
59
|
+
if (await fs.pathExists(path.join(outDir, '.nojekyll'))) return github;
|
|
60
|
+
|
|
61
|
+
const redirectsFile = path.join(outDir, '_redirects');
|
|
62
|
+
if (await fs.pathExists(redirectsFile)) {
|
|
63
|
+
const text = await fs.readFile(redirectsFile, 'utf8');
|
|
64
|
+
return redirectCatchAll(text) ? netlify : cloudflare;
|
|
65
|
+
}
|
|
66
|
+
if (await fs.pathExists(path.join(outDir, '_headers'))) return cloudflare;
|
|
67
|
+
|
|
68
|
+
return none;
|
|
69
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// commands/export/hosts/netlify.js
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { HEADER_RULES, redirectCatchAll, redirectLines } from './shared.js';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
id: 'netlify',
|
|
8
|
+
// Netlify's shadowing rules keep existing files first, so the catch-all is
|
|
9
|
+
// safe for assets and gives unknown routes the shell with a 200.
|
|
10
|
+
spaMode: 'redirects',
|
|
11
|
+
clientRedirectPages: false,
|
|
12
|
+
ruleFiles: ['_redirects', '_headers'],
|
|
13
|
+
|
|
14
|
+
async write({ outDir, redirects, spa }) {
|
|
15
|
+
const redirectBlock = redirects.length ? `${redirectLines(redirects)}\n` : '';
|
|
16
|
+
const spaBlock = spa ? '/* /index.html 200\n' : '';
|
|
17
|
+
await fs.writeFile(path.join(outDir, '_redirects'), `${redirectBlock}${spaBlock}`, 'utf8');
|
|
18
|
+
await fs.writeFile(path.join(outDir, '_headers'), HEADER_RULES, 'utf8');
|
|
19
|
+
return ['_redirects', '_headers'];
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
async check({ outDir, explicitHost }) {
|
|
23
|
+
const issues = [];
|
|
24
|
+
const warnings = [];
|
|
25
|
+
const hasRedirects = await fs.pathExists(path.join(outDir, '_redirects'));
|
|
26
|
+
const hasHeaders = await fs.pathExists(path.join(outDir, '_headers'));
|
|
27
|
+
|
|
28
|
+
if (hasRedirects !== hasHeaders) {
|
|
29
|
+
issues.push('_redirects and _headers must be written together (Cloudflare/Netlify).');
|
|
30
|
+
}
|
|
31
|
+
if (explicitHost && !hasRedirects) {
|
|
32
|
+
issues.push('Netlify export is missing _redirects.');
|
|
33
|
+
}
|
|
34
|
+
return { issues, warnings };
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
async spaFallback({ outDir }) {
|
|
38
|
+
const redirectsFile = path.join(outDir, '_redirects');
|
|
39
|
+
if (!await fs.pathExists(redirectsFile)) return false;
|
|
40
|
+
return redirectCatchAll(await fs.readFile(redirectsFile, 'utf8'));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// commands/export/hosts/nginx.js
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { nginxSecurityHeaders } from './shared.js';
|
|
5
|
+
|
|
6
|
+
const security = nginxSecurityHeaders(' ');
|
|
7
|
+
|
|
8
|
+
export default {
|
|
9
|
+
id: 'nginx',
|
|
10
|
+
spaMode: 'try-files',
|
|
11
|
+
clientRedirectPages: false,
|
|
12
|
+
ruleFiles: ['nginx.conf'],
|
|
13
|
+
|
|
14
|
+
async write({ outDir, redirects, spa }) {
|
|
15
|
+
const redirectBlock = redirects
|
|
16
|
+
.map(({ from, to, status }) => `location = ${from} {\n return ${status} ${to};\n}\n`)
|
|
17
|
+
.join('\n');
|
|
18
|
+
const fallback = spa ? 'try_files $uri $uri/ /index.html;' : 'try_files $uri $uri/ =404;';
|
|
19
|
+
|
|
20
|
+
const nginx = `root /var/www/dist-static;
|
|
21
|
+
index index.html;
|
|
22
|
+
|
|
23
|
+
${redirectBlock}location / {
|
|
24
|
+
${fallback}
|
|
25
|
+
${security}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
error_page 404 /404.html;
|
|
29
|
+
|
|
30
|
+
location /bundles/ {
|
|
31
|
+
${security}
|
|
32
|
+
add_header Cache-Control "no-store";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
location = /slice-env.json {
|
|
36
|
+
${security}
|
|
37
|
+
add_header Cache-Control "no-store";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
location = /slice-env.generated.js {
|
|
41
|
+
${security}
|
|
42
|
+
add_header Cache-Control "no-store";
|
|
43
|
+
}
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
await fs.writeFile(path.join(outDir, 'nginx.conf'), nginx, 'utf8');
|
|
47
|
+
return ['nginx.conf'];
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
async check({ outDir, explicitHost }) {
|
|
51
|
+
const issues = [];
|
|
52
|
+
const warnings = [];
|
|
53
|
+
const file = path.join(outDir, 'nginx.conf');
|
|
54
|
+
const hasNginx = await fs.pathExists(file);
|
|
55
|
+
|
|
56
|
+
if (hasNginx) {
|
|
57
|
+
const text = await fs.readFile(file, 'utf8');
|
|
58
|
+
if (!/error_page\s+404\s+\/404\.html/.test(text)) {
|
|
59
|
+
issues.push('nginx.conf does not map 404 to /404.html.');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (explicitHost && !hasNginx) {
|
|
63
|
+
issues.push('nginx export is missing nginx.conf.');
|
|
64
|
+
}
|
|
65
|
+
return { issues, warnings };
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
async spaFallback({ outDir }) {
|
|
69
|
+
const file = path.join(outDir, 'nginx.conf');
|
|
70
|
+
if (!await fs.pathExists(file)) return false;
|
|
71
|
+
return /try_files\s+\$uri\s+\$uri\/\s+\/index\.html/.test(await fs.readFile(file, 'utf8'));
|
|
72
|
+
}
|
|
73
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// commands/export/hosts/none.js
|
|
2
|
+
import { clientSpaFallback } from './shared.js';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
id: 'none',
|
|
6
|
+
// Portable export: no host files. `--spa` writes the shell as 404.html; the
|
|
7
|
+
// server in front decides the status.
|
|
8
|
+
spaMode: 'client-404',
|
|
9
|
+
clientRedirectPages: true,
|
|
10
|
+
ruleFiles: [],
|
|
11
|
+
|
|
12
|
+
async write() {
|
|
13
|
+
return [];
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
async check() {
|
|
17
|
+
return { issues: [], warnings: [] };
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
spaFallback({ outDir }) {
|
|
21
|
+
return clientSpaFallback(outDir);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// commands/export/hosts/s3.js
|
|
2
|
+
import { clientSpaFallback } from './shared.js';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
id: 's3',
|
|
6
|
+
// S3/CloudFront are configured in the bucket/distribution, not in the export.
|
|
7
|
+
// `--spa` writes the shell as 404.html; a CloudFront function is required for
|
|
8
|
+
// a true 200 navigation fallback.
|
|
9
|
+
spaMode: 'client-404',
|
|
10
|
+
clientRedirectPages: true,
|
|
11
|
+
ruleFiles: [],
|
|
12
|
+
|
|
13
|
+
async write() {
|
|
14
|
+
return [];
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
async check() {
|
|
18
|
+
return { issues: [], warnings: [] };
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
spaFallback({ outDir }) {
|
|
22
|
+
return clientSpaFallback(outDir);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// commands/export/hosts/shared.js
|
|
2
|
+
//
|
|
3
|
+
// Pieces every host preset shares: the security headers, the Cloudflare/Netlify
|
|
4
|
+
// `_headers` text, redirect lines and the marker used by the client-side
|
|
5
|
+
// 404 fallback.
|
|
6
|
+
import fs from 'fs-extra';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
|
|
9
|
+
export const SECURITY_HEADERS = [
|
|
10
|
+
['X-Content-Type-Options', 'nosniff'],
|
|
11
|
+
['X-Frame-Options', 'DENY'],
|
|
12
|
+
['Referrer-Policy', 'strict-origin-when-cross-origin']
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
export const HEADER_RULES = `/*
|
|
16
|
+
X-Content-Type-Options: nosniff
|
|
17
|
+
X-Frame-Options: DENY
|
|
18
|
+
Referrer-Policy: strict-origin-when-cross-origin
|
|
19
|
+
/bundles/*
|
|
20
|
+
Cache-Control: no-store
|
|
21
|
+
/slice-env.json
|
|
22
|
+
Cache-Control: no-store
|
|
23
|
+
/slice-env.generated.js
|
|
24
|
+
Cache-Control: no-store
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
// Written into the `404.html` shell used by GitHub Pages, S3 and `--host none`.
|
|
28
|
+
// `--check` uses it to know that unknown routes render the app.
|
|
29
|
+
export const CLIENT_SPA_MARKER = '<!-- slice:client-spa-fallback -->';
|
|
30
|
+
|
|
31
|
+
export function redirectLines(redirects) {
|
|
32
|
+
return redirects.map(({ from, to, status }) => `${from} ${to} ${status}`).join('\n');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function redirectCatchAll(text) {
|
|
36
|
+
return /^\s*\/\*\s+\/index\.html\s+200\s*$/m.test(text);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function readJsonIfExists(file) {
|
|
40
|
+
if (!await fs.pathExists(file)) return null;
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(await fs.readFile(file, 'utf8'));
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function securityHeaderObjects() {
|
|
49
|
+
return SECURITY_HEADERS.map(([key, value]) => ({ key, value }));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function nginxSecurityHeaders(indent = ' ') {
|
|
53
|
+
return SECURITY_HEADERS
|
|
54
|
+
.map(([key, value]) => `${indent}add_header ${key} "${value}" always;`)
|
|
55
|
+
.join('\n');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function clientSpaFallback(outDir) {
|
|
59
|
+
const file = path.join(outDir, '404.html');
|
|
60
|
+
if (!await fs.pathExists(file)) return false;
|
|
61
|
+
return (await fs.readFile(file, 'utf8')).includes(CLIENT_SPA_MARKER);
|
|
62
|
+
}
|