Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Opts = {
legacy?: boolean;
bundler?: 'rollup' | 'webpack';
ext?: string;
oncompile?: ({ type, result }: { type: string, result: CompileResult }) => void;
oncompile?: ({ type, result }: { type: string; result: CompileResult }) => void;
};

export async function build({
Expand Down
62 changes: 31 additions & 31 deletions src/api/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import { copy_runtime } from './utils/copy_runtime';
import { rimraf, mkdirp } from './utils/fs_utils';

type Opts = {
cwd?: string,
src?: string,
dest?: string,
routes?: string,
output?: string,
static?: string,
'dev-port'?: number,
live?: boolean,
hot?: boolean,
'devtools-port'?: number,
bundler?: 'rollup' | 'webpack',
port?: number,
ext: string
cwd?: string;
src?: string;
dest?: string;
routes?: string;
output?: string;
static?: string;
'dev-port'?: number;
live?: boolean;
hot?: boolean;
'devtools-port'?: number;
bundler?: 'rollup' | 'webpack';
port?: number;
ext: string;
};

export function dev(opts: Opts) {
Expand Down Expand Up @@ -128,9 +128,9 @@ class Watcher extends EventEmitter {
async init() {
if (this.port) {
if (!await ports.check(this.port)) {
this.emit('fatal', <FatalEvent>{
this.emit('fatal', {
message: `Port ${this.port} is unavailable`
});
} as FatalEvent);
return;
}
} else {
Expand Down Expand Up @@ -164,9 +164,9 @@ class Watcher extends EventEmitter {
cwd, src, dest, routes, output
});
} catch (err) {
this.emit('fatal', <FatalEvent>{
this.emit('fatal', {
message: err.message
});
} as FatalEvent);
return;
}

Expand All @@ -192,10 +192,10 @@ class Watcher extends EventEmitter {
cwd, src, dest, routes, output
});
} catch (error) {
this.emit('error', <ErrorEvent>{
this.emit('error', {
type: 'manifest',
error
});
} as ErrorEvent);
}
}
)
Expand All @@ -217,9 +217,9 @@ class Watcher extends EventEmitter {
const compilers: Compilers = await create_compilers(this.bundler, cwd, src, dest, true);

const emitFatal = () => {
this.emit('fatal', <FatalEvent>{
this.emit('fatal', {
message: `Server crashed`
});
} as FatalEvent);

this.crashed = true;
this.proc = null;
Expand All @@ -239,10 +239,10 @@ class Watcher extends EventEmitter {

return ports.wait(this.port)
.then((() => {
this.emit('ready', <ReadyEvent>{
this.emit('ready', {
port: this.port,
process: this.proc
});
} as ReadyEvent);

if (this.hot && this.bundler === 'webpack') {
this.dev_server.send({
Expand All @@ -257,9 +257,9 @@ class Watcher extends EventEmitter {
.catch(err => {
if (this.crashed) return;

this.emit('fatal', <FatalEvent>{
this.emit('fatal', {
message: `Server is not listening on port ${this.port}`
});
} as FatalEvent);
});
};

Expand Down Expand Up @@ -393,33 +393,33 @@ class Watcher extends EventEmitter {
};

process.nextTick(() => {
this.emit('invalid', <InvalidEvent>{
this.emit('invalid', {
changed: Array.from(this.current_build.changed),
invalid: {
server: this.current_build.rebuilding.has('server'),
client: this.current_build.rebuilding.has('client'),
serviceworker: this.current_build.rebuilding.has('serviceworker'),
}
});
} as InvalidEvent);

this.restarting = false;
});
}
}

watch(compiler: Compiler, { name, invalid = noop, handle_result = noop }: {
name: string,
name: string;
invalid?: (filename: string) => void;
handle_result?: (result: CompileResult) => void;
}) {
compiler.oninvalid(invalid);

compiler.watch((error?: Error, result?: CompileResult) => {
if (error) {
this.emit('error', <ErrorEvent>{
this.emit('error', {
type: name,
error
});
} as ErrorEvent);
} else {
this.emit('build', {
type: name,
Expand Down Expand Up @@ -489,7 +489,7 @@ class DevServer {

function watch_dir(
dir: string,
filter: ({ path, stats }: { path: string, stats: fs.Stats }) => boolean,
filter: ({ path, stats }: { path: string; stats: fs.Stats }) => boolean,
callback: () => void
) {
let watch: any;
Expand Down
38 changes: 19 additions & 19 deletions src/api/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ import { noop } from './utils/noop';
import { parse as parseLinkHeader } from 'http-link-header';
import { rimraf, copy, mkdirp } from './utils/fs_utils';

const writeFile = promisify(fs.writeFile)
const writeFile = promisify(fs.writeFile);

type Opts = {
build_dir?: string,
export_dir?: string,
cwd?: string,
static?: string,
basepath?: string,
host_header?: string,
timeout?: number | false,
concurrent?: number,
build_dir?: string;
export_dir?: string;
cwd?: string;
static?: string;
basepath?: string;
host_header?: string;
timeout?: number | false;
concurrent?: number;
oninfo?: ({ message }: { message: string }) => void;
onfile?: ({ file, size, status }: { file: string, size: number, status: number }) => void;
entry?: string;
};

type Ref = {
uri: string,
rel: string,
as: string
uri: string;
rel: string;
as: string;
};

type URL = url.UrlWithStringQuery;
Expand All @@ -42,7 +42,7 @@ function resolve(from: string, to: string) {
}

function cleanPath(path: string) {
return path.replace(/^\/|\/$|\/*index(.html)*$|.html$/g, '')
return path.replace(/^\/|\/$|\/*index(.html)*$|.html$/g, '');
}

function get_href(attrs: string) {
Expand Down Expand Up @@ -110,7 +110,7 @@ async function _export({
const saved = new Set();

function save(url: string, status: number, type: string, body: string) {
let { pathname } = resolve(origin, url);
const { pathname } = resolve(origin, url);
let file = decodeURIComponent(pathname.slice(1));

if (saved.has(file)) return;
Expand Down Expand Up @@ -141,7 +141,7 @@ async function _export({
function handle(url: URL, fetchOpts: FetchOpts, addCallback: Function) {
let pathname = url.pathname;
if (pathname !== '/service-worker-index.html') {
pathname = pathname.replace(fetchOpts.root.pathname, '') || '/'
pathname = pathname.replace(fetchOpts.root.pathname, '') || '/';
}

if (seen.has(pathname)) return;
Expand Down Expand Up @@ -190,11 +190,11 @@ async function _export({

if (range === 2 && type === 'text/html') {
// parse link rel=preload headers and embed them in the HTML
let link = parseLinkHeader(response.headers.get('Link') || '');
const link = parseLinkHeader(response.headers.get('Link') || '');
link.refs.forEach((ref: Ref) => {
if (ref.rel === 'preload') {
body = body.replace('</head>',
`<link rel="preload" as=${JSON.stringify(ref.as)} href=${JSON.stringify(ref.uri)}></head>`)
`<link rel="preload" as=${JSON.stringify(ref.as)} href=${JSON.stringify(ref.uri)}></head>`);
}
});

Expand All @@ -206,7 +206,7 @@ async function _export({
const base = resolve(url.href, base_href);

let match;
let pattern = /<a ([\s\S]+?)>/gm;
const pattern = /<a ([\s\S]+?)>/gm;

while (match = pattern.exec(cleaned)) {
const attrs = match[1];
Expand All @@ -216,7 +216,7 @@ async function _export({
const url = resolve(base.href, href);

if (url.protocol === protocol && url.host === host) {
handle(url, fetchOpts, queue.add)
handle(url, fetchOpts, queue.add);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/api/utils/export_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ type QueueOpts = {
callbacks?: {
onDone?: () => void;
[key: string]: Function;
}
};
};

// determines current state of a promise
function promiseState(p: Promise<any>) {
const t = {}
const t = {};
return Promise.race([p, t]).then(v => (v === t) ? "pending" : "fulfilled", () => "rejected");
}

// finds first non-pending promise in a list of promises
async function findNotPendingIndex(list: Promise<any>[]) {
async function findNotPendingIndex(list: Array<Promise<any>>) {
const states = await Promise.all(list.map(p => promiseState(p)));
return states.findIndex(state => state !== 'pending');
}

// filters any non-pending promises out of a list of promises
async function filterNotPending(list: Promise<any>[]) {
async function filterNotPending(list: Array<Promise<any>>) {
const states = await Promise.all(list.map(p => promiseState(p)));
return states.reduce((acc, curr, index) => {
if (curr === 'pending') {
Expand All @@ -56,12 +56,12 @@ async function filterNotPending(list: Promise<any>[]) {
// url array can contain any number of urls found during export process
// fetching array can contain a number of fetch api returns equal to the concurrent option
// saving array can contain a number of promisified writeFile returns equal to the concurrent option
function exportQueue({ concurrent, handleFetch, handleResponse, fetchOpts, callbacks } : QueueOpts) {
function exportQueue({ concurrent, handleFetch, handleResponse, fetchOpts, callbacks }: QueueOpts) {
const urls: URL[] = [];
let fetching : Promise<any>[] = [];
let saving : Promise<any>[] = [];
const fetching: Array<Promise<any>> = [];
let saving: Array<Promise<any>> = [];

function addToQueue(p: Promise<any>, queue: Promise<any>[]) {
function addToQueue(p: Promise<any>, queue: Array<Promise<any>>) {
const queuePromise = new Promise((res, rej) => {
p.then((ret?: any) => {
res(ret);
Expand Down Expand Up @@ -113,6 +113,6 @@ function exportQueue({ concurrent, handleFetch, handleResponse, fetchOpts, callb
callbacks[event] = fn;
}
};
};
}

export { exportQueue, FetchOpts, FetchRet };
Loading