Skip to content

Commit 0cd6daa

Browse files
author
Brijesh Bittu
committed
Suppress warnings in webpack
Also add descriptions for each bundler option and don't expose internal options to user.
1 parent 7fc8df0 commit 0cd6daa

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

packages/pigment-css-plugin/src/nextjs.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { NextConfig } from 'next';
33
import { findPagesDir } from 'next/dist/lib/find-pages-dir';
44

55
import webpackPlugin from './webpack';
6+
import type { ExcludePluginOptions } from './utils';
67

78
const NEXTJS_ARTIFACTS = 'nextjs-artifacts';
89

@@ -12,10 +13,7 @@ const extractionFile = path.join(
1213
'pigment-virtual.css',
1314
);
1415

15-
export type PigmentCSSConfig = Exclude<
16-
Parameters<typeof webpackPlugin>[0],
17-
'createResolver' | 'postTransform'
18-
>;
16+
export type PigmentCSSConfig = Exclude<Parameters<typeof webpackPlugin>[0], ExcludePluginOptions>;
1917

2018
export default function pigment(
2119
nextConfig: NextConfig,
@@ -101,13 +99,12 @@ export default function pigment(
10199
}),
102100
);
103101

102+
config.ignoreWarnings = config.ignoreWarnings ?? [];
103+
config.ignoreWarnings.push((warning: string) => warning.includes('pigment-virtual'));
104+
104105
if (typeof nextConfig.webpack === 'function') {
105106
return nextConfig.webpack(config, context);
106107
}
107-
config.ignoreWarnings = config.ignoreWarnings ?? [];
108-
config.ignoreWarnings.push({
109-
module: /(pigment-virtual\.css)|(core\/styles\.css)|(react\/styles\.css)/,
110-
});
111108
return config;
112109
};
113110
return {

packages/pigment-css-plugin/src/unplugin.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,31 @@ import { logger as wywLogger } from '@wyw-in-js/shared';
2323
import { AsyncResolver, handleUrlReplacement } from './utils';
2424

2525
type BundlerConfig = Omit<PigmentConfig, 'themeArgs'> & {
26+
/**
27+
* Extra filter to only allow files that satisfy this pattern
28+
*/
2629
include?: FilterPattern;
30+
/**
31+
* Extra filter to not transform files that satisfy this pattern.
32+
*/
2733
exclude?: FilterPattern;
34+
/**
35+
* The Theme object that'll be passed to the callback in the css or styled calls.
36+
*/
2837
theme?: Theme;
38+
/**
39+
* A list of package names that support the runtime implementation of Pigment CSS. This already includes
40+
* `@pigment-css/core`, `@pigment-css/react` and `@pigment-css/react-new`.
41+
*/
2942
runtimePackages?: string[];
30-
corePackages?: string[];
43+
/**
44+
* Extra package names that should always be transformed regardless of the `include` and `exclude` patterns.
45+
*/
46+
transformPackages?: string[];
47+
/**
48+
* If you want to support `sx` prop in your application, set this to true.
49+
* @default false
50+
*/
3151
transformSx?: boolean;
3252
nextJsOptions?: {
3353
dev: boolean;
@@ -37,6 +57,9 @@ type BundlerConfig = Omit<PigmentConfig, 'themeArgs'> & {
3757
};
3858
outputCss?: boolean;
3959
debug?: IFileReporterOptions | false;
60+
/**
61+
* Enable sourceMap for the generated css.
62+
*/
4063
sourceMap?: boolean;
4164
asyncResolve?: AsyncResolver;
4265
createResolver?: (ctx: any, projectPath: string, config?: any) => AsyncResolver;
@@ -141,7 +164,7 @@ export const plugin = createUnplugin<BundlerConfig>((options, meta) => {
141164
const {
142165
outputCss = true,
143166
theme = {},
144-
corePackages = [],
167+
transformPackages = [],
145168
runtimePackages: optRuntimePackages = [],
146169
debug = false,
147170
transformSx = true,
@@ -240,7 +263,7 @@ export const plugin = createUnplugin<BundlerConfig>((options, meta) => {
240263
plugins.push(
241264
getSxBabelUnplugin({
242265
name: `${baseName}/sx`,
243-
finalTransformLibraries: corePackages,
266+
finalTransformLibraries: transformPackages,
244267
filter,
245268
}),
246269
);
@@ -262,7 +285,7 @@ export const plugin = createUnplugin<BundlerConfig>((options, meta) => {
262285
},
263286
},
264287
transformInclude(id) {
265-
return isZeroRuntimeProcessableFile(id.split('?', 1)[0], corePackages) && filter(id);
288+
return isZeroRuntimeProcessableFile(id.split('?', 1)[0], transformPackages) && filter(id);
266289
},
267290
async transform(code, url) {
268291
const [filePath] = url.split('?', 1);

packages/pigment-css-plugin/src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type AsyncResolver = (
66
stack: string[],
77
) => Promise<string | null>;
88

9+
export type ExcludePluginOptions = 'createResolver' | 'postTransform' | 'nextJsOptions';
10+
911
/**
1012
* There might be a better way to do this in future, but due to the async
1113
* nature of the resolver, this is the best way currently to replace url()

packages/pigment-css-plugin/src/vite.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { optimizeDeps } from 'vite';
44
import { syncResolve } from '@wyw-in-js/shared';
55

66
import { plugin } from './unplugin';
7-
import { AsyncResolver } from './utils';
7+
import type { AsyncResolver, ExcludePluginOptions } from './utils';
88

99
export type PigmentCSSConfig = Exclude<
1010
Parameters<(typeof plugin)['vite']>[0],
11-
'createResolver' | 'postTransform'
11+
ExcludePluginOptions
1212
>;
1313

1414
export default function pigment(config: Parameters<(typeof plugin)['vite']>[0]) {

packages/pigment-css-plugin/src/webpack.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import * as path from 'node:path';
22

3-
import { NativeBuildContext } from 'unplugin';
3+
import type { NativeBuildContext } from 'unplugin';
44

55
import { plugin } from './unplugin';
6-
import { AsyncResolver } from './utils';
6+
import type { AsyncResolver, ExcludePluginOptions } from './utils';
77

88
export type PigmentCSSConfig = Exclude<
99
Parameters<(typeof plugin)['webpack']>[0],
10-
'createResolver' | 'postTransform'
10+
ExcludePluginOptions
1111
>;
1212

1313
export default function pigment(config: Parameters<(typeof plugin)['webpack']>[0]) {
1414
function createResolver(ctx: NativeBuildContext, projectPath: string): AsyncResolver {
1515
return async (what, importer) => {
1616
if (ctx.framework !== 'webpack') {
17-
throw new Error('Non-webpack bundlers not supported');
17+
throw new Error(`${process.env.PACKAGE_NAME}: Non-webpack bundlers are not supported`);
1818
}
1919

2020
const context = path.isAbsolute(importer)

0 commit comments

Comments
 (0)