Skip to content

Commit

Permalink
Merge 3775286 into 1e0cade
Browse files Browse the repository at this point in the history
  • Loading branch information
fnatte committed Mar 17, 2018
2 parents 1e0cade + 3775286 commit d70ea15
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 53 deletions.
52 changes: 11 additions & 41 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const path = require('path')
const loaderUtils = require('loader-utils')

const parseOptions = require('./options')
const parseRc = require('./rc')
const validateOptions = require('schema-utils')

const postcss = require('postcss')
const postcssrc = require('postcss-load-config')

const SyntaxError = require('./Error')

Expand Down Expand Up @@ -49,48 +49,18 @@ module.exports = function loader (css, map, meta) {

const sourceMap = options.sourceMap

Promise.resolve().then(() => {
const length = Object.keys(options)
.filter((option) => {
switch (option) {
// case 'exec':
case 'ident':
case 'config':
case 'sourceMap':
return
default:
return option
}
})
.length

if (length) {
return parseOptions.call(this, options)
}

const rc = {
path: path.dirname(file),
ctx: {
file: {
extname: path.extname(file),
dirname: path.dirname(file),
basename: path.basename(file)
},
options: {}
}
}

if (options.config) {
if (options.config.path) {
rc.path = path.resolve(options.config.path)
}

if (options.config.ctx) {
rc.ctx.options = options.config.ctx
}
// Parse config from webpack options and rc
Promise.all([
(options.postcssrc !== false) ? parseRc(file, options) : null,
parseOptions(options)
]).then((configs) => {
// Merge configs
const config = Object.assign({}, configs[0], configs[1])
if (configs.every((config) => config && config.plugins)) {
config.plugins = configs[0].plugins.concat(configs[1].plugins)
}

return postcssrc(rc.ctx, rc.path, { argv: false })
return config
}).then((config) => {
if (!config) config = {}

Expand Down
7 changes: 2 additions & 5 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

module.exports = function parseOptions (params) {
if (typeof params.plugins === 'function') {
params.plugins = params.plugins.call(this, this)
}

let plugins

if (typeof params.plugins === 'undefined') plugins = []
if (typeof params.plugins === 'function') plugins = params.plugins.call(this, this)
else if (typeof params.plugins === 'undefined') plugins = []
else if (Array.isArray(params.plugins)) plugins = params.plugins
else plugins = [ params.plugins ]

Expand Down
3 changes: 3 additions & 0 deletions lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
{ "instanceof": "Function" }
]
},
"postcssrc": {
"type": "boolean"
},
"sourceMap": {
"type": [ "string", "boolean" ]
}
Expand Down
32 changes: 32 additions & 0 deletions lib/rc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const path = require('path')
const postcssrc = require('postcss-load-config')

module.exports = function parseRc (file, options) {
const rc = {
path: path.dirname(file),
ctx: {
file: {
extname: path.extname(file),
dirname: path.dirname(file),
basename: path.basename(file)
},
options: {}
}
}

if (options.config) {
if (options.config.path) {
rc.path = path.resolve(options.config.path)
}

if (options.config.ctx) {
rc.ctx.options = options.config.ctx
}
}

return Promise.resolve().then(() => {
return postcssrc(rc.ctx, rc.path, { argv: false })
})
}
3 changes: 2 additions & 1 deletion test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ describe('Loader', () => {
const config = {
loader: {
options: {
plugins: []
plugins: [],
postcssrc: false
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/options/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options Config - {Object} - with postcssrc false 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;

exports[`Options Config - {Object} 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;

exports[`Options Config - Context - {Object} - with ident 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;

exports[`Options Config - Context - {Object} 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;

exports[`Options Config - Path - {String} 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;

exports[`Options Config - with bad option 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
35 changes: 35 additions & 0 deletions test/options/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ describe('Options', () => {
})
})

test('Config - {Object} - with postcssrc false', () => {
const config = {
loader: {
options: {
postcssrc: false
}
}
}

return webpack('css/index.js', config).then((stats) => {
const src = loader(stats).src

expect(src).toEqual("module.exports = \"a { color: black }\\n\"")
expect(src).toMatchSnapshot()
})
})

test('Config - Path - {String}', () => {
const config = {
loader: {
Expand Down Expand Up @@ -75,4 +92,22 @@ describe('Options', () => {
expect(src).toMatchSnapshot()
})
})

test('Config - with bad option', () => {
const config = {
loader: {
options: {
unsupportedOption: true,
postcssrc: false
}
}
}

return webpack('css/index.js', config).then((stats) => {
const src = loader(stats).src

expect(src).toEqual("module.exports = \"a { color: black }\\n\"")
expect(src).toMatchSnapshot()
})
})
})
6 changes: 4 additions & 2 deletions test/options/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe('Options', () => {
const config = {
loader: {
options: {
parser: 'sugarss'
parser: 'sugarss',
postcssrc: false
}
}
}
Expand All @@ -26,7 +27,8 @@ describe('Options', () => {
loader: {
options: {
ident: 'postcss',
parser: require('sugarss')
parser: require('sugarss'),
postcssrc: false
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions test/options/stringifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe('Options', () => {
const config = {
loader: {
options: {
stringifier: 'sugarss'
stringifier: 'sugarss',
postcssrc: false
}
}
}
Expand All @@ -26,7 +27,8 @@ describe('Options', () => {
loader: {
options: {
ident: 'postcss',
stringifier: require('sugarss')
stringifier: require('sugarss'),
postcssrc: false
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions test/options/syntax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe('Options', () => {
const config = {
loader: {
options: {
syntax: 'sugarss'
syntax: 'sugarss',
postcssrc: false
}
}
}
Expand All @@ -26,7 +27,8 @@ describe('Options', () => {
loader: {
options: {
ident: 'postcss',
syntax: require('sugarss')
syntax: require('sugarss'),
postcssrc: false
}
}
}
Expand Down

0 comments on commit d70ea15

Please sign in to comment.