Skip to content

Commit

Permalink
swc optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Oct 31, 2021
1 parent c8a64c9 commit 292e78f
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 4 deletions.
29 changes: 29 additions & 0 deletions packages/optimizers/swc/package.json
@@ -0,0 +1,29 @@
{
"name": "@parcel/optimizer-swc",
"version": "2.0.0",
"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/SwcOptimizer.js",
"source": "src/SwcOptimizer.js",
"engines": {
"node": ">= 12.0.0",
"parcel": "^2.0.0"
},
"dependencies": {
"@parcel/plugin": "^2.0.0",
"@parcel/source-map": "^2.0.0",
"@parcel/utils": "^2.0.0",
"@swc/core": "^1.2.106",
"nullthrows": "^1.1.1"
}
}
77 changes: 77 additions & 0 deletions packages/optimizers/swc/src/SwcOptimizer.js
@@ -0,0 +1,77 @@
// @flow

import nullthrows from 'nullthrows';
import {minify} from '@swc/core';
import {Optimizer} from '@parcel/plugin';
import {blobToString} from '@parcel/utils';
import SourceMap from '@parcel/source-map';

import path from 'path';

export default (new Optimizer({
async loadConfig({config, options}) {
let userConfig = await config.getConfigFrom(
path.join(options.projectRoot, 'index'),
['.terserrc', '.terserrc.js'],
);

if (userConfig) {
let isJavascript = path.extname(userConfig.filePath) === '.js';
if (isJavascript) {
config.invalidateOnStartup();
}
}

return userConfig?.contents;
},
async optimize({
contents,
map: originalMap,
bundle,
config: userConfig,
options,
getSourceMapReference,
}) {
if (!bundle.env.shouldOptimize) {
return {contents, map: originalMap};
}

let code = await blobToString(contents);
let config = {
mangle: true,
compress: true,
...userConfig,
sourceMap: bundle.env.sourceMap
? {
filename: path.relative(
options.projectRoot,
path.join(bundle.target.distDir, bundle.name),
),
}
: false,
toplevel:
bundle.env.outputFormat === 'esmodule' ||
bundle.env.outputFormat === 'commonjs',
module: bundle.env.outputFormat === 'esmodule',
};

let result = await minify(code, config);

let sourceMap = null;
let minifiedContents: string = nullthrows(result.code);
let resultMap = result.map;
if (resultMap) {
sourceMap = new SourceMap(options.projectRoot);
sourceMap.addVLQMap(JSON.parse(resultMap));
if (originalMap) {
sourceMap.extends(originalMap);
}
let sourcemapReference = await getSourceMapReference(sourceMap);
if (sourcemapReference) {
minifiedContents += `\n//# sourceMappingURL=${sourcemapReference}\n`;
}
}

return {contents: minifiedContents, map: sourceMap};
},
}): Optimizer);
100 changes: 96 additions & 4 deletions yarn.lock
Expand Up @@ -1904,6 +1904,18 @@
toml "^3.0.0"
tslib "^2.1.0"

"@napi-rs/triples@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@napi-rs/triples/-/triples-1.0.3.tgz#76d6d0c3f4d16013c61e45dfca5ff1e6c31ae53c"
integrity sha512-jDJTpta+P4p1NZTFVLHJ/TLFVYVcOqv6l8xwOeBKNPMgY/zDYH/YH7SJbvrr/h1RcS9GzbPcLKGzpuK9cV56UA==

"@node-rs/helper@^1.0.0":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@node-rs/helper/-/helper-1.2.1.tgz#e079b05f21ff4329d82c4e1f71c0290e4ecdc70c"
integrity sha512-R5wEmm8nbuQU0YGGmYVjEc0OHtYsuXdpRG+Ut/3wZ9XAvQWyThN08bTh2cBJgoZxHQUPtvRfeQuxcAgLuiBISg==
dependencies:
"@napi-rs/triples" "^1.0.3"

"@nodelib/fs.scandir@2.1.3":
version "2.1.3"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz#3a582bdb53804c6ba6d146579c46e52130cf4a3b"
Expand Down Expand Up @@ -2255,6 +2267,86 @@
deepmerge "^4.2.2"
svgo "^1.2.2"

"@swc/core-android-arm64@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-android-arm64/-/core-android-arm64-1.2.106.tgz#86fb16a40d112502051252dfa29c8482b341ce36"
integrity sha512-F5T6kP3yV9S0/oXyco305QaAyE6rLNsNSdR0QI4CtACwKadiPwTOptwNIDCiTNLNgWlWLQmIRkPpxg+G4doT6Q==

"@swc/core-darwin-arm64@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-darwin-arm64/-/core-darwin-arm64-1.2.106.tgz#02a9ce94fee6d7f8a81a94233e4c455d55273fed"
integrity sha512-bgKzzYLFnc+mv2mDS/DLwzBvx5DCC9ZCKYY46b4dAnBfasr+SMHj+v/WI84HtilbjLBMUfYZ2hgYKls3CebIIQ==

"@swc/core-darwin-x64@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-darwin-x64/-/core-darwin-x64-1.2.106.tgz#72448061266e9fb44427898bd13154ec3b63ebba"
integrity sha512-I5Uhit5RqbXaMIV2+v9jL+MIQeR3lT1DqVwzxZs1bTARclAheFZQpTmg+h6QmichjCiUT74SXQb6Apc/vqYKog==

"@swc/core-freebsd-x64@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-freebsd-x64/-/core-freebsd-x64-1.2.106.tgz#070ec3ab798009ac14a18c692ede1542b5640ef1"
integrity sha512-ZSK3vgzbA2Pkpw2LgHlAkUdx4okIpdXXTbLXuc5jkZMw1KhRWpeQaDlwbrN7XVynAYjkj2qgGQ7wv1tD43vQig==

"@swc/core-linux-arm-gnueabihf@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.2.106.tgz#a94a29bfe81425c6f90a27d5977e58cb469db0a0"
integrity sha512-WZh6XV8cQ9Fh3IQNX9z87Tv68+sLtfnT51ghMQxceRhfvc5gIaYW+PCppezDDdlPJnWXhybGWNPAl5SHppWb2g==

"@swc/core-linux-arm64-gnu@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.2.106.tgz#6ec685cd37ab3655dbc8eb07a38df6d9532ac32c"
integrity sha512-OSI9VUWPsRrCbUlRQ4KdYqdwV63VYBC5ahSNq+72DXhtRwVbLSFuF7MNsnXgUSMHidxbc0No3/bPPamshqHdsQ==

"@swc/core-linux-arm64-musl@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.2.106.tgz#805841ab7bcca134a2712fb1084c09338ef61168"
integrity sha512-de8AAUOP8D2/tZIpQ399xw+pGGKlR1+l5Jmy4lW7ixarEI4xKkBSF4bS9eXtC1jckmenzrLPiK/5sSbQSf6BWQ==

"@swc/core-linux-x64-gnu@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.2.106.tgz#d403dfce5d31dafd5d87491ce80ed603545e4864"
integrity sha512-QzFC7+lBSuVBmX5tS2pdM+74voiJcGgIMJ+x9pcjUu3GkDl3ow6WC6ta2WUzlgGopCGNp6IdZaFemKRzjLr3lw==

"@swc/core-linux-x64-musl@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.2.106.tgz#0fce602e60cf1c9d9e72d33083dda18b963b3b6d"
integrity sha512-QZ1gFqNiCJefkNMihbmYc7nr5stERyjoQpWgAIN6dzrgMUzRHXHGDRl/p1qsXW2VKos+okSdLwPFEmRT94H+1A==

"@swc/core-win32-arm64-msvc@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.2.106.tgz#c0e6f5b38a6eac8a5ce438492f23c4ffc47f326f"
integrity sha512-MbuQwk+s43bfBNnAZTKnoQlfo4UPSOsy6t9F15yU4P3rVUuFtcxdZg6CpDnUqNPbojILXujp8z4SSigRYh5cgg==

"@swc/core-win32-ia32-msvc@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.2.106.tgz#5cf4a824a5bc3cfa6346733a3407e423c1920020"
integrity sha512-BFxWpcPxsG2LLQZ+8K8ma45rbTckjpPbnvOOhybQ0hEhLgoVzMVPp3RIUGmC+RMZe6DkGSaEQf/Rjn2cbMdQhw==

"@swc/core-win32-x64-msvc@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.2.106.tgz#237d699d25944538fda57dd879478f2cc7835827"
integrity sha512-Emn5akqApGXzPsA7ntSXEohL0AH0WjQMHy6mT3MS9Yil42yTJ96dJGf68ejKVptxwg7Iz798mT+J9r1JbAFBgg==

"@swc/core@^1.2.106":
version "1.2.106"
resolved "https://registry.yarnpkg.com/@swc/core/-/core-1.2.106.tgz#d1ae8d5745b6b37fcc5c076e433eb31312c81372"
integrity sha512-9uw8gqU+lsk7KROAcSNhsrnBgNiC5H4MIaps5LlnnEevJmKu/o1ws22tXc2qjJg+F4/V1ynUbh8E0rYlmo1XGw==
dependencies:
"@node-rs/helper" "^1.0.0"
optionalDependencies:
"@swc/core-android-arm64" "^1.2.106"
"@swc/core-darwin-arm64" "^1.2.106"
"@swc/core-darwin-x64" "^1.2.106"
"@swc/core-freebsd-x64" "^1.2.106"
"@swc/core-linux-arm-gnueabihf" "^1.2.106"
"@swc/core-linux-arm64-gnu" "^1.2.106"
"@swc/core-linux-arm64-musl" "^1.2.106"
"@swc/core-linux-x64-gnu" "^1.2.106"
"@swc/core-linux-x64-musl" "^1.2.106"
"@swc/core-win32-arm64-msvc" "^1.2.106"
"@swc/core-win32-ia32-msvc" "^1.2.106"
"@swc/core-win32-x64-msvc" "^1.2.106"

"@swc/helpers@^0.2.11":
version "0.2.11"
resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.2.11.tgz#34c842dcd8182810b4ab72d0d1fc34b553909e2e"
Expand Down Expand Up @@ -3516,9 +3608,9 @@ caniuse-api@^3.0.0:
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001219:
version "1.0.30001271"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001271.tgz#0dda0c9bcae2cf5407cd34cac304186616cc83e8"
integrity sha512-BBruZFWmt3HFdVPS8kceTBIguKxu4f99n5JNp06OlPD/luoAMIaIK5ieV5YjnBLH3Nysai9sxj9rpJj4ZisXOA==
version "1.0.30001272"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001272.tgz#8e9790ff995e9eb6e1f4c45cd07ddaa87cddbb14"
integrity sha512-DV1j9Oot5dydyH1v28g25KoVm7l8MTxazwuiH3utWiAS6iL/9Nh//TGwqFEeqqN8nnWYQ8HHhUq+o4QPt9kvYw==

caseless@~0.12.0:
version "0.12.0"
Expand Down Expand Up @@ -9931,7 +10023,7 @@ performance-now@^2.1.0:
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=

picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.2.1, picomatch@^2.2.3:
picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3:
version "2.3.0"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
integrity sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==
Expand Down

0 comments on commit 292e78f

Please sign in to comment.