Skip to content

Commit c5e4089

Browse files
committed
dynamic import
1 parent adedbe8 commit c5e4089

15 files changed

+446230
-69
lines changed
18.2 MB
Binary file not shown.

build/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<head>
55
<meta charset="utf-8">
66
<meta http-equiv="X-UA-Compatible" content="chrome=1">
7-
<script src="script-runner.js"></script>
7+
<script src="yarn-loader-standalone.js"></script>
8+
<script src="script-runner.js"></script>
89
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
910
</head>
1011
</html>

build/script-runner.js

Lines changed: 253592 additions & 2 deletions
Large diffs are not rendered by default.

build/script-runner.js.map

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

build/vendors-node_modules_zokrates-js_pkg_index_js.script-runner.js

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

build/vendors-node_modules_zokrates-js_pkg_index_js.script-runner.js.map

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

build/yarn-loader-standalone.js

Lines changed: 89531 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
"@babel/preset-stage-0": "^7.0.0",
1818
"@babel/register": "^7.4.4",
1919
"@ethereum-waffle/chai": "^3.4.4",
20+
"@msgpack/msgpack": "^3.0.0-beta2",
2021
"@remixproject/plugin": "^0.3.1",
2122
"@remixproject/plugin-iframe": "^0.3.1",
2223
"@semaphore-protocol/contracts": "^3.11.0",
2324
"@semaphore-protocol/group": "^3.11.0",
2425
"@semaphore-protocol/identity": "^3.11.0",
2526
"@semaphore-protocol/proof": "^3.11.0",
27+
"@yarnpkg/core": "^4.0.0-rc.51",
28+
"@yarnpkg/plugin-node-modules": "^3.0.0-rc.9",
29+
"@yarnpkg/plugin-npm": "^3.0.0-rc.51",
30+
"@yarnpkg/plugin-pack": "^4.0.0-rc.51",
2631
"@zk-kit/incremental-merkle-tree": "^1.1.0",
2732
"babelify": "^10.0.0",
2833
"chai": "^4.3.6",
@@ -31,16 +36,23 @@
3136
"crypto-browserify": "^3.12.0",
3237
"ethers": "^5.4.7",
3338
"fast-async": "^7.0.6",
39+
"http2": "^3.3.7",
3440
"https-browserify": "^1.0.0",
41+
"idb": "5.0.8",
42+
"memfs": "^4.3.0",
3543
"mocha": "^9.2.0",
3644
"multihashes": "^4.0.3",
45+
"net-browserify": "^0.2.4",
3746
"node-polyfill-webpack-plugin": "^2.0.1",
3847
"path-browserify": "^1.0.1",
3948
"process": "^0.11.10",
49+
"querystring-es3": "^0.2.1",
4050
"snarkjs": "0.7.0",
4151
"starknet": "^3.15.0",
4252
"stream-browserify": "^3.0.0",
4353
"stream-http": "^3.2.0",
54+
"tls": "^0.0.1",
55+
"tty-browserify": "^0.0.1",
4456
"typescript": "^4.6.2",
4557
"web3": "^1.5.3",
4658
"webpack": "^5.74.0",

polyfills/v8.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as msgpack from "@msgpack/msgpack";
2+
const { Buffer } = require("buffer");
3+
4+
export let serialize = (v) =>
5+
Buffer.from(msgpack.encode(v, { extensionCodec }));
6+
export let deserialize = (v) => msgpack.decode(v, { extensionCodec });
7+
8+
// Derived fromƒ
9+
// https://github.com/msgpack/msgpack-javascript#extension-types
10+
const extensionCodec = new msgpack.ExtensionCodec();
11+
extensionCodec.register({
12+
type: 0,
13+
decode(value) {
14+
return new Set(msgpack.decode(value, { extensionCodec }));
15+
},
16+
encode(value) {
17+
return value instanceof Set
18+
? msgpack.encode([...value], { extensionCodec })
19+
: null;
20+
},
21+
});
22+
extensionCodec.register({
23+
type: 1,
24+
decode(value) {
25+
return new Map(msgpack.decode(value, { extensionCodec }));
26+
},
27+
encode(value) {
28+
return value instanceof Map
29+
? msgpack.encode([...value], { extensionCodec })
30+
: null;
31+
},
32+
});

src/script-runner.js

Lines changed: 82 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,46 @@ import "@babel/polyfill"
33
import * as ts from "typescript";
44
import { createClient } from '@remixproject/plugin-iframe'
55
import { PluginClient } from '@remixproject/plugin'
6-
import * as ethersJs from 'ethers' // eslint-disable-line
7-
import multihash from 'multihashes'
8-
import * as web3Js from 'web3'
96
import Web3 from 'web3'
107
import { waffleChai } from "@ethereum-waffle/chai";
11-
import * as starknet from 'starknet'
12-
import * as zokratesJs from 'zokrates-js';
13-
import * as circomlibjs from 'circomlibjs';
14-
const snarkjs = require('snarkjs');
15-
import * as zkkitIncrementalMerkleTree from '@zk-kit/incremental-merkle-tree';
16-
import * as semaphoreProtocolProof from '@semaphore-protocol/proof'
17-
// import * as semaphoreProtocolContracts from '@semaphore-protocol/contracts'
18-
import * as semaphoreProtocolGroup from '@semaphore-protocol/group'
19-
import * as semaphoreProtocolIdentity from '@semaphore-protocol/identity'
208
import './runWithMocha'
219
import * as path from 'path'
2210
import * as hhEtherMethods from './hardhat-ethers/methods'
11+
import { yarnLoader, yarnContext } from './yarn/yarn-loader'
2312
const chai = require('chai')
2413
chai.use(waffleChai)
2514

26-
window.starknet = starknet
2715
window.chai = chai
28-
window.ethers = ethersJs
29-
window.multihashes = multihash
30-
window['zokrates-js'] = zokratesJs
31-
window['snarkjs'] = snarkjs
32-
window['circomlibjs'] = circomlibjs
33-
window['@zk-kit/incremental-merkle-tree'] = zkkitIncrementalMerkleTree
34-
35-
window['@semaphore-protocol/proof'] = semaphoreProtocolProof
36-
// window['@semaphore-protocol/contracts'] = semaphoreProtocolContracts
37-
window['@semaphore-protocol/group'] = semaphoreProtocolGroup
38-
window['@semaphore-protocol/identity'] = semaphoreProtocolIdentity
3916

4017
const scriptReturns = {} // keep track of modules exported values
4118
const fileContents = {} // keep track of file content
42-
window.require = (module) => {
43-
if (module === 'web3') return web3Js
44-
if (window[module]) return window[module] // library
45-
else if ((module.endsWith('.json') || module.endsWith('.abi')) && window.__execPath__ && fileContents[window.__execPath__]) return JSON.parse(fileContents[window.__execPath__][module])
46-
else if (window.__execPath__ && scriptReturns[window.__execPath__]) return scriptReturns[window.__execPath__][module] // module exported values
47-
else throw new Error(`${module} module require is not supported by Remix IDE`)
19+
window.require = (module) => {
20+
if ((module.endsWith('.json') || module.endsWith('.abi')) && window.__execPath__ && fileContents[window.__execPath__]) return JSON.parse(fileContents[window.__execPath__][module])
21+
else if (window.__execPath__ && scriptReturns[window.__execPath__]) {
22+
let returns = scriptReturns[window.__execPath__][module] || scriptReturns[window.__execPath__][module + '.js'] // module exported values
23+
if (module === 'ethers') {
24+
// Support hardhat-ethers, See: https://hardhat.org/plugins/nomiclabs-hardhat-ethers.html
25+
returns.provider = new ethers.providers.Web3Provider(window.web3Provider)
26+
for(const method in hhEtherMethods) Object.defineProperty(returns, method, { value: hhEtherMethods[method]})
27+
}
28+
return returns
29+
} else {
30+
throw new Error(`${module} module require is not supported by Remix IDE`)
31+
}
4832
}
4933

5034
class CodeExecutor extends PluginClient {
35+
onActivation () {
36+
this.on('filePanel', 'setWorkspace', (workspace) => {
37+
console.log('opening a new workspace in script runner ', workspace)
38+
yarnLoader(this)
39+
})
40+
}
5141
async execute (script, filePath) {
5242
filePath = filePath || 'scripts/script.ts'
5343
const paths = filePath.split('/')
5444
paths.pop()
55-
const fromPath = paths.join('/') // get current execcution context path
45+
const fromPath = paths.join('/') // get current execution context path
5646
if (script) {
5747
try {
5848
script = ts.transpileModule(script, { moduleName: filePath, filePath,
@@ -76,16 +66,22 @@ class CodeExecutor extends PluginClient {
7666
if (!scriptReturns[fromPath]) scriptReturns[fromPath] = {}
7767
if (!fileContents[fromPath]) fileContents[fromPath] = {}
7868
const { returns, content } = await this.executeFile(absolutePath)
69+
7970
scriptReturns[fromPath][file] = returns
8071
fileContents[fromPath][file] = content
8172
}
8273

8374
// execute the script
84-
script = `const exports = {};
85-
const module = { exports: {} }
75+
script = `let exports = {};
76+
let module = { exports: {} }
8677
window.__execPath__ = "${fromPath}"
8778
${script};
88-
return exports || module.exports`
79+
if (Object.keys(exports).length) {
80+
return exports
81+
} else {
82+
return module.exports
83+
}
84+
`
8985
const returns = (new Function(script))()
9086
if (mocha.suite && ((mocha.suite.suites && mocha.suite.suites.length) || (mocha.suite.tests && mocha.suite.tests.length))) {
9187
console.log(`RUNS ${filePath}....`)
@@ -100,18 +96,61 @@ class CodeExecutor extends PluginClient {
10096
}
10197
}
10298

99+
async _resolveRemixFileSystem(fileName) {
100+
if (await this.call('fileManager', 'exists', fileName)) return { content: await this.call('fileManager', 'readFile', fileName) }
101+
if (await this.call('fileManager', 'exists', fileName + '.ts')) return { content: await this.call('fileManager', 'readFile', fileName + '.ts') }
102+
if (await this.call('fileManager', 'exists', fileName + '.js')) return { content: await this.call('fileManager', 'readFile', fileName + '.js') }
103+
return {content: null}
104+
}
105+
106+
async _resolveYarnFileSystem(fileName) {
107+
let path = fileName.startsWith('/app/node_modules/') ? fileName : `/app/node_modules/${fileName}`
108+
let content
109+
let stat
110+
try {
111+
stat = yarnContext.memfs.statSync(path)
112+
} catch (e) {}
113+
114+
if (stat && stat.isDirectory()) {
115+
const json = JSON.parse(yarnContext.memfs.readFileSync(`/app/node_modules/${fileName}/package.json`, 'utf8'))
116+
const path = `/app/node_modules/${fileName}/${json.main}`
117+
content = yarnContext.memfs.readFileSync(path, 'utf8')
118+
return { content, path }
119+
}
120+
121+
try {
122+
stat = yarnContext.memfs.statSync(path)
123+
} catch (e) {}
124+
125+
if (stat && stat.isFile()) {
126+
content = yarnContext.memfs.readFileSync(path, 'utf8')
127+
return { content, path }
128+
}
129+
130+
if (!path.endsWith('.js')) path = path + '.js'
131+
try {
132+
stat = yarnContext.memfs.statSync(path)
133+
} catch (e) {}
134+
135+
if (stat && stat.isFile()) {
136+
content = yarnContext.memfs.readFileSync(path, 'utf8')
137+
return { content, path }
138+
}
139+
return { content: null }
140+
}
141+
103142
async _resolveFile (fileName) {
104-
if (await this.call('fileManager', 'exists', fileName)) return await this.call('fileManager', 'readFile', fileName)
105-
if (await this.call('fileManager', 'exists', fileName + '.ts')) return await this.call('fileManager', 'readFile', fileName + '.ts')
106-
if (await this.call('fileManager', 'exists', fileName + '.js')) return await this.call('fileManager', 'readFile', fileName + '.js')
143+
const { content, path } = this._resolveRemixFileSystem(fileName)
144+
if (content) return { content, path }
145+
return this._resolveYarnFileSystem(fileName)
107146
}
108147

109148
async executeFile (fileName) {
110149
try {
111150
if (require(fileName)) return require(fileName)
112151
} catch (e) {}
113-
const content = await this._resolveFile(fileName)
114-
const returns = await this.execute(content, fileName)
152+
const { content, path } = await this._resolveFile(fileName)
153+
const returns = await this.execute(content, path || fileName)
115154
return {returns, content}
116155
}
117156
}
@@ -131,36 +170,30 @@ window.ethereum = web3Provider
131170

132171
window.web3 = new Web3(window.web3Provider)
133172

134-
// Support hardhat-ethers, See: https://hardhat.org/plugins/nomiclabs-hardhat-ethers.html
135-
const { ethers } = ethersJs
136-
ethers.provider = new ethers.providers.Web3Provider(window.web3Provider)
137-
window.hardhat = { ethers }
138-
for(const method in hhEtherMethods) Object.defineProperty(window.hardhat.ethers, method, { value: hhEtherMethods[method]})
139-
140173
console.logInternal = console.log
141174
console.log = function () {
142175
window.remix.emit('log', {
143-
data: Array.from(arguments).map((el) => JSON.parse(JSON.stringify(el)))
176+
data: Array.from(arguments).map((el) => el ? JSON.parse(JSON.stringify(el)) : 'undefined')
144177
})
145178
}
146179

147180
console.infoInternal = console.info
148181
console.info = function () {
149182
window.remix.emit('info', {
150-
data: Array.from(arguments).map((el) => JSON.parse(JSON.stringify(el)))
183+
data: Array.from(arguments).map((el) => el ? JSON.parse(JSON.stringify(el)) : 'undefined')
151184
})
152185
}
153186

154187
console.warnInternal = console.warn
155188
console.warn = function () {
156189
window.remix.emit('warn', {
157-
data: Array.from(arguments).map((el) => JSON.parse(JSON.stringify(el)))
190+
data: Array.from(arguments).map((el) => el ? JSON.parse(JSON.stringify(el)) : 'undefined')
158191
})
159192
}
160193

161194
console.errorInternal = console.error
162195
console.error = function () {
163196
window.remix.emit('error', {
164-
data: Array.from(arguments).map((el) => JSON.parse(JSON.stringify(el)))
197+
data: Array.from(arguments).map((el) => el ? JSON.parse(JSON.stringify(el)) : 'undefined')
165198
})
166199
}

0 commit comments

Comments
 (0)