Webpack -> Rollup

This commit is contained in:
Tony Garnock-Jones 2020-08-05 12:36:53 +02:00
parent 718f175b63
commit fa2c184545
28 changed files with 11202 additions and 73 deletions

2
.gitignore vendored
View File

@ -6,8 +6,6 @@ lib/
node_modules/ node_modules/
scratch/ scratch/
package-lock.json
*.did *.did
.do_built .do_built
.do_built.dir/ .do_built.dir/

11039
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,11 +6,14 @@
"@babel/plugin-syntax-jsx": "^7.10.1", "@babel/plugin-syntax-jsx": "^7.10.1",
"@babel/plugin-transform-react-jsx": "^7.10.1", "@babel/plugin-transform-react-jsx": "^7.10.1",
"@babel/preset-env": "^7.10.2", "@babel/preset-env": "^7.10.2",
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^8.4.0",
"esm": "^3.2.25",
"lerna": "^3.22.1", "lerna": "^3.22.1",
"mocha": "^7.2.0", "mocha": "^7.2.0",
"nyc": "^14.1.1", "nyc": "^14.1.1",
"webpack": "^4.43.0", "rollup": "^2.23.0"
"webpack-cli": "^3.3.11"
}, },
"dependencies": { "dependencies": {
"@syndicate-lang/core": "file:packages/core", "@syndicate-lang/core": "file:packages/core",

View File

@ -0,0 +1,3 @@
module.exports = {
randomBytes: void 0,
};

View File

@ -391,7 +391,7 @@ Dataspace.prototype._dotGraph = function () {
pieces.push('\n}'); pieces.push('\n}');
require('fs').writeFileSync('d.json', 'var dataspaceContents = ' + JSON.stringify({nodes, edges}, null, 2)); // require('fs').writeFileSync('d.json', 'var dataspaceContents = ' + JSON.stringify({nodes, edges}, null, 2));
return pieces.join(''); return pieces.join('');
}; };

View File

@ -378,7 +378,7 @@ Index.prototype.sendMessage = function(v, leafCallback) {
Node.prototype._debugString = function (outerIndent) { Node.prototype._debugString = function (outerIndent) {
const pieces = []; const pieces = [];
const inspect = require('util').inspect; const inspect = require('util').inspect || JSON.stringify;
function line(indent, content) { function line(indent, content) {
pieces.push(indent); pieces.push(indent);
pieces.push(content); pieces.push(content);

View File

@ -0,0 +1,3 @@
module.exports = {
inspect: void 0,
};

View File

@ -37,9 +37,7 @@ const worker_threads = (function () {
try { try {
return require('worker_threads'); return require('worker_threads');
} catch (_e) { } catch (_e) {
return { return require('./worker_stub.js');
__isDummyStub: true
};
} }
})(); })();

View File

@ -0,0 +1,3 @@
module.exports = {
__isDummyStub: true,
};

View File

@ -2,9 +2,9 @@ cd "$(dirname "$1")"
case "$1" in case "$1" in
*/all) */all)
for d in src/*.js; do [ -f "$d" ] && echo lib/$(basename "$d"); done | xargs redo-ifchange for d in src/*.js; do [ -f "$d" ] && echo lib/$(basename "$d"); done | xargs redo-ifchange
for d in *.webpack.config.js for d in *.dist.json
do do
[ -f "$d" ] && echo dist/$(basename "$d" .webpack.config.js).js [ -f "$d" ] && echo dist/$(basename "$d" .dist.json).js
done | xargs redo-ifchange done | xargs redo-ifchange
[ -f _all.do ] && redo-ifchange _all || true [ -f _all.do ] && redo-ifchange _all || true
;; ;;

View File

@ -40,35 +40,13 @@ case "$1" in
fi fi
;; ;;
*/dist/*) */dist/*)
# Conservatively assume the distribution depends on ALL the configfile=$(basename "$1" .js).dist.json
# local source files PLUS the lib/*.js of dependent syndicate
# packages, other than core which has src/*.js files instead.
#
for d in src/*.js; do echo lib/$(basename $d); done | xargs redo-ifchange
for dep in $(cat package.json | \
jq -r '.dependencies | to_entries[].key' | \
fgrep '@syndicate-lang/' | \
sed -e 's:^@syndicate-lang/::')
do
for srcfile in ../$dep/src/*.js
do
if [ -f $srcfile ]
then
if [ "$dep" = "core" ]
then
echo $srcfile
else
libfile=$(echo $srcfile | sed -e 's:/src/:/lib/:')
echo $libfile
fi
fi
done
done | xargs redo-ifchange
configfile=$(basename "$1" .js).webpack.config.js
redo-ifchange $configfile redo-ifchange $configfile
for maybedep in $(../../node_modules/.bin/webpack --config "$configfile" --json -o "$targettempfile" | jq -r '.modules[].identifier') redo-ifchange ../../rollup-redo
for maybedep in $(../../rollup-redo deps "$configfile" "$targettempfile")
do do
[ -f "$maybedep" ] && echo "$maybedep" [ -f "$maybedep" ] && echo "$maybedep"
done | xargs redo-ifchange done | xargs redo-ifchange
../../rollup-redo generate "$configfile" "$targettempfile"
;; ;;
esac esac

View File

@ -10,6 +10,6 @@
<div id="board-area" class="board"> <div id="board-area" class="board">
</div> </div>
</div> </div>
<script src="dist/main.js" type="text/javascript"></script> <script src="dist/main.js"></script>
</body> </body>
</html> </html>

View File

@ -0,0 +1,4 @@
{
"input": "lib/index.js",
"format": "iife"
}

View File

@ -1,7 +0,0 @@
module.exports = {
entry: "./lib/index.js",
mode: "development",
externals: {
crypto: 'null'
},
};

View File

@ -12,8 +12,6 @@
"dependencies": { "dependencies": {
"@syndicate-lang/core": "file:../core", "@syndicate-lang/core": "file:../core",
"@syndicate-lang/driver-browser-ui": "file:../driver-browser-ui", "@syndicate-lang/driver-browser-ui": "file:../driver-browser-ui",
"@syndicate-lang/driver-timer": "file:../driver-timer", "@syndicate-lang/driver-timer": "file:../driver-timer"
"webpack": "^4.27.1",
"webpack-cli": "^3.1.2"
} }
} }

View File

@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
import { $QuitDataspace, Inbound, Outbound, Dataspace, Double } from "@syndicate-lang/core"; import { bootModule, $QuitDataspace, Inbound, Outbound, Dataspace, Double } from "@syndicate-lang/core";
let UI = activate require("@syndicate-lang/driver-browser-ui"); let UI = activate require("@syndicate-lang/driver-browser-ui");
// @jsx UI.html // @jsx UI.html
@ -170,3 +170,9 @@ function spawnGame() {
} }
} }
} }
// Running Syndicate programs in node.js doesn't require this -- it
// automatically detects the "main" module -- but in the browser, when
// using rollup, the mechanism doesn't work properly so we explicitly
// activate *ourselves* here.
bootModule(module);

View File

@ -0,0 +1,3 @@
{
"format": "iife"
}

View File

@ -1,7 +0,0 @@
module.exports = {
entry: "./lib/chat.js",
mode: "development",
externals: {
crypto: 'null'
},
};

View File

@ -0,0 +1 @@
{}

View File

@ -1,7 +0,0 @@
module.exports = {
entry: "./lib/monitor.js",
mode: "development",
externals: {
crypto: 'null'
},
};

View File

@ -82,3 +82,5 @@ function randomName() {
return names[Math.floor(Math.random() * names.length)] + return names[Math.floor(Math.random() * names.length)] +
'_' + Math.floor(Math.random() * 990 + 10); '_' + Math.floor(Math.random() * 990 + 10);
} }
require('@syndicate-lang/core').bootModule(module);

View File

@ -144,12 +144,16 @@ function spawnMonitorAppServer(port) {
'<!DOCTYPE html>' + UI.htmlToString( '<!DOCTYPE html>' + UI.htmlToString(
<html> <html>
<head><meta charset="utf-8"></meta></head> <head><meta charset="utf-8"></meta></head>
<body><script src="dist/monitor.js"></script></body> <body><script type="module" src="dist/monitor.js"></script></body>
</html>)); </html>));
} }
function assertFileResponse(reqId, path) { function assertFileResponse(reqId, path) {
assert :snapshot Http.Response(reqId, 200, "OK", {}, fs.readFileSync(path)); let type = 'application/octet-stream';
if (path.endsWith('.js')) {
type = 'text/javascript';
}
assert :snapshot Http.Response(reqId, 200, "OK", {"Content-type": type}, fs.readFileSync(path));
} }
during Http.Request($reqId, server, 'get', ['chat.html'], _, _) during Http.Request($reqId, server, 'get', ['chat.html'], _, _)

View File

@ -112,3 +112,5 @@ spawn {
} }
} }
} }
require('@syndicate-lang/core').bootModule(module);

View File

@ -0,0 +1,4 @@
{
"input": "lib/index.js",
"format": "iife"
}

View File

@ -1,7 +0,0 @@
module.exports = {
entry: "./lib/index.js",
mode: "development",
externals: {
crypto: 'null'
},
};

View File

@ -68,3 +68,5 @@ spawn named 'alerter' {
alert("Hello!"); alert("Hello!");
} }
} }
require('@syndicate-lang/core').bootModule(module);

View File

@ -345,7 +345,7 @@ export default declare((api, options) => {
}; };
DATASPACE._currentFacet = SAVEDGLOBALFACET; DATASPACE._currentFacet = SAVEDGLOBALFACET;
SAVEDGLOBALFACET = null; SAVEDGLOBALFACET = null;
if (require.main === module) { if ((typeof require === 'undefined' ? {main: void 0} : require).main === module) {
SYNDICATE.bootModule(module); SYNDICATE.bootModule(module);
}`)({ }`)({
DATASPACE: state.DataspaceID, DATASPACE: state.DataspaceID,

106
rollup-redo Executable file
View File

@ -0,0 +1,106 @@
#!/usr/bin/env -S node -r esm
import * as rollup from 'rollup';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import * as path from 'path';
function computeBasename(f) {
const m = /^(.*).dist.json$/.exec(f);
if (!m) throw new Error(`Config filename ${f} does not match pattern`);
return m[1];
}
const [_node, _rollup_redo, mode, configJsonFilename, targetFilename] = process.argv;
const basename = computeBasename(configJsonFilename);
const config = require(`${process.cwd()}/${configJsonFilename}`);
const inputFile = ('input' in config)
? path.resolve(process.cwd(), config.input)
: `${process.cwd()}/lib/${basename}.js`;
async function build() {
const bundle = await rollup.rollup({
input: inputFile,
external: ['crypto'],
plugins: [
{
resolveId(toResolve, referencingModule) {
{
const m = /^@syndicate-lang\/(.*)$/.exec(toResolve);
if (m) {
if (mode === 'deps') {
console.log(`../${m[1]}/all`);
}
}
}
if (/^\0?util(\?commonjs.*)?$/.test(toResolve)) {
return '../core/src/util_stub.js';
}
if (/^\0?crypto(\?commonjs.*)?$/.test(toResolve)) {
return '../core/src/crypto_stub.js';
}
if (/^\0?worker_threads(\?commonjs.*)?$/.test(toResolve)) {
return '../core/src/worker_stub.js';
}
return null;
},
},
resolve({
browser: true,
}),
commonjs(),
json(),
],
onwarn(w, defaultHandler) {
if (((w.code === 'UNRESOLVED_IMPORT') ||
(w.code === 'MISSING_GLOBAL_NAME')) &&
(w.source.startsWith('@syndicate-lang/')))
{
return;
}
defaultHandler(w);
},
treeshake: {
moduleSideEffects: "no-external",
},
});
switch (mode) {
case 'deps': {
const deps = {};
for (const m of bundle.cache.modules) {
deps[m.id] = m.dependencies;
}
const seen = {};
function visit(id) {
if (id in seen) return;
if (!id.startsWith('/')) return;
seen[id] = true;
console.log(id);
for (const dep of (deps[id] || [])) {
visit(dep);
}
}
visit(inputFile);
}
case 'generate': {
await bundle.write({
output: {
file: targetFilename,
format: config.format || 'es',
name: config.name || 'Syndicate_' + basename,
},
});
break;
}
default:
console.error('Unknown mode', mode);
break;
}
}
build().then(null, console.error);