|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { OnLoadArgs, OnLoadResult, PluginBuild } from 'esbuild'; |
| 10 | +import assert from 'node:assert'; |
| 11 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 12 | +import { join } from 'node:path'; |
| 13 | +import { initializeHash } from '../../../utils/hash'; |
| 14 | +import { ComponentStylesheetBundler } from '../angular/component-stylesheets'; |
| 15 | +import { resetSassWorkerPoolCaches, shutdownSassWorkerPool } from './sass-language'; |
| 16 | +import { StylesheetLanguage, StylesheetPluginFactory } from './stylesheet-plugin-factory'; |
| 17 | + |
| 18 | +describe('StylesheetPluginFactory', () => { |
| 19 | + it('sets resolveDir and preserves semicolons in filename for inline component styles', async () => { |
| 20 | + const filename = '/workspace/src/dir;with;semicolons/app.component.ts'; |
| 21 | + const entryPath = `scss;abc123;${filename}`; |
| 22 | + let receivedFile: string | undefined; |
| 23 | + let receivedFormat: string | undefined; |
| 24 | + |
| 25 | + const language: StylesheetLanguage = { |
| 26 | + name: 'test-scss', |
| 27 | + componentFilter: /^scss;/, |
| 28 | + fileFilter: /\.scss$/, |
| 29 | + process(data, file, format) { |
| 30 | + receivedFile = file; |
| 31 | + receivedFormat = format; |
| 32 | + |
| 33 | + return { |
| 34 | + contents: data, |
| 35 | + loader: 'css', |
| 36 | + }; |
| 37 | + }, |
| 38 | + }; |
| 39 | + |
| 40 | + const factory = new StylesheetPluginFactory({ |
| 41 | + sourcemap: false, |
| 42 | + inlineComponentData: { |
| 43 | + [entryPath]: '.box { color: red; }', |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + const plugin = factory.create(language); |
| 48 | + let componentLoadCallback: |
| 49 | + | (( |
| 50 | + args: OnLoadArgs, |
| 51 | + ) => OnLoadResult | null | undefined | Promise<OnLoadResult | null | undefined>) |
| 52 | + | undefined; |
| 53 | + |
| 54 | + const buildStub = { |
| 55 | + onStart: () => {}, |
| 56 | + onLoad: ( |
| 57 | + options: { filter: RegExp; namespace?: string }, |
| 58 | + callback: ( |
| 59 | + args: OnLoadArgs, |
| 60 | + ) => OnLoadResult | null | undefined | Promise<OnLoadResult | null | undefined>, |
| 61 | + ) => { |
| 62 | + if (options.namespace === 'angular:styles/component') { |
| 63 | + componentLoadCallback = callback; |
| 64 | + } |
| 65 | + }, |
| 66 | + } as unknown as PluginBuild; |
| 67 | + |
| 68 | + await plugin.setup(buildStub); |
| 69 | + assert(componentLoadCallback, 'Expected onLoad callback for angular:styles/component'); |
| 70 | + |
| 71 | + const result = await componentLoadCallback({ |
| 72 | + path: entryPath, |
| 73 | + namespace: 'angular:styles/component', |
| 74 | + suffix: '', |
| 75 | + pluginData: undefined, |
| 76 | + with: {}, |
| 77 | + }); |
| 78 | + |
| 79 | + expect(receivedFormat).toBe('scss'); |
| 80 | + expect(receivedFile).toBe(filename); |
| 81 | + expect(result?.resolveDir).toBe('/workspace/src/dir;with;semicolons'); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('inline SCSS component stylesheet bundling', () => { |
| 85 | + let tempRoot: string; |
| 86 | + |
| 87 | + beforeAll(async () => { |
| 88 | + await initializeHash(); |
| 89 | + const baseTmpDir = process.env['TEST_TMPDIR']; |
| 90 | + assert(baseTmpDir, 'TEST_TMPDIR is not set'); |
| 91 | + tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-inline-style-test-')); |
| 92 | + }); |
| 93 | + |
| 94 | + afterAll(async () => { |
| 95 | + shutdownSassWorkerPool(); |
| 96 | + await rm(tempRoot, { force: true, recursive: true }); |
| 97 | + }); |
| 98 | + |
| 99 | + beforeEach(() => { |
| 100 | + resetSassWorkerPoolCaches(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('resolves relative url() assets in inline SCSS when component path contains semicolons', async () => { |
| 104 | + const componentDir = join(tempRoot, 'cmp;dir'); |
| 105 | + await mkdir(componentDir, { recursive: true }); |
| 106 | + await writeFile(join(componentDir, 'asset.svg'), '<svg></svg>'); |
| 107 | + |
| 108 | + const bundler = new ComponentStylesheetBundler( |
| 109 | + { |
| 110 | + workspaceRoot: tempRoot, |
| 111 | + optimization: false, |
| 112 | + inlineFonts: false, |
| 113 | + sourcemap: false, |
| 114 | + outputNames: { bundles: '[name]', media: 'media/[name]' }, |
| 115 | + target: ['es2022'], |
| 116 | + cacheOptions: { enabled: false, path: '', basePath: tempRoot }, |
| 117 | + }, |
| 118 | + 'scss', |
| 119 | + false, |
| 120 | + ); |
| 121 | + |
| 122 | + try { |
| 123 | + const result = await bundler.bundleInline( |
| 124 | + '$icon: "./asset.svg"; .icon { background: url($icon); }', |
| 125 | + join(componentDir, 'app.component.ts'), |
| 126 | + 'scss', |
| 127 | + ); |
| 128 | + |
| 129 | + assert(!result.errors, 'Expected inline SCSS bundling to succeed without errors'); |
| 130 | + expect(result.contents).toContain('asset.svg'); |
| 131 | + expect(result.outputFiles.some((f) => f.path.endsWith('asset.svg'))).toBeTrue(); |
| 132 | + } finally { |
| 133 | + await bundler.dispose(); |
| 134 | + } |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments