-
-
Notifications
You must be signed in to change notification settings - Fork 499
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
Showing
7 changed files
with
227 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,141 @@ | ||
import type { RuleSetRule, Configuration as WebpackConfig } from 'webpack'; | ||
import type { | ||
AssetModule, | ||
AssetModuleMatcher, | ||
AssetModuleType, | ||
} from '../types/asset-modules'; | ||
|
||
export function assetModuleByName(assetModuleName: AssetModuleType) { | ||
return (rule: RuleSetRule) => rule.type === assetModuleName; | ||
} | ||
|
||
const toMatchingAssetModule = ( | ||
rule: RuleSetRule, | ||
index: number | ||
): AssetModule => ({ | ||
rule, | ||
index, | ||
}); | ||
|
||
export function getAssetModule( | ||
webpackConfig: WebpackConfig, | ||
matcher: AssetModuleMatcher | ||
) { | ||
let matchingAssetModule: AssetModule | undefined; | ||
(webpackConfig.module?.rules as RuleSetRule[])?.some((rule, index) => { | ||
if (matcher(rule)) { | ||
matchingAssetModule = toMatchingAssetModule(rule, index); | ||
} | ||
return matchingAssetModule !== undefined; | ||
}); | ||
return { | ||
isFound: matchingAssetModule !== undefined, | ||
match: matchingAssetModule, | ||
}; | ||
} | ||
|
||
export function getAssetModules( | ||
webpackConfig: WebpackConfig, | ||
matcher: AssetModuleMatcher | ||
) { | ||
const matchingAssetModules: AssetModule[] = []; | ||
(webpackConfig.module?.rules as RuleSetRule[])?.forEach((rule, index) => { | ||
if (matcher(rule)) { | ||
matchingAssetModules.push(toMatchingAssetModule(rule, index)); | ||
} | ||
}); | ||
|
||
return { | ||
hasFoundAny: matchingAssetModules.length !== 0, | ||
matches: matchingAssetModules, | ||
}; | ||
} | ||
|
||
export function removeAssetModules( | ||
webpackConfig: WebpackConfig, | ||
matcher: AssetModuleMatcher | ||
) { | ||
const toRemove: number[] = []; | ||
(webpackConfig.module?.rules as RuleSetRule[])?.forEach((rule, index) => { | ||
if (matcher(rule)) { | ||
toRemove.push(index); | ||
} | ||
}); | ||
|
||
toRemove.forEach((index) => { | ||
webpackConfig.module?.rules?.splice(index, 1); | ||
}); | ||
|
||
return { | ||
rules: webpackConfig.module?.rules, | ||
removedCount: toRemove.length, | ||
}; | ||
} | ||
|
||
function addAssetModule( | ||
webpackConfig: WebpackConfig, | ||
matcher: AssetModuleMatcher, | ||
newAssetModule: RuleSetRule, | ||
positionAdapter: (index: number) => number | ||
) { | ||
const { match } = getAssetModule(webpackConfig, matcher); | ||
|
||
if (match !== undefined) { | ||
webpackConfig.module?.rules?.splice( | ||
positionAdapter(match.index), | ||
0, | ||
newAssetModule | ||
); | ||
|
||
return { isAdded: true }; | ||
} | ||
|
||
return { isAdded: false }; | ||
} | ||
|
||
export const addBeforeAssetModule = ( | ||
webpackConfig: WebpackConfig, | ||
matcher: AssetModuleMatcher, | ||
newAssetModule: RuleSetRule | ||
) => addAssetModule(webpackConfig, matcher, newAssetModule, (x) => x); | ||
|
||
export const addAfterAssetModule = ( | ||
webpackConfig: WebpackConfig, | ||
matcher: AssetModuleMatcher, | ||
newAssetModule: RuleSetRule | ||
) => addAssetModule(webpackConfig, matcher, newAssetModule, (x) => x + 1); | ||
|
||
function addAssetModules( | ||
webpackConfig: WebpackConfig, | ||
matcher: AssetModuleMatcher, | ||
newLoader: RuleSetRule, | ||
positionAdapter: (index: number) => number | ||
) { | ||
const { matches } = getAssetModules(webpackConfig, matcher); | ||
|
||
if (matches.length !== 0) { | ||
matches.forEach((match) => { | ||
webpackConfig.module?.rules?.splice( | ||
positionAdapter(match.index), | ||
0, | ||
newLoader | ||
); | ||
}); | ||
|
||
return { isAdded: true, addedCount: matches.length }; | ||
} | ||
|
||
return { isAdded: false, addedCount: 0 }; | ||
} | ||
|
||
export const addBeforeAssetModules = ( | ||
webpackConfig: WebpackConfig, | ||
matcher: AssetModuleMatcher, | ||
newAssetModule: RuleSetRule | ||
) => addAssetModules(webpackConfig, matcher, newAssetModule, (x) => x); | ||
|
||
export const addAfterAssetModules = ( | ||
webpackConfig: WebpackConfig, | ||
matcher: AssetModuleMatcher, | ||
newAssetModule: RuleSetRule | ||
) => addAssetModules(webpackConfig, matcher, newAssetModule, (x) => x + 1); |
Oops, something went wrong.