diff --git a/packages/vite/src/node/__tests__/plugins/css.spec.ts b/packages/vite/src/node/__tests__/plugins/css.spec.ts index 43ad248fb59bbe..c067db4a0935bf 100644 --- a/packages/vite/src/node/__tests__/plugins/css.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/css.spec.ts @@ -1,4 +1,7 @@ -import { cssUrlRE } from '../../plugins/css' +import { cssUrlRE, cssPlugin } from '../../plugins/css' +import { resolveConfig } from '../../config' +import fs from 'fs' +import path from 'path' describe('search css url function', () => { test('some spaces before it', () => { @@ -41,3 +44,71 @@ describe('search css url function', () => { ).toBe(true) }) }) + +describe('css path resolutions', () => { + const mockedProjectPath = '/foo/bar/project' + const mockedBarCssRelativePath = '/css/bar.module.css' + const mockedFooCssRelativePath = '/css/foo.module.css' + + test('cssmodule compose/from path resolutions', async () => { + const config = await resolveConfig( + { + resolve: { + alias: [ + { + find: '@', + replacement: mockedProjectPath + } + ] + } + }, + 'serve' + ) + + const { transform, buildStart } = cssPlugin(config) + + await buildStart.call({}) + + const mockFs = jest + .spyOn(fs, 'readFile') + // @ts-ignore jest.spyOn not recognize overrided `fs.readFile` definition. + .mockImplementationOnce((p, encoding, callback) => { + expect(p).toBe(path.join(mockedProjectPath, mockedBarCssRelativePath)) + expect(encoding).toBe('utf-8') + callback( + null, + Buffer.from(` +.bar { + display: block; + background: #f0f; +} + `) + ) + }) + + const { code } = await transform.call( + { + addWatchFile() { + return + } + }, + ` +.foo { + composes: bar from '@${mockedBarCssRelativePath}'; +} + `, + path.join(mockedProjectPath, mockedFooCssRelativePath) + ) + + expect(code).toBe(` +._bar_soicv_2 { + display: block; + background: #f0f; +} +._foo_ede2y_2 { +} + `) + + mockFs.mockReset() + }) +})