@granite-js/mpack 1.0.41 → 2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @granite-js/mpack
2
2
 
3
+ ## 2.0.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 864fa2a: Remove runtime module edges left behind by inline Flow type-only imports.
8
+ - @granite-js/plugin-core@2.0.0
9
+ - @granite-js/utils@2.0.0
10
+
11
+ ## 1.0.42
12
+
13
+ ### Patch Changes
14
+
15
+ - @granite-js/plugin-core@1.0.42
16
+ - @granite-js/utils@1.0.42
17
+
3
18
  ## 1.0.41
4
19
 
5
20
  ### Patch Changes
@@ -35,6 +35,79 @@ var import_generator = require("@babel/generator");
35
35
  var import_flow_remove_types = __toESM(require("flow-remove-types"));
36
36
  var HermesParser = __toESM(require("hermes-parser"));
37
37
  var import_defineStepName = require("../../../../utils/defineStepName");
38
+ const HERMES_PARSE_OPTIONS = { flow: "all", babel: true };
39
+ function isRecord(value) {
40
+ return typeof value === "object" && value !== null;
41
+ }
42
+ function isMutableProgram(value) {
43
+ return isRecord(value) && Array.isArray(value.body);
44
+ }
45
+ function isGeneratorNode(value) {
46
+ return isRecord(value) && typeof value.type === "string";
47
+ }
48
+ function getProgram(ast) {
49
+ if (!isRecord(ast) || !isMutableProgram(ast.program)) {
50
+ return null;
51
+ }
52
+ return ast.program;
53
+ }
54
+ function getImportDeclaration(node) {
55
+ if (!isRecord(node) || node.type !== "ImportDeclaration" || !Array.isArray(node.specifiers)) {
56
+ return null;
57
+ }
58
+ return {
59
+ importKind: node.importKind,
60
+ specifiers: node.specifiers,
61
+ start: node.start,
62
+ type: "ImportDeclaration"
63
+ };
64
+ }
65
+ function isTypeSpecifier(specifier) {
66
+ if (!isRecord(specifier)) {
67
+ return false;
68
+ }
69
+ return specifier.importKind === "type" || specifier.importKind === "typeof";
70
+ }
71
+ function collectTypeOnlyImportOffsets(code) {
72
+ const ast = HermesParser.parse(code, HERMES_PARSE_OPTIONS);
73
+ const program = getProgram(ast);
74
+ const offsets = /* @__PURE__ */ new Set();
75
+ if (program == null) {
76
+ return offsets;
77
+ }
78
+ for (const node of program.body) {
79
+ const declaration = getImportDeclaration(node);
80
+ if (declaration == null || typeof declaration.start !== "number" || declaration.specifiers.length === 0) {
81
+ continue;
82
+ }
83
+ const isTypeOnly = declaration.importKind === "type" || declaration.importKind === "typeof" || declaration.specifiers.every(isTypeSpecifier);
84
+ if (isTypeOnly) {
85
+ offsets.add(declaration.start);
86
+ }
87
+ }
88
+ return offsets;
89
+ }
90
+ function removeTypeOnlyImports(ast, originalCode) {
91
+ const program = getProgram(ast);
92
+ if (program == null || !originalCode.includes("type")) {
93
+ return;
94
+ }
95
+ const hasEmptyImport = program.body.some((node) => {
96
+ const declaration = getImportDeclaration(node);
97
+ return declaration != null && declaration.specifiers.length === 0;
98
+ });
99
+ if (!hasEmptyImport) {
100
+ return;
101
+ }
102
+ const typeOnlyOffsets = collectTypeOnlyImportOffsets(originalCode);
103
+ if (typeOnlyOffsets.size === 0) {
104
+ return;
105
+ }
106
+ program.body = program.body.filter((node) => {
107
+ const declaration = getImportDeclaration(node);
108
+ return !(declaration != null && declaration.specifiers.length === 0 && typeof declaration.start === "number" && typeOnlyOffsets.has(declaration.start));
109
+ });
110
+ }
38
111
  function createFlowStripStep() {
39
112
  const flowStripStep = async function flowStrip(code, args) {
40
113
  const shouldTransform = args.path.endsWith(".js") || args.path.endsWith(".jsx");
@@ -43,10 +116,11 @@ function createFlowStripStep() {
43
116
  }
44
117
  try {
45
118
  const transformedCode = (0, import_flow_remove_types.default)(code, {});
46
- const parsedAst = HermesParser.parse(transformedCode.toString(), {
47
- flow: "all",
48
- babel: true
49
- });
119
+ const parsedAst = HermesParser.parse(transformedCode.toString(), HERMES_PARSE_OPTIONS);
120
+ removeTypeOnlyImports(parsedAst, code);
121
+ if (!isGeneratorNode(parsedAst)) {
122
+ return { code };
123
+ }
50
124
  const transformedResult = (0, import_generator.generate)(parsedAst);
51
125
  return { code: transformedResult?.code ?? code };
52
126
  } catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@granite-js/mpack",
3
- "version": "1.0.41",
3
+ "version": "2.0.0",
4
4
  "description": "A bundler for Granite apps",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -92,8 +92,8 @@
92
92
  "@babel/traverse": "7.28.5",
93
93
  "@babel/types": "7.28.5",
94
94
  "@fastify/middie": "8.3.0",
95
- "@granite-js/plugin-core": "1.0.41",
96
- "@granite-js/utils": "1.0.41",
95
+ "@granite-js/plugin-core": "2.0.0",
96
+ "@granite-js/utils": "2.0.0",
97
97
  "@react-native-community/cli-plugin-metro": "11.3.7",
98
98
  "@react-native-community/cli-server-api": "11.3.7",
99
99
  "@react-native-community/cli-tools": "11.3.7",