@jsenv/ast 4.0.0 → 4.0.3

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,12 +1,12 @@
1
1
  {
2
2
  "name": "@jsenv/ast",
3
- "version": "4.0.0",
3
+ "version": "4.0.3",
4
4
  "description": "Parse and transform HTML,CSS and JS",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/jsenv/core",
9
- "directory": "packages/ast"
9
+ "directory": "packages/internal/ast"
10
10
  },
11
11
  "publishConfig": {
12
12
  "access": "public"
@@ -33,12 +33,12 @@
33
33
  "test": "node --conditions=development ./scripts/test.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@babel/parser": "7.21.8",
36
+ "@babel/parser": "7.22.4",
37
37
  "acorn": "8.8.2",
38
38
  "acorn-import-assertions": "1.9.0",
39
39
  "acorn-walk": "8.2.0",
40
40
  "parse5": "7.1.2",
41
- "postcss": "8.4.23",
41
+ "postcss": "8.4.24",
42
42
  "postcss-value-parser": "4.2.0"
43
43
  }
44
44
  }
@@ -61,7 +61,11 @@ export const parseHtmlString = (
61
61
 
62
62
  export const stringifyHtmlAst = (
63
63
  htmlAst,
64
- { cleanupJsenvAttributes = false, cleanupPositionAttributes = false } = {},
64
+ {
65
+ preserveLineBreaks = true,
66
+ cleanupJsenvAttributes = false,
67
+ cleanupPositionAttributes = false,
68
+ } = {},
65
69
  ) => {
66
70
  if (cleanupJsenvAttributes || cleanupPositionAttributes) {
67
71
  const htmlNode = findHtmlChildNode(
@@ -98,8 +102,10 @@ export const stringifyHtmlAst = (
98
102
  });
99
103
  }
100
104
  }
101
- // ensure body and html have \n
102
- ensureLineBreaksBetweenHtmlNodes(htmlAst);
105
+ if (preserveLineBreaks) {
106
+ // ensure body and html have \n
107
+ ensureLineBreaksBetweenHtmlNodes(htmlAst);
108
+ }
103
109
  const htmlString = serialize(htmlAst);
104
110
  return htmlString;
105
111
  };
@@ -9,34 +9,28 @@ import { createJsParseError } from "./js_parse_error.js";
9
9
 
10
10
  export const applyBabelPlugins = async ({
11
11
  babelPlugins,
12
- urlInfo,
12
+ input,
13
+ inputIsJsModule,
14
+ inputUrl,
15
+ outputUrl,
13
16
  ast,
14
17
  options = {},
15
18
  }) => {
16
- const sourceType = {
17
- [urlInfo.type]: undefined,
18
- js_module: "module",
19
- js_classic: "classic",
20
- }[urlInfo.type];
21
- const url = urlInfo.originalUrl;
22
- const generatedUrl = urlInfo.generatedUrl;
23
- const content = urlInfo.content;
24
-
25
19
  if (babelPlugins.length === 0) {
26
- return { code: content };
20
+ return { code: input };
27
21
  }
28
22
  const { transformAsync, transformFromAstAsync } = await import("@babel/core");
29
- const sourceFileName = url.startsWith("file:")
30
- ? fileURLToPath(url)
23
+ const sourceFileName = inputUrl.startsWith("file:")
24
+ ? fileURLToPath(inputUrl)
31
25
  : undefined;
32
26
  options = {
33
27
  ast: false,
34
28
  // https://babeljs.io/docs/en/options#source-map-options
35
29
  sourceMaps: true,
36
30
  sourceFileName,
37
- filename: generatedUrl
38
- ? generatedUrl.startsWith("file:")
39
- ? fileURLToPath(url)
31
+ filename: outputUrl
32
+ ? outputUrl.startsWith("file:")
33
+ ? fileURLToPath(inputUrl)
40
34
  : undefined
41
35
  : sourceFileName,
42
36
  configFile: false,
@@ -45,19 +39,17 @@ export const applyBabelPlugins = async ({
45
39
  // consider using startColumn and startLine for inline scripts?
46
40
  // see https://github.com/babel/babel/blob/3ee9db7afe741f4d2f7933c519d8e7672fccb08d/packages/babel-parser/src/options.js#L36-L39
47
41
  parserOpts: {
48
- sourceType,
42
+ sourceType: inputIsJsModule ? "module" : "classic",
49
43
  // allowAwaitOutsideFunction: true,
50
44
  plugins: [
51
45
  // "importMeta",
52
46
  // "topLevelAwait",
53
- ...(sourceType === "module"
54
- ? ["dynamicImport", "importAssertions"]
55
- : []),
47
+ ...(inputIsJsModule ? ["dynamicImport", "importAssertions"] : []),
56
48
  "jsx",
57
49
  "classProperties",
58
50
  "classPrivateProperties",
59
51
  "classPrivateMethods",
60
- ...(useTypeScriptExtension(url) ? ["typescript"] : []),
52
+ ...(useTypeScriptExtension(inputUrl) ? ["typescript"] : []),
61
53
  ...(options.parserPlugins || []),
62
54
  ].filter(Boolean),
63
55
  },
@@ -70,18 +62,18 @@ export const applyBabelPlugins = async ({
70
62
  };
71
63
  try {
72
64
  if (ast) {
73
- const result = await transformFromAstAsync(ast, content, options);
65
+ const result = await transformFromAstAsync(ast, input, options);
74
66
  return result;
75
67
  }
76
- const result = await transformAsync(content, options);
68
+ const result = await transformAsync(input, options);
77
69
  return result;
78
70
  } catch (error) {
79
71
  if (error && error.code === "BABEL_PARSE_ERROR") {
80
72
  throw createJsParseError({
81
73
  message: error.message,
82
74
  reasonCode: error.reasonCode,
83
- content,
84
- url,
75
+ content: input,
76
+ url: inputUrl,
85
77
  line: error.loc.line,
86
78
  column: error.loc.column,
87
79
  });