Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jul 28, 2017
0 parents commit 9c31782
Show file tree
Hide file tree
Showing 8 changed files with 906 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// http://eslint.org/docs/user-guide/configuring

module.exports = {
root: true,
parserOptions: {
sourceType: 'module'
},
env: {
browser: true,
jasmine: true,
},
extends: 'airbnb-base',
// required to lint *.vue files
plugins: [
'jest',
],
globals: {
jest: true,
test: true,
},
// add your custom rules here
rules: {
// 4 space indent
'indent': [ 'error', 4 ],
// Don't enforce a blank line or not at the beginning of a block
'padded-blocks': 0,
// Don't enforce one-var for now
'one-var': 0,
// Require spaces in array brackets, unless it's an array of objects
'array-bracket-spacing': [ 'error', 'always', { 'objectsInArrays': false } ],
// Allow unary + and -- operators
'no-plusplus': 0,
// don't require .vue extension when importing
'import/extensions': ['error', 'always', {
'js': 'never',
'vue': 'never'
}],
// allow optionalDependencies
'import/no-extraneous-dependencies': ['error', {
'optionalDependencies': ['test/unit/index.js']
}],
'no-console': 0,
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Matt Brophy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
234 changes: 234 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
# vue-themed-style-loader

A Webpack plugin to be used in conjunction with [vue-loader](https://github.com/vuejs/vue-loader/) to assist in generating themed builds of a [Vue.js](https://vuejs.org/) application.


## Usage

To use the `vue-themed-style-loader`, simply install the theme:

```
npm install --save-dev vue-themed-style-loader
```

And then add an entry to your webpack configuration file, after the `vue-loader`:

```js
...
module: {
rules: [{
test: /\.vue$/,
loader: 'vue-loader',
options: { ... },
}, {
test: /\.vue$/,
loader: 'vue-themed-style-loader',
options: {
theme: 'your-theme-name',
},
}]
},
...
```

And then begin specifying themes in your Vue component styles:

```vue
// Base theme
<style>
.classname {
color: black;
}
</style>
// Bold theme
<style theme="bold">
.heading {
font-weight: bold;
}
</style>
// Underline theme
<style theme="underline">
.heading {
text-decoration: underline;
}
</style>
```


## Use Case

Consider this simple Vue Single File Component that renders and styles a dynamic `<h1>` tag:

```vue
<template>
<h1 class="heading">{{title}}</h1>
</template>
<script>
export default {
name: 'heading-h1',
props: [ 'title' ],
};
</script>
<style>
.heading { color: black; }
</style>
```

### Themed display

Considering applying different styling "themes" which will alter the color of the heading, which may normally be done via a parent CSS class:

```
<style>
.heading { color: black; }
.theme-red .heading { color: red; }
.theme-blue .heading { color: blue }
</style>
```

This will certainly work, however, it doesn't scale very well as your application and number of themes grows. The size of you stylesheet grows at least linearly with the number of themes, even though only one theme is likely being used at any given point in time.

Instead, it would be ideal for our resulting stylesheet to only include the styles relevant to our current theme:

```css
/* styles.css */
.heading { color: black; }

/* styles-red.css */
.heading { color: black; }
.theme-red .heading { color: red; }

/* styles-blue.css */
.heading { color: black; }
.theme-blue .heading { color: blue; }
```

Or, even better, in the cases where a theme completely overrides a base style, it would be ideal to remove the base style altogether:

```css
/* styles.css */
.heading { color: black; }

/* styles-red.css */
.theme-red .heading { color: red; }

/* styles-blue.css */
.theme-blue .heading { color: blue; }
```

And, now that the base styles aren't being included, we no longer need the parent theme class anymore, and can reduce our output themed stylesheets to simply:

```css
/* styles.css */
.heading { color: black; }

/* styles-red.css */
.heading { color: red; }

/* styles-blue.css */
.heading { color: blue; }
```

This is exactly what `vue-themed-style-loader` set's out to do :)


## Example

Let's alter the `<style>` sections of our component to use the `vue-themed-style-loader` to generate the proper themed output:

```vue
// Base, unthemed styles
<style>
.heading { color: black; }
</style>
// "red" theme
<style theme="red">
.heading { color: red; }
</style>
// "red" theme
<style theme="blue">
.heading { color: blue; }
</style>
```

Now, add the loader to your webpack config. It is important to note that because all webpack loaders are run from right-to-left (see (Pitching Loaders)[https://webpack.js.org/api/loaders/#pitching-loader]), the `vue-themed-style-loader` must be specified _after_ the `vue-loader`. this ensures it will execute _before_ the `vue-loader` to discard inactive themed style sections.

Here's an example `webpack.config.js`:

```js
...
module: {
rules: [{
test: /\.vue$/,
loader: 'vue-loader',
options: { ... },
}, {
test: /\.vue$/,
loader: 'vue-themed-style-loader',
options: {
theme: 'red',
},
}]
},
...
```

In this setup, with the `"red"` theme specified, the loader will only preserve unthemed and `theme="red"` `<style>` sections in your component, and will remove the `theme="blue"` section.

### Replacing

In cases where a given theme section wants to completely replace the base styles, the `replace` attribute can be specified on the `<style>` block:

```
<style>
.heading { color: black; }
</style>
<style theme="red" replace>
.heading { color: red; }
</style>
<style theme="blue">
.heading { color: blue; }
</style>
```

This will result in the base styles also being stripped, and _only_ the `theme="red"` styles being included in the output. If a single `replace` section is found for the active theme, then _all_ corresponding base styles will be stripped

### Scoped styles

The removal algorithm operates independently on normal and scoped style blocks. So, it can be chosen to replace in one scenario and inherit in another. For example:

```
<style>
.heading { color: black; }
</style>
<style scoped>
.heading { font-weight: bold; }
</style>
<style theme="red" replace>
.heading { color: red; }
</style>
<style scoped theme="red">
.heading { text-decoration: underline; }
</style>
```

In this scenario, the scoped base style would be maintained because no scoped sections for the active theme specified the `replace` attribute.

```css
.heading { font-weight: bold; }
.heading { color: red; }
.heading { text-decoration: underline; }
```
8 changes: 8 additions & 0 deletions __mocks__/loader-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const loaderUtils = jest.genMockFromModule('loader-utils');

loaderUtils.getOptions = jest.fn(() => ({
theme: null,
debug: false,
}));

module.exports = loaderUtils;
Loading

0 comments on commit 9c31782

Please sign in to comment.