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