fix: remove estree/astring hack with serialize-javascript library

This commit is contained in:
Nate Moore 2021-12-07 10:55:34 -06:00
parent 85ecaab0cf
commit 72b136498b
5 changed files with 55 additions and 86 deletions

View file

@ -71,14 +71,13 @@
"@proload/plugin-tsm": "^0.1.0",
"@types/babel__core": "^7.1.15",
"@web/parse5-utils": "^1.3.0",
"astring": "^1.7.5",
"babel-plugin-module-resolver": "^4.1.0",
"ci-info": "^3.2.0",
"common-ancestor-path": "^1.0.1",
"connect": "^3.7.0",
"eol": "^0.9.1",
"es-module-lexer": "^0.9.3",
"esbuild": "0.13.7",
"estree-util-value-to-estree": "^1.2.0",
"estree-walker": "^3.0.0",
"fast-glob": "^3.2.7",
"fast-xml-parser": "^4.0.0-beta.3",
@ -99,6 +98,7 @@
"sass": "^1.43.4",
"semver": "^7.3.5",
"send": "^0.17.1",
"serialize-javascript": "^6.0.0",
"shiki": "^0.9.10",
"shorthash": "^0.0.2",
"slash": "^4.0.0",

View file

@ -0,0 +1,5 @@
declare module 'serialize-javascript' {
function serialize(value: any): string;
export default serialize;
}

View file

@ -14,16 +14,16 @@ import { resolveDependency } from './util.js';
// Some packages are just external, and thats the way it goes.
const ALWAYS_EXTERNAL = new Set([
...builtinModules.map((name) => `node:${name}`),
'@sveltejs/vite-plugin-svelte',
'estree-util-value-to-estree',
'micromark-util-events-to-acorn',
'node-fetch',
'prismjs',
'shiki',
'shorthash',
'unified',
'whatwg-url',
...builtinModules.map((name) => `node:${name}`),
'@sveltejs/vite-plugin-svelte',
'serialize-javascript',
'micromark-util-events-to-acorn',
'node-fetch',
'prismjs',
'shiki',
'shorthash',
'unified',
'whatwg-url',
]);
const ALWAYS_NOEXTERNAL = new Set([
'astro', // This is only because Vite's native ESM doesn't resolve "exports" correctly.
@ -40,38 +40,42 @@ interface CreateViteOptions {
/** Return a common starting point for all Vite actions */
export async function createVite(inlineConfig: ViteConfigWithSSR, { astroConfig, logging, devServer }: CreateViteOptions): Promise<ViteConfigWithSSR> {
// First, start with the Vite configuration that Astro core needs
let viteConfig: ViteConfigWithSSR = {
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.projectRoot)), // using local caches allows Astro to be used in monorepos, etc.
clearScreen: false, // we want to control the output, not Vite
logLevel: 'error', // log errors only
optimizeDeps: {
entries: ['src/**/*'], // Try and scan a users project (wont catch everything),
},
plugins: [
configAliasVitePlugin({ config: astroConfig }),
// First, start with the Vite configuration that Astro core needs
let viteConfig: ViteConfigWithSSR = {
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.projectRoot)), // using local caches allows Astro to be used in monorepos, etc.
clearScreen: false, // we want to control the output, not Vite
logLevel: 'error', // log errors only
optimizeDeps: {
entries: ['src/**/*'], // Try and scan a users project (wont catch everything),
},
plugins: [
configAliasVitePlugin({ config: astroConfig }),
astroVitePlugin({ config: astroConfig, devServer, logging }),
markdownVitePlugin({ config: astroConfig, devServer }),
jsxVitePlugin({ config: astroConfig, logging }),
astroPostprocessVitePlugin({ config: astroConfig, devServer }),
],
publicDir: fileURLToPath(astroConfig.public),
root: fileURLToPath(astroConfig.projectRoot),
envPrefix: 'PUBLIC_',
server: {
force: true, // force dependency rebuild (TODO: enabled only while next is unstable; eventually only call in "production" mode?)
hmr: process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'production' ? false : undefined, // disable HMR for test
// handle Vite URLs
proxy: {
// add proxies here
},
},
// Note: SSR API is in beta (https://vitejs.dev/guide/ssr.html)
ssr: {
external: [...ALWAYS_EXTERNAL],
noExternal: [...ALWAYS_NOEXTERNAL],
},
};
astroPostprocessVitePlugin({ config: astroConfig, devServer })
],
publicDir: fileURLToPath(astroConfig.public),
root: fileURLToPath(astroConfig.projectRoot),
envPrefix: 'PUBLIC_',
server: {
force: true, // force dependency rebuild (TODO: enabled only while next is unstable; eventually only call in "production" mode?)
hmr: process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'production' ? false : undefined, // disable HMR for test
// handle Vite URLs
proxy: {
// add proxies here
},
fs: {
// Allow serving files from one level up to the project root
allow: ['..']
}
},
// Note: SSR API is in beta (https://vitejs.dev/guide/ssr.html)
ssr: {
external: [...ALWAYS_EXTERNAL],
noExternal: [...ALWAYS_NOEXTERNAL],
},
};
// Add in Astro renderers, which will extend the base config
for (const name of astroConfig.renderers) {

View file

@ -1,34 +1,11 @@
import type { AstroComponentMetadata } from '../../@types/astro';
import type { SSRElement, SSRResult } from '../../@types/astro';
import { valueToEstree } from 'estree-util-value-to-estree';
import * as astring from 'astring';
import { hydrationSpecifier, serializeListValue } from './util.js';
const { generate, GENERATOR } = astring;
// INVESTIGATE: What features are we getting from this that we need?
// JSON.stringify has a "replacer" argument.
// A more robust version alternative to `JSON.stringify` that can handle most values
// see https://github.com/remcohaszing/estree-util-value-to-estree#readme
const customGenerator: astring.Generator = {
...GENERATOR,
Literal(node, state) {
if (node.raw != null) {
// escape closing script tags in strings so browsers wouldn't interpret them as
// closing the actual end tag in HTML
state.write(node.raw.replace('</script>', '<\\/script>'));
} else {
GENERATOR.Literal(node, state);
}
},
};
import type { SSRElement } from '../../@types/astro';
import { serializeListValue } from './util.js';
import serialize from 'serialize-javascript';
// Serializes props passed into a component so that they can be reused during hydration.
// The value is any
export function serializeProps(value: any) {
return generate(valueToEstree(value), {
generator: customGenerator,
});
return serialize(value)
}
const HydrationDirectives = ['load', 'idle', 'media', 'visible', 'only'];

View file

@ -2587,11 +2587,6 @@ ast-types@^0.13.2:
dependencies:
tslib "^2.0.1"
astring@^1.7.5:
version "1.8.1"
resolved "https://registry.yarnpkg.com/astring/-/astring-1.8.1.tgz#a91c4afd4af3523e11f31242a3d5d9af62bb6cc6"
integrity sha512-Aj3mbwVzj7Vve4I/v2JYOPFkCGM2YS7OqQTNSxmUR+LECRpokuPgAYghePgr6SALDo5bD5DlfbSaYjOzGJZOLQ==
async@0.9.x:
version "0.9.2"
resolved "https://registry.yarnpkg.com/async/-/async-0.9.2.tgz#aea74d5e61c1f899613bf64bda66d4c78f2fd17d"
@ -4362,13 +4357,6 @@ estree-util-is-identifier-name@^2.0.0:
resolved "https://registry.yarnpkg.com/estree-util-is-identifier-name/-/estree-util-is-identifier-name-2.0.0.tgz#e2d3d2ae3032c017b2112832bfc5d8ba938c8010"
integrity sha512-aXXZFVMnBBDRP81vS4YtAYJ0hUkgEsXea7lNKWCOeaAquGb1Jm2rcONPB5fpzwgbNxulTvrWuKnp9UElUGAKeQ==
estree-util-value-to-estree@^1.2.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/estree-util-value-to-estree/-/estree-util-value-to-estree-1.3.0.tgz#1d3125594b4d6680f666644491e7ac1745a3df49"
integrity sha512-Y+ughcF9jSUJvncXwqRageavjrNPAI+1M/L3BI3PyLp1nmgYTGUXU6t5z1Y7OWuThoDdhPME07bQU+d5LxdJqw==
dependencies:
is-plain-obj "^3.0.0"
estree-util-visit@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/estree-util-visit/-/estree-util-visit-1.1.0.tgz#c0ea7942c40ac7889a77b57a11e92f987744bc6f"
@ -5612,11 +5600,6 @@ is-plain-obj@^2.1.0:
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287"
integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==
is-plain-obj@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-3.0.0.tgz#af6f2ea14ac5a646183a5bbdb5baabbc156ad9d7"
integrity sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==
is-plain-obj@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-4.0.0.tgz#06c0999fd7574edf5a906ba5644ad0feb3a84d22"
@ -8270,7 +8253,7 @@ send@^0.17.1:
range-parser "~1.2.1"
statuses "~1.5.0"
serialize-javascript@6.0.0:
serialize-javascript@6.0.0, serialize-javascript@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-6.0.0.tgz#efae5d88f45d7924141da8b5c3a7a7e663fefeb8"
integrity sha512-Qr3TosvguFt8ePWqsvRfrKyQXIiW+nGbYpy8XK24NQHE83caxWt+mIymTT19DGFbNWNLfEwsrkSmN64lVWB9ag==