-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: workaround for css-loader path issue
- Loading branch information
1 parent
6a28f01
commit 7d218f5
Showing
7 changed files
with
94 additions
and
11 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
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,62 @@ | ||
const { dirname } = require('path'); | ||
const { create } = require('enhanced-resolve'); | ||
const postcssModulesExtractImports = require('postcss-modules-extract-imports'); | ||
|
||
const resolve = create.sync({ | ||
extensions: ['.css'], | ||
mainFields: ['style'], | ||
symlinks: false, | ||
}); | ||
const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)'|(global))$/; | ||
|
||
const plugin = postcssModulesExtractImports(); | ||
|
||
(function patchModules() { | ||
module.children | ||
.filter(mod => mod.id.includes('postcss-modules-extract-imports')) | ||
.forEach(mod => { | ||
// XXX: The following line mutates exports of a CommonJS module | ||
mod.exports = postcssModulesExtractImportsPatched; | ||
}); | ||
})(); | ||
|
||
function postcssModulesExtractImportsPatched() { | ||
return css => { | ||
const inputPath = dirname(css.source.input.file); | ||
// XXX: The "patchDeclarationValue" mutates the CSS AST | ||
findComposesDeclarations(css).forEach(patchDeclarationValue(inputPath)); | ||
return plugin(css); | ||
}; | ||
} | ||
|
||
function findComposesDeclarations(css) { | ||
return css.nodes | ||
.filter(node => node.type === 'rule') | ||
.map(rule => rule.nodes) | ||
.flat() | ||
.filter(node => node.type === 'decl' && node.prop === 'composes'); | ||
} | ||
|
||
const patchDeclarationValue = inputPath => declaration => { | ||
const matches = declaration.value.match(matchImports); | ||
if (matches) { | ||
const [, symbols, doubleQuotePath, singleQuotePath, global] = matches; | ||
|
||
if (!global) { | ||
const importPath = doubleQuotePath || singleQuotePath; | ||
|
||
if (!isRelative(inputPath, importPath)) { | ||
declaration.value = `${symbols} from '~${importPath}'`; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
function isRelative(inputPath, importPath) { | ||
try { | ||
resolve(inputPath, `./${importPath}`); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} |
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
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