Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom styleModule #171

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {OnLoadResult} from 'esbuild'
import {StringOptions} from 'sass-embedded'
import {sassPlugin} from './plugin'
import {CustomStyleModule} from './utils'

export type Type = 'css' | 'style' | 'css-text' | 'lit-css'

Expand All @@ -11,6 +12,11 @@ export type SassPluginOptions = StringOptions<'async'> & {
*/
filter?: RegExp

/**
* Custom style module to use when @param type is "style"
*/
customStyleModule?: CustomStyleModule;

/**
* Function to transform import path. Not just paths by @import
* directive, but also paths imported by ts code.
Expand Down
4 changes: 2 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin {
cssChunks[name] = contents
contents = `import '${name}';`
} else if (type === 'style') {
contents = makeModule(String(contents), 'style', nonce)
contents = makeModule(String(contents), 'style', options.customStyleModule, nonce);
} else {
return {
errors: [{text: `unsupported type '${type}' for postCSS modules`}]
Expand All @@ -130,7 +130,7 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin {
warnings,
watchFiles
} : {
contents: makeModule(cssText, type, nonce),
contents: makeModule(cssText, type, options.customStyleModule, nonce),
loader: 'js',
resolveDir,
warnings,
Expand Down
12 changes: 7 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,18 @@ document.head
export {css};
`

export function makeModule(contents: string, type: Type, nonce?: string) {
export type CustomStyleModule = (cssText: string, nonce?: string) => string;

export function makeModule(contents: string, type: Type, customStyleModule?: CustomStyleModule, nonce?: string) {
switch (type) {
case 'style':
return styleModule(contents, nonce)
return customStyleModule ? customStyleModule(contents, nonce) : styleModule(contents, nonce);
case 'lit-css':
return cssResultModule(contents)
return cssResultModule(contents);
case 'css-text':
return cssTextModule(contents)
return cssTextModule(contents);
default:
return contents
return contents;
}
}

Expand Down
Loading