Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit f52386d

Browse files
committed
fix: remove tilde character from import statements
This handles Webpack's convention of using a ~ character to indicate modules in node_modules folder. PostCss handles node_modules without such a character and in fact breaks if such a character is present.
1 parent fb4de48 commit f52386d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ const readPkgUp = require('read-pkg-up');
88
const postcss = require('postcss');
99
const atImport = require('postcss-import');
1010

11+
const removeTilde = postcss.plugin(
12+
'postcss-import-remove-tilde',
13+
() =>
14+
function(css) {
15+
css.walkAtRules('import', rule => {
16+
rule.params = rule.params.replace(
17+
/((?:url\s*\(\s*)?['"])~/,
18+
'$1'
19+
);
20+
});
21+
}
22+
);
23+
1124
module.exports.identifyCssModule = async function identifyCssModule(filePath) {
1225
const { pkg: { name, version }, path: packagePath } = await readPkgUp({
1326
normalize: false,
@@ -21,6 +34,7 @@ module.exports.identifyCssModule = async function identifyCssModule(filePath) {
2134
module.exports.bundleCssModule = async function bundleCssModule(filePath) {
2235
const fileContents = await readFile(filePath, 'utf8');
2336
const { css } = await postcss()
37+
.use(removeTilde())
2438
.use(atImport())
2539
.process(fileContents, { from: filePath });
2640
return css;
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/* my-module-3/main.css */
22

3-
@import url('./dep.css');
4-
@import url('dep/main.css');
3+
@import url("./dep.css");
4+
@import url("dep/main.css");
5+
@import url("~@dep/main.css");
6+
@import "dep/secondary.css";
7+
@import "~dep/secondary.css";

0 commit comments

Comments
 (0)