PlayForm / Compress

🗜️ Compress —
https://playform.github.io/Compress/
Other
510 stars 12 forks source link

error Cannot access uninitialized variable. #224

Closed lucascompython closed 7 months ago

lucascompython commented 11 months ago

After installing astro-compress when I try to build I get the following error:

error   Cannot access uninitialized variable.
  File:
    /home/lucas/Desktop/Separador/node_modules/astro-compress/Target/Function/Integration.js
  Code:
    1 | var P=(...[r={}])=>{Object.entries(r).forEach(([a,e])=>Object.defineProperty(r,a,{value:e===!0?d[a]:r[a]}));const{Path:o,Cache:s,Logger:w,Map:m,Exclude:h,Action:y,CSS:S,HTML:g,Image:j,JavaScript:b,SVG:A,Parser:l}=f(d,r),n=new Set;return typeof o<"u"&&(Array.isArray(o)||o instanceof Set)&&o.forEach(a=>n.add(a)),typeof l=="object"&&Object.entries(l).forEach(([a,e])=>Object.defineProperty(l,a,{value:Array.isArray(e)?e:[e]})),{name:"astro-compress",hooks:{"astro:build:done":async({dir:a})=>{if(typeof m=="object"){n.size||n.add(a),typeof s=="object"&&s.Search===I&&(s.Search=a);for(const[e,i]of Object.entries({CSS:S,HTML:g,Image:j,JavaScript:b,SVG:A})){if(!(i&&m[e])||typeof i!="object")return;p=f(y,f(y,{Wrote:async({Buffer:t,Input:c})=>{switch(e){case"CSS":return(await import("csso")).minify(t.toString(),i.csso).css;case"HTML":return await(await import("html-minifier-terser")).minify(t.toString(),i["html-minifier-terser"]);case"JavaScript":return(await(await import("terser")).minify(t.toString(),i.terser)).code??t;case"Image":return await(await import("../Function/Image/Writesharp.js")).default(i.sharp,{Buffer:t,Input:c});case"SVG":{const{data:T}=(await import("svgo")).optimize(t.toString(),i.svgo);return T??t}default:return t}},Fulfilled:async t=>t.Files>0?`Successfully compressed a total of ${t.Files} ${e} ${t.Files===1?"file":"files"} for ${await(await import("files-pipe/Target/Function/Bytes.js")).default(t.Info.Total)}.`:!1})),e==="Image"&&(p=f(p,{Read:async({Input:t})=>{const{format:c}=await u(t).metadata();return u(t,{failOn:"none",sequentialRead:!0,unlimited:!0,animated:c==="webp"||c==="gif"})}}));for(const t of n)await(await(await(await new(await import("files-pipe")).default(s,w).In(t)).By(m[e]??"**/*")).Not(h)).Pipe(p)}}}}}};const{default:d}=await import("../Variable/Option.js"),{default:{Cache:{Search:I}}}=await import("files-pipe/Target/Variable/Option.js"),{default:f}=await import("typescript-esbuild/Target/Function/Merge.js"),{default:u}=await import("sharp");let p;export{d as Default,u as Defaultsharp,f as Merge,I as Search,p as _Action,P as default};
  Stacktrace:
ReferenceError: Cannot access uninitialized variable.
    at <anonymous> (/home/lucas/Desktop/Separador/node_modules/astro-compress/Target/Function/Integration.js:0:741)
    at astro:build:done (/home/lucas/Desktop/Separador/node_modules/astro-compress/Target/Function/Integration.js:0:514)
    at <anonymous> (/home/lucas/Desktop/Separador/node_modules/astro/dist/integrations/index.js:330:51)
    at processTicksAndRejections (:61:39)

I am using Bun 1.0.13

NikolaRHristov commented 11 months ago

Can you tell me more about your version, node version as well (if any) and maybe the configuration options.

lucascompython commented 11 months ago

Sure here is my astro.config.mjs:

import { defineConfig } from "astro/config";

import compress from "astro-compress";

// https://astro.build/config
export default defineConfig({
  vite: {
    server: {
      watch: {
        ignored: ["**/target/**"]
      }
    }
  },
  integrations: [compress()]
});

tsconfig.json:

{
  "extends": "astro/tsconfigs/strict",
    "compilerOptions": {
    "types": ["bun-types"]
  }
}

My node version is 21.2.0. Astro version: 3.5.5 Astro-Compress version: 2.2.3

NikolaRHristov commented 11 months ago

I'm unable to reproduce this on my local environment, can I ask you to open the file in VSCode, format it so that it's not minified or use a pretty print online https://beautifier.io/, save it and run again so we can debug this further.

hiteshjoshi commented 11 months ago

Had the same issue, moved form bun to yarn. It worked. I don't think bun is quite a replacement for node/npm yet.

NikolaRHristov commented 11 months ago

@hiteshjoshi It's working for me locally with this template https://github.com/Playform/AstroStarterTemplate on bun 1.0.13 node v21.2.0

Please if you can, either @hiteshjoshi or @lucascompython unminify (prettify whatever) the file and run build again, so we can debug this further. <3

lucascompython commented 11 months ago

So I prettified the file and I got the following output:

 error   Cannot access uninitialized variable.
  File:
    /home/lucas/Desktop/Separador/node_modules/astro-compress/Target/Function/Integration.js:37:28
  Code:
    36 |                         if (!(i && m[e]) || typeof i != "object") return;
    > 37 |                         p = f(y, f(y, {
         |                            ^
      38 |                             Wrote: async ({
      39 |                                 Buffer: t,
      40 |                                 Input: c
  Stacktrace:
ReferenceError: Cannot access uninitialized variable.
    at <anonymous> (/home/lucas/Desktop/Separador/node_modules/astro-compress/Target/Function/Integration.js:37:28)
    at astro:build:done (/home/lucas/Desktop/Separador/node_modules/astro-compress/Target/Function/Integration.js:24:21)
    at <anonymous> (/home/lucas/Desktop/Separador/node_modules/astro/dist/integrations/index.js:330:51)
    at processTicksAndRejections (:61:39)

I also noticed that if I try to add astro-compress with bunx --bun astro add astro-compress the program crashes in the confirmation part. But this could be "normal", in the Astro documentation they do not use the --bun flag.

NikolaRHristov commented 11 months ago

Hi, @lucascompython, @hiteshjoshi, can you try v2.2.5 and see if the error fixed itself 😁 It's my only hope at this point.

lucascompython commented 7 months ago

Hi. I am sorry for the late reply. But it is working now. Thank you!