diff options
author | 2022-01-31 22:11:22 +0100 | |
---|---|---|
committer | 2022-01-31 16:11:22 -0500 | |
commit | 95b9740431d40be9630dfc13783512aee6ec5c65 (patch) | |
tree | 6489a953f7ff73ef8f449338daeec5140cf74548 | |
parent | de9fadbaede6b91b1eb203b6b57ea663fea48ee0 (diff) | |
download | astro-95b9740431d40be9630dfc13783512aee6ec5c65.tar.gz astro-95b9740431d40be9630dfc13783512aee6ec5c65.tar.zst astro-95b9740431d40be9630dfc13783512aee6ec5c65.zip |
Add Astro.resolve deprecation warning case for script tags (#2493)
* Adding script.ts util for checking scripts files path
* Adding deprecation message `Astro.resolve()` case for scripts files with suggestions
-rw-r--r-- | packages/astro/src/core/ssr/result.ts | 12 | ||||
-rw-r--r-- | packages/astro/src/core/ssr/script.ts | 8 |
2 files changed, 20 insertions, 0 deletions
diff --git a/packages/astro/src/core/ssr/result.ts b/packages/astro/src/core/ssr/result.ts index 7aca848a2..5a03ab769 100644 --- a/packages/astro/src/core/ssr/result.ts +++ b/packages/astro/src/core/ssr/result.ts @@ -3,6 +3,7 @@ import type { AstroConfig, AstroGlobal, AstroGlobalPartial, Params, Renderer, SS import { bold } from 'kleur/colors'; import { canonicalURL as getCanonicalURL } from '../util.js'; import { isCSSRequest } from './css.js'; +import { isScriptRequest } from './script.js'; import { renderSlot } from '../../runtime/server/index.js'; import { warn, LogOptions } from '../logger.js'; @@ -50,6 +51,17 @@ export function createResult(args: CreateResultArgs): SSRResult { @import "${path}"; </style> `; + } else if (isScriptRequest(path)) { + extra = `It looks like you are resolving scripts. If you are adding a script tag, replace with this: + +<script type="module" src={(await import("${path}?url")).default}></script> + +or consider make it a module like so: + +<script type="module" hoist> + import MyModule from "${path}"; +</script> +`; } warn( diff --git a/packages/astro/src/core/ssr/script.ts b/packages/astro/src/core/ssr/script.ts new file mode 100644 index 000000000..ec6e9ace3 --- /dev/null +++ b/packages/astro/src/core/ssr/script.ts @@ -0,0 +1,8 @@ +export const SCRIPT_EXTENSIONS = new Set(['.js', '.ts']); + +const scriptRe = new RegExp( + `\\.(${Array.from(SCRIPT_EXTENSIONS) + .map((s) => s.slice(1)) + .join('|')})($|\\?)` +); +export const isScriptRequest = (request: string): boolean => scriptRe.test(request);
\ No newline at end of file |