Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: scope clear module to project modules only #48

Merged
merged 32 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
89ca46f
wipp
EvanBacon May 11, 2021
87eda98
wip
EvanBacon May 11, 2021
3e28a30
added more previews
EvanBacon May 12, 2021
821ea8d
added more files
EvanBacon May 12, 2021
3d43ccb
added json support
EvanBacon May 12, 2021
4a47e11
fix reloading
EvanBacon May 12, 2021
e2a0636
update to support new introspection
EvanBacon May 20, 2021
bdc34a1
Added gradleProperties
EvanBacon May 20, 2021
380c283
wip
EvanBacon May 20, 2021
6690c67
Merge branch 'main' into @evanbacon/config-preview
EvanBacon May 26, 2021
eaa5423
Fixed feature
EvanBacon May 26, 2021
53238ff
bump
EvanBacon May 28, 2021
0f660d5
Merge branch 'main' into @evanbacon/config-preview
EvanBacon May 28, 2021
bf3facb
refactor
EvanBacon May 28, 2021
09c15b5
refactor
EvanBacon May 28, 2021
57bd00a
Update IntrospectCodeProvider.ts
EvanBacon Jun 7, 2021
7c7a078
Merge branch 'main' into @evanbacon/config-preview
EvanBacon Jun 7, 2021
aae735c
Update yarn.lock
EvanBacon Jun 7, 2021
74dc69f
refactor
EvanBacon Jun 7, 2021
1b728a2
refactor
EvanBacon Jun 7, 2021
617e2e8
Update CodeProvider.ts
EvanBacon Jun 7, 2021
b0dd132
Update package.json
EvanBacon Jun 7, 2021
760e0b0
revert imports
EvanBacon Jun 7, 2021
209ce15
Update setupPreview.ts
EvanBacon Jun 7, 2021
aa1a6f3
Added details
EvanBacon Jun 7, 2021
0f0d8a1
scope clear module
EvanBacon Jun 8, 2021
c35eb7a
Merge branch 'main' into @evanbacon/config-preview
EvanBacon Jun 8, 2021
2704a04
chore: remove clear-module from dependencies
byCedric Jun 8, 2021
2d4e745
chore: fix linting issue in clear projects module
byCedric Jun 8, 2021
6f6743c
fix: add fallback for parent module
byCedric Jun 8, 2021
5aa2547
fix: add workaround to xdl config plugins
byCedric Jun 8, 2021
7ff55c0
docs: add command descriptions to readme
byCedric Jun 8, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"minimatch": "^3.0.4",
"patch-package": "^6.4.7",
"prettier": "^2.1.1",
"parent-module": "^2.0.0",
"raw-loader": "^4.0.2",
"semantic-release": "^17.4.3",
"typescript": "^4.2.4",
Expand Down
5 changes: 3 additions & 2 deletions src/preview/CodeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AndroidConfig, XML } from '@expo/config-plugins';
import plist from '@expo/plist';
import * as clearModule from 'clear-module';
import { clearProjectModules } from '../utils/clearModule';
import * as path from 'path';
import * as vscode from 'vscode';
import { window } from 'vscode';
Expand Down Expand Up @@ -151,7 +151,8 @@ export class CodeProvider implements vscode.TextDocumentContentProvider {
async update(): Promise<void> {
try {
// Reset all requires to ensure plugins update
clearModule.all();
clearProjectModules(this.projectRoot);

this.fileContents = this.formatWithLanguage(await this.getFileContents());
} catch (error) {
this.fileContents = '';
Expand Down
64 changes: 64 additions & 0 deletions src/utils/clearModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import parentModule from 'parent-module';
import * as path from 'path';
import resolveFrom from 'resolve-from';

const resolve = (moduleId: string) => {
try {
return resolveFrom(path.dirname(parentModule(__filename)!), moduleId);
} catch {}
};

const clear = (moduleId: string) => {
if (typeof moduleId !== 'string') {
throw new TypeError(`Expected a \`string\`, got \`${typeof moduleId}\``);
}

const filePath = resolve(moduleId);

if (!filePath) {
return;
}

// Delete itself from module parent
if (require.cache[filePath] && require.cache[filePath]?.parent) {
let i = require.cache[filePath]!.parent!.children.length;

while (i--) {
if (require.cache[filePath]!.parent!.children[i].id === filePath) {
require.cache[filePath]!.parent!.children.splice(i, 1);
}
}
}

// Remove all descendants from cache as well
if (require.cache[filePath]) {
const { children } = require.cache[filePath]!;

// Delete module from cache
delete require.cache[filePath];

for (const { id } of children) {
clear(id);
}
}
};

export function clearProjectModules(projectRoot: string) {
const directory = path.dirname(parentModule(__filename)!);

// for (const moduleId of Object.keys(require.cache)) {
// console.log(resolveFrom(directory, moduleId));
// }

for (const moduleId of Object.keys(require.cache)) {
if (
// Module is inside of project root
moduleId.includes(projectRoot) &&
// Ignore modules inside of node_modules
!/node_modules/.test(moduleId)
) {
console.log(resolveFrom(directory, moduleId));
clear(moduleId);
}
}
}