Skip to content

Commit

Permalink
[nextjs] Fix tsconfig's path resolving
Browse files Browse the repository at this point in the history
when trying to internpolate a styled component in another
Fixes #171
  • Loading branch information
brijeshb42 authored and Brijesh Bittu committed Sep 26, 2024
1 parent 84df11a commit bd24163
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
10 changes: 9 additions & 1 deletion apps/pigment-css-next-app/src/app/grid/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import Box from '@pigment-css/react/Box';
import Grid from '@pigment-css/react/Grid';
import { styled } from '@pigment-css/react';
import { Test } from '@/components/Test';
import GridDemo3 from './demo3';

const Item = styled.div`
background-color: #fff;
border: 1px solid #ced7e0;
color: #222;
padding: 8px;
border-radius: 4px;
text-align: center;
${Test} {
color: red;
}
`;

function GridDemo1() {
Expand Down Expand Up @@ -53,7 +59,9 @@ function GridDemo4() {
return (
<Grid container rowSpacing={1} columnSpacing={{ xs: 1, sm: 2, md: 3 }}>
<Grid size={6}>
<Item>1</Item>
<Item>
<Test>1</Test>
</Item>
</Grid>
<Grid size={6}>
<Item>2</Item>
Expand Down
3 changes: 2 additions & 1 deletion apps/pigment-css-next-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
}
],
"paths": {
"@/*": ["./src/*"]
"@/*": ["./src/*"],
"src/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
Expand Down
2 changes: 1 addition & 1 deletion packages/pigment-css-unplugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@wyw-in-js/shared": "^0.5.4",
"@wyw-in-js/transform": "^0.5.4",
"babel-plugin-define-var": "^0.1.0",
"unplugin": "^1.7.1"
"unplugin": "^1.11.0"
},
"devDependencies": {
"@types/babel__core": "^7.20.5",
Expand Down
44 changes: 23 additions & 21 deletions packages/pigment-css-unplugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
type Theme as BaseTheme,
type PluginCustomOptions,
} from '@pigment-css/react/utils';
import type { ResolvePluginInstance } from 'webpack';
import { styledEngineMockup } from '@pigment-css/react/internal';

import { handleUrlReplacement, type AsyncResolver } from './utils';

type NextMeta = {
Expand Down Expand Up @@ -185,32 +185,34 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
return isZeroRuntimeProcessableFile(id, finalTransformLibraries);
},
webpack(compiler) {
const resolverPlugin: ResolvePluginInstance = {
apply(resolver) {
webpackResolver = function webpackAsyncResolve(
what: string,
importer: string,
stack: string[],
) {
compiler.resolverFactory.hooks.resolver
.for('normal')
.tap(`${pluginName}Resolver`, (resolver) => {
webpackResolver = (what: string, importer: string, stack: string[]) => {
const context = path.isAbsolute(importer)
? path.dirname(importer)
: path.join(projectPath, path.dirname(importer));
return new Promise((resolve, reject) => {
resolver.resolve({}, context, what, { stack: new Set(stack) }, (err, result) => {
if (err) {
reject(err);
} else if (result) {
resolve(result);
} else {
reject(new Error(`${process.env.PACKAGE_NAME}: Cannot resolve ${what}`));
}
});
resolver.resolve(
{},
context,
what,
{
stack: new Set(stack),
},
(err, result) => {
if (typeof result !== 'string') {
reject(new Error(`${process.env.PACKAGE_NAME}: Could not resolve ${what}`));
} else if (result) {
resolve(result);
} else {
reject(err);
}
},
);
});
};
},
};
compiler.options.resolve.plugins = compiler.options.resolve.plugins || [];
compiler.options.resolve.plugins.push(resolverPlugin);
});
},
async transform(code, url) {
const [filePath] = url.split('?');
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bd24163

Please sign in to comment.