-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Will Binns-Smith
committed
Apr 7, 2020
1 parent
68a6867
commit 51dc23e
Showing
9 changed files
with
159 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,58 @@ | ||
// @flow strict-local | ||
|
||
import SourceMap from '@parcel/source-map'; | ||
import {Optimizer} from '@parcel/plugin'; | ||
import postcss from 'postcss'; | ||
// flowlint-next-line untyped-import:off | ||
import cssnano from 'cssnano'; | ||
|
||
export default new Optimizer({ | ||
async optimize({bundle, contents, map}) { | ||
async optimize({ | ||
bundle, | ||
contents: prevContents, | ||
getSourceMapReference, | ||
map: prevMap, | ||
options, | ||
}) { | ||
if (!bundle.env.minify) { | ||
return {contents, map}; | ||
return {contents: prevContents, map: prevMap}; | ||
} | ||
|
||
if (typeof contents !== 'string') { | ||
if (typeof prevContents !== 'string') { | ||
throw new Error( | ||
'CSSNanoOptimizer: Only string contents are currently supported', | ||
); | ||
} | ||
|
||
const results = await postcss([cssnano]).process(contents, { | ||
from: bundle.filePath, | ||
map: {inline: false}, | ||
const result = await postcss([cssnano]).process(prevContents, { | ||
// Suppress postcss's warning about a missing `from` property. In this | ||
// case, the input map contains all of the sources. | ||
from: undefined, | ||
map: { | ||
annotation: false, | ||
inline: false, | ||
prev: prevMap ? await prevMap.stringify({}) : null, | ||
}, | ||
}); | ||
|
||
console.log('MAP', results.map.constructor); | ||
let map; | ||
if (result.map != null) { | ||
map = new SourceMap(); | ||
let {mappings, sources, names} = result.map.toJSON(); | ||
map.addRawMappings(mappings, sources, names); | ||
} | ||
|
||
let contents = result.css; | ||
if (options.sourceMaps) { | ||
let reference = await getSourceMapReference(map); | ||
if (reference != null) { | ||
contents += '\n' + '/*# sourceMappingURL=' + reference + ' */\n'; | ||
} | ||
} | ||
|
||
return { | ||
contents: results.css, | ||
map: results.map, | ||
contents, | ||
map, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters