Skip to content

Commit

Permalink
Replace endsWith with includes to check adapter path (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Aug 29, 2024
1 parent fbd8df5 commit 33de175
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/pigment-css-react/src/utils/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ export function preprocessor(

return css;
}

export function matchAdapterPath(path: string) {
return path.includes('zero-styled');
}
4 changes: 2 additions & 2 deletions packages/pigment-css-react/tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
transform as wywTransform,
createFileReporter,
} from '@wyw-in-js/transform';
import { PluginCustomOptions, preprocessor } from '@pigment-css/react/utils';
import { matchAdapterPath, PluginCustomOptions, preprocessor } from '@pigment-css/react/utils';
import * as prettier from 'prettier';

import sxTransformPlugin from '../exports/sx-plugin';
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function runTransformation(absolutePath: string, options?: Transfor
return require.resolve(`../exports/styled`);
}

if (source !== '@pigment-css/react' && !source.endsWith('/zero-styled')) {
if (source !== '@pigment-css/react' && !matchAdapterPath(source)) {
return null;
}
return require.resolve(`../${pkgJson['wyw-in-js'].tags[tag]}`.replace('.js', ''));
Expand Down
9 changes: 9 additions & 0 deletions packages/pigment-css-react/tests/utils/adapter-path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect } from 'chai';
import { matchAdapterPath } from '@pigment-css/react/utils';

describe('matchAdapterPath', () => {
it('should match adapter path', () => {
expect(matchAdapterPath('../zero-styled')).to.equal(true);
expect(matchAdapterPath('../zero-styled/index.js')).to.equal(true);
});
});
3 changes: 2 additions & 1 deletion packages/pigment-css-unplugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
generateTokenCss,
generateThemeSource,
extendTheme,
matchAdapterPath,
type Theme as BaseTheme,
type PluginCustomOptions,
} from '@pigment-css/react/utils';
Expand Down Expand Up @@ -256,7 +257,7 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
if (tagResult) {
return tagResult;
}
if (source.endsWith('/zero-styled')) {
if (matchAdapterPath(source)) {
return require.resolve(`${process.env.RUNTIME_PACKAGE_NAME}/exports/${tag}`);
}
return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/pigment-css-vite-plugin/src/vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
type PluginOptions,
type IFileReporterOptions,
} from '@wyw-in-js/transform';
import { type PluginCustomOptions } from '@pigment-css/react/utils';
import { matchAdapterPath, type PluginCustomOptions } from '@pigment-css/react/utils';

export type VitePluginOptions = {
debug?: IFileReporterOptions | false | null | undefined;
Expand Down Expand Up @@ -238,7 +238,7 @@ export default function wywVitePlugin({
if (tagResult) {
return tagResult;
}
if (source.endsWith('/zero-styled')) {
if (matchAdapterPath(source)) {
return `${process.env.RUNTIME_PACKAGE_NAME}/exports/${tag}`;
}
return null;
Expand Down

0 comments on commit 33de175

Please sign in to comment.