Skip to content

Commit

Permalink
Merge branch 'main' into fix/missing-css-import-on-404-redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Aug 7, 2023
2 parents e2c006a + f6845d9 commit 77aae41
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-planets-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix filename generation for `.astro` pages
5 changes: 5 additions & 0 deletions .changeset/new-otters-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/rss': patch
---

Add URL to RSSOptions.site type
5 changes: 5 additions & 0 deletions .changeset/quick-eagles-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix inline root resolve logic
5 changes: 5 additions & 0 deletions .changeset/stupid-pants-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Ensure that injected routes from `node_modules` are properly detected
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
check:
name: astro check
runs-on: ubuntu-latest
timeout-minutes: 7
steps:
- name: Check out repository
uses: actions/checkout@v3
Expand Down
35 changes: 17 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,24 +230,23 @@ jobs:
- name: Remove docs translations except for English and Korean
run: find smoke/docs/src/content/docs ! -name 'en' ! -name 'ko' -type d -mindepth 1 -maxdepth 1 -exec rm -rf {} +

# TODO: enable when the script is updated
# - name: Check if docs changed
# id: changes
# uses: dorny/paths-filter@v2
# with:
# filters: |
# docs:
# - 'packages/integrations/*/README.md'
# - 'packages/astro/src/@types/astro.ts'
# - 'packages/astro/src/core/errors/errors-data.ts'
#
# - name: Build autogenerated docs pages from current astro branch
# if: ${{ steps.changes.outputs.docs == 'true' }}
# run: cd smoke/docs && pnpm docgen && pnpm docgen:errors && pnpm docgen:integrations
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# SOURCE_REPO: ${{ github.event.pull_request.head.repo.full_name || github.event.repository.full_name }}
# SOURCE_BRANCH: ${{ github.head_ref || github.ref_name }}
- name: Check if docs changed
id: changes
uses: dorny/paths-filter@v2
with:
filters: |
docs:
- 'packages/integrations/*/README.md'
- 'packages/astro/src/@types/astro.ts'
- 'packages/astro/src/core/errors/errors-data.ts'
- name: Build autogenerated docs pages from current astro branch
if: ${{ steps.changes.outputs.docs == 'true' }}
run: cd smoke/docs && pnpm docgen && pnpm docgen:errors && pnpm docgen:integrations
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SOURCE_REPO: ${{ github.event.pull_request.head.repo.full_name || github.event.repository.full_name }}
SOURCE_BRANCH: ${{ github.head_ref || github.ref_name }}

- name: Test
run: pnpm run test:smoke
Expand Down
2 changes: 1 addition & 1 deletion packages/astro-rss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type RSSOptions = {
* We recommend using the [endpoint context object](https://docs.astro.build/en/reference/api-reference/#contextsite),
* which includes the `site` configured in your project's `astro.config.*`
*/
site: z.infer<typeof rssOptionsValidator>['site'];
site: z.infer<typeof rssOptionsValidator>['site'] | URL;
/** List of RSS feed items to render. */
items: RSSFeedItem[] | GlobResult;
/** Specify arbitrary metadata on opening <xml> tag */
Expand Down
15 changes: 10 additions & 5 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,16 +1316,20 @@ export interface AstroUserConfig {
*/
export type InjectedScriptStage = 'before-hydration' | 'head-inline' | 'page' | 'page-ssr';

/**
* Resolved Astro Config
* Config with user settings along with all defaults filled in.
*/

export interface InjectedRoute {
pattern: string;
entryPoint: string;
prerender?: boolean;
}

export interface ResolvedInjectedRoute extends InjectedRoute {
resolvedEntryPoint?: URL;
}

/**
* Resolved Astro Config
* Config with user settings along with all defaults filled in.
*/
export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
// Public:
// This is a more detailed type than zod validation gives us.
Expand Down Expand Up @@ -1414,6 +1418,7 @@ export interface AstroSettings {
config: AstroConfig;
adapter: AstroAdapter | undefined;
injectedRoutes: InjectedRoute[];
resolvedInjectedRoutes: ResolvedInjectedRoute[];
pageExtensions: string[];
contentEntryTypes: ContentEntryType[];
dataEntryTypes: DataEntryType[];
Expand Down
19 changes: 6 additions & 13 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,19 +448,12 @@ export function makeAstroPageEntryPointFileName(
const pageModuleId = facadeModuleId
.replace(prefix, '')
.replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.');
let route = routes.find((routeData) => {
return routeData.route === pageModuleId;
});
let name = pageModuleId;
if (route) {
name = route.route;
}
if (name.endsWith('/')) name += 'index';
const fileName = `${name.replaceAll('[', '_').replaceAll(']', '_').replaceAll('...', '---')}.mjs`;
if (name.startsWith('..')) {
return `pages${fileName}`;
}
return fileName;
const route = routes.find((routeData) => routeData.component === pageModuleId);
const name = route?.route ?? pageModuleId;
return `pages${name
.replace(/\/$/, '/index')
.replaceAll(/[\[\]]/g, '_')
.replaceAll('...', '---')}.astro.mjs`;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/astro/src/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ async function loadConfig(
}
}

/**
* `AstroInlineConfig` is a union of `AstroUserConfig` and `AstroInlineOnlyConfig`.
* This functions splits it up.
*/
function splitInlineConfig(inlineConfig: AstroInlineConfig): {
inlineUserConfig: AstroUserConfig;
inlineOnlyConfig: AstroInlineOnlyConfig;
Expand All @@ -231,6 +235,12 @@ interface ResolveConfigResult {
astroConfig: AstroConfig;
}

/**
* Resolves the Astro config with a given inline config.
*
* @param inlineConfig An inline config that takes highest priority when merging and resolving the final config.
* @param command The running command that uses this config. Usually 'dev' or 'build'.
*/
export async function resolveConfig(
inlineConfig: AstroInlineConfig,
command: string,
Expand All @@ -239,6 +249,11 @@ export async function resolveConfig(
const root = resolveRoot(inlineConfig.root);
const { inlineUserConfig, inlineOnlyConfig } = splitInlineConfig(inlineConfig);

// If the root is specified, assign the resolved path so it takes the highest priority
if (inlineConfig.root) {
inlineUserConfig.root = root;
}

const userConfig = await loadConfig(root, inlineOnlyConfig.configFile, fsMod);
const mergedConfig = mergeConfig(userConfig, inlineUserConfig);
const astroConfig = await validateConfig(mergedConfig, root, command);
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/config/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function createBaseSettings(config: AstroConfig): AstroSettings {

adapter: undefined,
injectedRoutes: [],
resolvedInjectedRoutes: [],
pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS],
contentEntryTypes: [markdownContentEntryType],
dataEntryTypes: [
Expand Down
5 changes: 3 additions & 2 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ function isInPagesDir(file: URL, config: AstroConfig): boolean {
}

function isInjectedRoute(file: URL, settings: AstroSettings) {
for (const route of settings.injectedRoutes) {
if (file.toString().endsWith(route.entryPoint)) return true;
let fileURL = file.toString();
for (const route of settings.resolvedInjectedRoutes) {
if (route.resolvedEntryPoint && fileURL === route.resolvedEntryPoint.toString()) return true;
}
return false;
}
Expand Down
24 changes: 23 additions & 1 deletion packages/astro/src/vite-plugin-integrations-container/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { PluginContext } from 'rollup';
import type { Plugin as VitePlugin } from 'vite';
import type { AstroSettings } from '../@types/astro.js';
import type { AstroSettings, InjectedRoute, ResolvedInjectedRoute } from '../@types/astro.js';
import type { LogOptions } from '../core/logger/core.js';

import { normalizePath } from 'vite';
import { runHookServerSetup } from '../integrations/index.js';

/** Connect Astro integrations into Vite, as needed. */
Expand All @@ -16,5 +19,24 @@ export default function astroIntegrationsContainerPlugin({
configureServer(server) {
runHookServerSetup({ config: settings.config, server, logging });
},
async buildStart() {
// Ensure the injectedRoutes are all resolved to their final paths through Rollup
settings.resolvedInjectedRoutes = await Promise.all(
settings.injectedRoutes.map((route) => resolveEntryPoint.call(this, route))
);
},
};
}

async function resolveEntryPoint(
this: PluginContext,
route: InjectedRoute
): Promise<ResolvedInjectedRoute> {
const resolvedId = await this.resolve(route.entryPoint)
.then((res) => res?.id)
.catch(() => undefined);
if (!resolvedId) return route;

const resolvedEntryPoint = new URL(`file://${normalizePath(resolvedId)}`);
return { ...route, resolvedEntryPoint };
}
58 changes: 58 additions & 0 deletions packages/astro/test/units/build/static-build.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect } from 'chai';
import { makeAstroPageEntryPointFileName } from '../../../dist/core/build/static-build.js';

describe('astro/src/core/build', () => {
describe('makeAstroPageEntryPointFileName', () => {
const routes = [
{
route: '/',
component: 'src/pages/index.astro',
pathname: '/',
},
{
route: '/injected',
component: '../node_modules/my-dep/injected.astro',
pathname: '/injected',
},
{
route: '/injected-workspace',
component: '../../packages/demo/[...all].astro',
pathname: undefined,
},
{
route: '/blog/[year]/[...slug]',
component: 'src/pages/blog/[year]/[...slug].astro',
pathname: undefined,
},
];

it('handles local pages', async () => {
const input = '@astro-page:src/pages/index@_@astro';
const output = 'pages/index.astro.mjs';
const result = makeAstroPageEntryPointFileName('@astro-page:', input, routes);
expect(result).to.equal(output);
});

it('handles dynamic pages', async () => {
const input = '@astro-page:src/pages/blog/[year]/[...slug]@_@astro';
const output = 'pages/blog/_year_/_---slug_.astro.mjs';
const result = makeAstroPageEntryPointFileName('@astro-page:', input, routes);
expect(result).to.equal(output);
});

it('handles node_modules pages', async () => {
const input = '@astro-page:../node_modules/my-dep/injected@_@astro';
const output = 'pages/injected.astro.mjs';
const result = makeAstroPageEntryPointFileName('@astro-page:', input, routes);
expect(result).to.equal(output);
});

// Fix #7561
it('handles local workspace pages', async () => {
const input = '@astro-page:../../packages/demo/[...all]@_@astro';
const output = 'pages/injected-workspace.astro.mjs';
const result = makeAstroPageEntryPointFileName('@astro-page:', input, routes);
expect(result).to.equal(output);
});
});
});
18 changes: 18 additions & 0 deletions packages/astro/test/units/config/config-resolve.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from 'chai';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { resolveConfig } from '../../../dist/core/config/index.js';

describe('resolveConfig', () => {
it('resolves relative inline root correctly', async () => {
const { astroConfig } = await resolveConfig(
{
configFile: false,
root: 'relative/path',
},
'dev'
);
const expectedRoot = path.join(process.cwd(), 'relative/path/');
expect(fileURLToPath(astroConfig.root)).to.equal(expectedRoot);
});
});
4 changes: 1 addition & 3 deletions packages/integrations/lit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ customElements.define('my-element', MyElement);
Now, the component is ready to be imported via the Astro frontmatter:

```astro
// src/pages/index.astro
import { MyElement } from '../components/my-element.js';
// src/pages/index.astro import {MyElement} from '../components/my-element.js';
<MyElement />
```

Expand Down
Loading

0 comments on commit 77aae41

Please sign in to comment.