Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parcel 2: HTMLNano Optimizer (#3673)
- Loading branch information
1 parent
7175501
commit 7cf6a3b
Showing
5 changed files
with
71 additions
and
11 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
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
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,20 @@ | ||
{ | ||
"name": "@parcel/optimizer-htmlnano", | ||
"version": "2.0.0-alpha.2.1", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/parcel-bundler/parcel.git" | ||
}, | ||
"main": "src/HTMLNanoOptimizer.js", | ||
"engines": { | ||
"parcel": "^2.0.0-alpha.1.1" | ||
}, | ||
"dependencies": { | ||
"@parcel/plugin": "^2.0.0-alpha.2.1", | ||
"@parcel/utils": "^2.0.0-alpha.2", | ||
"htmlnano": "^0.2.2", | ||
"nullthrows": "^1.1.1", | ||
"posthtml": "^0.11.3" | ||
} | ||
} |
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,38 @@ | ||
// @flow strict-local | ||
|
||
// $FlowFixMe this is untyped | ||
import htmlnano from 'htmlnano'; | ||
import {loadConfig} from '@parcel/utils'; | ||
import {Optimizer} from '@parcel/plugin'; | ||
import posthtml from 'posthtml'; | ||
import path from 'path'; | ||
|
||
export default new Optimizer({ | ||
async optimize({contents, map, options}) { | ||
if (!options.minify) { | ||
return {contents, map}; | ||
} | ||
|
||
if (typeof contents !== 'string') { | ||
throw new Error( | ||
'HTMLNanoOptimizer: Only string contents are currently supported' | ||
); | ||
} | ||
|
||
let userConfig = await loadConfig( | ||
options.inputFS, | ||
path.join(options.rootDir, 'index.html'), | ||
['.htmlnanorc', '.htmlnanorc.js'] | ||
); | ||
|
||
const htmlNanoConfig = { | ||
minifyJs: false, | ||
...userConfig?.config | ||
}; | ||
|
||
return { | ||
contents: (await posthtml([htmlnano(htmlNanoConfig)]).process(contents)) | ||
.html | ||
}; | ||
} | ||
}); |