🎉 initiate project *astro_rewrite*
This commit is contained in:
parent
ffd4d5e86c
commit
2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions
3
node_modules/astro/dist/vite-plugin-jsx/import-source.d.ts
generated
vendored
Normal file
3
node_modules/astro/dist/vite-plugin-jsx/import-source.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
import type { TsConfigJson } from 'tsconfig-resolver';
|
||||
import type { AstroRenderer } from '../@types/astro';
|
||||
export declare function detectImportSource(code: string, jsxRenderers: Map<string, AstroRenderer>, tsConfig?: TsConfigJson): Promise<string | undefined>;
|
36
node_modules/astro/dist/vite-plugin-jsx/import-source.js
generated
vendored
Normal file
36
node_modules/astro/dist/vite-plugin-jsx/import-source.js
generated
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
import { parseNpmName } from "../core/util.js";
|
||||
async function detectImportSource(code, jsxRenderers, tsConfig) {
|
||||
var _a;
|
||||
let importSource = detectImportSourceFromComments(code);
|
||||
if (!importSource && /import/.test(code)) {
|
||||
importSource = await detectImportSourceFromImports(code, jsxRenderers);
|
||||
}
|
||||
if (!importSource && tsConfig) {
|
||||
importSource = (_a = tsConfig.compilerOptions) == null ? void 0 : _a.jsxImportSource;
|
||||
}
|
||||
return importSource;
|
||||
}
|
||||
const importsRE = /(?<!\/\/.*)(?<=^|;|\*\/)\s*(?:import(?!\s+type)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)|import\s*\(\s*("[^"]+"|'[^']+')\s*\))/gm;
|
||||
async function detectImportSourceFromImports(code, jsxRenderers) {
|
||||
let m;
|
||||
importsRE.lastIndex = 0;
|
||||
while ((m = importsRE.exec(code)) != null) {
|
||||
const spec = (m[1] || m[2]).slice(1, -1);
|
||||
const pkg = parseNpmName(spec);
|
||||
if (pkg && jsxRenderers.has(pkg.name)) {
|
||||
return pkg.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
function detectImportSourceFromComments(code) {
|
||||
const multiline = code.match(/\/\*\*?[\S\s]*\*\//gm) || [];
|
||||
for (const comment of multiline) {
|
||||
const [, lib] = comment.slice(0, -2).match(/@jsxImportSource\s*(\S+)/) || [];
|
||||
if (lib) {
|
||||
return lib.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
export {
|
||||
detectImportSource
|
||||
};
|
10
node_modules/astro/dist/vite-plugin-jsx/index.d.ts
generated
vendored
Normal file
10
node_modules/astro/dist/vite-plugin-jsx/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { type Plugin } from 'vite';
|
||||
import type { AstroSettings } from '../@types/astro';
|
||||
import type { LogOptions } from '../core/logger/core.js';
|
||||
interface AstroPluginJSXOptions {
|
||||
settings: AstroSettings;
|
||||
logging: LogOptions;
|
||||
}
|
||||
/** Use Astro config to allow for alternate or multiple JSX renderers (by default Vite will assume React) */
|
||||
export default function jsx({ settings, logging }: AstroPluginJSXOptions): Plugin;
|
||||
export {};
|
208
node_modules/astro/dist/vite-plugin-jsx/index.js
generated
vendored
Normal file
208
node_modules/astro/dist/vite-plugin-jsx/index.js
generated
vendored
Normal file
|
@ -0,0 +1,208 @@
|
|||
import {
|
||||
transformWithEsbuild
|
||||
} from "vite";
|
||||
import babel from "@babel/core";
|
||||
import * as colors from "kleur/colors";
|
||||
import path from "node:path";
|
||||
import { CONTENT_FLAG, PROPAGATED_ASSET_FLAG } from "../content/index.js";
|
||||
import { astroEntryPrefix } from "../core/build/plugins/plugin-component-entry.js";
|
||||
import { error } from "../core/logger/core.js";
|
||||
import { removeQueryString } from "../core/path.js";
|
||||
import { detectImportSource } from "./import-source.js";
|
||||
import tagExportsPlugin from "./tag.js";
|
||||
const JSX_EXTENSIONS = /* @__PURE__ */ new Set([".jsx", ".tsx", ".mdx"]);
|
||||
const IMPORT_STATEMENTS = {
|
||||
react: "import React from 'react'",
|
||||
preact: "import { h } from 'preact'",
|
||||
"solid-js": "import 'solid-js'",
|
||||
astro: "import 'astro/jsx-runtime'"
|
||||
};
|
||||
function getEsbuildLoader(filePath) {
|
||||
const fileExt = path.extname(filePath);
|
||||
if (fileExt === ".mdx")
|
||||
return "jsx";
|
||||
return fileExt.slice(1);
|
||||
}
|
||||
function collectJSXRenderers(renderers) {
|
||||
const renderersWithJSXSupport = renderers.filter((r) => r.jsxImportSource);
|
||||
return new Map(
|
||||
renderersWithJSXSupport.map((r) => [r.jsxImportSource, r])
|
||||
);
|
||||
}
|
||||
async function transformJSX({
|
||||
code,
|
||||
mode,
|
||||
id,
|
||||
ssr,
|
||||
renderer,
|
||||
root
|
||||
}) {
|
||||
const { jsxTransformOptions } = renderer;
|
||||
const options = await jsxTransformOptions({ mode, ssr });
|
||||
const plugins = [...options.plugins || []];
|
||||
if (ssr) {
|
||||
plugins.push(await tagExportsPlugin({ rendererName: renderer.name, root }));
|
||||
}
|
||||
const result = await babel.transformAsync(code, {
|
||||
presets: options.presets,
|
||||
plugins,
|
||||
cwd: process.cwd(),
|
||||
filename: id,
|
||||
ast: false,
|
||||
compact: false,
|
||||
sourceMaps: true,
|
||||
configFile: false,
|
||||
babelrc: false,
|
||||
inputSourceMap: options.inputSourceMap
|
||||
});
|
||||
if (!result)
|
||||
return null;
|
||||
if (renderer.name === "astro:jsx") {
|
||||
const { astro } = result.metadata;
|
||||
return {
|
||||
code: result.code || "",
|
||||
map: result.map,
|
||||
meta: {
|
||||
astro,
|
||||
vite: {
|
||||
// Setting this vite metadata to `ts` causes Vite to resolve .js
|
||||
// extensions to .ts files.
|
||||
lang: "ts"
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return {
|
||||
code: result.code || "",
|
||||
map: result.map
|
||||
};
|
||||
}
|
||||
const SPECIAL_QUERY_REGEX = new RegExp(
|
||||
`[?&](?:worker|sharedworker|raw|url|${CONTENT_FLAG}|${PROPAGATED_ASSET_FLAG})\\b`
|
||||
);
|
||||
function jsx({ settings, logging }) {
|
||||
let viteConfig;
|
||||
const jsxRenderers = /* @__PURE__ */ new Map();
|
||||
const jsxRenderersIntegrationOnly = /* @__PURE__ */ new Map();
|
||||
let astroJSXRenderer;
|
||||
let defaultJSXRendererEntry;
|
||||
return {
|
||||
name: "astro:jsx",
|
||||
enforce: "pre",
|
||||
// run transforms before other plugins
|
||||
async configResolved(resolvedConfig) {
|
||||
viteConfig = resolvedConfig;
|
||||
const possibleRenderers = collectJSXRenderers(settings.renderers);
|
||||
for (const [importSource, renderer] of possibleRenderers) {
|
||||
jsxRenderers.set(importSource, renderer);
|
||||
if (importSource === "astro") {
|
||||
astroJSXRenderer = renderer;
|
||||
} else {
|
||||
jsxRenderersIntegrationOnly.set(importSource, renderer);
|
||||
}
|
||||
}
|
||||
defaultJSXRendererEntry = [...jsxRenderersIntegrationOnly.entries()][0];
|
||||
},
|
||||
async transform(code, id, opts) {
|
||||
const ssr = Boolean(opts == null ? void 0 : opts.ssr);
|
||||
if (SPECIAL_QUERY_REGEX.test(id) || id.startsWith(astroEntryPrefix)) {
|
||||
return null;
|
||||
}
|
||||
id = removeQueryString(id);
|
||||
if (!JSX_EXTENSIONS.has(path.extname(id))) {
|
||||
return null;
|
||||
}
|
||||
const { mode } = viteConfig;
|
||||
if (id.endsWith(".mdx")) {
|
||||
const { code: jsxCode2 } = await transformWithEsbuild(code, id, {
|
||||
loader: getEsbuildLoader(id),
|
||||
jsx: "preserve",
|
||||
sourcemap: "inline",
|
||||
tsconfigRaw: {
|
||||
compilerOptions: {
|
||||
// Ensure client:only imports are treeshaken
|
||||
// @ts-expect-error anticipate esbuild 0.18 feature
|
||||
verbatimModuleSyntax: false,
|
||||
importsNotUsedAsValues: "remove"
|
||||
}
|
||||
}
|
||||
});
|
||||
return transformJSX({
|
||||
code: jsxCode2,
|
||||
id,
|
||||
renderer: astroJSXRenderer,
|
||||
mode,
|
||||
ssr,
|
||||
root: settings.config.root
|
||||
});
|
||||
}
|
||||
if (defaultJSXRendererEntry && jsxRenderersIntegrationOnly.size === 1) {
|
||||
const { code: jsxCode2 } = await transformWithEsbuild(code, id, {
|
||||
loader: getEsbuildLoader(id),
|
||||
jsx: "preserve",
|
||||
sourcemap: "inline"
|
||||
});
|
||||
return transformJSX({
|
||||
code: jsxCode2,
|
||||
id,
|
||||
renderer: defaultJSXRendererEntry[1],
|
||||
mode,
|
||||
ssr,
|
||||
root: settings.config.root
|
||||
});
|
||||
}
|
||||
const importSource = await detectImportSource(code, jsxRenderers, settings.tsConfig);
|
||||
if (!importSource && defaultJSXRendererEntry) {
|
||||
const [defaultRendererName] = defaultJSXRendererEntry;
|
||||
error(
|
||||
logging,
|
||||
"renderer",
|
||||
`${colors.yellow(id)}
|
||||
Unable to resolve a renderer that handles this file! With more than one renderer enabled, you should include an import or use a pragma comment.
|
||||
Add ${colors.cyan(
|
||||
IMPORT_STATEMENTS[defaultRendererName] || `import '${defaultRendererName}';`
|
||||
)} or ${colors.cyan(`/** @jsxImportSource: ${defaultRendererName} */`)} to this file.
|
||||
`
|
||||
);
|
||||
return null;
|
||||
} else if (!importSource) {
|
||||
error(
|
||||
logging,
|
||||
"renderer",
|
||||
`${colors.yellow(id)}
|
||||
Unable to find a renderer for JSX. Do you have one configured in your Astro config? See this page to learn how:
|
||||
https://docs.astro.build/en/core-concepts/framework-components/#installing-integrations
|
||||
`
|
||||
);
|
||||
return null;
|
||||
}
|
||||
const selectedJsxRenderer = jsxRenderers.get(importSource);
|
||||
if (!selectedJsxRenderer) {
|
||||
error(
|
||||
logging,
|
||||
"renderer",
|
||||
`${colors.yellow(
|
||||
id
|
||||
)} No renderer installed for ${importSource}. Try adding \`@astrojs/${importSource}\` to your project.`
|
||||
);
|
||||
return null;
|
||||
}
|
||||
const { code: jsxCode } = await transformWithEsbuild(code, id, {
|
||||
loader: getEsbuildLoader(id),
|
||||
jsx: "preserve",
|
||||
sourcemap: "inline"
|
||||
});
|
||||
return await transformJSX({
|
||||
code: jsxCode,
|
||||
id,
|
||||
renderer: selectedJsxRenderer,
|
||||
mode,
|
||||
ssr,
|
||||
root: settings.config.root
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
export {
|
||||
jsx as default
|
||||
};
|
13
node_modules/astro/dist/vite-plugin-jsx/tag.d.ts
generated
vendored
Normal file
13
node_modules/astro/dist/vite-plugin-jsx/tag.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
import type { PluginObj } from '@babel/core';
|
||||
/**
|
||||
* This plugin handles every file that runs through our JSX plugin.
|
||||
* Since we statically match every JSX file to an Astro renderer based on import scanning,
|
||||
* it would be helpful to embed some of that metadata at runtime.
|
||||
*
|
||||
* This plugin crawls each export in the file and "tags" each export with a given `rendererName`.
|
||||
* This allows us to automatically match a component to a renderer and skip the usual `check()` calls.
|
||||
*/
|
||||
export default function tagExportsWithRenderer({ rendererName, }: {
|
||||
rendererName: string;
|
||||
root: URL;
|
||||
}): Promise<PluginObj>;
|
109
node_modules/astro/dist/vite-plugin-jsx/tag.js
generated
vendored
Normal file
109
node_modules/astro/dist/vite-plugin-jsx/tag.js
generated
vendored
Normal file
|
@ -0,0 +1,109 @@
|
|||
import * as t from "@babel/types";
|
||||
async function tagExportsWithRenderer({
|
||||
rendererName
|
||||
}) {
|
||||
return {
|
||||
visitor: {
|
||||
Program: {
|
||||
// Inject `import { __astro_tag_component__ } from 'astro/server/index.js'`
|
||||
enter(path) {
|
||||
path.node.body.splice(
|
||||
0,
|
||||
0,
|
||||
t.importDeclaration(
|
||||
[
|
||||
t.importSpecifier(
|
||||
t.identifier("__astro_tag_component__"),
|
||||
t.identifier("__astro_tag_component__")
|
||||
)
|
||||
],
|
||||
t.stringLiteral("astro/server/index.js")
|
||||
)
|
||||
);
|
||||
},
|
||||
// For each export we found, inject `__astro_tag_component__(exportName, rendererName)`
|
||||
exit(path, state) {
|
||||
const exportedIds = state.get("astro:tags");
|
||||
if (exportedIds) {
|
||||
for (const id of exportedIds) {
|
||||
path.node.body.push(
|
||||
t.expressionStatement(
|
||||
t.callExpression(t.identifier("__astro_tag_component__"), [
|
||||
t.identifier(id),
|
||||
t.stringLiteral(rendererName)
|
||||
])
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
ExportDeclaration: {
|
||||
/**
|
||||
* For default anonymous function export, we need to give them a unique name
|
||||
* @param path
|
||||
* @returns
|
||||
*/
|
||||
enter(path) {
|
||||
var _a;
|
||||
const node = path.node;
|
||||
if (!t.isExportDefaultDeclaration(node))
|
||||
return;
|
||||
if (t.isArrowFunctionExpression(node.declaration) || t.isCallExpression(node.declaration)) {
|
||||
const varName = t.isArrowFunctionExpression(node.declaration) ? "_arrow_function" : "_hoc_function";
|
||||
const uidIdentifier = path.scope.generateUidIdentifier(varName);
|
||||
path.insertBefore(
|
||||
t.variableDeclaration("const", [
|
||||
t.variableDeclarator(uidIdentifier, node.declaration)
|
||||
])
|
||||
);
|
||||
node.declaration = uidIdentifier;
|
||||
} else if (t.isFunctionDeclaration(node.declaration) && !((_a = node.declaration.id) == null ? void 0 : _a.name)) {
|
||||
const uidIdentifier = path.scope.generateUidIdentifier("_function");
|
||||
node.declaration.id = uidIdentifier;
|
||||
}
|
||||
},
|
||||
exit(path, state) {
|
||||
var _a, _b, _c;
|
||||
const node = path.node;
|
||||
if (node.exportKind === "type")
|
||||
return;
|
||||
if (t.isExportAllDeclaration(node))
|
||||
return;
|
||||
const addTag = (id) => {
|
||||
const tags = state.get("astro:tags") ?? [];
|
||||
state.set("astro:tags", [...tags, id]);
|
||||
};
|
||||
if (t.isExportNamedDeclaration(node) || t.isExportDefaultDeclaration(node)) {
|
||||
if (t.isIdentifier(node.declaration)) {
|
||||
addTag(node.declaration.name);
|
||||
} else if (t.isFunctionDeclaration(node.declaration) && ((_a = node.declaration.id) == null ? void 0 : _a.name)) {
|
||||
addTag(node.declaration.id.name);
|
||||
} else if (t.isVariableDeclaration(node.declaration)) {
|
||||
(_b = node.declaration.declarations) == null ? void 0 : _b.forEach((declaration) => {
|
||||
if (t.isArrowFunctionExpression(declaration.init) && t.isIdentifier(declaration.id)) {
|
||||
addTag(declaration.id.name);
|
||||
}
|
||||
});
|
||||
} else if (t.isObjectExpression(node.declaration)) {
|
||||
(_c = node.declaration.properties) == null ? void 0 : _c.forEach((property) => {
|
||||
if (t.isProperty(property) && t.isIdentifier(property.key)) {
|
||||
addTag(property.key.name);
|
||||
}
|
||||
});
|
||||
} else if (t.isExportNamedDeclaration(node) && !node.source) {
|
||||
node.specifiers.forEach((specifier) => {
|
||||
if (t.isExportSpecifier(specifier) && t.isIdentifier(specifier.exported)) {
|
||||
addTag(specifier.local.name);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
export {
|
||||
tagExportsWithRenderer as default
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue