@embeddable.com/sdk-core 2.6.0-next.0 → 2.6.0-next.2

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.
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Most of the code in this file is copied from the following file:
3
+ * https://github.com/swc-project/swc-node/blob/master/packages/register/esm.mts
4
+ *
5
+ * The main difference is that resolve function is modified to handle non-TS files.
6
+ *
7
+ * */
8
+
9
+ import { fileURLToPath, pathToFileURL } from "url";
10
+ import { dirname, join } from "path";
11
+ import ts from "typescript";
12
+ // @ts-expect-error
13
+ import { readDefaultTsConfig } from "@swc-node/register/read-default-tsconfig";
14
+ import { compile } from "./custom-esm-register.cjs";
15
+
16
+ // @ts-expect-error
17
+ const tsconfig = readDefaultTsConfig();
18
+ tsconfig.module = ts.ModuleKind.ESNext;
19
+ tsconfig.moduleResolution = ts.ModuleResolutionKind.Node16;
20
+
21
+ const moduleResolutionCache = ts.createModuleResolutionCache(
22
+ ts.sys.getCurrentDirectory(),
23
+ (x) => x,
24
+ tsconfig,
25
+ );
26
+ const host = {
27
+ fileExists: ts.sys.fileExists,
28
+ readFile: ts.sys.readFile,
29
+ };
30
+ const EXTENSIONS = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts];
31
+ const NON_JS_TS_EXTENSIONS = /\.(css|scss|less|sass|styl|json)$/;
32
+
33
+ const currentDir = dirname(fileURLToPath(import.meta.url));
34
+
35
+ export const resolve = async (specifier, context, nextResolve) => {
36
+ if (NON_JS_TS_EXTENSIONS.test(specifier)) {
37
+ const mockModulePath = pathToFileURL(
38
+ join(currentDir, "mock-module.js"),
39
+ ).href;
40
+
41
+ return {
42
+ format: "module",
43
+ url: mockModulePath,
44
+ shortCircuit: true,
45
+ };
46
+ }
47
+ const isTS = EXTENSIONS.some((ext) => specifier.endsWith(ext));
48
+ // entrypoint
49
+ if (!context.parentURL) {
50
+ return {
51
+ format: isTS ? "ts" : undefined,
52
+ url: specifier,
53
+ shortCircuit: true,
54
+ };
55
+ }
56
+ // import/require from external library
57
+ if (context.parentURL.includes("/node_modules/") && !isTS) {
58
+ return nextResolve(specifier);
59
+ }
60
+ const { resolvedModule } = ts.resolveModuleName(
61
+ specifier,
62
+ fileURLToPath(context.parentURL),
63
+ tsconfig,
64
+ host,
65
+ moduleResolutionCache,
66
+ );
67
+ // import from local project to local project TS file
68
+ if (
69
+ resolvedModule &&
70
+ !resolvedModule.resolvedFileName.includes("/node_modules/") &&
71
+ EXTENSIONS.includes(resolvedModule.extension)
72
+ ) {
73
+ return {
74
+ format: "ts",
75
+ url: pathToFileURL(resolvedModule.resolvedFileName).href,
76
+ shortCircuit: true,
77
+ };
78
+ }
79
+ // import from local project to either:
80
+ // - something TS couldn't resolve
81
+ // - external library
82
+ // - local project non-TS file
83
+ return nextResolve(specifier);
84
+ };
85
+ const tsconfigForSWCNode = {
86
+ ...tsconfig,
87
+ paths: undefined,
88
+ baseUrl: undefined,
89
+ };
90
+
91
+ export const load = async (url, context, nextLoad) => {
92
+ if (context.format === "ts") {
93
+ const { source } = await nextLoad(url, context);
94
+ const code =
95
+ typeof source === "string" ? source : Buffer.from(source).toString();
96
+ const compiled = await compile(
97
+ code,
98
+ fileURLToPath(url),
99
+ tsconfigForSWCNode,
100
+ true,
101
+ );
102
+ return {
103
+ format: "module",
104
+ source: compiled,
105
+ shortCircuit: true,
106
+ };
107
+ } else {
108
+ return nextLoad(url, context);
109
+ }
110
+ };
@@ -0,0 +1,2 @@
1
+ const { compile } = require("@swc-node/register/register");
2
+ module.exports = { compile };
@@ -0,0 +1,2 @@
1
+ // mock-module.js
2
+ export default {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@embeddable.com/sdk-core",
3
- "version": "2.6.0-next.0",
3
+ "version": "2.6.0-next.2",
4
4
  "description": "Core Embeddable SDK module responsible for web-components bundling and publishing.",
5
5
  "keywords": [
6
6
  "embeddable",
@@ -22,6 +22,7 @@
22
22
  "files": [
23
23
  "bin/",
24
24
  "lib/",
25
+ "loader/",
25
26
  "templates/",
26
27
  "configs/"
27
28
  ],