Skip to content

Commit 3098e60

Browse files
leeyi45martin-henz
andauthored
Make frontend work with new module loading (#2904)
* Make frontend work with new module loading * bumping js-slang to version needed by this PR * Update craco config to ignore warning caused by dynamic imports --------- Co-authored-by: henz <henz@comp.nus.edu.sg>
1 parent bcdf3be commit 3098e60

File tree

5 files changed

+36
-35
lines changed

5 files changed

+36
-35
lines changed

craco.config.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// eslint-disable-next-line @typescript-eslint/no-var-requires
1+
/* eslint-disable @typescript-eslint/no-var-requires */
22
const webpack = require('webpack');
33

4-
const cracoConfig = (module.exports = {
4+
const cracoConfig = {
55
webpack: {
66
configure: webpackConfig => {
77
// avoid the entire process.env being inserted into the service worker
@@ -62,11 +62,16 @@ const cracoConfig = (module.exports = {
6262
},
6363
});
6464

65-
// Ignore warnings for dependencies that do not ship with a source map.
66-
// This is because we cannot do anything about our dependencies.
67-
webpackConfig.ignoreWarnings = [{
65+
webpackConfig.ignoreWarnings = [{
66+
// Ignore warnings for dependencies that do not ship with a source map.
67+
// This is because we cannot do anything about our dependencies.
6868
module: /node_modules/,
6969
message: /Failed to parse source map/
70+
}, {
71+
// Ignore the warnings that occur because js-slang uses dynamic imports
72+
// to load Source modules
73+
module: /js-slang\/dist\/modules\/loader\/loaders.js/,
74+
message: /Critical dependency: the request of a dependency is an expression/
7075
}];
7176

7277
webpackConfig.plugins = [
@@ -78,7 +83,7 @@ const cracoConfig = (module.exports = {
7883
// Make the 'buffer' Node.js module available in the browser.
7984
new webpack.ProvidePlugin({
8085
Buffer: ['buffer', 'Buffer'],
81-
})
86+
}),
8287
];
8388

8489
// Workaround to suppress warnings caused by ts-morph in js-slang
@@ -153,7 +158,7 @@ const cracoConfig = (module.exports = {
153158
['@babel/preset-typescript']
154159
]
155160
}
156-
});
161+
}
157162

158163
const ignoreModulePaths = (...paths) => {
159164
const moduleRoot = replaceSlashes('/node_modules/');
@@ -166,3 +171,5 @@ const ignoreModulePaths = (...paths) => {
166171
const replaceSlashes = target => {
167172
return target.replaceAll('/', '[/\\\\]');
168173
};
174+
175+
module.exports = cracoConfig

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"flexboxgrid": "^6.3.1",
5151
"flexboxgrid-helpers": "^1.1.3",
5252
"hastscript": "^9.0.0",
53-
"js-slang": "^1.0.60",
53+
"js-slang": "^1.0.62",
5454
"js-yaml": "^4.1.0",
5555
"konva": "^9.2.0",
5656
"lodash": "^4.17.21",

src/commons/utils/CToWasmHelper.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ModulesGlobalConfig as CCompilerConfig } from '@sourceacademy/c-slang/ctowasm/dist';
2-
import { initModuleContext, loadModuleBundle } from 'js-slang/dist/modules/loader/moduleLoader';
3-
import { ModuleFunctions } from 'js-slang/dist/modules/moduleTypes';
4-
import { Context } from 'js-slang/dist/types';
1+
import type { ModulesGlobalConfig as CCompilerConfig } from '@sourceacademy/c-slang/ctowasm/dist';
2+
import loadSourceModules from 'js-slang/dist/modules/loader';
3+
import type { ModuleFunctions } from 'js-slang/dist/modules/moduleTypes';
4+
import type { Context } from 'js-slang/dist/types';
55

66
import { handleConsoleLog } from '../application/actions/InterpreterActions';
77

@@ -37,17 +37,19 @@ export async function loadModulesUsedInCProgram(
3737
if (!includedModules) {
3838
return allModuleFunctions;
3939
}
40-
for (const m of includedModules) {
40+
41+
const modulesToLoad = includedModules.filter(m => {
4142
const moduleName = m.slice(1, m.length - 1);
43+
return modulesAvailableForC.has(moduleName);
44+
});
45+
46+
const loadedModules = await loadSourceModules(new Set(modulesToLoad), context, true);
47+
Object.values(loadedModules).forEach(functions => {
48+
Object.entries(functions).forEach(([name, func]) => {
49+
allModuleFunctions[name] = func;
50+
});
51+
});
4252

43-
if (modulesAvailableForC.has(moduleName)) {
44-
await initModuleContext(moduleName, context, true);
45-
const moduleFuncs = await loadModuleBundle(moduleName, context);
46-
for (const moduleFunc of Object.keys(moduleFuncs)) {
47-
allModuleFunctions[moduleFunc] = moduleFuncs[moduleFunc];
48-
}
49-
}
50-
}
5153
const pixNFlixStart = allModuleFunctions['start'];
5254
allModuleFunctions['start'] = () => {
5355
specialCReturnObject = pixNFlixStart();

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'src/styles/index.scss';
22

33
import { Button } from '@blueprintjs/core';
44
import * as Sentry from '@sentry/browser';
5-
import { setModulesStaticURL } from 'js-slang/dist/modules/loader/moduleLoader';
5+
import { setModulesStaticURL } from 'js-slang/dist/modules/loader';
66
import { createRoot } from 'react-dom/client';
77
import { Provider } from 'react-redux';
88
import Constants, { Links } from 'src/commons/utils/Constants';

yarn.lock

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8556,10 +8556,10 @@ js-sdsl@4.3.0, js-sdsl@^4.1.4:
85568556
resolved "https://registry.yarnpkg.com/js-sdsl/-/js-sdsl-4.3.0.tgz#aeefe32a451f7af88425b11fdb5f58c90ae1d711"
85578557
integrity sha512-mifzlm2+5nZ+lEcLJMoBK0/IH/bDg8XnJfd/Wq6IP+xoCjLZsTOnV2QpxlVbX9bMnkl5PdEjNtBJ9Cj1NjifhQ==
85588558

8559-
js-slang@^1.0.60:
8560-
version "1.0.60"
8561-
resolved "https://registry.yarnpkg.com/js-slang/-/js-slang-1.0.60.tgz#71ffcf6cb094f66e2b97d48f63bbbd1707d6ecbe"
8562-
integrity sha512-rc972WwXP3O/pPNoXQi5DtEt2lzA4kjeOtmw7fUHGEtxnhqIr3TuA9tieYLyGoQptrvMMdrbQwsG7fqmg+sCzw==
8559+
js-slang@^1.0.62:
8560+
version "1.0.62"
8561+
resolved "https://registry.yarnpkg.com/js-slang/-/js-slang-1.0.62.tgz#516352f5db0738d1bca91b1146e3a7e012355127"
8562+
integrity sha512-rngDsPDpEsx2VFmJfHxbA9O2AYIR8czwyJgDUC0EeJCBYOVJf8TJLWJEN/85795x5xTrhaZ3qc/QyP84qv53Sw==
85638563
dependencies:
85648564
"@babel/parser" "^7.19.4"
85658565
"@joeychenofficial/alt-ergo-modified" "^2.4.0"
@@ -8575,7 +8575,6 @@ js-slang@^1.0.60:
85758575
lodash "^4.17.21"
85768576
node-getopt "^0.3.2"
85778577
source-map "0.7.3"
8578-
xmlhttprequest-ts "^1.0.1"
85798578

85808579
"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
85818580
version "4.0.0"
@@ -13004,7 +13003,7 @@ tsconfig-paths@^3.14.1:
1300413003
minimist "^1.2.6"
1300513004
strip-bom "^3.0.0"
1300613005

13007-
tslib@^1.8.1, tslib@^1.9.2:
13006+
tslib@^1.8.1:
1300813007
version "1.14.1"
1300913008
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
1301013009
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==
@@ -14019,13 +14018,6 @@ xmlchars@^2.2.0:
1401914018
resolved "https://registry.yarnpkg.com/xmlchars/-/xmlchars-2.2.0.tgz#060fe1bcb7f9c76fe2a17db86a9bc3ab894210cb"
1402014019
integrity sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==
1402114020

14022-
xmlhttprequest-ts@^1.0.1:
14023-
version "1.0.1"
14024-
resolved "https://registry.yarnpkg.com/xmlhttprequest-ts/-/xmlhttprequest-ts-1.0.1.tgz#7b3cb4a197aee38cf2d4f9dd6189d1d21f0835b2"
14025-
integrity sha512-x+7u8NpBcwfBCeGqUpdGrR6+kGUGVjKc4wolyCz7CQqBZQp7VIyaF1xAvJ7ApRzvLeuiC4BbmrA6CWH9NqxK/g==
14026-
dependencies:
14027-
tslib "^1.9.2"
14028-
1402914021
"xtend@>=4.0.0 <4.1.0-0", xtend@^4.0.0, xtend@^4.0.2:
1403014022
version "4.0.2"
1403114023
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"

0 commit comments

Comments
 (0)