Skip to content

Commit 465f232

Browse files
authored
only import specific lodash functions (#361)
1 parent 4eb77f4 commit 465f232

File tree

7 files changed

+66
-41
lines changed

7 files changed

+66
-41
lines changed

package-lock.json

Lines changed: 29 additions & 4 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"speakingurl": "^14.0.1",
4040
"classnames": "^2.3.1",
4141
"fractional-indexing": "^2.0.0",
42-
"lodash": "^4.17.21",
42+
"lodash.memoize": "^4.1.2",
4343
"mobx": "^6.2.0",
4444
"mobx-react-lite": "^3.2.0",
4545
"penpal": "^6.1.0",
@@ -103,7 +103,7 @@
103103
"@playwright/test": "^1.33.0",
104104
"@playwright/experimental-ct-react": "^1.36.0",
105105
"@typecell-org/shared-test": "^0.0.3",
106-
"@types/lodash": "^4.14.168",
106+
"@types/lodash.memoize": "^4.1.7",
107107
"@types/node": "^16.0.0",
108108
"@types/react": "^18.0.25",
109109
"@types/react-dom": "^18.0.9",

packages/editor/src/store/yjs-sync/remote/FetchRemote.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { makeObservable, observable, runInAction } from "mobx";
22
import { path, strings } from "vscode-lib";
33

44
import { ChildReference, IndexFileReference } from "@typecell-org/shared";
5-
import _ from "lodash";
5+
import memoize from "lodash.memoize";
66
import * as Y from "yjs";
77
import { filesToTreeNodes } from "../../../app/documentRenderers/project/directoryNavigation/treeNodeUtil";
88
import { HttpsIdentifier } from "../../../identifiers/HttpsIdentifier";
@@ -48,7 +48,7 @@ export default class FetchRemote extends Remote {
4848

4949
public constructor(
5050
_ydoc: Y.Doc,
51-
private readonly identifier: HttpsIdentifier
51+
private readonly identifier: HttpsIdentifier,
5252
) {
5353
super(_ydoc);
5454
makeObservable(this, {
@@ -85,7 +85,7 @@ export default class FetchRemote extends Remote {
8585
const project = new ProjectResource(newDoc, this.identifier); // TODO
8686

8787
const tree = filesToTreeNodes(
88-
indexFile.items.map((object) => ({ fileName: object }))
88+
indexFile.items.map((object) => ({ fileName: object })),
8989
);
9090

9191
tree.forEach((node) => {
@@ -100,7 +100,7 @@ export default class FetchRemote extends Remote {
100100
return newDoc;
101101
}
102102

103-
private fetchIndex = _.memoize(async (path: string) => {
103+
private fetchIndex = memoize(async (path: string) => {
104104
return (await (await fetch(path)).json()) as IndexFile;
105105
});
106106

@@ -115,7 +115,7 @@ export default class FetchRemote extends Remote {
115115

116116
return markdownToYDoc(
117117
contents,
118-
decodeURIComponent(path.basename(this.identifier.uri.path))
118+
decodeURIComponent(path.basename(this.identifier.uri.path)),
119119
);
120120
} else {
121121
// TODO: this is hacky. We should use json from parent route instead. Revise routing?

packages/engine/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"private": true,
55
"dependencies": {
66
"es-module-shims": "1.4.3",
7-
"lodash": "^4.17.21",
7+
"lodash.debounce": "^4.0.8",
8+
"lodash.memoize": "^4.1.2",
89
"mobx": "^6.2.0",
910
"react": "^18.2.0",
1011
"vscode-lib": "^0.1.2",
@@ -15,8 +16,9 @@
1516
"mocha": "^10.2.0",
1617
"@types/chai": "^4.3.0",
1718
"@types/mocha": "^10.0.1",
18-
"@types/lodash": "^4.14.182",
1919
"@types/react": "^18.0.25",
20+
"@types/lodash.memoize": "^4.1.7",
21+
"@types/lodash.debounce": "^4.0.7",
2022
"rimraf": "^3.0.2",
2123
"typescript": "5.0.4",
2224
"@playwright/test": "^1.33.0",

packages/engine/src/ReactiveEngine.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { CodeModel } from "@typecell-org/shared/src/codeModels/CodeModel.js";
3-
import _ from "lodash";
3+
import debounce from "lodash.debounce";
44
import { event, lifecycle } from "vscode-lib";
55
import { createCellEvaluator } from "./CellEvaluator.js";
66
import { TypeCellContext, createContext } from "./context.js";
@@ -57,9 +57,9 @@ export class ReactiveEngine<T extends CodeModel> extends lifecycle.Disposable {
5757
constructor(
5858
private resolveImport: (
5959
module: string,
60-
forModel: T
60+
forModel: T,
6161
) => Promise<ResolvedImport | undefined>,
62-
private debounceMillis = 100
62+
private debounceMillis = 100,
6363
) {
6464
super();
6565
}
@@ -72,7 +72,7 @@ export class ReactiveEngine<T extends CodeModel> extends lifecycle.Disposable {
7272
this._register(
7373
compiledModelProvider.onDidCreateModel((m) => {
7474
this.registerModel(m);
75-
})
75+
}),
7676
);
7777
}
7878

@@ -107,13 +107,13 @@ export class ReactiveEngine<T extends CodeModel> extends lifecycle.Disposable {
107107
}
108108
return ret.module;
109109
},
110-
(model, output) => this._onOutput.fire({ model, output })
110+
(model, output) => this._onOutput.fire({ model, output }),
111111
); // catch errors?
112112
};
113113
let prevValue: string | undefined = model.getValue();
114114

115115
// TODO: maybe only debounce (or increase debounce timeout) if an execution is still pending?
116-
const reEvaluate = _.debounce(() => {
116+
const reEvaluate = debounce(() => {
117117
if (model.getValue() !== prevValue) {
118118
// make sure there were actual changes from the previous value
119119

@@ -138,7 +138,7 @@ export class ReactiveEngine<T extends CodeModel> extends lifecycle.Disposable {
138138
evaluator.dispose();
139139
this.evaluatorCache.delete(model);
140140
}
141-
})
141+
}),
142142
);
143143
}
144144

@@ -156,7 +156,7 @@ export class ReactiveEngine<T extends CodeModel> extends lifecycle.Disposable {
156156
model: T,
157157
typecellContext: TypeCellContext<any>,
158158
resolveImport: (module: string) => Promise<any>,
159-
onOutput: (model: T, output: any) => void
159+
onOutput: (model: T, output: any) => void,
160160
) {
161161
if (!this.evaluatorCache.has(model)) {
162162
this.evaluatorCache.set(
@@ -166,8 +166,8 @@ export class ReactiveEngine<T extends CodeModel> extends lifecycle.Disposable {
166166
resolveImport,
167167
true,
168168
(output) => onOutput(model, output),
169-
() => this._onBeforeExecution.fire({ model })
170-
)
169+
() => this._onBeforeExecution.fire({ model }),
170+
),
171171
);
172172
}
173173
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

packages/engine/src/resolvers/ImportShimResolver.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import _ from "lodash";
2+
import memoize from "lodash.memoize";
33
import { ExternalModuleResolver } from "./ExternalModuleResolver.js";
44
import { LocalModuleResolver } from "./LocalModuleResolver.js";
55

66
let importShimCreated = false;
77

88
function createBlob(source: string) {
99
return URL.createObjectURL(
10-
new Blob([source], { type: "application/javascript" })
10+
new Blob([source], { type: "application/javascript" }),
1111
);
1212
}
1313

@@ -16,11 +16,11 @@ export class ImportShimResolver {
1616

1717
public constructor(
1818
private readonly resolvers: ExternalModuleResolver[],
19-
private readonly localResolver: LocalModuleResolver
19+
private readonly localResolver: LocalModuleResolver,
2020
) {
2121
if (importShimCreated) {
2222
throw new Error(
23-
"only 1 ImportShimResolver can exists because it uses a global importShim"
23+
"only 1 ImportShimResolver can exists because it uses a global importShim",
2424
);
2525
}
2626
importShimCreated = true;
@@ -30,7 +30,7 @@ export class ImportShimResolver {
3030
* Resolve a moduleName (e.g.: "lodash", "d3") to the actual module
3131
* using the resolvers passed to the constructor
3232
*/
33-
public resolveImport = _.memoize(this.doResolveImport);
33+
public resolveImport = memoize(this.doResolveImport);
3434

3535
private async doResolveImport(moduleName: string) {
3636
await this.initializeImportShim();
@@ -56,7 +56,7 @@ export class ImportShimResolver {
5656
for (const resolver of this.resolvers) {
5757
try {
5858
const module = await this.importShim(
59-
"use$" + resolver.name + "$" + moduleName
59+
"use$" + resolver.name + "$" + moduleName,
6060
);
6161
console.log("loaded module", moduleName, "using", resolver.name);
6262
return {
@@ -79,7 +79,7 @@ export class ImportShimResolver {
7979
resolver: ExternalModuleResolver,
8080
moduleName: string, // can be a relative URL, absolute URL, or "package name"
8181
parent: string, // the parent URL the package is loaded from
82-
importShimResolve: any // the original resolve function from es-module-shims
82+
importShimResolve: any, // the original resolve function from es-module-shims
8383
): Promise<string> {
8484
// first try to see if the LocalResolver wants to resolve this module
8585
const localURL = await this.tryLocalResolver(moduleName);
@@ -107,7 +107,7 @@ export class ImportShimResolver {
107107
// that we want to override with a package from LocalResolver
108108
const localURL = await this.tryLocalResolver(
109109
parsedModule.module,
110-
parsedModule.mode
110+
parsedModule.mode,
111111
);
112112
if (localURL) {
113113
return localURL;
@@ -121,7 +121,7 @@ export class ImportShimResolver {
121121
resolver,
122122
"maplibre-gl",
123123
parent,
124-
importShimResolve
124+
importShimResolve,
125125
);
126126
}
127127
}
@@ -142,7 +142,7 @@ export class ImportShimResolver {
142142

143143
private async tryLocalResolver(
144144
moduleName: string,
145-
mode?: string
145+
mode?: string,
146146
): Promise<string | undefined> {
147147
const local = await this.localResolver.getModule(moduleName, mode);
148148
if (local) {
@@ -169,11 +169,11 @@ export class ImportShimResolver {
169169
private getLocalURLForModule(
170170
loadedModule: any,
171171
moduleName: string,
172-
mode?: string
172+
mode?: string,
173173
) {
174174
const safeName = (moduleName + "/" + mode).replaceAll(
175175
/[^a-zA-Z0-9_]/g,
176-
"$"
176+
"$",
177177
);
178178

179179
if ((window as any)["__typecell_url_" + safeName]) {
@@ -183,7 +183,7 @@ export class ImportShimResolver {
183183

184184
(window as any)["__typecell_pkg_" + safeName] = loadedModule;
185185
const list = Object.keys(loadedModule).filter(
186-
(key) => key !== "default" && key !== "window"
186+
(key) => key !== "default" && key !== "window",
187187
);
188188

189189
const url = createBlob(`
@@ -202,7 +202,7 @@ export class ImportShimResolver {
202202
private onImportShimResolve = async (
203203
id: string,
204204
parent: string,
205-
importShimResolve: any
205+
importShimResolve: any,
206206
) => {
207207
// by default, try the first resolver
208208
let resolver = this.resolvers[0];
@@ -221,7 +221,7 @@ export class ImportShimResolver {
221221
// when there is a parent, this is expected (because it's a nested module).
222222
console.warn(
223223
"no explicit resolver detected in import, falling back to default",
224-
id
224+
id,
225225
);
226226
}
227227
}
@@ -230,7 +230,7 @@ export class ImportShimResolver {
230230
resolver,
231231
id,
232232
parent,
233-
importShimResolve
233+
importShimResolve,
234234
);
235235
return ret;
236236
};

packages/packager/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"@typecell-org/engine": "*",
88
"@typecell-org/parsers": "*",
99
"es-module-shims": "1.4.3",
10-
"lodash": "^4.17.21",
1110
"mobx": "^6.2.0",
1211
"react": "^18.2.0",
1312
"vscode-lib": "^0.1.2",
@@ -19,7 +18,6 @@
1918
"@types/fs-extra": "9.0.13",
2019
"@types/chai": "^4.3.0",
2120
"@types/mocha": "^10.0.1",
22-
"@types/lodash": "^4.14.182",
2321
"@types/react": "^18.0.25",
2422
"rimraf": "^3.0.2",
2523
"typescript": "5.0.4",

0 commit comments

Comments
 (0)