Skip to content

Commit

Permalink
refactor: remove /preview/ prefix in preview URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Aug 26, 2023
1 parent 2b01334 commit 05d0eaf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
8 changes: 6 additions & 2 deletions core/src/previewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ export class Previewer {
}
);
const router = express.Router();
router.get(/^\/preview\/.*:[^/]+\/$/, async (req, res) => {
const previewableId = req.path.substring(9, req.path.length - 1);
router.get(/^\/.*:[^/]+\/$/, async (req, res, next) => {
if (req.url.includes("?html-proxy")) {
next();
return;
}
const previewableId = req.path.substring(1, req.path.length - 1);
if (req.header("Accept") === "text/x-vite-ping") {
// This is triggered as part of HMR. Exit early.
res.writeHead(204).end();
Expand Down
36 changes: 27 additions & 9 deletions core/src/vite/plugins/virtual-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@ import type * as vite from "vite";
import { transformWithEsbuild } from "vite";

const VIRTUAL_PREFIX = `/@previewjs-virtual:`;
const VIRTUAL_PREFIX2 = `/@id/__x00__`;

const jsExtensions = new Set([".js", ".jsx", ".ts", ".tsx"]);

function maybeVirtual(originalId: string) {
let id = originalId;
if (id.startsWith(VIRTUAL_PREFIX)) {
id = id.slice(VIRTUAL_PREFIX.length);
}
if (id.startsWith("\0")) {
id = id.slice(1);
}
if (id.startsWith(VIRTUAL_PREFIX2)) {
id = id.slice(VIRTUAL_PREFIX2.length);
}
return [id, id !== originalId] as const;
}

export function virtualPlugin(options: {
logger: Logger;
reader: Reader;
Expand All @@ -20,14 +35,14 @@ export function virtualPlugin(options: {
const { reader, rootDir } = options;
return {
name: "previewjs:virtual-fs",
resolveId: async function (id, importer) {
const virtualImporter = importer?.startsWith(VIRTUAL_PREFIX) || false;
if (id.indexOf(`/node_modules/`) !== -1) {
resolveId: async function (originalId, originalImporter) {
const [importer, virtualImporter] = originalImporter
? maybeVirtual(originalImporter)
: ([null, false] as const);
if (originalId.indexOf(`/node_modules/`) !== -1) {
return null;
}
if (id.startsWith(VIRTUAL_PREFIX)) {
id = id.slice(VIRTUAL_PREFIX.length);
}
let [id] = maybeVirtual(originalId);
// Remove query params.
id = id.split("?", 2)[0]!;
const extension = path.extname(id);
Expand All @@ -38,9 +53,6 @@ export function virtualPlugin(options: {
if (!importer) {
return null;
}
if (virtualImporter) {
importer = importer.slice(VIRTUAL_PREFIX.length);
}
if (extension && !jsExtensions.has(extension) && extension !== ".svg") {
// Virtual files mess with CSS processors like postcss.
return path.join(path.dirname(importer), id);
Expand Down Expand Up @@ -70,6 +82,12 @@ export function virtualPlugin(options: {
if (id.startsWith(VIRTUAL_PREFIX)) {
id = id.slice(VIRTUAL_PREFIX.length);
}
if (id.startsWith("\0")) {
id = id.slice(1);
}
if (id.startsWith(VIRTUAL_PREFIX2)) {
id = id.slice(VIRTUAL_PREFIX2.length);
}
const resolved = await resolveAbsoluteModuleId(id);
if (!resolved) {
// This could be a file handled by another plugin, e.g. CSS modules.
Expand Down
1 change: 0 additions & 1 deletion core/src/vite/vite-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ export class ViteManager {
...this.options.config.vite,
configFile: false,
root: this.options.rootDir,
base: "/preview/",
optimizeDeps: {
entries: [],
esbuildOptions: {
Expand Down
2 changes: 1 addition & 1 deletion iframe/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class PreviewIframeControllerImpl implements PreviewIframeController {
if (!iframe) {
return;
}
iframe.src = `/preview/${id}/?t=${Date.now()}`;
iframe.src = `/${id}/?t=${Date.now()}`;
}

onPreviewMessage(message: PreviewToAppMessage) {
Expand Down

0 comments on commit 05d0eaf

Please sign in to comment.