Skip to content

Commit

Permalink
Add postcss vue-loader support
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Dec 3, 2018
1 parent d6d5762 commit 3677f24
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
5 changes: 5 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
plugins: {
'postcss-custom-properties': {}
}
};
28 changes: 26 additions & 2 deletions src/components/Vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,32 @@ class Vue {
* Update all preprocessor loaders to support CSS extraction.
*/
updateCssLoaders(webpackConfig) {
// Basic CSS
this.updateCssLoader('css', ['css-loader'], webpackConfig);
// Basic CSS and PostCSS
this.updateCssLoader(
'css',
[
{ loader: 'css-loader', options: { importLoaders: 1 } },
{
loader: 'postcss-loader',
options: (() => {
let postCssOptions = { ident: 'postcss' };

if (Mix.components.get('postCss')) {
postCssOptions.plugins = Mix.components.get(
'postCss'
).details[0].postCssPlugins;
} else if (
!File.exists(Mix.paths.root('postcss.config.js'))
) {
postCssOptions.plugins = [];
}

return postCssOptions;
})()
}
],
webpackConfig
);

// LESS
this.updateCssLoader(
Expand Down
25 changes: 25 additions & 0 deletions test/features/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,31 @@ test.cb.serial('it extracts vue .sass styles to a dedicated file', t => {
});
});

test.cb.serial('it extracts vue PostCSS styles to a dedicated file', t => {
mix.js(
'test/fixtures/fake-app/resources/assets/vue/app-with-vue-and-postcss.js',
'js/app.js'
).options({ extractVueStyles: 'css/components.css' });

compile(t, config => {
// In this example, postcss-loader is reading from postcss.config.js.
let expected = `
:root {
--color: white;
}
.hello {
color: white;
color: var(--color);
}
`;

t.is(
expected,
File.find('test/fixtures/fake-app/public/css/components.css').read()
);
});
});

test.cb.serial('it extracts vue Less styles to a dedicated file', t => {
mix.js(
'test/fixtures/fake-app/resources/assets/vue/app-with-vue-and-less.js',
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/fake-app/resources/assets/vue/BasicWithPostCss.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<template>
<div class="hello">{{ message }}</div>
</template>

<script>
module.exports = {
data() {
return {
message: 'Hello World'
};
}
};
</script>

<style>
:root {
--color: white;
}
.hello {
color: var(--color);
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'vue';
import BasicWithPostCss from './BasicWithPostCss.vue';

new Vue({
components: {
BasicWithPostCss
}
}).$mount('#app');

0 comments on commit 3677f24

Please sign in to comment.