Skip to content

Commit f25545a

Browse files
committed
fix(pitcher): change logic to avoid shuffling loader order
fixes #111
1 parent 9be9d1b commit f25545a

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/loaders/windicss-style-pitcher.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type webpack from 'webpack'
22

33
type LoaderTest = (l: { path: string; ident?: string }) => boolean
44

5-
const templatePitcherTest: LoaderTest = l => /(\/|\\|@)windicss-template/.test(l.path)
6-
const windiPitcherTest: LoaderTest = l => /(\/|\\|@)windicss-style-pitcher/.test(l.path)
5+
const windiTemplateTest: LoaderTest = l => /(\/|\\|@)windicss-template/.test(l.path)
6+
const windiStylePitcherTest: LoaderTest = l => /(\/|\\|@)windicss-style-pitcher/.test(l.path)
77
const postCssLoaderTest: LoaderTest = l => /(\/|\\|@)postcss-loader/.test(l.path)
88
const cssLoaderTest: LoaderTest = l => /(\/|\\|@)css-loader/.test(l.path)
99

@@ -13,27 +13,32 @@ const cssLoaderTest: LoaderTest = l => /(\/|\\|@)css-loader/.test(l.path)
1313
* We move it just after the PostCSS loader
1414
*/
1515
export const pitch = function(this: webpack.loader.LoaderContext, remainingRequest: string) {
16-
const findLoaderIndex = (test: LoaderTest) => this.loaders.findIndex(test)
17-
/**
18-
* Removes a loader recursively from the request and returns a found instance
19-
* @param test
20-
*/
21-
const removeLoader: (test: LoaderTest) => any = (test) => {
16+
const findLoaderIndex = (test: LoaderTest) => this.loaders.findIndex((loader) => {
17+
return test(loader) && !loader.normalExecuted
18+
})
19+
20+
const markLoaderAsExecuted: (test: LoaderTest) => any = (test) => {
2221
let index, loader
2322
while ((index = findLoaderIndex(test)) !== -1) {
2423
loader = this.loaders[index]
25-
this.loaders.splice(index, 1)
24+
/*
25+
* Hacky solution to avoid the loader from running.
26+
* Previously we removed the loader entirely however, this caused
27+
* a conflict with other loaders (see https://github.com/windicss/windicss-webpack-plugin/issues/111).
28+
*/
29+
loader.pitchExecuted = true
30+
loader.normalExecuted = true
2631
}
2732
return loader
2833
}
2934

3035
// remove the pitcher immediately
31-
removeLoader(windiPitcherTest)
36+
markLoaderAsExecuted(windiStylePitcherTest)
3237

3338
// make sure we're dealing with style-loader
3439
if (!remainingRequest.includes('&type=style')) {
3540
// clean up
36-
removeLoader(templatePitcherTest)
41+
markLoaderAsExecuted(windiTemplateTest)
3742
return
3843
}
3944

@@ -45,11 +50,11 @@ export const pitch = function(this: webpack.loader.LoaderContext, remainingReque
4550
// we couldn't find either PostCSS loader or CSS loader so we bail out
4651
if (newTemplateLoaderIndex === -1) {
4752
// clean up
48-
removeLoader(templatePitcherTest)
53+
markLoaderAsExecuted(windiTemplateTest)
4954
return
5055
}
5156

52-
const templateLoader = removeLoader(templatePitcherTest)
57+
const templateLoader = markLoaderAsExecuted(windiTemplateTest)
5358
// re-insert the template-loader in the right spot
5459
if (templateLoader)
5560
this.loaders.splice(newTemplateLoaderIndex + 1, 0, templateLoader)

0 commit comments

Comments
 (0)