@jsenv/plugin-minification 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@jsenv/plugin-minification",
3
+ "version": "1.0.0",
4
+ "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/jsenv/jsenv-core",
8
+ "directory": "packages/jsenv-plugin-minification"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./src/main.js"
17
+ },
18
+ "./*": "./*"
19
+ },
20
+ "files": [
21
+ "/src/"
22
+ ],
23
+ "scripts": {
24
+ "test": "node --conditions=development ./scripts/test.mjs"
25
+ },
26
+ "dependencies": {
27
+ "@jsenv/ast": "1.4.8",
28
+ "html-minifier": "4.0.0",
29
+ "terser": "5.16.1"
30
+ },
31
+ "peerDependencies": {
32
+ "@jsenv/core": "*"
33
+ },
34
+ "devDependencies": {
35
+ "@jsenv/core": "../../"
36
+ }
37
+ }
@@ -0,0 +1,9 @@
1
+ import { minifyWithParcel } from "@jsenv/ast"
2
+
3
+ export const minifyCss = ({ cssUrlInfo, context }) => {
4
+ const { code, map } = minifyWithParcel(cssUrlInfo, context)
5
+ return {
6
+ content: String(code),
7
+ sourcemap: map,
8
+ }
9
+ }
@@ -0,0 +1,15 @@
1
+ import { createRequire } from "node:module"
2
+
3
+ // https://github.com/kangax/html-minifier#options-quick-reference
4
+ export const minifyHtml = ({ htmlUrlInfo, options } = {}) => {
5
+ const require = createRequire(import.meta.url)
6
+ const { minify } = require("html-minifier")
7
+
8
+ const { collapseWhitespace = true, removeComments = true } = options
9
+
10
+ const htmlMinified = minify(htmlUrlInfo.content, {
11
+ collapseWhitespace,
12
+ removeComments,
13
+ })
14
+ return htmlMinified
15
+ }
@@ -0,0 +1,31 @@
1
+ // https://github.com/terser-js/terser#minify-options
2
+
3
+ export const minifyJs = async ({ jsUrlInfo, options }) => {
4
+ const url = jsUrlInfo.url
5
+ const content = jsUrlInfo.content
6
+ const sourcemap = jsUrlInfo.sourcemap
7
+ const isJsModule = jsUrlInfo.type === "js_module"
8
+
9
+ const { minify } = await import("terser")
10
+ const terserResult = await minify(
11
+ {
12
+ [url]: content,
13
+ },
14
+ {
15
+ sourceMap: {
16
+ ...(sourcemap ? { content: JSON.stringify(sourcemap) } : {}),
17
+ asObject: true,
18
+ includeSources: true,
19
+ },
20
+ module: isJsModule,
21
+ // We need to preserve "new InlineContent()" calls to be able to recognize them
22
+ // after minification in order to version urls inside inline content text
23
+ keep_fnames: /InlineContent/,
24
+ ...options,
25
+ },
26
+ )
27
+ return {
28
+ content: terserResult.code,
29
+ sourcemap: terserResult.map,
30
+ }
31
+ }
@@ -0,0 +1,77 @@
1
+ import { minifyHtml } from "./html/minify_html.js"
2
+ import { minifyCss } from "./css/minify_css.js"
3
+ import { minifyJs } from "./js/minify_js.js"
4
+ import { minifyJson } from "./json/minify_json.js"
5
+
6
+ export const jsenvPluginMinification = ({
7
+ html = {},
8
+ css = {},
9
+ js_classic = {},
10
+ js_module = {},
11
+ json = {},
12
+ svg = {},
13
+ } = {}) => {
14
+ const htmlMinifier = html
15
+ ? (urlInfo, context) =>
16
+ minifyHtml({
17
+ htmlUrlInfo: urlInfo,
18
+ context,
19
+ options: html === true ? {} : html,
20
+ })
21
+ : null
22
+ const svgMinifier = svg
23
+ ? (urlInfo, context) =>
24
+ minifyHtml({
25
+ htmlUrlInfo: urlInfo,
26
+ context,
27
+ options: svg === true ? {} : svg,
28
+ })
29
+ : null
30
+ const cssMinifier = css
31
+ ? (urlInfo, context) =>
32
+ minifyCss({
33
+ cssUrlInfo: urlInfo,
34
+ context,
35
+ options: css === true ? {} : css,
36
+ })
37
+ : null
38
+ const jsClassicMinifier = js_classic
39
+ ? (urlInfo, context) =>
40
+ minifyJs({
41
+ jsUrlInfo: urlInfo,
42
+ context,
43
+ options: js_classic === true ? {} : js_classic,
44
+ })
45
+ : null
46
+ const jsModuleMinifier = js_module
47
+ ? (urlInfo, context) =>
48
+ minifyJs({
49
+ jsUrlInfo: urlInfo,
50
+ context,
51
+ options: js_module === true ? {} : js_module,
52
+ })
53
+ : null
54
+ const jsonMinifier = json
55
+ ? (urlInfo, context) =>
56
+ minifyJson({
57
+ jsonUrlInfo: urlInfo,
58
+ context,
59
+ options: json === true ? {} : html,
60
+ })
61
+ : null
62
+
63
+ return {
64
+ name: "jsenv:minification",
65
+ appliesDuring: "build",
66
+ optimizeUrlContent: {
67
+ html: htmlMinifier,
68
+ svg: svgMinifier,
69
+ css: cssMinifier,
70
+ js_classic: jsClassicMinifier,
71
+ js_module: jsModuleMinifier,
72
+ json: jsonMinifier,
73
+ importmap: jsonMinifier,
74
+ webmanifest: jsonMinifier,
75
+ },
76
+ }
77
+ }
@@ -0,0 +1,8 @@
1
+ export const minifyJson = ({ jsonUrlInfo }) => {
2
+ const { content } = jsonUrlInfo
3
+ if (content.startsWith("{\n")) {
4
+ const jsonWithoutWhitespaces = JSON.stringify(JSON.parse(content))
5
+ return jsonWithoutWhitespaces
6
+ }
7
+ return null
8
+ }
package/src/main.js ADDED
@@ -0,0 +1 @@
1
+ export { jsenvPluginMinification } from "./jsenv_plugin_minification.js"