@jsenv/plugin-minification 1.6.3 → 1.7.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/plugin-minification",
3
- "version": "1.6.3",
3
+ "version": "1.7.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,28 @@
1
+ import { applyBabelPlugins } from "@jsenv/ast";
2
+
3
+ export const stripJsComments = async (urlInfo) => {
4
+ const result = await applyBabelPlugins({
5
+ input: urlInfo.content,
6
+ inputUrl: urlInfo.url,
7
+ inputIsJsModule: urlInfo.type === "js_module",
8
+ options: {
9
+ parserOpts: {
10
+ // needs because of "experimental_preserveFormat"
11
+ tokens: true,
12
+ createParenthesizedExpressions: true,
13
+ },
14
+ generatorOpts: {
15
+ comments: false,
16
+ retainLines: true,
17
+ retainFunctionParens: true,
18
+ experimental_preserveFormat: true, // see "experimental_preserveFormat" in https://babeljs.io/docs/babel-generator#options
19
+ },
20
+ },
21
+ });
22
+ const { ast, code, map } = result;
23
+ return {
24
+ content: code,
25
+ sourcemap: map,
26
+ contentAst: ast,
27
+ };
28
+ };
@@ -1,16 +1,29 @@
1
1
  import { minifyCss } from "./css/css_minification.js";
2
2
  import { minifyHtml } from "./html/html_minification.js";
3
3
  import { minifyJs } from "./js/js_minification.js";
4
+ import { stripJsComments } from "./js/js_strip_comments.js";
4
5
  import { minifyJson } from "./json/json_minification.js";
5
6
 
6
- export const jsenvPluginMinification = ({
7
- html = {},
8
- css = {},
9
- js_classic = {},
10
- js_module = {},
11
- json = {},
12
- svg = {},
13
- } = {}) => {
7
+ export const jsenvPluginMinification = (options, { preserveComments }) => {
8
+ if (options === false) {
9
+ options = {
10
+ html: null,
11
+ css: null,
12
+ js_classic: null,
13
+ js_module: null,
14
+ json: null,
15
+ svg: null,
16
+ };
17
+ }
18
+
19
+ const {
20
+ html = {},
21
+ css = {},
22
+ js_classic = {},
23
+ js_module = {},
24
+ json = {},
25
+ svg = {},
26
+ } = options;
14
27
  const htmlMinifier = html
15
28
  ? (urlInfo) => minifyHtml(urlInfo, html === true ? {} : html)
16
29
  : null;
@@ -22,10 +35,14 @@ export const jsenvPluginMinification = ({
22
35
  : null;
23
36
  const jsClassicMinifier = js_classic
24
37
  ? (urlInfo) => minifyJs(urlInfo, js_classic === true ? {} : js_classic)
25
- : null;
38
+ : preserveComments
39
+ ? null
40
+ : (urlInfo) => stripJsComments(urlInfo);
26
41
  const jsModuleMinifier = js_module
27
42
  ? (urlInfo) => minifyJs(urlInfo, js_module === true ? {} : js_module)
28
- : null;
43
+ : preserveComments
44
+ ? null
45
+ : (urlInfo) => stripJsComments(urlInfo);
29
46
  const jsonMinifier = json
30
47
  ? (urlInfo) => minifyJson(urlInfo, json === true ? {} : html)
31
48
  : null;