Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for multiple css entry files #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 44 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ module.exports = (opts) => {

const media = {};

function addMedia(key, css, query) {
if (!Array.isArray(media[key])) {
media[key] = [];
function addMedia(name, key, css, query) {
if (!Array.isArray(media?.[name]?.[key])) {
media[name] = {
...media[name],
[key]: new Array(),
};
}
media[key].push({ css, query });
media[name][key].push({ css, query });
}

function getMedia(key) {
const css = media[key].map((data) => data.css).join('\n');
const query = media[key][0].query;

return { css, query };

function getMedia(name, key) {
if (media?.[name]?.[key]?.length) {
const css = media[name][key].map((data) => data.css).join("\n");
const query = media[name][key][0].query;

return { css, query };
} else {
return {};
}
}

return {
Expand All @@ -66,7 +73,7 @@ module.exports = (opts) => {
if (queryname) {
const css = postcss.root().append(atRule).toString();

addMedia(queryname, css, query);
addMedia(name, queryname, css, query);
atRule.remove();
}
});
Expand All @@ -77,31 +84,32 @@ module.exports = (opts) => {
// gather promises only if output.path specified because otherwise
// nothing has been extracted
if (opts.output.path) {
Object.keys(media).forEach((queryname) => {
promises.push(
new Promise((resolve) => {
let { css } = getMedia(queryname);
const newFile = opts.output.name
.replace(/\[name\]/g, name)
.replace(/\[query\]/g, queryname)
.replace(/\[ext\]/g, ext);
const newFilePath = path.join(opts.output.path, newFile);
const newFileDir = path.dirname(newFilePath);

plugins.applyPlugins(css, newFilePath).then((css) => {
if (!fs.existsSync(path.dirname(newFilePath))) {
// make sure we can write
fs.mkdirSync(newFileDir, { recursive: true });
}
fs.writeFileSync(newFilePath, css);

if (opts.stats === true) {
console.log(green('[extracted media query]'), newFile);
}
resolve();
});
})
);
Object.entries(media).forEach(([name, value]) => {
Object.keys(value).forEach((queryname) => {
promises.push(
new Promise((resolve) => {
let { css } = getMedia(name, queryname);
const newFile = opts.output.name
.replace(/\[name\]/g, name)
.replace(/\[query\]/g, queryname)
.replace(/\[ext\]/g, ext);
const newFilePath = path.join(opts.output.path, newFile);
const newFileDir = path.dirname(newFilePath);
plugins.applyPlugins(css, newFilePath).then((css) => {
if (!fs.existsSync(path.dirname(newFilePath))) {
// make sure we can write
fs.mkdirSync(newFileDir, { recursive: true });
}
fs.writeFileSync(newFilePath, css);

if (opts.stats === true) {
console.log(green('[extracted media query]'), newFile);
}
resolve();
});
}),
);
});
});
}

Expand Down