mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-11 19:04:30 +00:00
emit a basepath event on first run
This commit is contained in:
@@ -6,7 +6,6 @@ import * as ports from 'port-authority';
|
||||
import mkdirp from 'mkdirp';
|
||||
import rimraf from 'rimraf';
|
||||
import format_messages from 'webpack-format-messages';
|
||||
import prettyMs from 'pretty-ms';
|
||||
import { locations } from '../config';
|
||||
import { EventEmitter } from 'events';
|
||||
import { create_routes, create_main_manifests, create_compilers, create_serviceworker_manifest } from '../core';
|
||||
@@ -172,6 +171,14 @@ class Watcher extends EventEmitter {
|
||||
}, process.env),
|
||||
stdio: ['ipc']
|
||||
});
|
||||
|
||||
this.proc.on('message', message => {
|
||||
if (message.__sapper__ && message.event === 'basepath') {
|
||||
this.emit('basepath', {
|
||||
basepath: message.basepath
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -66,7 +66,7 @@ async function execute(emitter: EventEmitter, {
|
||||
const saved = new Set();
|
||||
|
||||
proc.on('message', message => {
|
||||
if (!message.__sapper__) return;
|
||||
if (!message.__sapper__ || message.event !== 'file') return;
|
||||
|
||||
let file = new URL(message.url, origin).pathname.slice(1);
|
||||
let { body } = message;
|
||||
|
||||
@@ -58,6 +58,8 @@ export default function middleware({ App, routes, store }: {
|
||||
|
||||
const client_assets = JSON.parse(fs.readFileSync(path.join(output, 'client_assets.json'), 'utf-8'));
|
||||
|
||||
let emitted_basepath = false;
|
||||
|
||||
const middleware = compose_handlers([
|
||||
(req: Req, res: ServerResponse, next: () => void) => {
|
||||
if (req.baseUrl === undefined) {
|
||||
@@ -66,6 +68,16 @@ export default function middleware({ App, routes, store }: {
|
||||
: '';
|
||||
}
|
||||
|
||||
if (!emitted_basepath && process.send) {
|
||||
process.send({
|
||||
__sapper__: true,
|
||||
event: 'basepath',
|
||||
basepath: req.baseUrl
|
||||
});
|
||||
|
||||
emitted_basepath = true;
|
||||
}
|
||||
|
||||
if (req.path === undefined) {
|
||||
req.path = req.url.replace(/\?.*/, '');
|
||||
}
|
||||
@@ -276,6 +288,7 @@ function get_route_handler(chunks: Record<string, string>, App: Component, route
|
||||
if (process.send) {
|
||||
process.send({
|
||||
__sapper__: true,
|
||||
event: 'file',
|
||||
url: req.url,
|
||||
method: req.method,
|
||||
status: 200,
|
||||
@@ -315,6 +328,7 @@ function get_route_handler(chunks: Record<string, string>, App: Component, route
|
||||
|
||||
process.send({
|
||||
__sapper__: true,
|
||||
event: 'file',
|
||||
url: req.url,
|
||||
method: req.method,
|
||||
status: res.statusCode,
|
||||
|
||||
@@ -133,6 +133,7 @@ function run({ mode, basepath = '' }) {
|
||||
let capture;
|
||||
|
||||
let base;
|
||||
let captured_basepath;
|
||||
|
||||
const nightmare = new Nightmare();
|
||||
|
||||
@@ -179,7 +180,13 @@ function run({ mode, basepath = '' }) {
|
||||
let handler;
|
||||
|
||||
proc.on('message', message => {
|
||||
if (message.__sapper__) return;
|
||||
if (message.__sapper__) {
|
||||
if (message.event === 'basepath') {
|
||||
captured_basepath = basepath;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (handler) handler(message);
|
||||
});
|
||||
|
||||
@@ -597,6 +604,10 @@ function run({ mode, basepath = '' }) {
|
||||
assert.ok(!hasProgressIndicator);
|
||||
});
|
||||
});
|
||||
|
||||
it('emits a basepath', () => {
|
||||
assert.equal(captured_basepath, basepath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('headers', () => {
|
||||
|
||||
Reference in New Issue
Block a user