Skip to content

Commit 9ff0cc4

Browse files
committed
feat: add support css import in config
1 parent c993cbf commit 9ff0cc4

File tree

4 files changed

+2043
-501
lines changed

4 files changed

+2043
-501
lines changed

package.json

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"name": "vuefront-nuxt",
3-
"version": "0.3.13",
3+
"version": "0.3.15",
44
"description": "vuefront-nuxt",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/vuefront/vuefront-nuxt.git"
88
},
9+
"scripts": {
10+
"yalc-watch": "nodemon -e vue,js,scss,json -x 'yalc push'"
11+
},
912
"files": [
1013
"src",
1114
"types"
@@ -18,16 +21,19 @@
1821
"typings": "types/index.d.ts",
1922
"peerDependencies": {},
2023
"dependencies": {
21-
"@types/node": "^14.0.10",
24+
"@types/node": "^14.0.27",
2225
"apollo-boost": "^0.4.9",
2326
"cookie-universal": "^2.1.4",
24-
"graphql": "^15.0.0",
27+
"glob-all": "^3.2.1",
28+
"graphql": "^15.3.0",
2529
"isomorphic-fetch": "^2.2.1",
26-
"lodash": "^4.17.15",
30+
"lodash": "^4.17.19",
31+
"nodemon": "^2.0.4",
32+
"sass-resources-loader": "^2.0.3",
2733
"vue": "^2.6.11",
28-
"vue-apollo": "^3.0.3",
2934
"vue-graphql-loader": "^0.3.3",
30-
"vue-i18n": "^8.18.1",
31-
"vuefront": "^0.3.10"
32-
}
35+
"vue-i18n": "^8.20.0",
36+
"vuefront": "^0.3.12"
37+
},
38+
"devDependencies": {}
3339
}

src/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import setupRoutes from './setupRoutes'
22
import setupConfig from './setupConfig'
33
import setupBuild from './setupBuild'
44
import setupImages from './setupImages'
5+
const glob = require('glob-all')
56
const path = require('path')
67
const _ = require('lodash')
78
const ampify = require('./plugins/ampify')
89

910
export default async function vuefrontModule(_moduleOptions) {
11+
const resolver = (this.nuxt.resolver || this.nuxt)
12+
1013
const moduleOptions = { ...this.options.vuefront, ..._moduleOptions }
1114

1215
const theme = process.env.VUEFRONT_THEME || 'default'
@@ -146,6 +149,53 @@ export default async function vuefrontModule(_moduleOptions) {
146149
setupBuild.call(this, moduleOptions, themeOptions);
147150
})
148151

152+
const extendWithSassResourcesLoader = matchRegex => resources => (config) => {
153+
// Yes, using sass-resources-loader is **intended here**
154+
// Despite it's name it can be used for less as well!
155+
const sassResourcesLoader = {
156+
loader: 'sass-resources-loader', options: { resources }
157+
}
158+
159+
// Gather all loaders that test against scss or sass files
160+
const matchedLoaders = config.module.rules.filter(({ test = '' }) => {
161+
return test.toString().match(matchRegex)
162+
})
163+
164+
// push sass-resources-loader to each of them
165+
matchedLoaders.forEach((loader) => {
166+
loader.oneOf.forEach(rule => rule.use.push(sassResourcesLoader))
167+
})
168+
}
169+
170+
const retrieveStyleArrays = styleResourcesEntries =>
171+
styleResourcesEntries.reduce((normalizedObject, [key, value]) => {
172+
const wrappedValue = Array.isArray(value) ? value : [value]
173+
normalizedObject[key] = wrappedValue.reduce((acc, path) => {
174+
const possibleModulePath = resolver.resolveModule(path)
175+
176+
if (possibleModulePath) {
177+
// Path is mapped to module
178+
return acc.concat(possibleModulePath)
179+
}
180+
// Try to resolve alias, if not possible join with srcDir
181+
path = resolver.resolveAlias(path)
182+
// Try to glob (if it's a glob
183+
path = glob.sync(path)
184+
// Flatten this (glob could produce an array)
185+
return acc.concat(path)
186+
}, [])
187+
return normalizedObject
188+
}, {})
189+
190+
const styleResourcesEntries = Object.entries({scss: Object.values(themeOptions.cssImport)})
191+
192+
const {scss} = retrieveStyleArrays(styleResourcesEntries)
193+
194+
const extendScss = extendWithSassResourcesLoader(/scss/)
195+
196+
this.extendBuild(extendScss(scss))
197+
198+
149199
this.extendBuild((config, { isServer }) => {
150200
const { rules } = config.module
151201

src/plugin.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from 'vue'
22
import VueI18n from 'vue-i18n'
3-
import VueApollo from 'vue-apollo'
3+
44
import ApolloClient from "apollo-boost";
55
import _ from 'lodash'
66
import 'isomorphic-fetch'
@@ -13,7 +13,7 @@ const baseURL = process.browser
1313

1414
export const init = (ctx, inject) => {
1515
const client = new ApolloClient({
16-
uri: baseURL,
16+
uri: baseURL,
1717
headers: {
1818
accept: 'application/json; charset=UTF-8',
1919
'content-type': 'application/json; charset=UTF-8'
@@ -63,7 +63,6 @@ export const init = (ctx, inject) => {
6363
});
6464
}
6565
});
66-
Vue.use(VueApollo)
6766

6867
inject('vfapollo', client)
6968
}

0 commit comments

Comments
 (0)