@marko/vite 5.3.8 → 5.3.9
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/dist/cjs-interop-translate.d.ts +38 -0
- package/dist/index.mjs +118 -142
- package/package.json +1 -1
- package/dist/babel-plugin-cjs-interop.d.ts +0 -31
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { types as t } from "@marko/compiler";
|
|
2
|
+
/**
|
|
3
|
+
* This plugin is designed to transform imports within Marko files to interop between ESM and CJS.
|
|
4
|
+
* In Node, ESM files cannot reliably use named imports and default imports from CJS files.
|
|
5
|
+
* Additionally, modules which are transpiled from ESM to CJS will use a `__esModule` property to
|
|
6
|
+
* signal that the consuming ESM code should treat `exports.default` as the default import.
|
|
7
|
+
* This plugin only modifies imports it determined to be for CJS modules.
|
|
8
|
+
*
|
|
9
|
+
* Examples:
|
|
10
|
+
* 1. Named imports:
|
|
11
|
+
* Source: import { bar as baz } from 'foo';
|
|
12
|
+
* Becomes: import * as _foo from 'foo';
|
|
13
|
+
* const { bar: baz } = importNS(_foo);
|
|
14
|
+
*
|
|
15
|
+
* 2. Default imports:
|
|
16
|
+
* Source: import myFoo from 'foo';
|
|
17
|
+
* Becomes: import * as _myFoo from 'foo';
|
|
18
|
+
* const { default: myFoo } = importDefault(_myFoo);
|
|
19
|
+
*
|
|
20
|
+
* 3. Namespace imports:
|
|
21
|
+
* Source: import * as nsFoo from 'foo';
|
|
22
|
+
* Becomes: import * as _nsFoo from 'foo';
|
|
23
|
+
* const nsFoo = importNS(_nsFoo);
|
|
24
|
+
*
|
|
25
|
+
* 4. Default and named imports:
|
|
26
|
+
* Source: import myFoo, { bar as baz } from 'foo';
|
|
27
|
+
* Becomes: import * as _foo from 'foo';
|
|
28
|
+
* const { default: myFoo } = importDefault(_foo);
|
|
29
|
+
* const { bar: baz } = importNS(_foo);
|
|
30
|
+
*/
|
|
31
|
+
export declare const cjsInteropHelpersId = "\0marko-cjs-interop.js";
|
|
32
|
+
export declare const cjsInteropHelpersCode = "export const importNS = m => m && (m.default === void 0 || m.__esModule ? m : m.default);\nexport const importDefault = m => m?.default?.__esModule ? m.default : m;\n";
|
|
33
|
+
declare const _default: {
|
|
34
|
+
Program: {
|
|
35
|
+
exit(program: t.NodePath<t.Program>): void;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export default _default;
|
package/dist/index.mjs
CHANGED
|
@@ -7,8 +7,9 @@ import fs4 from "fs";
|
|
|
7
7
|
import { createRequire } from "module";
|
|
8
8
|
import path6 from "path";
|
|
9
9
|
|
|
10
|
-
// src/
|
|
11
|
-
import
|
|
10
|
+
// src/cjs-interop-translate.ts
|
|
11
|
+
import { types as t } from "@marko/compiler";
|
|
12
|
+
import { importNamed } from "@marko/compiler/babel-utils";
|
|
12
13
|
|
|
13
14
|
// src/resolve.ts
|
|
14
15
|
import fs from "fs";
|
|
@@ -52,134 +53,107 @@ function isCJSModule(id, fromFile) {
|
|
|
52
53
|
return isCJS;
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
// src/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (t.isImportSpecifier(s)) {
|
|
72
|
-
(imports ||= []).push({
|
|
73
|
-
name: t.isStringLiteral(s.imported) ? s.imported.value : s.imported.name,
|
|
74
|
-
alias: s.local.name
|
|
75
|
-
});
|
|
76
|
-
} else if (t.isImportDefaultSpecifier(s)) {
|
|
77
|
-
defaultImportId = s.local;
|
|
78
|
-
} else if (t.isImportNamespaceSpecifier(s)) {
|
|
79
|
-
namespaceId = s.local;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
const rawImport = path7.scope.generateUidIdentifier(
|
|
83
|
-
namespaceId?.name || defaultImportId?.name || path7.node.source.value
|
|
84
|
-
);
|
|
85
|
-
path7.node.specifiers = [t.importNamespaceSpecifier(rawImport)];
|
|
86
|
-
if (defaultImportId) {
|
|
87
|
-
path7.insertAfter(
|
|
88
|
-
t.variableDeclaration("const", [
|
|
89
|
-
t.variableDeclarator(
|
|
90
|
-
t.objectPattern([
|
|
91
|
-
t.objectProperty(t.identifier("default"), defaultImportId)
|
|
92
|
-
]),
|
|
93
|
-
t.conditionalExpression(
|
|
94
|
-
t.optionalMemberExpression(
|
|
95
|
-
t.memberExpression(rawImport, t.identifier("default")),
|
|
96
|
-
t.identifier("__esModule"),
|
|
97
|
-
false,
|
|
98
|
-
true
|
|
99
|
-
),
|
|
100
|
-
t.memberExpression(rawImport, t.identifier("default")),
|
|
101
|
-
rawImport
|
|
102
|
-
)
|
|
103
|
-
)
|
|
104
|
-
])
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
if (namespaceId) {
|
|
108
|
-
path7.insertAfter(
|
|
109
|
-
t.variableDeclaration("const", [
|
|
110
|
-
t.variableDeclarator(
|
|
111
|
-
namespaceId,
|
|
112
|
-
t.conditionalExpression(
|
|
113
|
-
t.logicalExpression(
|
|
114
|
-
"||",
|
|
115
|
-
t.binaryExpression(
|
|
116
|
-
"===",
|
|
117
|
-
t.optionalMemberExpression(
|
|
118
|
-
rawImport,
|
|
119
|
-
t.identifier("default"),
|
|
120
|
-
false,
|
|
121
|
-
true
|
|
122
|
-
),
|
|
123
|
-
t.unaryExpression("void", t.numericLiteral(0), true)
|
|
124
|
-
),
|
|
125
|
-
t.optionalMemberExpression(
|
|
126
|
-
rawImport,
|
|
127
|
-
t.identifier("__esModule"),
|
|
128
|
-
false,
|
|
129
|
-
true
|
|
130
|
-
)
|
|
131
|
-
),
|
|
132
|
-
rawImport,
|
|
133
|
-
t.memberExpression(rawImport, t.identifier("default"))
|
|
134
|
-
)
|
|
135
|
-
)
|
|
136
|
-
])
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
if (imports) {
|
|
140
|
-
path7.insertAfter(
|
|
141
|
-
t.variableDeclaration("const", [
|
|
142
|
-
t.variableDeclarator(
|
|
143
|
-
t.objectPattern(
|
|
144
|
-
imports.map(
|
|
145
|
-
({ name, alias }) => t.objectProperty(
|
|
146
|
-
t.identifier(name),
|
|
147
|
-
t.identifier(alias),
|
|
148
|
-
false,
|
|
149
|
-
name === alias
|
|
150
|
-
)
|
|
151
|
-
)
|
|
152
|
-
),
|
|
153
|
-
t.conditionalExpression(
|
|
154
|
-
t.logicalExpression(
|
|
155
|
-
"||",
|
|
156
|
-
t.binaryExpression(
|
|
157
|
-
"===",
|
|
158
|
-
t.optionalMemberExpression(
|
|
159
|
-
rawImport,
|
|
160
|
-
t.identifier("default"),
|
|
161
|
-
false,
|
|
162
|
-
true
|
|
163
|
-
),
|
|
164
|
-
t.unaryExpression("void", t.numericLiteral(0), true)
|
|
165
|
-
),
|
|
166
|
-
t.optionalMemberExpression(
|
|
167
|
-
rawImport,
|
|
168
|
-
t.identifier("__esModule"),
|
|
169
|
-
false,
|
|
170
|
-
true
|
|
171
|
-
)
|
|
172
|
-
),
|
|
173
|
-
rawImport,
|
|
174
|
-
t.memberExpression(rawImport, t.identifier("default"))
|
|
175
|
-
)
|
|
176
|
-
)
|
|
177
|
-
])
|
|
178
|
-
);
|
|
56
|
+
// src/cjs-interop-translate.ts
|
|
57
|
+
var cjsInteropHelpersId = "\0marko-cjs-interop.js";
|
|
58
|
+
var cjsInteropHelpersCode = `export const importNS = m => m && (m.default === void 0 || m.__esModule ? m : m.default);
|
|
59
|
+
export const importDefault = m => m?.default?.__esModule ? m.default : m;
|
|
60
|
+
`;
|
|
61
|
+
var cjs_interop_translate_default = {
|
|
62
|
+
Program: {
|
|
63
|
+
exit(program) {
|
|
64
|
+
const { cjsInteropMarkoVite } = program.hub.file.markoOpts;
|
|
65
|
+
if (!cjsInteropMarkoVite) return;
|
|
66
|
+
const { filter } = cjsInteropMarkoVite;
|
|
67
|
+
const children = program.get("body");
|
|
68
|
+
for (let i = children.length; i--; ) {
|
|
69
|
+
const child = children[i];
|
|
70
|
+
if (child.isImportDeclaration()) {
|
|
71
|
+
translateImport(child, filter);
|
|
179
72
|
}
|
|
180
73
|
}
|
|
181
74
|
}
|
|
182
|
-
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
function translateImport(importDecl, filter) {
|
|
78
|
+
if (!importDecl.node.specifiers.length || /\.(?:mjs|marko)$|\?/.test(importDecl.node.source.value) || filter?.(importDecl.node.source.value) === false || !isCJSModule(
|
|
79
|
+
importDecl.node.source.value,
|
|
80
|
+
importDecl.hub.file.opts.filename
|
|
81
|
+
)) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
let namespaceId;
|
|
85
|
+
let defaultImportId;
|
|
86
|
+
let imports;
|
|
87
|
+
for (const s of importDecl.node.specifiers) {
|
|
88
|
+
if (t.isImportSpecifier(s)) {
|
|
89
|
+
(imports ||= []).push({
|
|
90
|
+
name: t.isStringLiteral(s.imported) ? s.imported.value : s.imported.name,
|
|
91
|
+
alias: s.local.name
|
|
92
|
+
});
|
|
93
|
+
} else if (t.isImportDefaultSpecifier(s)) {
|
|
94
|
+
defaultImportId = s.local;
|
|
95
|
+
} else if (t.isImportNamespaceSpecifier(s)) {
|
|
96
|
+
namespaceId = s.local;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const rawImport = importDecl.scope.generateUidIdentifier(
|
|
100
|
+
namespaceId?.name || defaultImportId?.name || importDecl.node.source.value
|
|
101
|
+
);
|
|
102
|
+
importDecl.node.specifiers = [t.importNamespaceSpecifier(rawImport)];
|
|
103
|
+
if (defaultImportId) {
|
|
104
|
+
importDecl.insertAfter(
|
|
105
|
+
t.variableDeclaration("const", [
|
|
106
|
+
t.variableDeclarator(
|
|
107
|
+
t.objectPattern([
|
|
108
|
+
t.objectProperty(t.identifier("default"), defaultImportId)
|
|
109
|
+
]),
|
|
110
|
+
t.callExpression(
|
|
111
|
+
importNamed(
|
|
112
|
+
importDecl.hub.file,
|
|
113
|
+
cjsInteropHelpersId,
|
|
114
|
+
"importDefault"
|
|
115
|
+
),
|
|
116
|
+
[rawImport]
|
|
117
|
+
)
|
|
118
|
+
)
|
|
119
|
+
])
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
if (namespaceId) {
|
|
123
|
+
importDecl.insertAfter(
|
|
124
|
+
t.variableDeclaration("const", [
|
|
125
|
+
t.variableDeclarator(
|
|
126
|
+
namespaceId,
|
|
127
|
+
t.callExpression(
|
|
128
|
+
importNamed(importDecl.hub.file, cjsInteropHelpersId, "importNS"),
|
|
129
|
+
[rawImport]
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
])
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
if (imports) {
|
|
136
|
+
importDecl.insertAfter(
|
|
137
|
+
t.variableDeclaration("const", [
|
|
138
|
+
t.variableDeclarator(
|
|
139
|
+
t.objectPattern(
|
|
140
|
+
imports.map(
|
|
141
|
+
({ name, alias }) => t.objectProperty(
|
|
142
|
+
t.identifier(name),
|
|
143
|
+
t.identifier(alias),
|
|
144
|
+
false,
|
|
145
|
+
name === alias
|
|
146
|
+
)
|
|
147
|
+
)
|
|
148
|
+
),
|
|
149
|
+
t.callExpression(
|
|
150
|
+
importNamed(importDecl.hub.file, cjsInteropHelpersId, "importNS"),
|
|
151
|
+
[rawImport]
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
])
|
|
155
|
+
);
|
|
156
|
+
}
|
|
183
157
|
}
|
|
184
158
|
|
|
185
159
|
// src/esbuild-plugin.ts
|
|
@@ -290,9 +264,9 @@ import { relativeImportPath } from "relative-import-path";
|
|
|
290
264
|
var programGlobImports = /* @__PURE__ */ new WeakMap();
|
|
291
265
|
var glob_import_transform_default = {
|
|
292
266
|
MetaProperty(tag) {
|
|
293
|
-
const
|
|
294
|
-
if (
|
|
295
|
-
const callExpression =
|
|
267
|
+
const memberExpression = tag.parentPath;
|
|
268
|
+
if (memberExpression.node.type === "MemberExpression" && memberExpression.node.property.type === "Identifier" && memberExpression.node.property.name === "glob") {
|
|
269
|
+
const callExpression = memberExpression.parentPath;
|
|
296
270
|
if (callExpression?.node.type === "CallExpression") {
|
|
297
271
|
const args = callExpression.get("arguments").map((arg) => arg.evaluate().value);
|
|
298
272
|
if (args[1]?.eager) {
|
|
@@ -963,28 +937,23 @@ function markoPlugin(opts = {}) {
|
|
|
963
937
|
if (linked) {
|
|
964
938
|
baseConfig.markoViteLinked = linked;
|
|
965
939
|
}
|
|
966
|
-
const
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
plugin({
|
|
970
|
-
filter: isBuild || isTest ? void 0 : (path7) => !/^\./.test(path7)
|
|
971
|
-
})
|
|
972
|
-
)
|
|
973
|
-
});
|
|
940
|
+
const cjsInteropMarkoVite = {
|
|
941
|
+
filter: isBuild || isTest ? void 0 : (path7) => !/^\./.test(path7)
|
|
942
|
+
};
|
|
974
943
|
ssrConfig = {
|
|
975
944
|
...baseConfig,
|
|
976
945
|
output: "html"
|
|
977
946
|
};
|
|
978
947
|
ssrCjsConfig = {
|
|
979
948
|
...ssrConfig,
|
|
980
|
-
|
|
949
|
+
cjsInteropMarkoVite
|
|
981
950
|
};
|
|
982
951
|
domConfig = {
|
|
983
952
|
...baseConfig,
|
|
984
953
|
output: "dom"
|
|
985
954
|
};
|
|
986
955
|
if (isTest) {
|
|
987
|
-
domConfig.
|
|
956
|
+
domConfig.cjsInteropMarkoVite = cjsInteropMarkoVite;
|
|
988
957
|
}
|
|
989
958
|
hydrateConfig = {
|
|
990
959
|
...baseConfig,
|
|
@@ -1015,6 +984,7 @@ function markoPlugin(opts = {}) {
|
|
|
1015
984
|
if (!registeredTagLib) {
|
|
1016
985
|
registeredTagLib = true;
|
|
1017
986
|
compiler2.taglib.register("@marko/vite", {
|
|
987
|
+
translate: cjs_interop_translate_default,
|
|
1018
988
|
transform: glob_import_transform_default,
|
|
1019
989
|
"<head>": { transformer: render_assets_transform_default },
|
|
1020
990
|
"<body>": { transformer: render_assets_transform_default },
|
|
@@ -1127,8 +1097,8 @@ function markoPlugin(opts = {}) {
|
|
|
1127
1097
|
basePath = config.base;
|
|
1128
1098
|
cacheDir = config.cacheDir && normalizePath(config.cacheDir);
|
|
1129
1099
|
getMarkoAssetFns = void 0;
|
|
1130
|
-
for (const
|
|
1131
|
-
const fn =
|
|
1100
|
+
for (const plugin of config.plugins) {
|
|
1101
|
+
const fn = plugin.api?.getMarkoAssetCodeForEntry;
|
|
1132
1102
|
if (fn) {
|
|
1133
1103
|
if (getMarkoAssetFns) {
|
|
1134
1104
|
getMarkoAssetFns.push(fn);
|
|
@@ -1222,6 +1192,9 @@ function markoPlugin(opts = {}) {
|
|
|
1222
1192
|
}
|
|
1223
1193
|
},
|
|
1224
1194
|
async resolveId(importee, importer, importOpts, ssr = importOpts.ssr) {
|
|
1195
|
+
if (importee === cjsInteropHelpersId) {
|
|
1196
|
+
return cjsInteropHelpersId;
|
|
1197
|
+
}
|
|
1225
1198
|
if (virtualFiles.has(importee)) {
|
|
1226
1199
|
return importee;
|
|
1227
1200
|
}
|
|
@@ -1282,6 +1255,9 @@ function markoPlugin(opts = {}) {
|
|
|
1282
1255
|
},
|
|
1283
1256
|
async load(rawId) {
|
|
1284
1257
|
const id = stripViteQueries(rawId);
|
|
1258
|
+
if (id === cjsInteropHelpersId) {
|
|
1259
|
+
return cjsInteropHelpersCode;
|
|
1260
|
+
}
|
|
1285
1261
|
if (id === renderAssetsRuntimeId) {
|
|
1286
1262
|
return renderAssetsRuntimeCode;
|
|
1287
1263
|
}
|
package/package.json
CHANGED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { PluginObj } from "@babel/core";
|
|
2
|
-
/**
|
|
3
|
-
* This plugin is designed to transform imports within Marko files to interop between ESM and CJS.
|
|
4
|
-
* In Node, ESM files cannot reliably use named imports and default imports from CJS files.
|
|
5
|
-
* Additionally, modules which are transpiled from ESM to CJS will use a `__esModule` property to
|
|
6
|
-
* signal that the consuming ESM code should treat `exports.default` as the default import.
|
|
7
|
-
* This plugin only modifies imports it determined to be for CJS modules
|
|
8
|
-
*
|
|
9
|
-
* Examples
|
|
10
|
-
* 1. Source: ```import { bar as baz } from 'foo';```
|
|
11
|
-
* Becomes:```
|
|
12
|
-
* import * as _foo from 'foo';
|
|
13
|
-
* const { bar: baz } = _foo?.default === void 0 || _foo?.__esModule ? _foo : _foo.default;
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* 2. Source: ```import myFoo from 'foo';```
|
|
17
|
-
* Becomes: ```
|
|
18
|
-
* import * as _myFoo from 'foo';
|
|
19
|
-
* const { default: myFoo } = _myFoo?.__esModule ? _myFoo.default : _myFoo;
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* 3. Source: ```import foo, * as nsFoo from 'foo';```
|
|
23
|
-
* Becomes: ```
|
|
24
|
-
* import * as _nsFoo from 'foo';
|
|
25
|
-
* const nsFoo = _nsFoo?.default === void 0 || _nsFoo?.__esModule ? _nsFoo : _nsFoo.default
|
|
26
|
-
* const { default: myFoo } = _nsFoo?.__esModule ? _nsFoo.default : _nsFoo
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export default function plugin(options: {
|
|
30
|
-
filter?: (path: string) => boolean;
|
|
31
|
-
}): PluginObj;
|