Skip to content

Commit

Permalink
Move astro-basic test to use static build (#2682)
Browse files Browse the repository at this point in the history
* Move some tests over to the static build (#2677)

* Move some tests over to the static build

* Fix assets tests

* Fix the assets tests

* Fix for the client:only components

* Moves asset tests to the static build

* Move postcss test over to static build

* Bring back legacy build for astro-basic test

* Move astro-basic test to use static build
  • Loading branch information
matthewp authored Mar 1, 2022
1 parent 465d989 commit c8cbe4b
Show file tree
Hide file tree
Showing 28 changed files with 64 additions and 228 deletions.
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"test:match": "mocha --timeout 15000 -g"
},
"dependencies": {
"@astrojs/compiler": "^0.11.4",
"@astrojs/compiler": "^0.12.0-next.5",
"@astrojs/language-server": "^0.8.6",
"@astrojs/markdown-remark": "^0.6.2",
"@astrojs/prism": "0.4.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export async function staticBuild(opts: StaticBuildOptions) {
const topLevelImports = new Set([
// Any component that gets hydrated
...metadata.hydratedComponentPaths(),
// Client-only components
...metadata.clientOnlyComponentPaths(),
// Any hydration directive like astro/client/idle.js
...metadata.hydrationDirectiveSpecifiers(),
// The client path for each renderer
Expand Down Expand Up @@ -181,6 +183,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
logLevel: 'error',
mode: 'production',
build: {
...viteConfig.build,
emptyOutDir: false,
manifest: ssr,
minify: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?
basename,
ext,
parts,
file: slash(file),
file: file.replace(/\\/g, '/'),
isDir,
isIndex,
isPage,
Expand Down
16 changes: 16 additions & 0 deletions packages/astro/src/runtime/server/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface ComponentMetadata {
interface CreateMetadataOptions {
modules: ModuleInfo[];
hydratedComponents: any[];
clientOnlyComponents: any[];
hydrationDirectives: Set<string>;
hoisted: any[];
}
Expand All @@ -22,6 +23,7 @@ export class Metadata {
public modules: ModuleInfo[];
public hoisted: any[];
public hydratedComponents: any[];
public clientOnlyComponents: any[];
public hydrationDirectives: Set<string>;

private metadataCache: Map<any, ComponentMetadata | null>;
Expand All @@ -30,6 +32,7 @@ export class Metadata {
this.modules = opts.modules;
this.hoisted = opts.hoisted;
this.hydratedComponents = opts.hydratedComponents;
this.clientOnlyComponents = opts.clientOnlyComponents;
this.hydrationDirectives = opts.hydrationDirectives;
this.mockURL = new URL(filePathname, 'http://example.com');
this.metadataCache = new Map<any, ComponentMetadata | null>();
Expand Down Expand Up @@ -66,6 +69,19 @@ export class Metadata {
}
}

*clientOnlyComponentPaths() {
const found = new Set<string>();
for (const metadata of this.deepMetadata()) {
for (const component of metadata.clientOnlyComponents) {
const path = metadata.resolvePath(component);
if (path && !found.has(path)) {
found.add(path);
yield path;
}
}
}
}

/**
* Gets all of the hydration specifiers used within this component.
*/
Expand Down
16 changes: 10 additions & 6 deletions packages/astro/test/astro-assets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ describe('Assets', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-assets/',
buildOptions: { legacyBuild: true } // TODO make this test work without legacyBuild
vite: {
build: {
assetsInlineLimit: 0
}
}
});
await fixture.build();
});
Expand All @@ -22,7 +26,7 @@ describe('Assets', () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const imgPath = $('img').attr('src');
const data = await fixture.readFile('/' + imgPath);
const data = await fixture.readFile( imgPath);
expect(!!data).to.equal(true);
});

Expand All @@ -32,7 +36,7 @@ describe('Assets', () => {
const srcset = $('img').attr('srcset');
const candidates = matchSrcset(srcset);
const match = candidates.find((a) => a.density === 2);
const data = await fixture.readFile('/' + match.url);
const data = await fixture.readFile(match.url);
expect(!!data).to.equal(true);
});

Expand All @@ -42,22 +46,22 @@ describe('Assets', () => {
const srcset = $('img').attr('srcset');
const candidates = matchSrcset(srcset);
const match = candidates.find((a) => a.density === 3);
const data = await fixture.readFile('/' + match.url);
const data = await fixture.readFile(match.url);
expect(!!data).to.equal(true);
});

it('built image from an import specifier', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const src = '/' + $('#import-no-url').attr('src');
const src = $('#import-no-url').attr('src');
const data = await fixture.readFile(src);
expect(!!data).to.equal(true);
});

it('built image from an import specifier using ?url', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const src = '/' + $('#import-url').attr('src');
const src = $('#import-url').attr('src');
const data = await fixture.readFile(src);
expect(!!data).to.equal(true);
});
Expand Down
1 change: 0 additions & 1 deletion packages/astro/test/astro-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('Astro basics', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-basic/',
buildOptions: { legacyBuild: true } // TODO make this test work without legacyBuild
});
await fixture.build();
previewServer = await fixture.preview();
Expand Down
16 changes: 3 additions & 13 deletions packages/astro/test/astro-client-only.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe('Client only components', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-client-only/',
buildOptions: { legacyBuild: true } // TODO make this test work without legacyBuild
});
await fixture.build();
});
Expand All @@ -19,19 +18,10 @@ describe('Client only components', () => {

// test 1: <astro-root> is empty
expect($('astro-root').html()).to.equal('');
const src = $('script').attr('src');
const $script = $('script');
const script = $script.html();

const script = await fixture.readFile(src);
// test 2: svelte renderer is on the page
const exp = /import\("(.\/client.*)"\)/g;
let match, svelteRenderer;
while ((match = exp.exec(script))) {
svelteRenderer = match[1].replace(/^\./, '/assets/');
}
expect(svelteRenderer).to.be.ok;

// test 3: can load svelte renderer
const svelteClient = await fixture.readFile(svelteRenderer);
expect(svelteClient).to.be.ok;
expect(/import\(".\/PersistentCounter.*/g.test(script)).to.be.ok;
});
});
56 changes: 0 additions & 56 deletions packages/astro/test/astro-components.test.js

This file was deleted.

12 changes: 8 additions & 4 deletions packages/astro/test/astro-scripts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ describe('Scripts (hoisted and not)', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-scripts/',
buildOptions: { legacyBuild: true } // TODO make this test work without legacyBuild
vite: {
build: {
assetsInlineLimit: 0
}
}
});
await fixture.build();
});
Expand Down Expand Up @@ -41,8 +45,8 @@ describe('Scripts (hoisted and not)', () => {
// test 2: attr removed
expect($('script').attr('data-astro')).to.equal(undefined);

let entryURL = path.join('inline', $('script').attr('src'));
let inlineEntryJS = await fixture.readFile(entryURL);
const entryURL = $('script').attr('src');
const inlineEntryJS = await fixture.readFile(entryURL);

// test 3: the JS exists
expect(inlineEntryJS).to.be.ok;
Expand All @@ -56,7 +60,7 @@ describe('Scripts (hoisted and not)', () => {
expect($('script')).to.have.lengthOf(2);

let el = $('script').get(1);
let entryURL = path.join('external', $(el).attr('src'));
let entryURL = $(el).attr('src');
let externalEntryJS = await fixture.readFile(entryURL);

// test 2: the JS exists
Expand Down
48 changes: 0 additions & 48 deletions packages/astro/test/cli.test.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import p2Url from '../images/penguin2.jpg?url';
</style>
<body>
<h1>Icons</h1>
<img src={Astro.resolve('../images/twitter.png')} srcset={`${Astro.resolve('../images/twitter.png')} 1x, ${Astro.resolve('../images/[email protected]')} 2x, ${Astro.resolve('../images/[email protected]')} 3x`} />
<img src={(await import('../images/twitter.png')).default} srcset={`${(await import('../images/twitter.png')).default} 1x, ${(await import('../images/[email protected]')).default} 2x, ${(await import('../images/[email protected]')).default} 3x`} />
<img srcset="https://ik.imagekit.io/demo/tr:w-300,h-300/medium_cafe_B1iTdD0C.jpg, https://ik.imagekit.io/demo/tr:w-450,h-450/medium_cafe_B1iTdD0C.jpg 600w, https://ik.imagekit.io/demo/tr:w-600,h-600/medium_cafe_B1iTdD0C.jpg 800w">
<img srcset="https://ik.imagekit.io/demo/tr:w-300,h-300/medium_cafe_B1iTdD0C.jpg, https://ik.imagekit.io/demo/tr:w-450,h-450/medium_cafe_B1iTdD0C.jpg 1.5x, https://ik.imagekit.io/demo/tr:w-600,h-600/medium_cafe_B1iTdD0C.jpg 2x">
<!--
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit c8cbe4b

Please sign in to comment.