This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shaun Lloyd
committed
Apr 24, 2023
1 parent
759f55b
commit 4c042fb
Showing
6 changed files
with
121 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import type { RuleSetRule, Configuration as WebpackConfig } from "webpack"; | ||
import type { AddonStylingOptions } from "../types"; | ||
|
||
const isRuleForLESS = (rule: RuleSetRule) => | ||
typeof rule !== "string" && | ||
rule.test instanceof RegExp && | ||
rule.test.test("test.less"); | ||
|
||
const buildStyleLoader = (options: AddonStylingOptions) => ({ | ||
loader: require.resolve("style-loader"), | ||
}); | ||
|
||
const buildCssLoader = ({ cssModules, postCss }: AddonStylingOptions) => { | ||
const importSettings = { importLoaders: postCss ? 2 : 1 }; | ||
const moduleSettings = cssModules ? { modules: { auto: true } } : {}; | ||
|
||
return { | ||
loader: require.resolve("css-loader"), | ||
options: { | ||
...importSettings, | ||
...moduleSettings, | ||
}, | ||
}; | ||
}; | ||
|
||
const buildPostCssLoader = ({ postCss }: AddonStylingOptions) => { | ||
const implementationOptions = | ||
typeof postCss === "object" ? { ...postCss } : {}; | ||
|
||
return { | ||
loader: require.resolve("postcss-loader"), | ||
options: { | ||
...implementationOptions, | ||
}, | ||
}; | ||
}; | ||
|
||
const buildLessLoader = ({ less }: AddonStylingOptions) => { | ||
const implementationOptions = | ||
typeof less === "object" && less.hasOwnProperty("implementation") | ||
? { implementation: less.implementation } | ||
: {}; | ||
|
||
const additionalData = | ||
typeof less === "object" && less.hasOwnProperty("additionalData") | ||
? { additionalData: less.additionalData } | ||
: {}; | ||
|
||
return { | ||
loader: require.resolve("less-loader"), | ||
options: { | ||
sourceMap: true, | ||
lessOptions: less.lessOptions ?? {}, | ||
...implementationOptions, | ||
...additionalData, | ||
}, | ||
}; | ||
}; | ||
|
||
const LESS_FILE_REGEX = /\.less$/i; | ||
const buildLessRule = (options: AddonStylingOptions): RuleSetRule => { | ||
if (options.scssBuildRule) return options.scssBuildRule; | ||
|
||
const buildRule = [ | ||
buildStyleLoader(options), | ||
buildCssLoader(options), | ||
...(options.postCss ? [buildPostCssLoader(options)] : []), | ||
buildLessLoader(options), | ||
]; | ||
return { | ||
test: LESS_FILE_REGEX, | ||
use: buildRule, | ||
sideEffects: true, | ||
}; | ||
}; | ||
|
||
export const patchOrAddLessRule = ( | ||
config: WebpackConfig, | ||
options: AddonStylingOptions | ||
): void => { | ||
// If the user doesn't want to patch webpack for postcss or css modules | ||
if (!options.less && !options.lessBuildRule) { | ||
// return without adjusting config | ||
return; | ||
} | ||
|
||
const rules = config.module?.rules; | ||
|
||
const rule = buildLessRule(options); | ||
const ruleIndex = rules?.findIndex(isRuleForLESS); | ||
|
||
if (ruleIndex === -1) { | ||
// If no existing css rule, add it | ||
rules?.push(rule); | ||
} else { | ||
// If existing css rule, replace it | ||
rules[ruleIndex] = rule; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
import type { RuleSetRule } from "webpack"; | ||
|
||
interface LessConfig { | ||
lessOptions?: Record<string, any>; | ||
implementation?: string; | ||
additionalData?: unknown; | ||
} | ||
|
||
export interface AddonStylingOptions { | ||
cssBuildRule?: RuleSetRule; | ||
cssModules?: boolean; | ||
less?: LessConfig; | ||
lessBuildRule?: RuleSetRule; | ||
postCss?: boolean | object; | ||
sass?: boolean | object; | ||
cssModules?: boolean; | ||
cssBuildRule?: RuleSetRule; | ||
scssBuildRule?: RuleSetRule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters