★ vue-cli3 全面配置css
npx create-nuxt-app <項目名> 或 npx create-nuxt-app <項目名>
能夠配置在客戶端和服務端共享的環境變量html
module.exports = { env: { baseUrl: process.env.BASE_URL || 'http://localhost:3000' } }
經過如下兩種方式來使用 baseUrl 變量vue
package.json 中添加 analyze 命令node
"analyze": "nuxt build --analyze"
修改 nuxt.config.jswebpack
export default { build: { analyza: { analyzeMode: 'static' } } }
npm i -S @nuxtjs/style-resources npm i -D sass-loader node-sass
修改 nuxt.config.jsgit
export default { modules: [ '@nuxtjs/style-resources', ], styleResources: { scss: '~/assets/scss/variable.scss' } }
npm i -D nuxt-sass-resources-loader sass-loader node-sass
修改 nuxt.config.jsgithub
export default { modules: [ ['nuxt-sass-resources-loader', ['~/assets/scss/variable.scss']] ], styleResources: { scss: '~/assets/scss/variable.scss' } }
npm i -D babel-plugin-component // or yarn add -D babel-plugin-component
修改 nuxt.config.jsweb
module.exports = { plugins: ['@/plugins/element-ui'], build: { babel: { plugins: [ [ 'component', { libraryName: 'element-ui', styleLibraryName: 'theme-chalk' } ] ] } }, }
修改 plugins/element-ui.jsvue-cli
import Vue from 'vue' import { Button, Loading, Notification, Message, MessageBox } from 'element-ui' import lang from 'element-ui/lib/locale/lang/zh-CN' import locale from 'element-ui/lib/locale' // configure language locale.use(lang) // set Vue.use(Loading.directive) Vue.prototype.$loading = Loading.service Vue.prototype.$msgbox = MessageBox Vue.prototype.$alert = MessageBox.alert Vue.prototype.$confirm = MessageBox.confirm Vue.prototype.$prompt = MessageBox.prompt Vue.prototype.$notify = Notification Vue.prototype.$message = Message // import components Vue.use(Button); // or // Vue.component(Button.name, Button)
npm i -D hard-source-webpack-plugin
修改 nuxt.config.jsnpm
module.exports = { build: { extractCSS: true, extend(config, ctx) { if (ctx.isDev) { config.plugins.push( new HardSourceWebpackPlugin({ cacheDirectory: '.cache/hard-source/[confighash]' }) ) } } } }
npm i --D glob-all purgecss-webpack-plugin
若安裝失敗,請先用管理員身份安裝如下全局依賴
npm install --global windows-build-tools 或 yarn global add windows-build-tools
修改 nuxt.config.js
const PurgecssPlugin = require('purgecss-webpack-plugin') const glob = require('glob-all') const path = require('path') const resolve = dir => path.resolve(__dirname, dir); module.exports = { build: { extractCSS: true, extend(config, ctx) { if (!ctx.isDev) { config.plugins.push( new PurgecssPlugin({ paths: glob.sync([ resolve('./pages/**/*.vue'), resolve('./layouts/**/*.vue'), resolve('./components/**/*.vue') ]), extractors: [ { extractor: class Extractor { static extract(content) { const validSection = content.replace( /<style([\s\S]*?)<\/style>+/gim, "" ); return validSection.match(/[A-Za-z0-9-_:/]+/g) || []; } }, extensions: ['vue'] } ], whitelist: ['html', 'body', 'nuxt-progress'] }) ) } } } }
npm i shrink-ray-current
若安裝失敗,請先用管理員身份安裝如下全局依賴
npm install --global windows-build-tools 或 yarn global add windows-build-tools
修改 nuxt.config.js
export default { render: { http2: { push: true }, compressor: shrinkRay() } }