Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #500 from Shopify/add-css-concat-dependencies
Browse files Browse the repository at this point in the history
Add @import file as dependencies in concat-css-loader
  • Loading branch information
t-kelly authored Apr 18, 2018
2 parents 38a222d + 52f7491 commit c88e79f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions packages/concat-style-loader/concat.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs');
const path = require('path');

function concatStyles(content, rootPath) {
const assets = parseImports(content, rootPath).reverse();
function concatStyles(content, rootPath, loader) {
const assets = parseImports(content, rootPath, loader).reverse();

assets.forEach((asset) => {
asset.content = concatStyles(asset.content, asset.path);
Expand All @@ -19,25 +19,27 @@ function inlineAsset(content, asset) {
return content.slice(0, startIndex) + asset.content + content.slice(endIndex);
}

function parseImports(content, rootPath) {
function parseImports(content, rootPath, loader) {
const matches = getImportStatements(content);
return matches.map((match) => {
const url = getImportURL(match[0]);
const assetPath = path.resolve(path.dirname(rootPath), url);

const content = fetchAssetContent(assetPath);
const content = fetchAssetContent(assetPath, loader);

return {path: assetPath, content, match};
});
}

function fetchAssetContent(assetPath) {
function fetchAssetContent(assetPath, loader) {
if (!fs.existsSync(assetPath)) {
throw new Error(
`Concat Style Loader Error: Cannot find asset '${assetPath}'`,
);
}

loader && loader.addDependency(assetPath);

return fs.readFileSync(assetPath, 'utf8');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/concat-style-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function(content) {
const options = getOptions(this);
const rootPath = this.resourcePath;

content = concatStyles(content, rootPath);
content = concatStyles(content, rootPath, this);

return `module.exports = ${JSON.stringify(content)}`;
};

0 comments on commit c88e79f

Please sign in to comment.