Skip to content
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
20 changes: 20 additions & 0 deletions packages/vue-component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ As an alternative to scoped styles, you can use CSS modules to scope your CSS to
</script>
```

By default, your styles will be assigned to the `$style` computed property. You can customize this by setting the module attribute. This also allows you to create multiple "modules" in one component file:

```html
<style module="foo">
.color {
color: orange;
}
</style>
<style module="bar">
.color {
color: purple;
}
</style>

<template>
<div :class="foo.color">Foo Text</div>
<div :class="bar.color">Bar Text</div>
</template>
```

Note: composing from other files is not supported by the built-in CSS modules processor. See the community packages.

### Language packages
Expand Down
17 changes: 12 additions & 5 deletions packages/vue-component/plugin/tag-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ VueComponentTagHandler = class VueComponentTagHandler {

// CSS Modules
let isAsync = false;
let defaultModuleName = '$style';
if (styleTag.attribs.module) {
if (global.vue.cssModules) {
try {
Expand All @@ -328,7 +329,11 @@ VueComponentTagHandler = class VueComponentTagHandler {
css = result.css;
cssMap = result.map;
if (result.cssModules) {
cssModules = { ...(cssModules || {}), ...result.cssModules };
const moduleName = typeof styleTag.attribs.module === 'string' ? styleTag.attribs.module : defaultModuleName;
if (cssModules === undefined) {
cssModules = {};
}
cssModules[moduleName] = { ...(cssModules[moduleName] || {}), ...result.cssModules };
}
if (result.js) {
js += result.js;
Expand All @@ -344,18 +349,20 @@ VueComponentTagHandler = class VueComponentTagHandler {
});
}
} else {
const moduleName = typeof styleTag.attribs.module === 'string' ? styleTag.attribs.module : '';
const scopedModuleName = moduleName ? `_${moduleName}` : '';
const moduleName = typeof styleTag.attribs.module === 'string' ? styleTag.attribs.module : defaultModuleName;
const scopedModuleName = moduleName !== defaultModuleName ? `__${moduleName}` : '';
plugins.push(postcssModules({
getJSON(cssFilename, json) {
cssModules = { ...(cssModules || {}), ...json };
if (cssModules === undefined)
cssModules = {};
cssModules[moduleName] = { ...(cssModules[moduleName] || {}), ...json };
},
generateScopedName(exportedName, filePath) {
const path = require('path');
let sanitisedPath = path.relative(process.cwd(), filePath).replace(/.*\{}[/\\]/, '').replace(/.*\{.*?}/, 'packages').replace(/\.[^\.\/\\]+$/, '').replace(/[\W_]+/g, '_');
const filename = path.basename(filePath).replace(/\.[^\.\/\\]+$/, '').replace(/[\W_]+/g, '_');
sanitisedPath = sanitisedPath.replace(new RegExp(`_(${filename})$`), '__$1');
return `_${sanitisedPath}__${exportedName}`;
return `_${sanitisedPath}${scopedModuleName}__${exportedName}`;
},
}));
isAsync = true;
Expand Down
5 changes: 3 additions & 2 deletions packages/vue-component/plugin/vue-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,11 @@ function generateJs (vueId, inputFile, compileResult, isHotReload = false) {

// CSS Modules
if(compileResult.cssModules) {
const modules = Object.keys(compileResult.cssModules);
const modulesCode = '__vue_options__.computed = __vue_options__.computed || {};\n' +
`__vue_options__.computed.$style = function() {\n return ${JSON.stringify(compileResult.cssModules)}\n};\n`;
modules.map(module=>`__vue_options__.computed['${module}'] = function() {\n return ${JSON.stringify(compileResult.cssModules[module])}\n};\n`).join('\n');
js += modulesCode;
console.log(modulesCode)
// console.log(modulesCode);
}

// Package context
Expand Down