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

Commit

Permalink
fix: remove tilde character from import statements
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
digitalsadhu committed May 11, 2018
1 parent fb4de48 commit f52386d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ const readPkgUp = require('read-pkg-up');
const postcss = require('postcss');
const atImport = require('postcss-import');

const removeTilde = postcss.plugin(
'postcss-import-remove-tilde',
() =>
function(css) {
css.walkAtRules('import', rule => {
rule.params = rule.params.replace(
/((?:url\s*\(\s*)?['"])~/,
'$1'
);
});
}
);

module.exports.identifyCssModule = async function identifyCssModule(filePath) {
const { pkg: { name, version }, path: packagePath } = await readPkgUp({
normalize: false,
Expand All @@ -21,6 +34,7 @@ module.exports.identifyCssModule = async function identifyCssModule(filePath) {
module.exports.bundleCssModule = async function bundleCssModule(filePath) {
const fileContents = await readFile(filePath, 'utf8');
const { css } = await postcss()
.use(removeTilde())
.use(atImport())
.process(fileContents, { from: filePath });
return css;
Expand Down
7 changes: 5 additions & 2 deletions test/test-assets/my-module-3/css/main.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* my-module-3/main.css */

@import url('./dep.css');
@import url('dep/main.css');
@import url("./dep.css");
@import url("dep/main.css");
@import url("~@dep/main.css");
@import "dep/secondary.css";
@import "~dep/secondary.css";

0 comments on commit f52386d

Please sign in to comment.