Skip to content

Commit

Permalink
Generate typings for code in app-util (#121)
Browse files Browse the repository at this point in the history
* build app util types with tsc

* fixing errors and added types to clean

* switching interface to type

* types to interfaces

* building types as part of rollup build

* made comment less ambiguous

* fixing compiler errors

* unused parameters

* made tsconfig extend root

* explicitly setting declarations true in app-utils
  • Loading branch information
ehrencrona authored Nov 19, 2020
1 parent 6b53582 commit c91d879
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 22 deletions.
7 changes: 4 additions & 3 deletions packages/adapter-netlify/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { APIGatewayProxyHandler } from 'aws-lambda';
import { URLSearchParams } from 'url';
import { render } from '@sveltejs/app-utils/renderer';
import type { PageComponentManifest, EndpointManifest } from '@sveltejs/app-utils';
import type { PageComponentManifest, EndpointManifest, Method } from '@sveltejs/app-utils';

const manifest = require('./manifest.js');
const client = require('./client.json');
Expand Down Expand Up @@ -32,7 +32,7 @@ export const handler: APIGatewayProxyHandler = async (event) => {

const rendered = await render({
host: null, // TODO
method: httpMethod,
method: httpMethod as Method,
headers,
path,
query
Expand All @@ -44,7 +44,8 @@ export const handler: APIGatewayProxyHandler = async (event) => {
root,
setup,
load: (route: PageComponentManifest | EndpointManifest) => require(`./routes/${route.name}.js`),
dev: false
dev: false,
only_prerender: false
});

if (rendered) {
Expand Down
11 changes: 6 additions & 5 deletions packages/adapter-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parse, URLSearchParams } from 'url';
import sirv from 'sirv';
import { render } from '@sveltejs/app-utils/renderer';
import { get_body } from '@sveltejs/app-utils/http';
import type { PageComponentManifest, EndpointManifest } from '@sveltejs/app-utils';
import type { PageComponentManifest, EndpointManifest, Method } from '@sveltejs/app-utils';

const manifest = require('./manifest.js');
const client = require('./client.json');
Expand Down Expand Up @@ -38,9 +38,9 @@ const server = http.createServer((req, res) => {

const rendered = await render({
host: null, // TODO
method: req.method,
headers: req.headers,
path: parsed.pathname,
method: req.method as Method,
headers: req.headers as Record<string, string>, // TODO: what about repeated headers, i.e. string[]
path: parsed.pathname as string,
body: await get_body(req),
query: new URLSearchParams(parsed.query || '')
}, {
Expand All @@ -51,7 +51,8 @@ const server = http.createServer((req, res) => {
root,
setup,
load: (route: PageComponentManifest | EndpointManifest) => require(`./routes/${route.name}.js`),
dev: false
dev: false,
only_prerender: false
});

if (rendered) {
Expand Down
1 change: 1 addition & 0 deletions packages/app-utils/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
/http
/renderer
/types
/build
3 changes: 2 additions & 1 deletion packages/app-utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export {
SSRComponentModule,
Page,
PageContext,
Query
Query,
Method
} from './src/types';
2 changes: 1 addition & 1 deletion packages/app-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@sveltejs/app-utils",
"version": "0.0.17",
"scripts": {
"clean": "node rimraf.js common files http renderer",
"clean": "node rimraf.js common files http renderer types build",
"dev": "npm run clean && rollup -cw",
"build": "npm run clean && rollup -c",
"lint": "eslint --ignore-pattern node_modules/ --ignore-pattern dist/ --ignore-pattern files/ --ignore-pattern http/ --ignore-pattern renderer/ \"**/*.{ts,js,svelte}\" && npm run check-format",
Expand Down
29 changes: 24 additions & 5 deletions packages/app-utils/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import pkg from './package.json';
const fs = require('fs');
const path = require('path');

const input = {};
Object.keys(pkg.exports).forEach(key => {
Expand All @@ -27,10 +29,27 @@ export default {
],
plugins: [
nodeResolve(),
typescript()
typescript({ useTsconfigDeclarationDir: true }),
{
name: 'copy-types',
resolveId: () => null,
load: () => null,
writeBundle: () => {
copyRecursiveSync('build/types', '.');
copyRecursiveSync('src/types', 'types');
}
}
],
external: [
...require('module').builtinModules,
...Object.keys(pkg.dependencies)
]
external: [...require('module').builtinModules, ...Object.keys(pkg.dependencies)]
};

function copyRecursiveSync(src, dest) {
if (fs.existsSync(src) && fs.statSync(src).isDirectory()) {
fs.mkdirSync(dest, { recursive: true });
fs.readdirSync(src).forEach(file =>
copyRecursiveSync(path.join(src, file), path.join(dest, file))
);
} else {
fs.copyFileSync(src, dest);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { URLSearchParams } from 'url';

// do not import this file from outside app-utils
// these types are re-exported in /index.d.ts and should be imported from "@sveltejs/app-utils"

export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS';

export type Headers = Record<string, string>;
Expand All @@ -8,7 +11,7 @@ export interface IncomingRequest {
host: string | null; // TODO is this actually necessary?
method: Method;
headers: Headers;
body: any; // TODO
body?: any; // TODO
path: string;
query: URLSearchParams;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/app-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"lib": ["es2020", "DOM"],
"declaration": true,
"declarationDir": "build/types",
}
}
1 change: 0 additions & 1 deletion packages/kit/src/api/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { copy_assets } from '../utils';
import { create_app } from '../../core/create_app';
import { SvelteAppConfig } from '../../interfaces';
import { css_injection } from './css_injection';
import { EndpointManifest } from '@sveltejs/app-utils';

const exec = promisify(child_process.exec);

Expand Down
10 changes: 5 additions & 5 deletions packages/kit/src/api/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ManifestData, ReadyEvent } from '../../interfaces';
import { mkdirp } from '@sveltejs/app-utils/files';
import { render } from '@sveltejs/app-utils/renderer';
import { get_body } from '@sveltejs/app-utils/http';
import { SSRComponentModule, SetupModule } from '@sveltejs/app-utils';
import { SSRComponentModule, SetupModule, Method } from '@sveltejs/app-utils';
import { DevConfig, Loader } from './types';
import { copy_assets } from '../utils';
import { readFileSync } from 'fs';
Expand Down Expand Up @@ -157,8 +157,8 @@ class Watcher extends EventEmitter {

const rendered = await render({
host: req.headers.host,
headers: req.headers,
method: req.method,
headers: req.headers as Record<string, string>,
method: req.method as Method,
path: parsed.pathname,
query: new URLSearchParams(parsed.query),
body
Expand All @@ -170,11 +170,11 @@ class Watcher extends EventEmitter {
entry: 'main/runtime/navigation.js',
deps: {}
},
files: 'build',
dev: true,
root,
setup,
load: route => load(route.url.replace(/\.\w+$/, '.js')) // TODO is the replace still necessary?
load: route => load(route.url.replace(/\.\w+$/, '.js')), // TODO is the replace still necessary?
only_prerender: false
});

if (rendered) {
Expand Down

0 comments on commit c91d879

Please sign in to comment.