Skip to content

Commit

Permalink
Cleanup dependencies (#5773)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jan 10, 2023
1 parent 10137cd commit 4a1cabf
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 110 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-carpets-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Cleanup dependencies
11 changes: 1 addition & 10 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
"@babel/traverse": "^7.18.2",
"@babel/types": "^7.18.4",
"@types/babel__core": "^7.1.19",
"@types/html-escaper": "^3.0.0",
"@types/yargs-parser": "^21.0.0",
"acorn": "^8.8.1",
"boxen": "^6.2.1",
Expand All @@ -138,22 +137,15 @@
"fast-glob": "^3.2.11",
"github-slugger": "^2.0.0",
"gray-matter": "^4.0.3",
"html-entities": "^2.3.3",
"html-escaper": "^3.0.3",
"import-meta-resolve": "^2.1.0",
"kleur": "^4.1.4",
"magic-string": "^0.27.0",
"mime": "^3.0.0",
"ora": "^6.1.0",
"path-browserify": "^1.0.1",
"path-to-regexp": "^6.2.1",
"postcss": "^8.4.14",
"postcss-load-config": "^3.1.4",
"preferred-pm": "^3.0.3",
"prompts": "^2.4.2",
"recast": "^0.20.5",
"rehype": "^12.0.1",
"resolve": "^1.22.0",
"semver": "^7.3.7",
"server-destroy": "^1.0.1",
"shiki": "^0.11.1",
Expand Down Expand Up @@ -182,10 +174,9 @@
"@types/diff": "^5.0.2",
"@types/estree": "^0.0.51",
"@types/hast": "^2.3.4",
"@types/html-escaper": "^3.0.0",
"@types/mime": "^2.0.3",
"@types/mocha": "^9.1.1",
"@types/parse5": "^6.0.3",
"@types/path-browserify": "^1.0.0",
"@types/prettier": "^2.6.3",
"@types/prompts": "^2.0.14",
"@types/resolve": "^1.20.2",
Expand Down
33 changes: 0 additions & 33 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import type { RehypePlugin, RemarkPlugin, RemarkRehype } from '@astrojs/markdown-remark';
import { markdownConfigDefaults } from '@astrojs/markdown-remark';
import type * as Postcss from 'postcss';
import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
import type { AstroUserConfig, ViteUserConfig } from '../../@types/astro';

import { OutgoingHttpHeaders } from 'http';
import postcssrc from 'postcss-load-config';
import { BUNDLED_THEMES } from 'shiki';
import { fileURLToPath } from 'url';
import { z } from 'zod';
import { appendForwardSlash, prependForwardSlash, trimSlashes } from '../path.js';
import { isObject } from '../util.js';

const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
root: '.',
Expand Down Expand Up @@ -183,35 +179,6 @@ export const AstroConfigSchema = z.object({
legacy: z.object({}).optional().default({}),
});

interface PostCSSConfigResult {
options: Postcss.ProcessOptions;
plugins: Postcss.Plugin[];
}

async function resolvePostcssConfig(inlineOptions: any, root: URL): Promise<PostCSSConfigResult> {
if (isObject(inlineOptions)) {
const options = { ...inlineOptions };
delete options.plugins;
return {
options,
plugins: inlineOptions.plugins || [],
};
}
const searchPath = typeof inlineOptions === 'string' ? inlineOptions : fileURLToPath(root);
try {
// @ts-ignore
return await postcssrc({}, searchPath);
} catch (err: any) {
if (!/No PostCSS Config found/.test(err.message)) {
throw err;
}
return {
options: {},
plugins: [],
};
}
}

export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
// We need to extend the global schema to add transforms that are relative to root.
// This is type checked against the global schema to make sure we still match.
Expand Down
24 changes: 3 additions & 21 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import markdownVitePlugin from '../vite-plugin-markdown/index.js';
import astroScannerPlugin from '../vite-plugin-scanner/index.js';
import astroScriptsPlugin from '../vite-plugin-scripts/index.js';
import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js';
import { resolveDependency } from './util.js';

interface CreateViteOptions {
settings: AstroSettings;
Expand All @@ -33,7 +32,7 @@ interface CreateViteOptions {
fs?: typeof nodeFs;
}

const ALWAYS_NOEXTERNAL = new Set([
const ALWAYS_NOEXTERNAL = [
// This is only because Vite's native ESM doesn't resolve "exports" correctly.
'astro',
// Vite fails on nested `.astro` imports without bundling
Expand All @@ -43,21 +42,7 @@ const ALWAYS_NOEXTERNAL = new Set([
'@nanostores/preact',
// fontsource packages are CSS that need to be processed
'@fontsource/*',
]);

function getSsrNoExternalDeps(projectRoot: URL): string[] {
let noExternalDeps = [];
for (const dep of ALWAYS_NOEXTERNAL) {
try {
resolveDependency(dep, projectRoot);
noExternalDeps.push(dep);
} catch {
// ignore dependency if *not* installed / present in your project
// prevents hard error from Vite!
}
}
return noExternalDeps;
}
];

/** Return a common starting point for all Vite actions */
export async function createVite(
Expand Down Expand Up @@ -166,10 +151,7 @@ export async function createVite(
dedupe: ['astro'],
},
ssr: {
noExternal: [
...getSsrNoExternalDeps(settings.config.root),
...astroPkgsConfig.ssr.noExternal,
],
noExternal: [...ALWAYS_NOEXTERNAL, ...astroPkgsConfig.ssr.noExternal],
// shiki is imported by Code.astro, which is no-externalized (processed by Vite).
// However, shiki's deps are in CJS and trips up Vite's dev SSR transform, externalize
// shiki to load it with node instead.
Expand Down
9 changes: 5 additions & 4 deletions packages/astro/src/core/render/ssr-element.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import slashify from 'slash';
import type { SSRElement } from '../../@types/astro';

import npath from 'path-browserify';
import { appendForwardSlash } from '../../core/path.js';
import { appendForwardSlash, removeLeadingForwardSlash } from '../../core/path.js';

function getRootPath(base?: string): string {
return appendForwardSlash(new URL(base || '/', 'http://localhost/').pathname);
}

function joinToRoot(href: string, base?: string): string {
return npath.posix.join(getRootPath(base), href);
const rootPath = getRootPath(base);
const normalizedHref = slashify(href);
return appendForwardSlash(rootPath) + removeLeadingForwardSlash(normalizedHref);
}

export function createLinkStylesheetElement(href: string, base?: string): SSRElement {
Expand Down
11 changes: 1 addition & 10 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from 'fs';
import path from 'path';
import resolve from 'resolve';
import slash from 'slash';
import { fileURLToPath, pathToFileURL } from 'url';
import { fileURLToPath } from 'url';
import { normalizePath } from 'vite';
import type { AstroConfig, AstroSettings, RouteType } from '../@types/astro';
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './constants.js';
Expand Down Expand Up @@ -83,14 +82,6 @@ export function parseNpmName(
};
}

export function resolveDependency(dep: string, projectRoot: URL) {
const resolved = resolve.sync(dep, {
basedir: fileURLToPath(projectRoot),
});
// For Windows compat, we need a fully resolved `file://` URL string
return pathToFileURL(resolved).toString();
}

/**
* Convert file URL to ID for viteServer.moduleGraph.idToModuleMap.get(:viteID)
* Format:
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/template/4xx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encode } from 'html-entities';
import { escape } from 'html-escaper';
import { baseCSS } from './css.js';

interface ErrorTemplateOptions {
Expand Down Expand Up @@ -58,7 +58,7 @@ export default function template({
${
body ||
`
<pre>Path: ${encode(pathname)}</pre>
<pre>Path: ${escape(pathname)}</pre>
`
}
</main>
Expand Down
7 changes: 1 addition & 6 deletions packages/astro/src/vite-plugin-jsx/tag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { PluginObj } from '@babel/core';
import * as t from '@babel/types';
import { resolve as importMetaResolve } from 'import-meta-resolve';
import { fileURLToPath } from 'url';

/**
* This plugin handles every file that runs through our JSX plugin.
Expand All @@ -18,9 +16,6 @@ export default async function tagExportsWithRenderer({
rendererName: string;
root: URL;
}): Promise<PluginObj> {
const astroServerPath = fileURLToPath(
await importMetaResolve('astro/server/index.js', root.toString())
);
return {
visitor: {
Program: {
Expand All @@ -36,7 +31,7 @@ export default async function tagExportsWithRenderer({
t.identifier('__astro_tag_component__')
),
],
t.stringLiteral(astroServerPath)
t.stringLiteral('astro/server/index.js')
)
);
},
Expand Down
26 changes: 2 additions & 24 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 4a1cabf

Please sign in to comment.