2022-03-24 17:11:26 +00:00
|
|
|
import {getAppRoot} from 'app/server/lib/places';
|
(core) Speed up and upgrade build.
Summary:
- Upgrades to build-related packages:
- Upgrade typescript, related libraries and typings.
- Upgrade webpack, eslint; add tsc-watch, node-dev, eslint_d.
- Build organization changes:
- Build webpack from original typescript, transpiling only; with errors still
reported by a background tsc watching process.
- Typescript-related changes:
- Reduce imports of AWS dependencies (very noticeable speedup)
- Avoid auto-loading global @types
- Client code is now built with isolatedModules flag (for safe transpilation)
- Use allowJs to avoid copying JS files manually.
- Linting changes
- Enhance Arcanist ESLintLinter to run before/after commands, and set up to use eslint_d
- Update eslint config, and include .eslintignore to avoid linting generated files.
- Include a bunch of eslint-prompted and eslint-generated fixes
- Add no-unused-expression rule to eslint, and fix a few warnings about it
- Other items:
- Refactor cssInput to avoid circular dependency
- Remove a bit of unused code, libraries, dependencies
Test Plan: No behavior changes, all existing tests pass. There are 30 tests fewer reported because `test_gpath.py` was removed (it's been unused for years)
Reviewers: paulfitz
Reviewed By: paulfitz
Subscribers: paulfitz
Differential Revision: https://phab.getgrist.com/D3498
2022-06-27 20:09:41 +00:00
|
|
|
import {fromCallback, listenPromise} from 'app/server/lib/serverUtils';
|
2022-07-04 14:14:55 +00:00
|
|
|
import express from 'express';
|
2022-03-24 17:11:26 +00:00
|
|
|
import * as http from 'http';
|
|
|
|
import {AddressInfo, Socket} from 'net';
|
|
|
|
import * as path from 'path';
|
|
|
|
import {fixturesRoot} from 'test/server/testUtils';
|
|
|
|
|
|
|
|
export interface Serving {
|
|
|
|
url: string;
|
2022-06-13 03:30:07 +00:00
|
|
|
shutdown: () => Promise<void>;
|
2022-03-24 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Adds static files from a directory.
|
|
|
|
// By default exposes /fixture/sites
|
|
|
|
export function addStatic(app: express.Express, rootDir?: string) {
|
|
|
|
// mix in a copy of the plugin api
|
|
|
|
app.use(/^\/(grist-plugin-api.js)$/, (req, res) =>
|
|
|
|
res.sendFile(req.params[0], {root:
|
|
|
|
path.resolve(getAppRoot(), "static")}));
|
|
|
|
app.use(express.static(rootDir || path.resolve(fixturesRoot, "sites"), {
|
|
|
|
setHeaders: (res) => {
|
|
|
|
res.set("Access-Control-Allow-Origin", "*");
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve from a directory.
|
|
|
|
export async function serveStatic(rootDir: string): Promise<Serving> {
|
|
|
|
return serveSomething(app => addStatic(app, rootDir));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve a string of html.
|
|
|
|
export async function serveSinglePage(html: string): Promise<Serving> {
|
|
|
|
return serveSomething(app => {
|
|
|
|
app.get('', (req, res) => res.send(html));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function serveCustomViews(): Promise<Serving> {
|
|
|
|
return serveStatic(path.resolve(fixturesRoot, "sites"));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function serveSomething(setup: (app: express.Express) => void, port= 0): Promise<Serving> {
|
|
|
|
const app = express();
|
|
|
|
const server = http.createServer(app);
|
(core) Speed up and upgrade build.
Summary:
- Upgrades to build-related packages:
- Upgrade typescript, related libraries and typings.
- Upgrade webpack, eslint; add tsc-watch, node-dev, eslint_d.
- Build organization changes:
- Build webpack from original typescript, transpiling only; with errors still
reported by a background tsc watching process.
- Typescript-related changes:
- Reduce imports of AWS dependencies (very noticeable speedup)
- Avoid auto-loading global @types
- Client code is now built with isolatedModules flag (for safe transpilation)
- Use allowJs to avoid copying JS files manually.
- Linting changes
- Enhance Arcanist ESLintLinter to run before/after commands, and set up to use eslint_d
- Update eslint config, and include .eslintignore to avoid linting generated files.
- Include a bunch of eslint-prompted and eslint-generated fixes
- Add no-unused-expression rule to eslint, and fix a few warnings about it
- Other items:
- Refactor cssInput to avoid circular dependency
- Remove a bit of unused code, libraries, dependencies
Test Plan: No behavior changes, all existing tests pass. There are 30 tests fewer reported because `test_gpath.py` was removed (it's been unused for years)
Reviewers: paulfitz
Reviewed By: paulfitz
Subscribers: paulfitz
Differential Revision: https://phab.getgrist.com/D3498
2022-06-27 20:09:41 +00:00
|
|
|
await listenPromise(server.listen(port));
|
2022-03-24 17:11:26 +00:00
|
|
|
|
|
|
|
const connections = new Set<Socket>();
|
|
|
|
server.on('connection', (conn) => {
|
|
|
|
connections.add(conn);
|
|
|
|
conn.on('close', () => connections.delete(conn));
|
|
|
|
});
|
|
|
|
|
2022-06-13 03:30:07 +00:00
|
|
|
async function shutdown() {
|
|
|
|
await fromCallback(cb => server.close(cb));
|
2022-03-24 17:11:26 +00:00
|
|
|
for (const conn of connections) { conn.destroy(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
port = (server.address() as AddressInfo).port;
|
|
|
|
app.set('port', port);
|
|
|
|
setup(app);
|
|
|
|
const url = `http://localhost:${port}`;
|
|
|
|
return {url, shutdown};
|
|
|
|
}
|