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

Manage the path through a function given as option #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,43 @@ groups: {
}
```

### pathIncludes

This option let's you define what the path of the file in question needs to contain in order to be included in the process. The option is defined as an `Array` and the contents are of the type `String`. So an example could be:
```javascript
pathIncludes: ['header-site', 'footer-site']

// Path: /Users/[user]/project/header-site/index.css -> true
// Path: /Users/[user]/project/post-list/index.css -> false
// Path: /Users/[user]/project/subComponents/footer-site/index.css -> true
```

### outputFileName

If emitted, the plugin will automatically generate the filename for each CSS that is extracted. If defined, as a `Function`, you gain control of the output filename for each extracted CSS, where the `Function` returns the name for the given file as a `String`.

The function receives one parameter in the form of an `Object`, containing both the options you provide to the plugin, and the following properties:

| name | type |
| ---------- | ------- |
| groups | Object |
| filename | String |
| basename | String |
| path | String |
| queryname | String |
| groupname | String |

The option can be used like this:
```javascript
outputFileName: ({ path, queryname }) => {
// Path: /Users/[user]/project/component-name/index.vue
// Queryname: desktop
const pathParts = path.split('/');
return `${pathParts[pathParts.length - 2]}-${queryname}`;
},

```

## Other Webpack Plugins

This plugin plays together well with the following other webpack plugins.
Expand Down
2 changes: 2 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module.exports = function(source) {
isIncluded = true;
} else if (options.include instanceof RegExp && options.basename.match(options.include)) {
isIncluded = true;
} else if (options.pathIncludes instanceof Array && options.pathIncludes.find(inc => options.path.indexOf(inc) > -1)) {
isIncluded = true;
} else if (options.include === true) {
isIncluded = true;
}
Expand Down
8 changes: 5 additions & 3 deletions src/postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = postcss.plugin('MediaQueryPostCSS', options => {

const css = postcss.root().append(atRule).toString();
const query = atRule.params;

store.addMedia(name, css, options.path, query);
}

Expand Down Expand Up @@ -54,8 +54,10 @@ module.exports = postcss.plugin('MediaQueryPostCSS', options => {

if (queryname) {
const groupName = getGroupName(options.basename);
const name = groupName ? `${groupName}-${queryname}` : `${options.basename}-${queryname}`;

const name = options.outputFileName instanceof Function
? options.outputFileName({ ...options, queryname, groupName })
: groupName ? `${groupName}-${queryname}` : `${options.basename}-${queryname}`;

addToStore(name, atRule);
atRule.remove();
}
Expand Down