Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup dependencies #5773

Merged
merged 7 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 2 additions & 11 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
"@proload/core": "^0.3.3",
"@proload/plugin-tsm": "^0.2.1",
"@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 @@ -140,22 +139,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",
"shiki": "^0.11.1",
"sirv": "^2.0.2",
Expand All @@ -164,7 +156,6 @@
"strip-ansi": "^7.0.1",
"supports-esm": "^1.0.0",
"tsconfig-resolver": "^3.0.1",
"typescript": "*",
bluwy marked this conversation as resolved.
Show resolved Hide resolved
"unist-util-visit": "^4.1.0",
"vfile": "^5.3.2",
"vite": "^4.0.3",
Expand All @@ -184,10 +175,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 All @@ -209,6 +199,7 @@
"rollup": "^3.9.0",
"sass": "^1.52.2",
"srcset-parse": "^1.1.0",
"typescript": "*",
"unified": "^10.1.2"
},
"engines": {
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 @@ -180,35 +176,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> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is unused. The postcss and postcss-load-config deps can be removed along.

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;
}
Comment on lines -48 to -60
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vite doesn't have an issue now if the deps are not installed. This enables removing resolveDependency and the resolve package.

];

/** 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);
Comment on lines -11 to +12
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place we use the path-browserify dep. I've emulated the posix join with this.

For reference, here's it's implementation: https://github.com/browserify/path-browserify/blob/872fec31a8bac7b9b43be0e54ef3037e0202c5fb/index.js#L180-L197

}

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';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

html-entities and html-escaper do the same thing. Since we use html-escaper more, I've swap to it. html-entities was only used here once.

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())
);
Comment on lines -21 to -23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed anymore as I made a change to dedupe astro, which means we can refer to the string unresolved. We do the same for Astro compile too:

internalURL: 'astro/server/index.js',

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
Loading