Skip to content
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

Fix npm install #1407

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.18.0",
"autoprefixer": "^10.2.6",
"cheerio": "^1.0.0-rc.6",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still needed? Assuming this can be moved into astro unless other packages use it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests do use it, but all those tests currently live in packages/astro so it could be moved to devDeps there, I suppose. We’ll probably use it there for quite some time, so it’s basically the same effect.

"cheerio": "^1.0.0-rc.10",
"cheerio-select-tmp": "^0.1.1",
"del": "^6.0.0",
"esbuild": "^0.11.17",
Expand Down
1 change: 1 addition & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/rimraf": "^3.0.2",
"@web/rollup-plugin-html": "^1.9.1",
"astring": "^1.7.5",
"cheerio": "^1.0.0-rc.10",
"ci-info": "^3.2.0",
"connect": "^3.7.0",
"es-module-lexer": "^0.7.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/runtime/hmr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '/@vite/client';
import '@vite/client';

if (import.meta.hot) {
const parser = new DOMParser();
Expand Down
117 changes: 59 additions & 58 deletions packages/astro/src/runtime/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,66 +169,67 @@ export async function ssr({ astroConfig, filePath, logging, mode, origin, pathna
pageProps = { ...matchedStaticPath.props } || {};
}

// 3. render page
if (!browserHash && (viteServer as any)._optimizeDepsMetadata?.browserHash) browserHash = (viteServer as any)._optimizeDepsMetadata.browserHash; // note: this is "private" and may change over time
const fullURL = new URL(pathname, origin);

const Component = await mod.default;
const ext = path.posix.extname(filePath.pathname);
if (!Component)
throw new Error(`Expected an exported Astro component but received typeof ${typeof Component}`);

if (!Component.isAstroComponentFactory) throw new Error(`Unable to SSR non-Astro component (${route?.component})`);

const result = {
styles: new Set(),
scripts: new Set(),
/** This function returns the `Astro` faux-global */
createAstro: (props: any) => {
const site = new URL(origin);
const url = new URL('.' + pathname, site);
const canonicalURL = getCanonicalURL(pathname, astroConfig.buildOptions.site || origin)
const fetchContent = createFetchContent(fileURLToPath(filePath));
return {
isPage: true,
site,
request: { url, canonicalURL },
props,
fetchContent
// 3. render page
if (!browserHash && (viteServer as any)._optimizeDepsMetadata?.browserHash) browserHash = (viteServer as any)._optimizeDepsMetadata.browserHash; // note: this is "private" and may change over time
const fullURL = new URL(pathname, origin);

const Component = await mod.default;
const ext = path.posix.extname(filePath.pathname);
if (!Component) throw new Error(`Expected an exported Astro component but received typeof ${typeof Component}`);

if (!Component.isAstroComponentFactory) throw new Error(`Unable to SSR non-Astro component (${route?.component})`);

const result = {
styles: new Set(),
scripts: new Set(),
/** This function returns the `Astro` faux-global */
createAstro: (props: any) => {
const site = new URL(origin);
const url = new URL('.' + pathname, site);
const canonicalURL = getCanonicalURL(pathname, astroConfig.buildOptions.site || origin);
const fetchContent = createFetchContent(fileURLToPath(filePath));
return {
isPage: true,
site,
request: { url, canonicalURL },
props,
fetchContent,
};
},
_metadata: { importedModules, renderers },
};

const createFetchContent = (currentFilePath: string) => {
const fetchContentCache = new Map<string, any>();
return async (pattern: string) => {
const cwd = path.dirname(currentFilePath);
const cacheKey = `${cwd}:${pattern}`;
if (fetchContentCache.has(cacheKey)) {
return fetchContentCache.get(cacheKey);
}
const files = await glob(pattern, { cwd, absolute: true });
const contents = await Promise.all(
files.map(async (file) => {
const { metadata: astro = {}, frontmatter = {} } = (await viteServer.ssrLoadModule(file)) as any;
return { ...frontmatter, astro };
})
);
fetchContentCache.set(cacheKey, contents);
return contents;
};
},
_metadata: { importedModules, renderers },
}

const createFetchContent = (currentFilePath: string) => {
const fetchContentCache = new Map<string, any>();
return async (pattern: string) => {
const cwd = path.dirname(currentFilePath);
const cacheKey = `${cwd}:${pattern}`;
if (fetchContentCache.has(cacheKey)) {
return fetchContentCache.get(cacheKey);
}
const files = await glob(pattern, { cwd, absolute: true });
const contents = await Promise.all(files.map(async file => {
const { metadata: astro = {}, frontmatter = {} } = (await viteServer.ssrLoadModule(file)) as any;
return { ...frontmatter, astro };
}))
fetchContentCache.set(cacheKey, contents);
return contents;
};

let html = await renderPage(result, Component, {}, null);

// 4. modify response
if (mode === 'development') {
// inject Astro HMR code
html = injectAstroHMR(html);
// inject Vite HMR code
html = injectViteClient(html);
// replace client hydration scripts
html = resolveNpmImports(html);
}
}

let html = await renderPage(result, Component, {}, null);

// 4. modify response
if (mode === 'development') {
// inject Astro HMR code
html = injectAstroHMR(html);
// inject Vite HMR code
html = injectViteClient(html);
// replace client hydration scripts
html = resolveNpmImports(html);
}

// 5. finish
return html;
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3389,7 +3389,7 @@ cheerio-select@^1.5.0:
domhandler "^4.2.0"
domutils "^2.7.0"

cheerio@^1.0.0-rc.6, cheerio@~1.0.0-rc.3:
cheerio@^1.0.0-rc.10, cheerio@~1.0.0-rc.3:
version "1.0.0-rc.10"
resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-1.0.0-rc.10.tgz#2ba3dcdfcc26e7956fc1f440e61d51c643379f3e"
integrity sha512-g0J0q/O6mW8z5zxQ3A8E8J1hUgp4SMOvEoW/x84OwyHKe/Zccz83PVT4y5Crcr530FV6NgmKI1qvGTKVl9XXVw==
Expand Down