-
-
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.
Add optimizer plugin for new CSS compiler (#7340)
- Loading branch information
1 parent
8cabeae
commit b9426b8
Showing
4 changed files
with
150 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "@parcel/optimizer-css", | ||
"version": "2.0.1", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/parcel" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/parcel-bundler/parcel.git" | ||
}, | ||
"main": "lib/CSSOptimizer.js", | ||
"source": "src/CSSOptimizer.js", | ||
"engines": { | ||
"node": ">= 12.0.0", | ||
"parcel": "^2.0.1" | ||
}, | ||
"dependencies": { | ||
"@parcel/css": "1.0.0-alpha.4", | ||
"@parcel/plugin": "^2.0.1", | ||
"@parcel/source-map": "^2.0.0", | ||
"@parcel/utils": "^2.0.1", | ||
"browserslist": "^4.6.6" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// @flow strict-local | ||
|
||
import SourceMap from '@parcel/source-map'; | ||
import {Optimizer} from '@parcel/plugin'; | ||
// $FlowFixMe | ||
import {transform} from '@parcel/css'; | ||
import {blobToBuffer} from '@parcel/utils'; | ||
import browserslist from 'browserslist'; | ||
|
||
export default (new Optimizer({ | ||
async optimize({ | ||
bundle, | ||
contents: prevContents, | ||
getSourceMapReference, | ||
map: prevMap, | ||
options, | ||
}) { | ||
if (!bundle.env.shouldOptimize) { | ||
return {contents: prevContents, map: prevMap}; | ||
} | ||
|
||
let targets = getTargets(bundle.env.engines.browsers); | ||
let code = await blobToBuffer(prevContents); | ||
let result = transform({ | ||
filename: bundle.name, | ||
code, | ||
minify: true, | ||
source_map: !!bundle.env.sourceMap, | ||
targets, | ||
}); | ||
|
||
let map; | ||
if (result.map != null) { | ||
map = new SourceMap(options.projectRoot); | ||
map.addVLQMap(JSON.parse(result.map)); | ||
} | ||
|
||
let contents = result.code; | ||
if (bundle.env.sourceMap) { | ||
let reference = await getSourceMapReference(map); | ||
if (reference != null) { | ||
contents += '\n' + '/*# sourceMappingURL=' + reference + ' */\n'; | ||
} | ||
} | ||
|
||
return { | ||
contents, | ||
map, | ||
}; | ||
}, | ||
}): Optimizer); | ||
|
||
const BROWSER_MAPPING = { | ||
and_chr: 'chrome', | ||
and_ff: 'firefox', | ||
ie_mob: 'ie', | ||
op_mob: 'opera', | ||
and_qq: null, | ||
and_uc: null, | ||
baidu: null, | ||
bb: null, | ||
kaios: null, | ||
op_mini: null, | ||
}; | ||
|
||
let cache = new Map(); | ||
|
||
function getTargets(browsers) { | ||
if (browsers == null) { | ||
return undefined; | ||
} | ||
|
||
let cached = cache.get(browsers); | ||
if (cached != null) { | ||
return cached; | ||
} | ||
|
||
let targets = {}; | ||
for (let browser of browserslist(browsers)) { | ||
let [name, v] = browser.split(' '); | ||
if (BROWSER_MAPPING[name] === null) { | ||
continue; | ||
} | ||
|
||
let version = parseVersion(v); | ||
if (version == null) { | ||
continue; | ||
} | ||
|
||
if (targets[name] == null || version < targets[name]) { | ||
targets[name] = version; | ||
} | ||
} | ||
|
||
cache.set(browsers, targets); | ||
return targets; | ||
} | ||
|
||
function parseVersion(version) { | ||
let [major, minor = 0, patch = 0] = version | ||
.split('-')[0] | ||
.split('.') | ||
.map(v => parseInt(v, 10)); | ||
|
||
if (isNaN(major) || isNaN(minor) || isNaN(patch)) { | ||
return null; | ||
} | ||
|
||
return (major << 16) | (minor << 8) | patch; | ||
} |
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