Merge branch 'master' into nolan/preload-in-export

This commit is contained in:
Rich Harris
2019-02-17 09:51:03 -04:00
committed by GitHub
12 changed files with 607 additions and 1647 deletions

View File

@@ -1,6 +1,6 @@
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as sander from 'sander';
import * as url from 'url';
import fetch from 'node-fetch';
import * as yootils from 'yootils';
@@ -10,6 +10,7 @@ import minify_html from './utils/minify_html';
import Deferred from './utils/Deferred';
import { noop } from './utils/noop';
import { parse as parseLinkHeader } from 'http-link-header';
import { rimraf, copy, mkdirp } from './utils/fs_utils';
type Opts = {
build_dir?: string,
@@ -54,20 +55,15 @@ async function _export({
export_dir = path.resolve(cwd, export_dir, basepath);
// Prep output directory
sander.rimrafSync(export_dir);
rimraf(export_dir);
sander.copydirSync(static_files).to(export_dir);
sander.copydirSync(build_dir, 'client').to(export_dir, 'client');
copy(static_files, export_dir);
copy(path.join(build_dir, 'client'), path.join(export_dir, 'client'));
copy(path.join(build_dir, 'service-worker.js'), path.join(export_dir, 'service-worker.js'));
copy(path.join(build_dir, 'service-worker.js.map'), path.join(export_dir, 'service-worker.js.map'));
if (sander.existsSync(build_dir, 'service-worker.js')) {
sander.copyFileSync(build_dir, 'service-worker.js').to(export_dir, 'service-worker.js');
}
if (sander.existsSync(build_dir, 'service-worker.js.map')) {
sander.copyFileSync(build_dir, 'service-worker.js.map').to(export_dir, 'service-worker.js.map');
}
const port = await ports.find(3000);
const defaultPort = process.env.PORT ? parseInt(process.env.PORT) : 3000;
const port = await ports.find(defaultPort);
const protocol = 'http:';
const host = `localhost:${port}`;
@@ -92,8 +88,8 @@ async function _export({
const seen = new Set();
const saved = new Set();
function save(path: string, status: number, type: string, body: string) {
const { pathname } = resolve(origin, path);
function save(url: string, status: number, type: string, body: string) {
const { pathname } = resolve(origin, url);
let file = decodeURIComponent(pathname.slice(1));
if (saved.has(file)) return;
@@ -114,7 +110,9 @@ async function _export({
status
});
sander.writeFileSync(export_dir, file, body);
const export_file = path.join(export_dir, file);
mkdirp(path.dirname(export_file));
fs.writeFileSync(export_file, body);
}
proc.on('message', message => {
@@ -217,4 +215,4 @@ async function _export({
function get_href(attrs: string) {
const match = /href\s*=\s*(?:"(.*?)"|'(.+?)'|([^\s>]+))/.exec(attrs);
return match[1] || match[2] || match[3];
}
}