From acd1ec950e328e506df1f6fe08c2655bdabbf02a Mon Sep 17 00:00:00 2001 From: peng <414326615@qq.com> Date: Fri, 1 Mar 2024 16:35:29 +0800 Subject: [PATCH] Adapt node extname --- .../astro/src/assets/utils/transformToPath.ts | 11 +++--- packages/astro/src/core/create-vite.ts | 3 +- .../test/astro-assets-prefix-mult-cdn.test.js | 38 +++++++++---------- .../astro-assets-prefix/src/pages/index.astro | 2 +- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/packages/astro/src/assets/utils/transformToPath.ts b/packages/astro/src/assets/utils/transformToPath.ts index 41f33aed8536..54872a73460a 100644 --- a/packages/astro/src/assets/utils/transformToPath.ts +++ b/packages/astro/src/assets/utils/transformToPath.ts @@ -36,12 +36,13 @@ export function hashTransform( } export function getAssetsPrefix(fileType: string, assetsPrefix?: AssetsPrefix):string { - if (!assetsPrefix) return '' - if (typeof assetsPrefix === 'string') return assetsPrefix + if (!assetsPrefix) return ''; + if (typeof assetsPrefix === 'string') return assetsPrefix; + fileType = fileType[0] === '.' ? fileType.slice(1) : fileType; if (assetsPrefix[fileType]) { - return assetsPrefix[fileType] + return assetsPrefix[fileType]; } else if (assetsPrefix.defaultAssetsPrefix) { - return assetsPrefix.defaultAssetsPrefix + return assetsPrefix.defaultAssetsPrefix; } - return '' + return ''; } \ No newline at end of file diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 275cb09cc573..56c2711f7a42 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -35,6 +35,7 @@ import { createViteLogger } from './logger/vite.js'; import { vitePluginMiddleware } from './middleware/vite-plugin.js'; import { joinPaths } from './path.js'; import { getAssetsPrefix } from '../assets/utils/transformToPath.js'; +import { isObject } from './util.js'; interface CreateViteOptions { settings: AstroSettings; @@ -311,5 +312,5 @@ function isCommonNotAstro(dep: string): boolean { } function stringifyForDefine(value: string | undefined | object): string { - return typeof value === 'string' ? JSON.stringify(value) : 'undefined'; + return (typeof value === "string" || isObject(value)) ? JSON.stringify(value) : "undefined"; } diff --git a/packages/astro/test/astro-assets-prefix-mult-cdn.test.js b/packages/astro/test/astro-assets-prefix-mult-cdn.test.js index 14ef1d7e34bd..dd412deba0cc 100644 --- a/packages/astro/test/astro-assets-prefix-mult-cdn.test.js +++ b/packages/astro/test/astro-assets-prefix-mult-cdn.test.js @@ -1,4 +1,5 @@ -import { expect } from 'chai'; +import assert from 'node:assert/strict'; +import { before, describe, it } from 'node:test'; import * as cheerio from 'cheerio'; import testAdapter from './test-adapter.js'; import { loadFixture } from './test-utils.js'; @@ -34,7 +35,7 @@ describe('Assets Prefix Multiple CDN - Static', () => { const $ = cheerio.load(html); const stylesheets = $('link[rel="stylesheet"]'); stylesheets.each((i, el) => { - expect(el.attribs.href).to.match(cssAssetsPrefixRegex); + assert.match(el.attribs.href, cssAssetsPrefixRegex) }); }); @@ -42,40 +43,39 @@ describe('Assets Prefix Multiple CDN - Static', () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html); const imgAsset = $('#image-asset'); - expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + assert.match(imgAsset.attr('src'), defaultAssetsPrefixRegex); }); it('react component astro-island should import from jsAssetsPrefix', async () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html); const island = $('astro-island'); - expect(island.attr('component-url')).to.match(jsAssetsPrefixRegex); - expect(island.attr('renderer-url')).to.match(jsAssetsPrefixRegex); + assert.match(island.attr('component-url'), jsAssetsPrefixRegex); + assert.match(island.attr('renderer-url'), jsAssetsPrefixRegex); }); it('import.meta.env.ASSETS_PREFIX works', async () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html); const env = $('#assets-prefix-env'); - expect(env.text()).to.equal(JSON.stringify(assetsPrefix)); + assert.equal(env.text(), JSON.stringify(assetsPrefix)); }); it('markdown image src start with assetsPrefix', async () => { const html = await fixture.readFile('/markdown/index.html'); const $ = cheerio.load(html); - const imgAsset = $('img'); - expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + const imgAssets = $('img'); + assert.match(imgAssets.attr('src'), defaultAssetsPrefixRegex); }); it('content collections image src start with assetsPrefix', async () => { const html = await fixture.readFile('/blog/index.html'); const $ = cheerio.load(html); const imgAsset = $('img'); - expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + assert.match(imgAsset.attr('src'), defaultAssetsPrefixRegex); }); }); - describe('Assets Prefix Multiple CDN, server', () => { let app; @@ -95,43 +95,43 @@ describe('Assets Prefix Multiple CDN, server', () => { it('all stylesheets should start with assetPrefix', async () => { const request = new Request('http://example.com/custom-base/'); const response = await app.render(request); - expect(response.status).to.equal(200); + assert.equal(response.status, 200); const html = await response.text(); const $ = cheerio.load(html); const stylesheets = $('link[rel="stylesheet"]'); stylesheets.each((i, el) => { - expect(el.attribs.href).to.match(cssAssetsPrefixRegex); + assert.match(el.attribs.href, cssAssetsPrefixRegex); }); }); it('image src start with assetsPrefix', async () => { const request = new Request('http://example.com/custom-base/'); const response = await app.render(request); - expect(response.status).to.equal(200); + assert.equal(response.status, 200); const html = await response.text(); const $ = cheerio.load(html); const imgAsset = $('#image-asset'); - expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + assert.match(imgAsset.attr('src'), defaultAssetsPrefixRegex); }); it('react component astro-island should import from assetsPrefix', async () => { const request = new Request('http://example.com/custom-base/'); const response = await app.render(request); - expect(response.status).to.equal(200); + assert.equal(response.status, 200); const html = await response.text(); const $ = cheerio.load(html); const island = $('astro-island'); - expect(island.attr('component-url')).to.match(jsAssetsPrefixRegex); - expect(island.attr('renderer-url')).to.match(jsAssetsPrefixRegex); + assert.match(island.attr('component-url'), jsAssetsPrefixRegex); + assert.match(island.attr('renderer-url'), jsAssetsPrefixRegex); }); it('markdown optimized image src does not start with assetsPrefix in SSR', async () => { const request = new Request('http://example.com/custom-base/markdown/'); const response = await app.render(request); - expect(response.status).to.equal(200); + assert.equal(response.status, 200); const html = await response.text(); const $ = cheerio.load(html); const imgAsset = $('img'); - expect(imgAsset.attr('src')).to.not.match(defaultAssetsPrefixRegex); + assert.doesNotMatch(imgAsset.attr('src'), defaultAssetsPrefixRegex); }); }); diff --git a/packages/astro/test/fixtures/astro-assets-prefix/src/pages/index.astro b/packages/astro/test/fixtures/astro-assets-prefix/src/pages/index.astro index 67f6e97fa56c..4158153205f4 100644 --- a/packages/astro/test/fixtures/astro-assets-prefix/src/pages/index.astro +++ b/packages/astro/test/fixtures/astro-assets-prefix/src/pages/index.astro @@ -13,7 +13,7 @@ import Counter from '../components/Counter.jsx'; penguin penguin -

{import.meta.env.ASSETS_PREFIX}

+

{typeof import.meta.env.ASSETS_PREFIX === 'string' ? import.meta.env.ASSETS_PREFIX : JSON.stringify(import.meta.env.ASSETS_PREFIX)}