Skip to content

Commit 3b6b72a

Browse files
authored
Rewrite resolver (#286)
* wip: rewrite resolvers * rewrite resolver * fix
1 parent 43c19c8 commit 3b6b72a

17 files changed

+388
-273
lines changed

jest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
// preset: "ts-jest",
3+
projects: ["<rootDir>/packages/*/jest.config.js"],
4+
testEnvironment: "jsdom",
5+
// roots: ["<rootDir>"],
6+
// modulePaths: ["<rootDir>"],
7+
// moduleNameMapper: {
8+
// },
9+
};

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/editor/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"copy-docs": "rimraf public/_docs && mkdir public/_docs && node copy-docs.mjs > public/_docs/index.json && cp -rf ../../docs/. public/_docs",
125125
"build": "tsc -v && npm run copytypes && npm run copy-docs",
126126
"build-react": "cross-env NODE_OPTIONS=--max_old_space_size=8192 CI=true craco build",
127+
"build-react-stats": "npm run build-react -- --stats && npx webpack-bundle-analyzer build/bundle-stats.json",
127128
"lint": "eslint src",
128129
"test": "craco test",
129130
"test-no-watch": "craco test --watchAll=false",

packages/editor/src/@types/probe.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ declare module "probe.gl" {}
33
declare module "@loaders.gl/loader-utils" {}
44

55
declare module "@deck.gl/react" {}
6+
7+
declare module "@deck.gl/aggregation-layers" {}

packages/editor/src/runtime/compiler/SourceModelCompiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class SourceModelCompiler extends lifecycle.Disposable {
1717
return `define(["require", "exports", "markdown-it"], function (require, exports, markdown_it_1) {
1818
"use strict";
1919
Object.defineProperty(exports, "__esModule", { value: true });
20-
const md = markdown_it_1.default({
20+
const md = markdown_it_1({
2121
html: true,
2222
linkify: true,
2323
typographer: true,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { LocalModuleResolver } from "@typecell-org/engine";
2+
import * as markdownit from "markdown-it";
3+
import * as react from "react";
4+
import * as reactdnd from "react-dnd";
5+
import * as reactdom from "react-dom";
6+
import * as jsxruntime from "react/jsx-runtime";
7+
8+
const sz = require("frontend-collective-react-dnd-scrollzone");
9+
10+
async function resolveNestedModule(id: string, mode?: string) {
11+
if (id === "markdown-it") {
12+
return markdownit;
13+
}
14+
15+
if (id === "react" && (!mode || mode === "imports/optimized/react.js")) {
16+
return react;
17+
}
18+
19+
if (id === "react" && mode === "imports/unoptimized/jsx-runtime.js") {
20+
return jsxruntime;
21+
}
22+
23+
if (id === "react-dom") {
24+
return reactdom;
25+
}
26+
27+
if (id === "frontend-collective-react-dnd-scrollzone") {
28+
return sz;
29+
}
30+
31+
if (id === "react-dnd") {
32+
return reactdnd;
33+
}
34+
35+
// We might want to remove hardcoded dependency for deckgl... We can't do this for every library..
36+
37+
if (id === "@deck.gl/react") {
38+
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
39+
return await import("@deck.gl/react");
40+
}
41+
42+
if (id === "probe.gl") {
43+
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
44+
return await import("probe.gl");
45+
}
46+
47+
if (id === "@loaders.gl/core") {
48+
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
49+
return await import("probe.gl");
50+
}
51+
52+
if (id === "@deck.gl/aggregation-layers") {
53+
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
54+
return await import("@deck.gl/aggregation-layers");
55+
}
56+
57+
if (id === "@loaders.gl/images") {
58+
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
59+
return await import("@loaders.gl/images");
60+
}
61+
62+
return undefined;
63+
}
64+
65+
export const LocalResolver = new LocalModuleResolver(resolveNestedModule);

packages/editor/src/runtime/executor/resolver/resolver.ts

Lines changed: 8 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,19 @@
11
import {
22
CodeModel,
33
Engine,
4+
ImportShimResolver,
45
ResolvedImport,
56
SkypackResolver,
67
} from "@typecell-org/engine";
7-
import * as markdownit from "markdown-it";
8-
import * as react from "react";
9-
import * as jsxruntime from "react/jsx-runtime";
10-
import * as reactdnd from "react-dnd";
11-
import * as reactdom from "react-dom";
12-
import * as probe from "probe.gl";
13-
import * as dreact from "@deck.gl/react";
14-
158
import getExposeGlobalVariables from "../lib/exports";
9+
import { LocalResolver } from "./LocalResolver";
1610
import { TypeCellCompiledCodeProvider } from "./typecell/TypeCellCompiledCodeProvider";
1711

18-
const agg = require("@deck.gl/aggregation-layers") as any;
19-
const lutil = require("@loaders.gl/core") as any;
20-
const limg = require("@loaders.gl/images") as any;
21-
const sz = require("frontend-collective-react-dnd-scrollzone");
22-
23-
// TODO: make async
24-
function resolveNestedModule(id: string, mode?: string) {
25-
function isModule(id: string, moduleName: string) {
26-
return id === moduleName || id === "https://cdn.skypack.dev/" + moduleName;
27-
}
28-
29-
if (isModule(id, "markdown-it")) {
30-
return markdownit;
31-
}
32-
33-
if (isModule(id, "react") && mode === "imports/optimized/react.js") {
34-
return react;
35-
}
36-
37-
if (isModule(id, "react") && mode === "imports/unoptimized/jsx-runtime.js") {
38-
return jsxruntime;
39-
}
40-
41-
if (isModule(id, "react-dom")) {
42-
return reactdom;
43-
}
44-
45-
if (isModule(id, "frontend-collective-react-dnd-scrollzone")) {
46-
return sz;
47-
}
48-
49-
if (isModule(id, "react-dnd")) {
50-
return reactdnd;
51-
}
52-
53-
// We might want to remove hardcoded dependency for deckgl... We can't do this for every library..
54-
55-
if (isModule(id, "@deck.gl/react")) {
56-
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
57-
return dreact;
58-
}
59-
60-
if (isModule(id, "probe.gl")) {
61-
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
62-
return probe;
63-
}
64-
65-
if (isModule(id, "@loaders.gl/core")) {
66-
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
67-
return lutil;
68-
}
69-
70-
if (isModule(id, "@deck.gl/aggregation-layers")) {
71-
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
72-
return agg;
73-
}
74-
75-
if (isModule(id, "@loaders.gl/images")) {
76-
// workaround for https://github.com/skypackjs/skypack-cdn/issues/242
77-
return limg;
78-
}
79-
80-
return undefined;
81-
}
82-
83-
const skypackResolver = new SkypackResolver(resolveNestedModule);
84-
85-
// todo: caches
12+
const skypackResolver = new SkypackResolver();
13+
const importShimResolver = new ImportShimResolver(
14+
[skypackResolver],
15+
LocalResolver
16+
);
8617

8718
const cache = new Map<string, ResolvedImport>();
8819

@@ -125,7 +56,7 @@ async function resolveImport(
12556
) => TypeCellCompiledCodeProvider
12657
): Promise<ResolvedImport> {
12758
if (!moduleName.startsWith("!@")) {
128-
return skypackResolver.resolveImport(moduleName);
59+
return importShimResolver.resolveImport(moduleName);
12960
}
13061

13162
if (!createTypeCellCompiledCodeProvider) {

packages/engine/jest.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
preset: "ts-jest",
3+
// transform: {
4+
// "^.+\\.(ts|tsx)?$": "ts-jest",
5+
// "^.+\\.(js|jsx)$": "babel-jest",
6+
// },
7+
// projects: ["<rootDir>/packages/*/jest.config.js"],
8+
testEnvironment: "jsdom",
9+
roots: ["<rootDir>"],
10+
modulePaths: ["<rootDir>"],
11+
testMatch: ["**/__tests__/**/*.ts?(x)", "**/?(*.)+(spec|test).ts?(x)"],
12+
moduleNameMapper: {
13+
"@typecell-org/common": "<rootDir>/../common/src",
14+
"^lib0/([a-zA-Z-]*)$": "<rootDir>/../../node_modules/lib0/dist/$1.cjs",
15+
"^y-protocols/([a-zA-Z-]*)$":
16+
"<rootDir>/../../node_modules/y-protocols/dist/$1.cjs",
17+
},
18+
setupFiles: ["<rootDir>/src/setupTests.ts"],
19+
};

packages/engine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.3",
44
"private": true,
55
"dependencies": {
6-
"es-module-shims": "0.7.1",
6+
"es-module-shims": "1.4.3",
77
"lodash": "^4.17.21",
88
"mobx": "^6.2.0",
99
"react": "^17.0.2",

packages/engine/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export * from "./Engine";
22
export * from "./CodeModel";
33
export * from "./resolvers/SkypackResolver";
4+
export * from "./resolvers/LocalModuleResolver";
5+
export * from "./resolvers/ImportShimResolver";
46
export * from "./reactView";

0 commit comments

Comments
 (0)