@@ -2,11 +2,14 @@ import setupRoutes from './setupRoutes'
2
2
import setupConfig from './setupConfig'
3
3
import setupBuild from './setupBuild'
4
4
import setupImages from './setupImages'
5
+ const glob = require ( 'glob-all' )
5
6
const path = require ( 'path' )
6
7
const _ = require ( 'lodash' )
7
8
const ampify = require ( './plugins/ampify' )
8
9
9
10
export default async function vuefrontModule ( _moduleOptions ) {
11
+ const resolver = ( this . nuxt . resolver || this . nuxt )
12
+
10
13
const moduleOptions = { ...this . options . vuefront , ..._moduleOptions }
11
14
12
15
const theme = process . env . VUEFRONT_THEME || 'default'
@@ -146,6 +149,53 @@ export default async function vuefrontModule(_moduleOptions) {
146
149
setupBuild . call ( this , moduleOptions , themeOptions ) ;
147
150
} )
148
151
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
+
149
199
this . extendBuild ( ( config , { isServer } ) => {
150
200
const { rules } = config . module
151
201
0 commit comments