Skip to content

Commit

Permalink
fix postcss options, closed #57
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Jan 11, 2018
1 parent 861aa3d commit 92f1394
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 12 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"homepage": "https://github.com/egoist/rollup-plugin-postcss#readme",
"devDependencies": {
"autoprefixer": "^7.2.4",
"babel-jest": "^22.0.4",
"babel-preset-env": "^1.5.1",
"bili": "^1.0.0",
Expand Down Expand Up @@ -73,4 +74,4 @@
]
]
}
}
}
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ export default (options = {}) => {
minimize: inferOption(options.minimize, false),
/** Postcss config file */
config: inferOption(options.config, {}),
/** Parser */
parser: options.parser
/** PostCSS options */
postcss: {
parser: options.parser,
plugins: options.plugins,
syntax: options.syntax,
stringifier: options.stringifier,
exec: options.exec
}

This comment has been minimized.

Copy link
@egoist

egoist Jan 11, 2018

Author Owner

Doc needed.

}
let use = options.use || []
use.unshift(['postcss', postcssLoaderOptions])
Expand Down
20 changes: 13 additions & 7 deletions src/postcss-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function ensureClassName(name) {
return name
}

function ensurePostCSSOption(option) {
return typeof option === 'string' ? localRequire(option) : option
}

export default {
name: 'postcss',
test: /\.(css|sss)$/,
Expand All @@ -55,7 +59,7 @@ export default {
{}

const options = this.options
const plugins = [...(options.plugins || []), ...(config.plugins || [])]
const plugins = [...(options.postcss.plugins || []), ...(config.plugins || [])]
const shouldExtract = options.extract
const shouldInject = options.inject

Expand All @@ -77,20 +81,22 @@ export default {
}

const postcssOpts = {
parser: this.options.parser,
...this.options.postcss,
...config.options,
// Followings are never modified by user config config
from: this.id,
to: this.id,
map: this.sourceMap ?
shouldExtract ?
{ inline: false, annotation: false } :
{ inline: true, annotation: false } :
false,
...config.options
false
}
delete postcssOpts.plugins

if (typeof postcssOpts.parser === 'string') {
postcssOpts.parser = localRequire(postcssOpts.parser)
}
postcssOpts.parser = ensurePostCSSOption(postcssOpts.parser)
postcssOpts.syntax = ensurePostCSSOption(postcssOpts.syntax)
postcssOpts.stringifier = ensurePostCSSOption(postcssOpts.stringifier)

if (map && postcssOpts.map) {
postcssOpts.map.prev = typeof map === 'string' ? JSON.parse(map) : map
Expand Down
35 changes: 35 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,41 @@ __$$styleInject(css);
"
`;

exports[`postcss-options: js code 1`] = `
"'use strict';
function __$styleInject(css, ref) {
if ( ref === void 0 ) ref = {};
var insertAt = ref.insertAt;
if (!css) { return }
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild);
} else {
head.appendChild(style);
}
} else {
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
var css = \\"body {\\\\n display: -webkit-box;\\\\n display: -ms-flexbox;\\\\n display: flex;\\\\n}\\\\n\\";
__$$styleInject(css);
"
`;

exports[`sass: js code 1`] = `
"'use strict';
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,13 @@ snapshot({
]
}
})

snapshot({
title: 'postcss-options',
input: 'postcss-options/index.js',
options: {
plugins: [
require('autoprefixer')()
]
}
})
26 changes: 24 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ autoprefixer@^6.3.1:
postcss "^5.2.16"
postcss-value-parser "^3.2.3"

autoprefixer@^7.2.4:
version "7.2.4"
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-7.2.4.tgz#29b367c03876a29bfd3721260d945e3545666c8d"
dependencies:
browserslist "^2.10.2"
caniuse-lite "^1.0.30000784"
normalize-range "^0.1.2"
num2fraction "^1.2.2"
postcss "^6.0.15"
postcss-value-parser "^3.2.3"

aws-sign2@~0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.6.0.tgz#14342dd38dbcc94d0e5b87d763cd63612c0e794f"
Expand Down Expand Up @@ -983,6 +994,13 @@ browserslist@^2.1.2:
caniuse-lite "^1.0.30000718"
electron-to-chromium "^1.3.18"

browserslist@^2.10.2:
version "2.11.0"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-2.11.0.tgz#50350d6873a82ebe0f3ae5483658c571ae5f9d7d"
dependencies:
caniuse-lite "^1.0.30000784"
electron-to-chromium "^1.3.30"

bser@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/bser/-/bser-2.0.0.tgz#9ac78d3ed5d915804fd87acb158bc797147a1719"
Expand Down Expand Up @@ -1083,6 +1101,10 @@ caniuse-lite@^1.0.30000718:
version "1.0.30000721"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000721.tgz#931a21a7bd85016300328d21f126d84b73437d35"

caniuse-lite@^1.0.30000784:
version "1.0.30000789"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000789.tgz#2e3d937b267133f63635ef7f441fac66360fc889"

capture-stack-trace@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/capture-stack-trace/-/capture-stack-trace-1.0.0.tgz#4a6fa07399c26bba47f0b2496b4d0fb408c5550d"
Expand Down Expand Up @@ -1629,7 +1651,7 @@ electron-releases@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/electron-releases/-/electron-releases-2.1.0.tgz#c5614bf811f176ce3c836e368a0625782341fd4e"

electron-to-chromium@^1.2.7:
electron-to-chromium@^1.2.7, electron-to-chromium@^1.3.30:
version "1.3.30"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.30.tgz#9666f532a64586651fc56a72513692e820d06a80"
dependencies:
Expand Down Expand Up @@ -4334,7 +4356,7 @@ postcss@^6.0.1:
source-map "^0.5.7"
supports-color "^4.2.1"

postcss@^6.0.14:
postcss@^6.0.14, postcss@^6.0.15:
version "6.0.16"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-6.0.16.tgz#112e2fe2a6d2109be0957687243170ea5589e146"
dependencies:
Expand Down

0 comments on commit 92f1394

Please sign in to comment.