1- import { WebpackConfig , get } from '@easy-webpack/core'
1+ import { WebpackConfig , get } from '@easy-webpack/core' ;
22const ExtractTextPlugin = require ( 'extract-text-webpack-plugin' )
33
44/**
5- * CSS loader support for *.less
6- * @param sourceMap enable generating source maps (slower build)
7- * @param extractCss leave empty to skip extracting CSS or define an object with filename and optionally allChunks (boolean)
5+ * LESS loader support for *.less
6+ * filename: name of the extracted file
7+ * allChunks: should we extract all chunks to the file?
8+ * sourceMap: do you want a sourceMap generated? (takes longer)
9+ * extractText: do you want to extract all css to a separate file? boolean, configuration object or instance of ExtractTextPlugin, defaults to true
10+ * resolveRelativeUrl: boolean or object with parameters
811 */
9- export = function less ( extractCss = { filename : '[name].css' , allChunks : false , sourceMap : false } ) {
12+ export = function less ( { filename = '[name].css' , allChunks = false , sourceMap = false , extractText = undefined , resolveRelativeUrl = undefined } = { } ) {
1013 return function less ( this : WebpackConfig ) : WebpackConfig {
11- const extractCSSinstance = extractCss ? new ExtractTextPlugin ( extractCss . filename || '[name].css' , extractCss ) : null
12- const cssLoader = `css${ extractCss . sourceMap ? '?sourceMap!less' : '!less' } `
14+ const loaders = [ 'style' , `css${ sourceMap ? '?sourceMap' : '' } ` ]
15+
16+ if ( resolveRelativeUrl ) {
17+ loaders . push ( `resolve-url${ sourceMap ? '?sourceMap' : '' } ` )
18+ sourceMap = true // source maps need to be on for this
19+ }
20+
21+ loaders . push ( `less${ sourceMap ? '?sourceMap' : '' } ` )
22+
23+ const extractCss = extractText === false
24+ const providedInstance = extractText instanceof ExtractTextPlugin
25+ if ( ! providedInstance )
26+ extractText = extractCss ? new ExtractTextPlugin ( filename , extractText instanceof Object ? extractText : { allChunks, sourceMap } ) : null
1327 const config = {
1428 module : {
1529 loaders : get ( this , 'module.loaders' , [ ] ) . concat ( [ {
1630 test : / \. l e s s $ / i,
17- loaders : extractCss ? extractCSSinstance . extract ( 'style' , cssLoader ) : [ 'style' , cssLoader ]
31+ loaders : extractCss ? extractText . extract ( ... loaders ) : loaders
1832 } ] )
1933 }
2034 } as WebpackConfig
21- if ( extractCSSinstance ) {
35+ if ( extractText && ! providedInstance ) {
2236 config . plugins = [
2337 /**
2438 * Plugin: ExtractTextPlugin
2539 * It moves every import "style.css" in entry chunks into a single concatenated css output file.
2640 * So your styles are no longer inlined into the javascript, but separate in a css bundle file (styles.css).
2741 * If your total stylesheet volume is big, it will be faster because the stylesheet bundle is loaded in parallel to the javascript bundle.
2842 */
29- extractCSSinstance
43+ extractText
3044 ] . concat ( get ( this , 'plugins' , [ ] ) )
3145 }
46+ if ( resolveRelativeUrl instanceof Object ) {
47+ config [ 'resolveUrlLoader' ] = resolveRelativeUrl
48+ }
3249 return config
3350 }
3451}
0 commit comments