Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optimizer plugin for new CSS compiler #7340

Merged
merged 2 commits into from Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/core/diagnostic/src/diagnostic.js
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
@@ -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
@@ -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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'd return a parcel source map directly from @parcel/css. I think we can only return a buffer though, not an actual node source map object (they could be using different versions). But still should be faster than returning and re-parsing VLQ/JSON.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the buffer have a stable format/"ABI"?

}

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
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