Skip to content

Commit

Permalink
Merge pull request #175 from CloudCannon/fix/astro-param-errors
Browse files Browse the repository at this point in the history
Fix/astro param errors
  • Loading branch information
bglw authored Dec 5, 2023
2 parents 9254b11 + 53edbcc commit 86fb812
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

## Unreleased

* Fixes an error in Astro Bookshop, when spreading a prop that is possibly undefined.
* Fixes the Bookshop browser failing to load for Astro sites.
* Astro Bookshop will now detect component folders within subfolders of the Astro `src` folders.

## v3.8.1 (October 5, 2023)

* Fixes an error when loading Bookshop's dependencies through CloudCannon's client editing interface
Expand Down
15 changes: 9 additions & 6 deletions javascript-modules/browser/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /usr/bin/env node

import { tmpdir } from "os";
import { mkdtemp } from "fs/promises";
import path from "path";
import Builder from "@bookshop/builder";
import { Command } from "commander";
Expand All @@ -11,10 +12,12 @@ import BrowserServer from "./lib/build/browserServer.js";

export const runner = async (options) => {
const bookshopDirs = options.bookshop.map(d => path.join(process.cwd(), d));
const outputFile = options.output ? path.join(process.cwd(), options.output) : null;
const outputFile = options.output
? path.join(process.cwd(), options.output)
: path.join(await mkdtemp(path.join(tmpdir(), 'bookshop-')), 'app.js');
let port = options.port ?? null;
let server = null;
const watch = outputFile ? null : {
const watch = options.output ? null : {
onRebuild(error, result) {
if (error) {
console.error('📚 Renderer rebuild failed:', error)
Expand All @@ -24,12 +27,12 @@ export const runner = async (options) => {
},
};

if (outputFile && port) {
if (options.output && port) {
console.error(`Output file and port both specified — one or the other must be provided.`);
process.exit(1);
}

if (!outputFile) {
if (!options.output) {
port = 30775;
}

Expand All @@ -53,7 +56,7 @@ export const runner = async (options) => {
exclude: JSON.stringify(options.exclude || []),
onlyEngines: options.onlyEngines,
bookshopDirs: bookshopDirs,
hosted: !!outputFile,
hosted: !!options.output,
}

const output = await Builder(builderOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import processReactJSX from "./processors/react-jsx.js";
import processAstroComponent from "./processors/astro-component.js";
import processAstro from "./processors/astro.js";

const PAGE_REGEX = /.*src(\/|\\)(layouts|pages).*(\/|\\)(?<name>\w*)\.astro$/;
const PAGE_REGEX = /.*src((\/|\\)|(\/|\\).*(\/|\\))(layouts|pages).*(\/|\\)(?<name>\w*)\.astro$/;
const COMPONENT_REGEX =
/.*src(\/|\\)(components|shared(\/|\\)astro)(\/|\\)(?<component>.*)\.(astro|jsx|tsx)$/;
/.*src((\/|\\)|(\/|\\).*(\/|\\))(components|shared(\/|\\)astro)(\/|\\)(?<component>.*)\.(astro|jsx|tsx)$/;

const process = (src, id) => {
id = id.replace(cwd().replace(/\\/g, "/"), "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const process = (node, componentName) => {

spread.argument = parse(`
(() => {
if(${name}.__bookshop_path){
if(${name}?.__bookshop_path){
return {...${name}, __bookshop_path: ${name}.__bookshop_path};
}
return ${name};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ export default (src) => {
return $$render\`
\${(typeof $$maybeRenderHead !== 'undefined') ? $$maybeRenderHead($$result) : ''}
\${(${shouldDataBind} && bookshop_path !== null) ? $$render\`<!--databinding:#\${$$render(bookshop_path)}-->\`: ''}
\${(${shouldLiveRender} && ${component}.__bookshop_name) ? $$render\`<!--bookshop-live name(\${${component}.__bookshop_name}) params(\${$$render(params)})-->\`: ''}
\${(${shouldLiveRender} && ${component}?.__bookshop_name) ? $$render\`<!--bookshop-live name(\${${component}.__bookshop_name}) params(\${$$render(params)})-->\`: ''}
\${'REPLACE_ME'}
\${(${shouldLiveRender} && ${component}.__bookshop_name) ? $$render\`<!--bookshop-live end-->\`: ''}
\${(${shouldLiveRender} && ${component}?.__bookshop_name) ? $$render\`<!--bookshop-live end-->\`: ''}
\${(${shouldDataBind} && bookshop_path !== null) ? $$render\`<!--databindingend:#\${$$render(bookshop_path)}-->\`: ''}
\`})()`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export default (src, componentName) => {

spread.argument = parse(`
(() => {
if(${name}.__bookshop_path){
if(${name}?.__bookshop_path){
return {...${name}, __bookshop_path: ${name}.__bookshop_path};
}
return ${name};
Expand Down Expand Up @@ -274,7 +274,7 @@ export default (src, componentName) => {
};
node.body.body.unshift(
...parse(`
let __data_binding_path = ${name}?.__data_binding_path ?? ${name}.__bookshop_path;
let __data_binding_path = ${name}?.__data_binding_path ?? ${name}?.__bookshop_path;
`).program.body
);
node.body.body.unshift(
Expand Down

0 comments on commit 86fb812

Please sign in to comment.