-
-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
build: document is not defined #254
Comments
Stumbled upon that problem several times now. Before I was able to wrap it and create a mock window/document to make it build. Now I added an npm package that wants document (quill.js in my case) and I don't know what to do now. Project runs smooth in dev but wont build. Is there perhaps an option I overlooked that injects a mock document to make modules build? |
@Simon-He95 concerning your document problem: did you try tor wrap the problematic code portion(s) into try .. catch to handle the missing document issue? |
@antfu any idea? did I miss an option? |
I try to wrap the problematic code portion(s) into try .. catch to handle, the issue was resolved. |
Well ... As I have no idea about your code I have to freeride here ...
|
... I just had another look at the issues-page and this problem is prevalent when using vite-ssg, but it seems to be also solved (?) ... I have to take a look at their suggestions and see if they are feasable for my situation ... did you have a look there and tried some of their suggestions? |
... and ... yes, it would of course be awesome, if that would be something taken care of on vite-ssg's side ;) |
@blwtxc I sent a PR a few days ago to allow use |
Yeah,most of the problems are in build process, when build we might be able to wrap window/document into try...catch or mock window/document, If there are errors, let the errors be thrown at runtime |
@Simon-He95 the problem here is the executon of This line https://github.com/antfu/vite-ssg/blob/main/src/node/build.ts#L107 will load previous generated module, but the |
@userquin thanks a lot for chiming in and your reply!! :D |
@userquin I see what you mean...
at the start of the script, and then
just before
... but I'm quite sure that I overlooked something ;D |
@blwtxc you can just try modifying the |
@userquin Thanks! Already achieved it with |
@blwtxc about happy-dom, IIRC this guy didn't exist when Anthony started this repository... we use jsdom to do the SSG part, so be careful not to break it using the happy-dom patch |
@userquin Ok, thanks. If you want, I can let u know if I run into problems with |
@blwtxc what do you mean, adding also happy-dom to use it on SSG instead jsdom? If so, maybe you can change I'll move this issue to a discussion, my PR should fix this |
@userquin I think that is a brilliant idea! |
@userquin Ok ... had another look at the module code and I guess I understand what you mean. |
@blwtxc we should use/replace from this line https://github.com/antfu/vite-ssg/blob/main/src/node/build.ts#L167 to this another https://github.com/antfu/vite-ssg/blob/main/src/node/build.ts#L175 (both included, I'm assuming happy-dom can serialize the dom to html) EDIT: so we need to expose a common interface and move previous logic for example to the jsdom module, and then add the corresponding for happy-dom, once we have this use the mock value to load and use one of them The common interface abstracting the internals should be in its own module, then use that factory on the SSG loop for each page: export const SSGBuildContext {
document: Document
serialize: () => Awaitable<string>
} For import { JSDOM } from 'jsdom'
export function jsdom(renderedHTML) {
const jsdom= new JSDOM.JSDOM(renderedHTML)
return {
get document() { return jsdom.window.document },
serialize() { return jsdom.serialize() },
}
} For happy-dom we'll have something similar in its own module and also the corresponding Then use the |
@userquin I really shouldn't open my mouth if I don't know what I'm talking about :/ |
@userquin I see now where I made a mistake. |
@userquin I'm very sorry to have wasted your time! |
Under such scenario,I introduced an npm package component that relies on document
This error occurs that vite-ssg build
The text was updated successfully, but these errors were encountered: