From d55658f061bc4d95efa53e658cfbe407d94af284 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Sat, 2 Apr 2022 12:34:25 -0500 Subject: [PATCH] Implement new default `script` behavior, `style is:global` (#2961) * feat: implement RFC0016 * chore: update to latest compiler * chore: update compiler * test: fix script tests * test: update public tests * feat: throw a warning when referencing scripts in `public/` without `is:inline` --- .changeset/happy-carrots-carry.md | 5 +++++ examples/component/demo/src/pages/index.astro | 2 +- .../with-tailwindcss/src/components/Button.astro | 5 +++-- packages/astro/package.json | 2 +- packages/astro/src/core/render/result.ts | 9 ++++----- packages/astro/src/runtime/server/index.ts | 7 ++++--- packages/astro/src/vite-plugin-astro/index.ts | 12 ++++++++++-- packages/astro/test/astro-public.test.js | 2 +- .../astro-css-bundling/src/components/Nav.astro | 2 +- .../test/fixtures/astro-public/src/pages/index.astro | 6 +++--- .../astro-scripts/src/components/Inline.astro | 4 ++-- .../src/components/NoHoistClassic.astro | 2 +- .../astro-scripts/src/components/NoHoistModule.astro | 2 +- .../astro-scripts/src/components/Widget.astro | 2 +- .../astro-scripts/src/components/Widget2.astro | 2 +- .../fixtures/astro-scripts/src/pages/external.astro | 2 +- .../remote-css/src/components/CommonHead.astro | 4 ++-- pnpm-lock.yaml | 8 ++++---- 18 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 .changeset/happy-carrots-carry.md diff --git a/.changeset/happy-carrots-carry.md b/.changeset/happy-carrots-carry.md new file mode 100644 index 000000000..82cae9fe5 --- /dev/null +++ b/.changeset/happy-carrots-carry.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Implement [RFC0016](https://github.com/withastro/rfcs/blob/main/proposals/0016-style-script-defaults.md) which changes the default behavior of `script`, introduces `is:inline`, and changes ` +--- +import "${path}"; +--- `; } else if (isScriptRequest(path)) { extra = `It looks like you are resolving scripts. If you are adding a script tag, replace with this: @@ -134,7 +133,7 @@ export function createResult(args: CreateResultArgs): SSRResult { or consider make it a module like so: - `; diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index 7cf56c35f..7e8bc2b50 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -535,16 +535,17 @@ function getHTMLElementName(constructor: typeof HTMLElement) { } function renderElement(name: string, { props: _props, children = '' }: SSRElement, shouldEscape = true) { - // Do not print `hoist`, `lang`, `global` + // Do not print `hoist`, `lang`, `is:global` const { lang: _, 'data-astro-id': astroId, 'define:vars': defineVars, ...props } = _props; if (defineVars) { if (name === 'style') { - if (props.global) { + if (props['is:global']) { children = defineStyleVars(`:root`, defineVars) + '\n' + children; } else { children = defineStyleVars(`.astro-${astroId}`, defineVars) + '\n' + children; } - delete props.global; + delete props['is:global']; + delete props['is:scoped']; } if (name === 'script') { delete props.hoist; diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index aded8f425..7d5839e48 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -63,7 +63,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu const { query: fromQuery, filename } = parseAstroRequest(from); if (fromQuery.astro && isRelativePath(id) && fromQuery.type === 'script') { const resolvedURL = new URL(id, `file://${filename}`); - const resolved = resolvedURL.pathname; + const resolved = resolvedURL.pathname if (isBrowserPath(resolved)) { return relativeToRoot(resolved + resolvedURL.search); } @@ -96,7 +96,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu let source = await fs.promises.readFile(fileUrl, 'utf-8'); const isPage = fileUrl.pathname.startsWith(config.pages.pathname); if (isPage && config._ctx.scripts.some((s) => s.stage === 'page')) { - source += `\n'); - expect(indexHtml).to.include(''); + expect(indexHtml).to.include(''); expect(indexHtml).to.include(''); }); }); diff --git a/packages/astro/test/fixtures/astro-css-bundling/src/components/Nav.astro b/packages/astro/test/fixtures/astro-css-bundling/src/components/Nav.astro index 159ef6719..37a2cecf1 100644 --- a/packages/astro/test/fixtures/astro-css-bundling/src/components/Nav.astro +++ b/packages/astro/test/fixtures/astro-css-bundling/src/components/Nav.astro @@ -19,7 +19,7 @@ } - \ No newline at end of file + diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 814dbf0e8..b68e34713 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -442,7 +442,7 @@ importers: packages/astro: specifiers: - '@astrojs/compiler': ^0.13.2 + '@astrojs/compiler': ^0.14.1 '@astrojs/language-server': ^0.13.2 '@astrojs/markdown-remark': ^0.7.0 '@astrojs/prism': 0.4.1 @@ -528,7 +528,7 @@ importers: yargs-parser: ^21.0.1 zod: ^3.14.3 dependencies: - '@astrojs/compiler': 0.13.2 + '@astrojs/compiler': 0.14.1 '@astrojs/language-server': 0.13.2 '@astrojs/markdown-remark': link:../markdown/remark '@astrojs/prism': link:../astro-prism @@ -1713,8 +1713,8 @@ packages: leven: 3.1.0 dev: true - /@astrojs/compiler/0.13.2: - resolution: {integrity: sha512-0Un4CtLbhJljisFf9WaxK1TSV1oakR3Mh4x1Uyg1JHdyQ5te/1xcq+PWVaOyQc4lq4z8MYNFQb7hG66m0CeMtw==} + /@astrojs/compiler/0.14.1: + resolution: {integrity: sha512-dYgb52JvZE8jyDg84JkdJ/dTxRgHVbC47ou6Ymok/nZDh9kvlU7TJtEDCLlGfpfZTGvnkFTHMrz1kdbqCbFVCw==} dependencies: tsm: 2.2.1 uvu: 0.5.3