Skip to content

Commit 2527ea0

Browse files
committed
Update for Vue CLI 3
Note: - Switches from you ExtractTextWebpackPlugin to MiniCSSExtractPlugin. - Requires Tailwind’s expiremental shadowLookup flag to be enabled hello-world component loading
1 parent f960465 commit 2527ea0

File tree

6 files changed

+99
-14
lines changed

6 files changed

+99
-14
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# craft-vue
2-
3-
All the power of a Vue CLI's scaffolding & single file components tailored for a Craft CMS project.
1+
# craft-vue-tailwind
2+
Fork of the [craft-vue](https://github.com/chasegiunta/craft-vue) template that integrates the Tailwind CSS utility framework & removes unused CSS with Purgecss.
43

54
## What's Included
65

@@ -17,6 +16,7 @@ All the power of a Vue CLI's scaffolding & single file components tailored for a
1716
- Babel compiling
1817
- CSS across all components extracted into a single file and minified with [cssnano](https://github.com/ben-eb/cssnano)
1918
- Static assets compiled with version hashes for efficient long-term caching
19+
- Removes unused CSS with Purgecss. Includes whitelister to keep third-party libraries untouched.
2020
- Bundle size analytics
2121

2222
### Fork It And Make Your Own
@@ -42,6 +42,15 @@ composer create-project chasegiunta/craft-vue PATH
4242
# install dependencies
4343
npm install # yarn
4444

45+
# initialize Tailwind's config file (tailwind.js)
46+
./node_modules/.bin/tailwind init
47+
48+
# append shadowLookup experiment flag to end of tailwind.js file
49+
# (this step will be removed in an upcoming release)
50+
experiments: {
51+
shadowLookup: true,
52+
}
53+
4554
# run dev server (default runs on localhost:8080)
4655
npm run dev # yarn dev (alias for 'yarn serve')
4756

@@ -59,6 +68,10 @@ After running `npm run build`, the easiest way to test your build files locally
5968

6069
For a detailed explanation on how things work, check out the [Vue CLI docs](https://cli.vuejs.org/).
6170

71+
## Tailwind directives in .vue files
72+
73+
Using Tailwind directives (@apply, etc.) inside of Vue's single file components is now possible with the addition of an expirimental `shadowLookup` flag enabled in your tailwind.js file (added in Tailwind v0.6.2 - hooray!).
74+
6275
## Pre-Processors
6376

6477
This boilerplate has pre-configured CSS extraction for most popular CSS pre-processors including LESS, SASS, Stylus, and PostCSS. To use a pre-processor, all you need to do is install the appropriate webpack loader for it. For example, to use SASS:

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@
88
"build": "vue-cli-service build"
99
},
1010
"dependencies": {
11+
"postcss-import": "^12.0.0",
12+
"tailwindcss": "^0.6.4",
1113
"vue": "^2.5.16"
1214
},
1315
"devDependencies": {
1416
"@vue/cli-service": "^3.0.0-rc.10",
17+
"glob-all": "^3.1.0",
18+
"mini-css-extract-plugin": "^0.4.1",
19+
"purgecss-webpack-plugin": "^1.2.0",
20+
"purgecss-whitelister": "^2.1.0",
1521
"sane": "^3.0.0",
1622
"vue-template-compiler": "^2.5.16",
1723
"webpack-manifest-plugin": "^2.0.3"
1824
},
1925
"postcss": {
2026
"plugins": {
27+
"postcss-import": {},
28+
"tailwindcss": "./tailwind.js",
2129
"autoprefixer": {}
2230
}
2331
},

src/app.css

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
@tailwind preflight;
2+
@tailwind components;
3+
4+
/**
5+
* Here you would add any of your custom classes; stuff that you'd
6+
* want loaded *before* the utilities so that the utilities could still
7+
* override them.
8+
*/
9+
110
#app {
211
font-family: 'Avenir', Helvetica, Arial, sans-serif;
312
-webkit-font-smoothing: antialiased;
413
-moz-osx-font-smoothing: grayscale;
514
text-align: center;
615
color: #2c3e50;
716
margin-top: 60px;
8-
}
17+
}
18+
19+
@tailwind utilities;

src/components/HelloWorld.vue

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
<template>
22
<div class="hello">
33
<img src="/assets/logo.png">
4-
<h1>{{ msg }}</h1>
5-
<p>You're viewing the <code>&#060;HelloWorld&#062;</code> component!</p>
4+
<h1 class="mb-4">{{ msg }}</h1>
5+
<p class="mb-3">You're viewing the <code>&#060;HelloWorld&#062;</code> component!</p>
6+
<p class="mb-3">If this text is white, you've successfully installed Tailwind!</p>
67

7-
<p>Passing in some data from twig:</p>
8-
<code>
9-
Craft Version: {{ info.craftVersion }} (passed in as prop)<br>
10-
Environment: {{ info.environment }} (passed in as prop)<br>
11-
DB Name: <slot></slot> (passed in through slot)
12-
</code>
8+
<p class="mb-3">Passing in some data from twig:</p>
9+
10+
<div class="mt-3 p-4 inline-block rounded-lg leading-loose bg-black text-left">
11+
<code class="text-grey">
12+
<span class="text-white">Craft Version: </span>
13+
<span class="text-green-light">{{ info.craftVersion }}</span> (passed in as prop)<br>
14+
15+
<span class="text-white">Environment: </span>
16+
<span class="text-green-light">{{ info.environment }}</span> (passed in as prop)<br>
17+
18+
<span class="text-white">DB Name: </span>
19+
<span class="text-green-light"><slot></slot></span> (passed in through slot)
20+
</code>
21+
</div>
1322
</div>
1423
</template>
1524

@@ -27,6 +36,9 @@ export default {
2736

2837
<!-- Add "scoped" attribute to limit CSS to this component only -->
2938
<style scoped>
39+
.hello {
40+
@apply text-white
41+
}
3042
h1, h2 {
3143
font-weight: normal;
3244
}

templates/index.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{% endif %}
1212
</head>
1313

14-
<body>
14+
<body class="bg-grey-darkest">
1515
<div id="app">
1616
{% set info = {
1717
craftVersion: craft.app.getVersion(),

vue.config.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
const path = require('path')
22
const ManifestPlugin = require('webpack-manifest-plugin')
3+
const PurgecssPlugin = require('purgecss-webpack-plugin')
4+
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
5+
const whitelister = require('purgecss-whitelister')
6+
const glob = require('glob-all')
7+
38

49
config = {
510
https: false,
611
host: 'localhost',
712
port: 8080,
8-
watchDir: 'templates'
13+
watchDir: 'templates',
14+
// Whitelist selectors to stop purgecss from removing them from your CSS
15+
// You can pass in whole stylesheets to whitelist everything from thirdparty libs
16+
// Accepts string paths, array of strings, globby strings, or array of globby strings:
17+
// ['./node_modules/lib1/*.css', './node_modules/lib2/*.scss']
18+
purgecssWhitelist: [],
19+
// Whitelist based on a regular expression.
20+
// Ex: [/red$/] (selectors ending in 'red' will remain)
21+
// https://www.purgecss.com/whitelisting
22+
purgecssWhitelistPatterns: [],
23+
}
24+
25+
// Custom PurgeCSS extractor for Tailwind that allows special characters in
26+
// class names.
27+
// https://github.com/FullHuman/purgecss#extractor
28+
class TailwindExtractor {
29+
static extract(content) {
30+
return content.match(/[A-z0-9-:\/]+/g) || [];
31+
}
932
}
1033

1134
module.exports = {
@@ -34,6 +57,24 @@ module.exports = {
3457
plugins: [
3558
new ManifestPlugin({
3659
publicPath: '/dist/'
60+
}),
61+
new MiniCssExtractPlugin({
62+
filename: 'css/[name].[contenthash].css'
63+
}),
64+
new PurgecssPlugin({
65+
paths: glob.sync([
66+
path.join(__dirname, './templates/**/*.html'),
67+
path.join(__dirname, './templates/**/*.twig'),
68+
path.join(__dirname, './src/**/*.vue'),
69+
path.join(__dirname, './src/**/*.js')
70+
]),
71+
whitelist: whitelister(config.purgecssWhitelist),
72+
whitelistPatterns: config.purgecssWhitelistPatterns,
73+
extractors: [{
74+
extractor: TailwindExtractor,
75+
// Specify the file extensions to include when scanning for class names.
76+
extensions: ["html", "js", "twig", "vue"]
77+
}]
3778
})
3879
]
3980
},

0 commit comments

Comments
 (0)