[ci] yarn format
This commit is contained in:
parent
fd52bceea4
commit
8e3fd04dbd
14 changed files with 163 additions and 178 deletions
|
@ -97,7 +97,7 @@ class AstroBuilder {
|
|||
route,
|
||||
routeCache: this.routeCache,
|
||||
viteServer,
|
||||
})
|
||||
}),
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ class AstroBuilder {
|
|||
route,
|
||||
routeCache: this.routeCache,
|
||||
viteServer,
|
||||
})
|
||||
}),
|
||||
};
|
||||
})
|
||||
);
|
||||
|
@ -171,13 +171,13 @@ class AstroBuilder {
|
|||
allPages,
|
||||
pageNames,
|
||||
routeCache: this.routeCache,
|
||||
viteServer
|
||||
viteServer,
|
||||
}),
|
||||
rollupPluginAstroBuildCSS({
|
||||
astroPageStyleMap,
|
||||
astroStyleMap,
|
||||
chunkToReferenceIdMap,
|
||||
pureCSSChunks
|
||||
pureCSSChunks,
|
||||
}),
|
||||
...(viteConfig.plugins || []),
|
||||
],
|
||||
|
@ -202,7 +202,7 @@ class AstroBuilder {
|
|||
timer.sitemapStart = performance.now();
|
||||
if (this.config.buildOptions.sitemap && this.config.buildOptions.site) {
|
||||
const sitemapStart = performance.now();
|
||||
const sitemap = generateSitemap(pageNames.map(pageName => new URL(`/${pageName}`, this.config.buildOptions.site).href));
|
||||
const sitemap = generateSitemap(pageNames.map((pageName) => new URL(`/${pageName}`, this.config.buildOptions.site).href));
|
||||
const sitemapPath = new URL('./sitemap.xml', this.config.dist);
|
||||
await fs.promises.mkdir(new URL('./', sitemapPath), { recursive: true });
|
||||
await fs.promises.writeFile(sitemapPath, sitemap, 'utf8');
|
||||
|
|
1
packages/astro/src/core/build/types.d.ts
vendored
1
packages/astro/src/core/build/types.d.ts
vendored
|
@ -7,4 +7,3 @@ export interface PageBuildData {
|
|||
route: RouteData;
|
||||
}
|
||||
export type AllPagesData = Record<string, PageBuildData>;
|
||||
|
||||
|
|
|
@ -191,7 +191,7 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO
|
|||
},
|
||||
_metadata: {
|
||||
renderers,
|
||||
pathname
|
||||
pathname,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -217,7 +217,7 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO
|
|||
attrs: {
|
||||
rel: 'stylesheet',
|
||||
href,
|
||||
'data-astro-injected': true
|
||||
'data-astro-injected': true,
|
||||
},
|
||||
injectTo: 'head',
|
||||
});
|
||||
|
|
|
@ -272,16 +272,20 @@ export async function renderPage(result: SSRResult, Component: AstroComponentFac
|
|||
const template = await renderToString(result, Component, props, children);
|
||||
const styles = Array.from(result.styles)
|
||||
.filter(uniqueElements)
|
||||
.map((style) => renderElement('style', {
|
||||
.map((style) =>
|
||||
renderElement('style', {
|
||||
...style,
|
||||
props: { ...style.props, 'astro-style': true }
|
||||
}));
|
||||
props: { ...style.props, 'astro-style': true },
|
||||
})
|
||||
);
|
||||
const scripts = Array.from(result.scripts)
|
||||
.filter(uniqueElements)
|
||||
.map((script, i) => renderElement('script', {
|
||||
.map((script, i) =>
|
||||
renderElement('script', {
|
||||
...script,
|
||||
props: { ...script.props, 'astro-script': result._metadata.pathname + '/script-' + i }
|
||||
}));
|
||||
props: { ...script.props, 'astro-script': result._metadata.pathname + '/script-' + i },
|
||||
})
|
||||
);
|
||||
return template.replace('</head>', styles.join('\n') + scripts.join('\n') + '</head>');
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
import type { ResolveIdHook, LoadHook, RenderedChunk } from 'rollup';
|
||||
import type { Plugin as VitePlugin } from 'vite';
|
||||
|
||||
|
@ -7,7 +6,6 @@ import { getViteResolve, getViteLoad } from './resolve.js';
|
|||
import { getViteTransform, TransformHook } from '../vite-plugin-astro/styles.js';
|
||||
import * as path from 'path';
|
||||
|
||||
|
||||
const PLUGIN_NAME = '@astrojs/rollup-plugin-build-css';
|
||||
|
||||
// This is a virtual module that represents the .astro <style> usage on a page
|
||||
|
@ -125,7 +123,7 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
|
|||
const referenceId = this.emitFile({
|
||||
name: chunk.name + '.css',
|
||||
type: 'asset',
|
||||
source: chunkCSS
|
||||
source: chunkCSS,
|
||||
});
|
||||
pureCSSChunks.add(chunk);
|
||||
chunkToReferenceIdMap.set(chunk.fileName, referenceId);
|
||||
|
@ -141,6 +139,6 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
|
|||
delete bundle[chunkId];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -4,7 +4,8 @@ import type { ResolvedConfig, Plugin as VitePlugin } from 'vite';
|
|||
export function getVitePluginByName(viteConfig: ResolvedConfig, pluginName: string): VitePlugin {
|
||||
const plugin = viteConfig.plugins.find(({ name }) => name === pluginName);
|
||||
if (!plugin) throw new Error(`${pluginName} plugin couldn’t be found`);
|
||||
return plugin;}
|
||||
return plugin;
|
||||
}
|
||||
|
||||
export function getViteResolvePlugin(viteConfig: ResolvedConfig): VitePlugin {
|
||||
return getVitePluginByName(viteConfig, 'vite:resolve');
|
||||
|
|
|
@ -8,10 +8,7 @@ function fromEntries<V>(entries: [string, V][]) {
|
|||
return obj;
|
||||
}
|
||||
|
||||
export function addRollupInput(
|
||||
inputOptions: InputOptions,
|
||||
newInputs: string[]
|
||||
): InputOptions {
|
||||
export function addRollupInput(inputOptions: InputOptions, newInputs: string[]): InputOptions {
|
||||
// Add input module ids to existing input option, whether it's a string, array or object
|
||||
// this way you can use multiple html plugins all adding their own inputs
|
||||
if (!inputOptions.input) {
|
||||
|
@ -37,10 +34,7 @@ export function addRollupInput(
|
|||
...inputOptions,
|
||||
input: {
|
||||
...inputOptions.input,
|
||||
...fromEntries(
|
||||
newInputs
|
||||
.map(i => [i.split('/').slice(-1)[0].split('.')[0], i]),
|
||||
),
|
||||
...fromEntries(newInputs.map((i) => [i.split('/').slice(-1)[0].split('.')[0], i])),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -11,9 +11,7 @@ function getSrcSetUrls(srcset: string) {
|
|||
return [];
|
||||
}
|
||||
const srcsetParts = srcset.includes(',') ? srcset.split(',') : [srcset];
|
||||
const urls = srcsetParts
|
||||
.map(url => url.trim())
|
||||
.map(url => (url.includes(' ') ? url.split(' ')[0] : url));
|
||||
const urls = srcsetParts.map((url) => url.trim()).map((url) => (url.includes(' ') ? url.split(' ')[0] : url));
|
||||
return urls;
|
||||
}
|
||||
|
||||
|
@ -101,20 +99,9 @@ export function isHashedAsset(node: Element) {
|
|||
}
|
||||
}
|
||||
|
||||
export function resolveAssetFilePath(
|
||||
browserPath: string,
|
||||
htmlDir: string,
|
||||
projectRootDir: string,
|
||||
absolutePathPrefix?: string,
|
||||
) {
|
||||
const _browserPath =
|
||||
absolutePathPrefix && browserPath[0] === '/'
|
||||
? '/' + npath.posix.relative(absolutePathPrefix, browserPath)
|
||||
: browserPath;
|
||||
return npath.join(
|
||||
_browserPath.startsWith('/') ? projectRootDir : htmlDir,
|
||||
_browserPath.split('/').join(npath.sep),
|
||||
);
|
||||
export function resolveAssetFilePath(browserPath: string, htmlDir: string, projectRootDir: string, absolutePathPrefix?: string) {
|
||||
const _browserPath = absolutePathPrefix && browserPath[0] === '/' ? '/' + npath.posix.relative(absolutePathPrefix, browserPath) : browserPath;
|
||||
return npath.join(_browserPath.startsWith('/') ? projectRootDir : htmlDir, _browserPath.split('/').join(npath.sep));
|
||||
}
|
||||
|
||||
export function getSourceAttribute(node: Element) {
|
||||
|
@ -160,16 +147,16 @@ export function getSourcePaths(node: Element) {
|
|||
throw new Error(`Missing attribute ${key} in element ${node.nodeName}`);
|
||||
}
|
||||
|
||||
let paths: {path: string, location: Location}[] = [];
|
||||
let paths: { path: string; location: Location }[] = [];
|
||||
if (src && key === 'srcset') {
|
||||
paths = getSrcSetUrls(src).map(path => ({
|
||||
paths = getSrcSetUrls(src).map((path) => ({
|
||||
path,
|
||||
location
|
||||
location,
|
||||
}));
|
||||
} else if (src) {
|
||||
paths.push({
|
||||
path: src,
|
||||
location
|
||||
location,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -183,7 +170,7 @@ export function getTextContent(node: Node): string {
|
|||
if (adapter.isTextNode(node)) {
|
||||
return node.value || '';
|
||||
}
|
||||
const subtree = findNodes(node, n => adapter.isTextNode(n));
|
||||
const subtree = findNodes(node, (n) => adapter.isTextNode(n));
|
||||
return subtree.map(getTextContent).join('');
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
import type { AstroConfig, RouteCache } from '../@types/astro-core';
|
||||
import type { LogOptions } from '../core/logger';
|
||||
import type { ViteDevServer, Plugin as VitePlugin } from 'vite';
|
||||
|
@ -94,7 +93,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
renderedPageMap.set(id, html);
|
||||
|
||||
const document = parse5.parse(html, {
|
||||
sourceCodeLocationInfo: true
|
||||
sourceCodeLocationInfo: true,
|
||||
});
|
||||
|
||||
const frontEndImports = [];
|
||||
|
@ -149,13 +148,13 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
|
||||
if (frontEndImports.length) {
|
||||
htmlInput.add(id);
|
||||
const jsSource = frontEndImports.map(sid => `import '${sid}';`).join('\n');
|
||||
const jsSource = frontEndImports.map((sid) => `import '${sid}';`).join('\n');
|
||||
astroPageMap.set(id, jsSource);
|
||||
}
|
||||
|
||||
if (assetImports.length) {
|
||||
const pageStyleId = getAstroPageStyleId(pathname);
|
||||
const jsSource = assetImports.map(sid => `import '${sid}';`).join('\n');
|
||||
const jsSource = assetImports.map((sid) => `import '${sid}';`).join('\n');
|
||||
astroPageStyleMap.set(pageStyleId, jsSource);
|
||||
assetInput.add(pageStyleId);
|
||||
}
|
||||
|
@ -171,7 +170,6 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
return outOptions;
|
||||
},
|
||||
|
||||
|
||||
async resolveId(id) {
|
||||
switch (true) {
|
||||
case astroScriptMap.has(id):
|
||||
|
@ -213,7 +211,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
return `assets/${pageName}.[hash].js`;
|
||||
}
|
||||
return 'assets/[name].[hash].js';
|
||||
}
|
||||
},
|
||||
});
|
||||
return outputOptions;
|
||||
},
|
||||
|
@ -238,7 +236,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
const referenceId = this.emitFile({
|
||||
type: 'asset',
|
||||
name: npath.basename(assetPath),
|
||||
source: await dataPromise
|
||||
source: await dataPromise,
|
||||
});
|
||||
assetIdMap.set(assetPath, referenceId);
|
||||
}
|
||||
|
@ -272,23 +270,26 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
const key = pathname + relPath + attrs.rel || 'stylesheet';
|
||||
if (!linkChunksAdded.has(key)) {
|
||||
linkChunksAdded.add(key);
|
||||
insertBefore(lastNode.parentNode, createElement('link', {
|
||||
insertBefore(
|
||||
lastNode.parentNode,
|
||||
createElement('link', {
|
||||
rel: 'stylesheet',
|
||||
...attrs,
|
||||
href: relPath
|
||||
}), lastNode);
|
||||
href: relPath,
|
||||
}),
|
||||
lastNode
|
||||
);
|
||||
added = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return added;
|
||||
}
|
||||
};
|
||||
|
||||
for (const [id, html] of renderedPageMap) {
|
||||
const pathname = id.substr(ASTRO_PAGE_PREFIX.length);
|
||||
const document = parse5.parse(html, {
|
||||
sourceCodeLocationInfo: true
|
||||
sourceCodeLocationInfo: true,
|
||||
});
|
||||
|
||||
if (facadeIdMap.has(id)) {
|
||||
|
@ -301,10 +302,14 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
if (getAttribute(script, 'astro-script')) {
|
||||
if (i === 0) {
|
||||
const relPath = npath.posix.relative(pathname, bundlePath);
|
||||
insertBefore(script.parentNode, createScript({
|
||||
insertBefore(
|
||||
script.parentNode,
|
||||
createScript({
|
||||
type: 'module',
|
||||
src: relPath
|
||||
}), script);
|
||||
src: relPath,
|
||||
}),
|
||||
script
|
||||
);
|
||||
}
|
||||
remove(script);
|
||||
}
|
||||
|
@ -320,7 +325,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
switch (rel) {
|
||||
case 'stylesheet': {
|
||||
if (!pageCSSAdded) {
|
||||
const attrs = Object.fromEntries(node.attrs.map(attr => [attr.name, attr.value]));
|
||||
const attrs = Object.fromEntries(node.attrs.map((attr) => [attr.name, attr.value]));
|
||||
delete attrs['data-astro-injected'];
|
||||
pageCSSAdded = appendStyleChunksBefore(node, pathname, cssChunkMap.get(styleId), attrs);
|
||||
}
|
||||
|
@ -329,7 +334,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
}
|
||||
case 'preload': {
|
||||
if (getAttribute(node, 'as') === 'style') {
|
||||
const attrs = Object.fromEntries(node.attrs.map(attr => [attr.name, attr.value]));
|
||||
const attrs = Object.fromEntries(node.attrs.map((attr) => [attr.name, attr.value]));
|
||||
appendStyleChunksBefore(node, pathname, cssChunkMap.get(styleId), attrs);
|
||||
remove(node);
|
||||
}
|
||||
|
@ -351,7 +356,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
if (hasSrcSet(node)) {
|
||||
const srcset = getAttribute(node, 'srcset')!;
|
||||
let changedSrcset = srcset;
|
||||
const urls = matchSrcset(srcset).map(c => c.url);
|
||||
const urls = matchSrcset(srcset).map((c) => c.url);
|
||||
for (const url of urls) {
|
||||
if (assetIdMap.has(url)) {
|
||||
const referenceId = assetIdMap.get(url)!;
|
||||
|
@ -383,9 +388,9 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
|||
this.emitFile({
|
||||
fileName: outPath,
|
||||
source: outHTML,
|
||||
type: 'asset'
|
||||
type: 'asset',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import { loadFixture } from './test-utils.js';
|
|||
import srcsetParse from 'srcset-parse';
|
||||
|
||||
// This package isn't real ESM, so have to coerce it
|
||||
const matchSrcset = (srcsetParse).default;
|
||||
const matchSrcset = srcsetParse.default;
|
||||
|
||||
// Asset bundling
|
||||
describe('Assets', () => {
|
||||
|
@ -28,7 +28,7 @@ describe('Assets', () => {
|
|||
const $ = cheerio.load(html);
|
||||
const srcset = $('img').attr('srcset');
|
||||
const candidates = matchSrcset(srcset);
|
||||
const match = candidates.find(a => a.density === 2);
|
||||
const match = candidates.find((a) => a.density === 2);
|
||||
const data = await fixture.readFile('/' + match.url);
|
||||
expect(!!data).to.equal(true);
|
||||
});
|
||||
|
@ -38,7 +38,7 @@ describe('Assets', () => {
|
|||
const $ = cheerio.load(html);
|
||||
const srcset = $('img').attr('srcset');
|
||||
const candidates = matchSrcset(srcset);
|
||||
const match = candidates.find(a => a.density === 3);
|
||||
const match = candidates.find((a) => a.density === 3);
|
||||
const data = await fixture.readFile('/' + match.url);
|
||||
expect(!!data).to.equal(true);
|
||||
});
|
||||
|
|
|
@ -11,9 +11,7 @@ describe('Styles SSR', () => {
|
|||
});
|
||||
|
||||
it('Has <link> tags', async () => {
|
||||
const MUST_HAVE_LINK_TAGS = [
|
||||
'assets/index'
|
||||
];
|
||||
const MUST_HAVE_LINK_TAGS = ['assets/index'];
|
||||
|
||||
const html = await fixture.readFile('/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
@ -71,8 +69,7 @@ describe('Styles SSR', () => {
|
|||
let scopedClass;
|
||||
|
||||
// test 1: <style> tag in <head> is transformed
|
||||
const css = raw
|
||||
.replace(/\.astro-[A-Za-z0-9-]+/, (match) => {
|
||||
const css = raw.replace(/\.astro-[A-Za-z0-9-]+/, (match) => {
|
||||
scopedClass = match; // get class hash from result
|
||||
return match;
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue