Skip to content

Commit 20aeab4

Browse files
kendrickwegoist
authored andcommitted
perform minimize after extract is finished (#153)
* perform minimize after extract is finished * Allow boolean plugins support (#142) Allow boolean plugins similar to how Rollup plugins work. In the Rollup plugins array you may write the following to only run a plugin for production usage: ```js plugins: [ (process.env.NODE_ENV === 'production' && terser()) ] ``` This pull request allow the same approach for PostCSS plugins: ```js plugins: [ (process.env.NODE_ENV === 'production' && cssnano()) ] ``` [Example of how Rollup has implemented this feature.](https://github.com/rollup/rollup/blob/f58d1e60a3da4b8470e5d56aa0caef2ecadec2b8/src/rollup/index.ts#L107) * perform minimize after extract is finished * tweaks
1 parent 57dac24 commit 20aeab4

File tree

5 files changed

+869
-24
lines changed

5 files changed

+869
-24
lines changed

src/index.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ function inferOption(option, defaultValue) {
1616

1717
export default (options = {}) => {
1818
const filter = createFilter(options.include, options.exclude)
19-
const postcssPlugins = Array.isArray(options.plugins) ? options.plugins.filter(Boolean) : options.plugins
19+
const postcssPlugins = Array.isArray(options.plugins) ?
20+
options.plugins.filter(Boolean) :
21+
options.plugins
2022
const sourceMap = options.sourceMap
2123
const postcssLoaderOptions = {
2224
/** Inject CSS as `<style>` to `<head>` */
@@ -99,9 +101,9 @@ export default (options = {}) => {
99101
const { modules } = bundle[path.relative(dir, opts.file)]
100102
if (modules) {
101103
const fileList = Object.keys(modules)
102-
entries.sort((a, b) => (
103-
fileList.indexOf(a.id) - fileList.indexOf(b.id)
104-
))
104+
entries.sort(
105+
(a, b) => fileList.indexOf(a.id) - fileList.indexOf(b.id)
106+
)
105107
}
106108
for (const res of entries) {
107109
const relative = path.relative(dir, res.id)
@@ -137,7 +139,25 @@ export default (options = {}) => {
137139
}
138140
}
139141

140-
const { code, codeFileName, map, mapFileName } = getExtracted()
142+
let { code, codeFileName, map, mapFileName } = getExtracted()
143+
// Perform cssnano on the extracted file
144+
if (options.minimize) {
145+
const cssOpts = postcssLoaderOptions.minimize
146+
if (sourceMap === 'inline') {
147+
cssOpts.map = { inline: true }
148+
} else if (sourceMap === true && map) {
149+
cssOpts.map = { prev: map }
150+
cssOpts.to = codeFileName
151+
}
152+
153+
const result = await require('cssnano').process(code, cssOpts)
154+
code = result.css
155+
156+
if (sourceMap === true && result.map && result.map.toString) {
157+
map = result.map.toString()
158+
}
159+
}
160+
141161
const codeFile = {
142162
fileName: codeFileName,
143163
isAsset: true,

src/postcss-loader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ export default {
9393
)
9494
}
9595

96-
if (options.minimize) {
96+
// If shouldExtract, minimize is done after all CSS are extracted to a file
97+
if (!shouldExtract && options.minimize) {
9798
plugins.push(require('cssnano')(options.minimize))
9899
}
99100

test/__snapshots__/index.test.js.snap

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,7 @@ console.log(css, css$1);
368368
"
369369
`;
370370

371-
exports[`minimize extract: css code 1`] = `
372-
"body{color:red}
373-
.bar{color:red}
374-
body{color:red;background:red}
375-
#sidebar{width:30%;background-color:#faa}
376-
#header{color:#6c94be}
377-
.pcss{color:red}"
378-
`;
371+
exports[`minimize extract: css code 1`] = `".bar,body{color:red}body{background:red}#sidebar{width:30%;background-color:#faa}#header{color:#6c94be}.pcss{color:red}"`;
379372

380373
exports[`minimize extract: js code 1`] = `
381374
"'use strict';
@@ -384,6 +377,32 @@ console.log(undefined, undefined);
384377
"
385378
`;
386379

380+
exports[`minimize extract-sourcemap-inline: css code 1`] = `
381+
".bar,body{color:red}body{background:red}#sidebar{width:30%;background-color:#faa}#header{color:#6c94be}.pcss{color:red}
382+
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxpbnB1dCBjc3MgMj4iXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUUEsVUFIRSxTQU1GLENBSEEsS0FFRSxjQUNGLENBR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GUVVORkxGZEJRVmM3UTBGRFdqczdRVU5HUkR0RlFVTkZMRmxCUVVFN1JVRkRRU3hwUWtGQlFUdERRME5FTzBGQlEwUXNNRVJCUVRCRU8wRkRTakZFTzBWQlEwVXNWMEZCVlR0RlFVTldMSFZDUVVGelFpeEZRVUZIT3p0QlEwVXpRaXgxUTBGQmRVTTdRVU5FZGtNN1JVRkRSU3hsUVVGQk8wTkRSa1E3TzBGRFJrUTdSVUZEUlN4WFFVRlhPME5CUTFvaUxDSm1hV3hsSWpvaVluVnVaR3hsTG1OemN5SXNJbk52ZFhKalpYTkRiMjUwWlc1MElqcGJJbUp2WkhrZ2UxeHVJQ0JqYjJ4dmNqb2djbVZrTzF4dWZWeHVJaXdpTG1KaGNpQjdYRzRnSUdOdmJHOXlPaUJ5WldRN1hHNTlYRzRpTEc1MWJHd3NiblZzYkN4dWRXeHNMRzUxYkd3c2JuVnNiQ3h1ZFd4c0xDSXVjR056Y3lCN1hHNGdJR052Ykc5eU9pQnlaV1E3WEc1OVhHNGlYWDA9Ki8iXX0= */"
383+
`;
384+
385+
exports[`minimize extract-sourcemap-inline: js code 1`] = `
386+
"'use strict';
387+
388+
console.log(undefined, undefined);
389+
"
390+
`;
391+
392+
exports[`minimize extract-sourcemap-true: css code 1`] = `
393+
".bar,body{color:red}body{background:red}#sidebar{width:30%;background-color:#faa}#header{color:#6c94be}.pcss{color:red}
394+
/*# sourceMappingURL=bundle.css.map */"
395+
`;
396+
397+
exports[`minimize extract-sourcemap-true: css map 1`] = `"{\\"version\\":3,\\"sources\\":[\\"test/fixtures/simple/style.styl\\",\\"bar.css\\",\\"bundle.css\\",\\"style.sass\\",\\"test/fixtures/simple/style.less\\",\\"style.pcss\\"],\\"names\\":[],\\"mappings\\":\\"AAAA,UCCE,SCUF,CFXA,KAEE,cESF,CCXA,SACE,SAAA,CACA,qBAAuB,CCCzB,QACE,aFeF,CGnBA,MACE,SHsBF\\",\\"file\\":\\"bundle.css\\",\\"sourcesContent\\":[null,\\".bar {\\\\n color: red;\\\\n}\\\\n\\",\\"body {\\\\n color: red;\\\\n}\\\\n\\\\n.bar {\\\\n color: red;\\\\n}\\\\n\\\\nbody {\\\\n color: #f00;\\\\n background: #f00;\\\\n}\\\\n/*# sourceMappingURL=test/fixtures/simple/style.css.map */\\\\n#sidebar {\\\\n width: 30%;\\\\n background-color: #faa; }\\\\n\\\\n#header {\\\\n color: #6c94be;\\\\n}\\\\n\\\\n.pcss {\\\\n color: red;\\\\n}\\\\n\\\\n/*# sourceMappingURL=bundle.css.map */\\",\\"#sidebar {\\\\n width: 30%;\\\\n background-color: #faa; }\\\\n\\",null,\\".pcss {\\\\n color: red;\\\\n}\\\\n\\"]}"`;
398+
399+
exports[`minimize extract-sourcemap-true: js code 1`] = `
400+
"'use strict';
401+
402+
console.log(undefined, undefined);
403+
"
404+
`;
405+
387406
exports[`minimize inject: js code 1`] = `
388407
"'use strict';
389408

test/index.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ snapshotMany('minimize', [
152152
minimize: true,
153153
extract: true
154154
}
155+
},
156+
{
157+
title: 'extract-sourcemap-true',
158+
input: 'simple/index.js',
159+
options: {
160+
minimize: true,
161+
extract: true,
162+
sourceMap: true
163+
}
164+
},
165+
{
166+
title: 'extract-sourcemap-inline',
167+
input: 'simple/index.js',
168+
options: {
169+
minimize: true,
170+
extract: true,
171+
sourceMap: 'inline'
172+
}
155173
}
156174
])
157175

0 commit comments

Comments
 (0)