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

feat: add option.base #164

Merged
merged 6 commits into from
Apr 30, 2017
Merged
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,39 @@ require('style-loader/url?{attrs:{prop: "value"}}!file-loader!style.css')
// will create link tag <link rel="stylesheet" type="text/css" href="[path]/style.css" prop="value">
```

#### `base`
This setting is primarily used as a workaround for [css clashes](https://github.com/webpack-contrib/style-loader/issues/163) when using one or more [DllPlugin](https://robertknight.github.io/posts/webpack-dll-plugins/)'s. `base` allows you to prevent either the *app*'s css (or *DllPlugin2*'s css) from overwriting *DllPlugin1*'s css by specifying a css module id base which is greater than the range used by *DllPlugin1* e.g.:
* webpack.dll1.config.js
```
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
}
```
* webpack.dll2.config.js
```
{
test: /\.css$/,
use: [
{ loader: 'style-loader', options: { base: 1000 } },
'css-loader'
]
}
```
* webpack.app.config.js
```
{
test: /\.css$/,
use: [
{ loader: 'style-loader', options: { base: 2000 } },
'css-loader'
]
}
```

### Recommended configuration

By convention the reference-counted API should be bound to `.useable.css` and the simple API to `.css` (similar to other file types, i.e. `.useable.less` and `.less`).
Expand Down
8 changes: 4 additions & 4 deletions addStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = function(list, options) {
// By default, add <style> tags to the bottom of the target
if (typeof options.insertAt === "undefined") options.insertAt = "bottom";

var styles = listToStyles(list);
var styles = listToStyles(list, options);
addStylesToDom(styles, options);

return function update(newList) {
Expand All @@ -64,7 +64,7 @@ module.exports = function(list, options) {
mayRemove.push(domStyle);
}
if(newList) {
var newStyles = listToStyles(newList);
var newStyles = listToStyles(newList, options);
addStylesToDom(newStyles, options);
}
for(var i = 0; i < mayRemove.length; i++) {
Expand Down Expand Up @@ -100,12 +100,12 @@ function addStylesToDom(styles, options) {
}
}

function listToStyles(list) {
function listToStyles(list, options) {
var styles = [];
var newStyles = {};
for(var i = 0; i < list.length; i++) {
var item = list[i];
var id = item[0];
var id = options.base ? item[0] + options.base : item[0];
var css = item[1];
var media = item[2];
var sourceMap = item[3];
Expand Down