Skip to content

Commit

Permalink
Add optimizer plugin for new CSS compiler (#7340)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored Nov 24, 2021
1 parent 8cabeae commit b9426b8
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/core/diagnostic/src/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ export function errorToDiagnostic(
origin: defaultValues?.origin ?? 'Error',
message: escapeMarkdown(error.message),
name: error.name,
stack: error.highlightedCodeFrame ?? error.codeFrame ?? error.stack,
stack:
codeFrames == null
? error.highlightedCodeFrame ?? error.codeFrame ?? error.stack
: undefined,
codeFrames,
},
];
Expand Down
29 changes: 29 additions & 0 deletions packages/optimizers/css/package.json
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"
}
}
110 changes: 110 additions & 0 deletions packages/optimizers/css/src/CSSOptimizer.js
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;
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,13 @@
dependencies:
"@octokit/openapi-types" "^6.2.0"

"@parcel/css@1.0.0-alpha.4":
version "1.0.0-alpha.4"
resolved "https://registry.yarnpkg.com/@parcel/css/-/css-1.0.0-alpha.4.tgz#967520401053f8eb0d5f23ed58b4afbcebc6c9de"
integrity sha512-/wd7WWesAekEexrL7slIkb1gNXX/XYF8bMZUEcEKjDdkfne5SyINLTGbV3WzvsEjzRwgBL5kBET5nX59GJ3JVg==
dependencies:
detect-libc "^1.0.3"

"@parcel/source-map@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@parcel/source-map/-/source-map-2.0.0.tgz#41cf004109bbf277ceaf096a58838ff6a59af774"
Expand Down

0 comments on commit b9426b8

Please sign in to comment.