Skip to content

Commit 799a57e

Browse files
Nathan ReidGuillaume Chau
authored andcommitted
enable custom css modules compilers (#132)
* enable custom css modules compilers * remove leftover console.log * add CSS modules usage sample and mention nathantreid:vue-css-modules comunity package. * move package reference to lang section
1 parent b152e97 commit 799a57e

File tree

4 files changed

+77
-17
lines changed

4 files changed

+77
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Currently supported and possible future features (in no particular order) are:
139139
- [x] Easy localization with vue-i18n out-of-the-box integration, auto-detection, server-side injection and key-in-hand ui ![vue](https://img.shields.io/badge/vue-1.x-green.svg)
140140
- [x] Easy state management with vuex integration ![vue](https://img.shields.io/badge/vue-1.x-green.svg)
141141
- [x] Use Blaze templates in your vue app ![vue](https://img.shields.io/badge/vue-1.x-green.svg) ![vue](https://img.shields.io/badge/vue-2.x-brightgreen.svg)
142+
- [x] `module` attribute on `<style>` in .vue files
142143
- [ ] *Typescript official integration in .vue files*
143144
- [ ] *Server-side rendering (Vue 2.x)*
144145
- [ ] *`src` attribute support in `.vue` files*

packages/vue-component/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,32 @@ a {
7272
</style>
7373
```
7474

75+
### CSS Modules
76+
77+
As an alternative to scoped styles, you can use CSS modules to scope your CSS to your components by adding the `module` attribute to any `<style>` tag in your component file and accessing the styles via the `$style` property:
78+
```html
79+
<style module>
80+
/* Will only be applied to this component <a> elements */
81+
.red {
82+
color: red;
83+
}
84+
</style>
85+
86+
<template>
87+
<div :class="$style.red">Red Text</div>
88+
</template>
89+
90+
<script>
91+
export default {
92+
created() {
93+
console.log(this.$style.red);
94+
}
95+
}
96+
</script>
97+
```
98+
99+
Note: composing from other files is not supported by the built-in CSS modules processor. See the community packages.
100+
75101
### Language packages
76102

77103
Using the power of preprocessors, you can use a different language (like less or jade) by adding a `lang` attribute on your `<template>`, `<script>` or `<style>` tags.
@@ -91,6 +117,9 @@ Official packages for `<style>` tag:
91117
- [akryum:vue-stylus](https://github.com/Akryum/meteor-vue-component/tree/master/packages/vue-stylus)
92118

93119
Community packages welcomed (add a your package with a PR)!
120+
Community packages for `<style>` tag:
121+
122+
- [nathantreid:vue-css-modules](https://github.com/nathantreid/vue-css-modules) enables interop with nathantreid:css-modules, including support for composing from other files.
94123

95124
### Manual import
96125

packages/vue-component/plugin/tag-handler.js

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -313,22 +313,53 @@ VueComponentTagHandler = class VueComponentTagHandler {
313313
// CSS Modules
314314
let isAsync = false;
315315
if (styleTag.attribs.module) {
316-
const moduleName = typeof styleTag.attribs.module === 'string' ? styleTag.attribs.module : '';
317-
const scopedModuleName = moduleName ? `_${moduleName}` : '';
318-
plugins.push(postcssModules({
319-
getJSON(cssFilename, json) {
320-
console.log('getjson')
321-
cssModules = { ...(cssModules || {}), ...json };
322-
},
323-
generateScopedName(exportedName, filePath) {
324-
const path = require('path');
325-
let sanitisedPath = path.relative(process.cwd(), filePath).replace(/.*\{}[/\\]/, '').replace(/.*\{.*?}/, 'packages').replace(/\.[^\.\/\\]+$/, '').replace(/[\W_]+/g, '_');
326-
const filename = path.basename(filePath).replace(/\.[^\.\/\\]+$/, '').replace(/[\W_]+/g, '_');
327-
sanitisedPath = sanitisedPath.replace(new RegExp(`_(${filename})$`), '__$1');
328-
return `_${sanitisedPath}__${exportedName}`;
329-
},
330-
}));
331-
isAsync = true;
316+
if (global.vue.cssModules) {
317+
try {
318+
let compile = global.vue.cssModules;
319+
//console.log(`Compiling <style> css modules ${lang}...`);
320+
let result = compile({
321+
source: css,
322+
map: cssMap,
323+
inputFile: this.inputFile,
324+
dependencyManager: this.dependencyManager,
325+
tag: styleTag,
326+
});
327+
// console.log('Css result', result);
328+
css = result.css;
329+
cssMap = result.map;
330+
if (result.cssModules) {
331+
cssModules = { ...(cssModules || {}), ...result.cssModules };
332+
}
333+
if (result.js) {
334+
js += result.js;
335+
}
336+
} catch (e) {
337+
throwCompileError({
338+
inputFile: this.inputFile,
339+
tag: 'style',
340+
charIndex: styleTag.tagStartIndex,
341+
action: 'compiling css modules',
342+
error: e,
343+
showError: true
344+
});
345+
}
346+
} else {
347+
const moduleName = typeof styleTag.attribs.module === 'string' ? styleTag.attribs.module : '';
348+
const scopedModuleName = moduleName ? `_${moduleName}` : '';
349+
plugins.push(postcssModules({
350+
getJSON(cssFilename, json) {
351+
cssModules = { ...(cssModules || {}), ...json };
352+
},
353+
generateScopedName(exportedName, filePath) {
354+
const path = require('path');
355+
let sanitisedPath = path.relative(process.cwd(), filePath).replace(/.*\{}[/\\]/, '').replace(/.*\{.*?}/, 'packages').replace(/\.[^\.\/\\]+$/, '').replace(/[\W_]+/g, '_');
356+
const filename = path.basename(filePath).replace(/\.[^\.\/\\]+$/, '').replace(/[\W_]+/g, '_');
357+
sanitisedPath = sanitisedPath.replace(new RegExp(`_(${filename})$`), '__$1');
358+
return `_${sanitisedPath}__${exportedName}`;
359+
},
360+
}));
361+
isAsync = true;
362+
}
332363
}
333364

334365
// Autoprefixer

packages/vue-component/plugin/vue-compiler.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,6 @@ function generateJs (vueId, inputFile, compileResult, isHotReload = false) {
582582

583583
// CSS Modules
584584
if(compileResult.cssModules) {
585-
console.log('adding css modules', JSON.stringify(compileResult.cssModules))
586585
const modulesCode = '__vue_options__.computed = __vue_options__.computed || {};\n' +
587586
`__vue_options__.computed.$style = function() {\n return ${JSON.stringify(compileResult.cssModules)}\n};\n`;
588587
js += modulesCode;

0 commit comments

Comments
 (0)