-
-
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
Showing
3 changed files
with
202 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
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,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); |
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