@@ -2,11 +2,14 @@ import setupRoutes from './setupRoutes'
22import setupConfig from './setupConfig'
33import setupBuild from './setupBuild'
44import setupImages from './setupImages'
5+ const glob = require ( 'glob-all' )
56const path = require ( 'path' )
67const _ = require ( 'lodash' )
78const ampify = require ( './plugins/ampify' )
89
910export 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 ( / s c s s / )
195+
196+ this . extendBuild ( extendScss ( scss ) )
197+
198+
149199 this . extendBuild ( ( config , { isServer } ) => {
150200 const { rules } = config . module
151201
0 commit comments