Skip to content

Commit

Permalink
Merge branch 'main' into svelte-4
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jun 25, 2023
2 parents 2ba65e0 + 869197a commit 840da6b
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-birds-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix missing styles for Markdoc files in development
5 changes: 5 additions & 0 deletions .changeset/early-students-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Remove `slash` package
5 changes: 5 additions & 0 deletions .changeset/rich-pumpkins-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/internal-helpers': patch
---

Add `slash` path utility
5 changes: 1 addition & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ jobs:
if: ${{ github.repository_owner == 'withastro' }}
uses: withastro/automation/.github/workflows/congratsbot.yml@main
with:
GITHUB_REPO: ${{ github.repository }}
COMMIT_AUTHOR: ${{ github.event.commits[0].author.name }}
COMMIT_MESSAGE: ${{ github.event.commits[0].message }}
COMMIT_ID: ${{ github.event.commits[0].id }}
EMOJIS: '🎉,🎊,🧑‍🚀,🥳,🙌,🚀,👏,<:houston_golden:1068575433647456447>,<:astrocoin:894990669515489301>,<:astro_pride:1085944201587458169>'
secrets:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_CONGRATS }}

Expand Down
2 changes: 0 additions & 2 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,8 @@
"semver": "^7.5.2",
"server-destroy": "^1.0.1",
"shiki": "^0.14.1",
"slash": "^4.0.0",
"string-width": "^5.1.2",
"strip-ansi": "^7.1.0",
"supports-esm": "^1.0.0",
"tsconfig-resolver": "^3.0.1",
"typescript": "*",
"unist-util-visit": "^4.1.2",
Expand Down
6 changes: 2 additions & 4 deletions packages/astro/src/assets/utils/emitAsset.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import slash from 'slash';
import { prependForwardSlash } from '../../core/path.js';
import { prependForwardSlash, slash } from '../../core/path.js';
import { imageMetadata, type Metadata } from './metadata.js';

export async function emitESMImage(
Expand Down Expand Up @@ -46,7 +45,6 @@ export async function emitESMImage(
}

function fileURLToNormalizedPath(filePath: URL): string {
// Uses `slash` package instead of Vite's `normalizePath`
// to avoid CJS bundling issues.
// Uses `slash` instead of Vite's `normalizePath` to avoid CJS bundling issues.
return slash(fileURLToPath(filePath) + filePath.search).replace(/\\/g, '/');
}
77 changes: 35 additions & 42 deletions packages/astro/src/core/render/dev/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,45 @@ export async function* crawlGraph(
const entryIsStyle = isCSSRequest(id);

for (const importedModule of entry.importedModules) {
// A propagation stopping point is a module with the ?astroPropagatedAssets flag.
// When we encounter one of these modules we don't want to continue traversing.
let isPropagationStoppingPoint = false;
if (!importedModule.id) continue;

// some dynamically imported modules are *not* server rendered in time
// to only SSR modules that we can safely transform, we check against
// a list of file extensions based on our built-in vite plugins
if (importedModule.id) {
// Strip special query params like "?content".
// NOTE: Cannot use `new URL()` here because not all IDs will be valid paths.
// For example, `virtual:image-loader` if you don't have the plugin installed.
const importedModulePathname = importedModule.id.replace(STRIP_QUERY_PARAMS_REGEX, '');
// If the entry is a style, skip any modules that are not also styles.
// Tools like Tailwind might add HMR dependencies as `importedModules`
// but we should skip them--they aren't really imported. Without this,
// every hoisted script in the project is added to every page!
if (entryIsStyle && !isCSSRequest(importedModulePathname)) {
continue;
}
const isFileTypeNeedingSSR = fileExtensionsToSSR.has(
npath.extname(importedModulePathname)
);
isPropagationStoppingPoint = ASTRO_PROPAGATED_ASSET_REGEX.test(importedModule.id);
if (
isFileTypeNeedingSSR &&
// Should not SSR a module with ?astroPropagatedAssets
!isPropagationStoppingPoint
) {
const mod = loader.getModuleById(importedModule.id);
if (!mod?.ssrModule) {
try {
await loader.import(importedModule.id);
} catch {
/** Likely an out-of-date module entry! Silently continue. */
}

// Strip special query params like "?content".
// NOTE: Cannot use `new URL()` here because not all IDs will be valid paths.
// For example, `virtual:image-loader` if you don't have the plugin installed.
const importedModulePathname = importedModule.id.replace(STRIP_QUERY_PARAMS_REGEX, '');
// If the entry is a style, skip any modules that are not also styles.
// Tools like Tailwind might add HMR dependencies as `importedModules`
// but we should skip them--they aren't really imported. Without this,
// every hoisted script in the project is added to every page!
if (entryIsStyle && !isCSSRequest(importedModulePathname)) {
continue;
}

const isFileTypeNeedingSSR = fileExtensionsToSSR.has(npath.extname(importedModulePathname));
// A propagation stopping point is a module with the ?astroPropagatedAssets flag.
// When we encounter one of these modules we don't want to continue traversing.
const isPropagationStoppingPoint = ASTRO_PROPAGATED_ASSET_REGEX.test(importedModule.id);
if (
isFileTypeNeedingSSR &&
// Should not SSR a module with ?astroPropagatedAssets
!isPropagationStoppingPoint
) {
const mod = loader.getModuleById(importedModule.id);
if (!mod?.ssrModule) {
try {
await loader.import(importedModule.id);
} catch {
/** Likely an out-of-date module entry! Silently continue. */
}
}
}
if (urlDeps.includes(urlId(importedModule.url)) && !isPropagationStoppingPoint) {

// Make sure the `importedModule` traversed is explicitly imported by the user, and not by HMR
if (urlDeps.includes(importedModule.url) && !isPropagationStoppingPoint) {
importedModules.add(importedModule);
}
}
Expand All @@ -102,18 +103,10 @@ export async function* crawlGraph(
}
}

// Virtual modules URL should start with /@id/ but do not
function urlId(url: string) {
if (url.startsWith('astro:scripts')) {
return '/@id/' + url;
}
return url;
}

function getDepsFromEntry(entry: ModuleNode) {
let deps = entry.ssrTransformResult?.deps ?? [];
if (entry.ssrTransformResult?.dynamicDeps) {
return deps.concat(entry.ssrTransformResult.dynamicDeps);
deps = deps.concat(entry.ssrTransformResult.dynamicDeps);
}
return deps;
return deps.map((dep) => unwrapId(dep));
}
7 changes: 3 additions & 4 deletions packages/astro/src/core/render/ssr-element.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import slashify from 'slash';
import type { SSRElement } from '../../@types/astro';
import { joinPaths, prependForwardSlash } from '../../core/path.js';
import { joinPaths, prependForwardSlash, slash } from '../../core/path.js';
import type { StylesheetAsset } from '../app/types';

export function createAssetLink(href: string, base?: string, assetsPrefix?: string): string {
if (assetsPrefix) {
return joinPaths(assetsPrefix, slashify(href));
return joinPaths(assetsPrefix, slash(href));
} else if (base) {
return prependForwardSlash(joinPaths(base, slashify(href)));
return prependForwardSlash(joinPaths(base, slash(href)));
} else {
return href;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import type { LogOptions } from '../../logger/core';
import nodeFs from 'fs';
import { createRequire } from 'module';
import path from 'path';
import slash from 'slash';
import { fileURLToPath } from 'url';
import { getPrerenderDefault } from '../../../prerender/utils.js';
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from '../../constants.js';
import { warn } from '../../logger/core.js';
import { removeLeadingForwardSlash } from '../../path.js';
import { removeLeadingForwardSlash, slash } from '../../path.js';
import { resolvePages } from '../../util.js';
import { getRouteGenerator } from './generator.js';
const require = createRequire(import.meta.url);
Expand Down
14 changes: 8 additions & 6 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import fs from 'fs';
import path from 'path';
import slash from 'slash';
import { fileURLToPath } from 'url';
import { normalizePath } from 'vite';
import type { AstroConfig, AstroSettings, RouteType } from '../@types/astro';
import { isServerLikeOutput } from '../prerender/utils.js';
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './constants.js';
import type { ModuleLoader } from './module-loader';
import { prependForwardSlash, removeTrailingForwardSlash } from './path.js';
import { prependForwardSlash, removeTrailingForwardSlash, slash } from './path.js';

/** Returns true if argument is an object of any prototype/class (but not null). */
export function isObject(value: unknown): value is Record<string, any> {
Expand Down Expand Up @@ -90,15 +89,18 @@ export function parseNpmName(
* Windows: C:/Users/astro/code/my-project/src/pages/index.astro
*/
export function viteID(filePath: URL): string {
return slash(fileURLToPath(filePath) + filePath.search).replace(/\\/g, '/');
return slash(fileURLToPath(filePath) + filePath.search);
}

export const VALID_ID_PREFIX = `/@id/`;
export const NULL_BYTE_PLACEHOLDER = `__x00__`;

// Strip valid id prefix. This is prepended to resolved Ids that are
// not valid browser import specifiers by the importAnalysis plugin.
// Strip valid id prefix and replace null byte placeholder. Both are prepended to resolved ids
// as they are not valid browser import specifiers (by the Vite's importAnalysis plugin)
export function unwrapId(id: string): string {
return id.startsWith(VALID_ID_PREFIX) ? id.slice(VALID_ID_PREFIX.length) : id;
return id.startsWith(VALID_ID_PREFIX)
? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0')
: id;
}

export function resolvePages(config: AstroConfig) {
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/vite-plugin-load-fallback/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nodeFs from 'fs';
import npath from 'path';
import slashify from 'slash';
import type * as vite from 'vite';
import { slash } from '../core/path.js';

type NodeFileSystemModule = typeof nodeFs;

Expand Down Expand Up @@ -47,7 +47,7 @@ export default function loadFallbackPlugin({
async resolveId(id, parent) {
// See if this can be loaded from our fs
if (parent) {
const candidateId = npath.posix.join(npath.posix.dirname(slashify(parent)), id);
const candidateId = npath.posix.join(npath.posix.dirname(slash(parent)), id);
try {
// Check to see if this file exists and is not a directory.
const stats = await fs.promises.stat(candidateId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { fileURLToPath } from 'node:url';
import nodeFS from 'node:fs';
import path from 'node:path';
import slash from 'slash';

import { runInContainer } from '../../../dist/core/dev/index.js';
import { attachContentServerListeners } from '../../../dist/content/index.js';
Expand All @@ -11,7 +10,9 @@ const root = new URL('../../fixtures/alias/', import.meta.url);

function getTypesDts() {
const typesdtsURL = new URL('../../../content-types.template.d.ts', import.meta.url);
const relpath = slash(path.relative(fileURLToPath(root), fileURLToPath(typesdtsURL)));
const relpath = path
.relative(fileURLToPath(root), fileURLToPath(typesdtsURL))
.replace(/\\/g, '/');
return {
[relpath]: nodeFS.readFileSync(typesdtsURL, 'utf-8'),
};
Expand Down
1 change: 0 additions & 1 deletion packages/integrations/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"chai": "^4.3.7",
"cheerio": "1.0.0-rc.12",
"mocha": "^9.2.2",
"slash": "^4.0.0",
"wrangler": "^2.0.23"
}
}
5 changes: 2 additions & 3 deletions packages/integrations/cloudflare/test/prerender.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import slash from 'slash';

describe('Prerendering', () => {
/** @type {import('./test-utils').Fixture} */
Expand All @@ -21,7 +20,7 @@ describe('Prerendering', () => {

it('includes prerendered routes in the routes.json config', async () => {
const foundRoutes = JSON.parse(await fixture.readFile('/_routes.json')).exclude.map((r) =>
slash(r)
r.replace(/\\/g, '/')
);
const expectedExcludedRoutes = ['/_worker.js', '/one/index.html', '/one/'];

Expand All @@ -48,7 +47,7 @@ describe('Hybrid rendering', () => {

it('includes prerendered routes in the routes.json config', async () => {
const foundRoutes = JSON.parse(await fixture.readFile('/_routes.json')).exclude.map((r) =>
slash(r)
r.replace(/\\/g, '/')
);
const expectedExcludedRoutes = ['/_worker.js', '/index.html', '/'];

Expand Down
4 changes: 4 additions & 0 deletions packages/internal-helpers/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ export function removeQueryString(path: string) {
export function isRemotePath(src: string) {
return /^(http|ftp|https|ws):?\/\//.test(src) || src.startsWith('data:');
}

export function slash(path: string) {
return path.replace(/\\/g, '/');
}
25 changes: 0 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 840da6b

Please sign in to comment.