Skip to content

Commit 2af058a

Browse files
committed
feat(all): normalize features across css configs
1 parent bf3ae96 commit 2af058a

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
"devDependencies": {
3232
"ava": "^0.15.2",
3333
"semantic-release": "^4.3.5",
34-
"ts-node": "^0.9.1",
35-
"tslint": "^3.11.0",
36-
"tslint-config-standard": "^1.2.2",
34+
"ts-node": "^0.9.3",
35+
"tslint": "^3.13.0",
36+
"tslint-config-standard": "^1.3.0",
3737
"typescript": ">=1.9.0-dev.20160619-1.0 || ^2.0.0",
38-
"typings": "^1.3.0"
38+
"typings": "^1.3.1"
3939
},
4040
"dependencies": {
4141
"css-loader": "^0.23.1",

src/index.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
1-
import {WebpackConfig, get} from '@easy-webpack/core'
1+
import {WebpackConfig, get} from '@easy-webpack/core';
22
const 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: /\.less$/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

Comments
 (0)