@ryziz/cli 0.0.47 → 0.0.49
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/package.json +1 -1
- package/src/build.js +20 -7
package/package.json
CHANGED
package/src/build.js
CHANGED
|
@@ -43,11 +43,24 @@ export default (isDev) => task('Building', {
|
|
|
43
43
|
'Functions': async (c) => {
|
|
44
44
|
const scanApiRoutes = () => {
|
|
45
45
|
const files = existsSync(c.src) ? readdirSync(c.src).filter((f) => /^api\..+\.js$/.test(f)) : [];
|
|
46
|
-
return files.map((file) =>
|
|
47
|
-
file,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
return files.map((file) => {
|
|
47
|
+
const content = readFileSync(join(c.src, file), 'utf8');
|
|
48
|
+
const hasConfig = /export\s+(const|let|var)\s+config\s*=/.test(content);
|
|
49
|
+
return {
|
|
50
|
+
file,
|
|
51
|
+
path: ('/api/' + file.slice(4, -3)).replace(/^\/api\/index$/, '/api/').replace(/\./g, '/').replace(/\$/g, ':'),
|
|
52
|
+
functionName: file.slice(0, -3).replace(/\./g, '-').replace(/\$/g, ''),
|
|
53
|
+
hasConfig,
|
|
54
|
+
};
|
|
55
|
+
}).sort((a, b) => {
|
|
56
|
+
const aDepth = a.path.split('/').length;
|
|
57
|
+
const bDepth = b.path.split('/').length;
|
|
58
|
+
const aHasParam = a.path.includes(':');
|
|
59
|
+
const bHasParam = b.path.includes(':');
|
|
60
|
+
if (aDepth !== bDepth) return bDepth - aDepth;
|
|
61
|
+
if (aHasParam !== bHasParam) return aHasParam ? 1 : -1;
|
|
62
|
+
return a.path.localeCompare(b.path);
|
|
63
|
+
});
|
|
51
64
|
};
|
|
52
65
|
|
|
53
66
|
c.functionsBuilder = await context({
|
|
@@ -56,8 +69,8 @@ export default (isDev) => task('Building', {
|
|
|
56
69
|
external: ['firebase-admin', 'firebase-functions', 'express'],
|
|
57
70
|
plugins: [
|
|
58
71
|
{ name: 'log', setup: (b) => b.onEnd(() => { c.onFunctionsBuilt?.(); }) },
|
|
59
|
-
{ name: 'var', setup: (b) => { b.onResolve({ filter: /^virtual:api-routes$/ }, (a) => ({ path: a.path, namespace: 'var' })); b.onLoad({ filter: /.*/, namespace: 'var' }, () => { const routes = scanApiRoutes(); const requires = routes.map((r, i) => `const
|
|
60
|
-
{ name: 'fb', setup: (b) => b.onEnd(() => { const routes = scanApiRoutes(); const cfg = JSON.parse(readFileSync(join(c.functionsPkg, 'firebase.json'))); writeFileSync(join(c.dist, 'firebase.json'), JSON.stringify({ ...cfg, hosting: { ...cfg.hosting, rewrites: [...
|
|
72
|
+
{ name: 'var', setup: (b) => { b.onResolve({ filter: /^virtual:api-routes$/ }, (a) => ({ path: a.path, namespace: 'var' })); b.onLoad({ filter: /.*/, namespace: 'var' }, () => { const routes = scanApiRoutes(); const requires = routes.map((r, i) => `const module${i} = require('./src/${r.file}');`).join('\n'); const entries = routes.map((r, i) => `{path:'${r.path}',handler:module${i}.default || module${i},config:module${i}.config,functionName:'${r.functionName}'}`).join(','); return { contents: `${requires}\nmodule.exports = [${entries}];`, loader: 'js', resolveDir: c.root, watchDirs: [c.src] }; }); } },
|
|
73
|
+
{ name: 'fb', setup: (b) => b.onEnd(() => { const routes = scanApiRoutes(); const cfg = JSON.parse(readFileSync(join(c.functionsPkg, 'firebase.json'))); const rewrites = routes.map((r) => ({ source: r.path, function: r.hasConfig ? r.functionName : 'api' })); writeFileSync(join(c.dist, 'firebase.json'), JSON.stringify({ ...cfg, hosting: { ...cfg.hosting, rewrites: [...rewrites, { source: '**', destination: '/index.html' }] } }, null, 2)); }) },
|
|
61
74
|
],
|
|
62
75
|
});
|
|
63
76
|
|