CloudCannon / bookshop

📚 A component development workflow for static websites.
MIT License
247 stars 21 forks source link

[Astro] ENV_BOOKSHOP_LIVE undefined in ts files #201

Open oliverlynch opened 4 days ago

oliverlynch commented 4 days ago

In my Astro site I have a util.ts file to de-duplicate some functions I need for different components. Recently I wanted to move some functions that require sharp. In astro components containing the dynamic import in a simple !ENV_BOOKSHOP_LIVE conditional statement allowed me to import external libraries that are incompatible with bookshop. Unfortunately when moving these functions into my .ts file the build fails.

 > node_modules/sharp/lib/utility.js:6:23: error: Could not resolve "node:events" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    6 │ const events = require('node:events');
      ╵                        ~~~~~~~~~~~~~

 > node_modules/sharp/lib/constructor.js:6:21: error: Could not resolve "node:util" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    6 │ const util = require('node:util');
      ╵                      ~~~~~~~~~~~

 > node_modules/sharp/lib/output.js:6:21: error: Could not resolve "node:path" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    6 │ const path = require('node:path');
      ╵                      ~~~~~~~~~~~

It seems like this happens because ENV_BOOKSHOP_LIVE is not defined in typescript files during the bookshop build.