2020-08-27 18:45:54 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-03-08 14:40:21 +01:00
|
|
|
const fsRoot = require("fs");
|
2020-08-27 18:45:54 +02:00
|
|
|
const fs = fsRoot.promises;
|
2021-03-08 14:40:21 +01:00
|
|
|
const path = require("path");
|
|
|
|
const { rollup } = require('rollup');
|
|
|
|
const { fileURLToPath } = require('url');
|
|
|
|
const { dirname } = require('path');
|
2020-08-27 18:45:54 +02:00
|
|
|
// needed to translate commonjs modules to esm
|
2021-03-08 14:40:21 +01:00
|
|
|
const commonjs = require('@rollup/plugin-commonjs');
|
|
|
|
const { nodeResolve } = require('@rollup/plugin-node-resolve');
|
2020-08-27 18:45:54 +02:00
|
|
|
|
|
|
|
const projectDir = path.join(__dirname, "../");
|
|
|
|
|
2021-03-08 14:40:21 +01:00
|
|
|
async function removeDirIfExists(targetDir) {
|
|
|
|
try {
|
|
|
|
await fs.rmdir(targetDir, {recursive: true});
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== "ENOENT") {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:58:35 +02:00
|
|
|
/** function used to resolve common-js require calls below. */
|
|
|
|
function packageIterator(request, start, defaultIterator) {
|
|
|
|
// this is just working for bs58, would need to tune it further for other dependencies
|
|
|
|
if (request === "safe-buffer") {
|
|
|
|
return [path.join(projectDir, "/scripts/package-overrides/safe-buffer")];
|
|
|
|
} else if (request === "buffer/") {
|
|
|
|
return [path.join(projectDir, "/scripts/package-overrides/buffer")];
|
|
|
|
} else {
|
|
|
|
return defaultIterator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:45:54 +02:00
|
|
|
async function commonjsToESM(src, dst) {
|
|
|
|
// create js bundle
|
|
|
|
const bundle = await rollup({
|
|
|
|
input: src,
|
2020-09-16 16:58:35 +02:00
|
|
|
plugins: [commonjs(), nodeResolve({
|
|
|
|
browser: true,
|
|
|
|
preferBuiltins: false,
|
|
|
|
customResolveOptions: {packageIterator}
|
|
|
|
})]
|
2020-08-27 18:45:54 +02:00
|
|
|
});
|
|
|
|
const {output} = await bundle.generate({
|
|
|
|
format: 'es'
|
|
|
|
});
|
|
|
|
const code = output[0].code;
|
|
|
|
await fs.writeFile(dst, code, "utf8");
|
|
|
|
}
|
|
|
|
|
2020-08-27 19:51:04 +02:00
|
|
|
async function populateLib() {
|
|
|
|
const libDir = path.join(projectDir, "lib/");
|
|
|
|
await removeDirIfExists(libDir);
|
|
|
|
await fs.mkdir(libDir);
|
2021-03-08 14:40:21 +01:00
|
|
|
const olmSrcDir = path.dirname(require.resolve("olm"));
|
2020-08-27 19:51:04 +02:00
|
|
|
const olmDstDir = path.join(libDir, "olm/");
|
|
|
|
await fs.mkdir(olmDstDir);
|
|
|
|
for (const file of ["olm.js", "olm.wasm", "olm_legacy.js"]) {
|
|
|
|
await fs.symlink(path.join(olmSrcDir, file), path.join(olmDstDir, file));
|
|
|
|
}
|
|
|
|
// transpile another-json to esm
|
|
|
|
await fs.mkdir(path.join(libDir, "another-json/"));
|
2020-08-27 18:45:54 +02:00
|
|
|
await commonjsToESM(
|
2021-03-08 14:40:21 +01:00
|
|
|
require.resolve('another-json/another-json.js'),
|
2020-08-27 19:51:04 +02:00
|
|
|
path.join(libDir, "another-json/index.js")
|
2020-08-27 18:45:54 +02:00
|
|
|
);
|
2020-09-17 17:55:35 +02:00
|
|
|
// transpile bs58 to esm
|
|
|
|
await fs.mkdir(path.join(libDir, "bs58/"));
|
2020-09-16 16:58:35 +02:00
|
|
|
await commonjsToESM(
|
2021-03-08 14:40:21 +01:00
|
|
|
require.resolve('bs58/index.js'),
|
2020-09-17 17:55:35 +02:00
|
|
|
path.join(libDir, "bs58/index.js")
|
|
|
|
);
|
|
|
|
// transpile base64-arraybuffer to esm
|
|
|
|
await fs.mkdir(path.join(libDir, "base64-arraybuffer/"));
|
|
|
|
await commonjsToESM(
|
2021-03-08 14:40:21 +01:00
|
|
|
require.resolve('base64-arraybuffer/lib/base64-arraybuffer.js'),
|
2020-09-17 17:55:35 +02:00
|
|
|
path.join(libDir, "base64-arraybuffer/index.js")
|
2020-09-16 16:58:35 +02:00
|
|
|
);
|
2020-09-18 12:04:22 +02:00
|
|
|
// this probably should no go in here, we can just import "aes-js" from legacy-extras.js
|
|
|
|
// as that file is never loaded from a browser
|
|
|
|
|
2020-09-16 18:20:39 +02:00
|
|
|
// transpile aesjs to esm
|
|
|
|
await fs.mkdir(path.join(libDir, "aes-js/"));
|
|
|
|
await commonjsToESM(
|
2021-03-08 14:40:21 +01:00
|
|
|
require.resolve('aes-js/index.js'),
|
2020-09-16 18:20:39 +02:00
|
|
|
path.join(libDir, "aes-js/index.js")
|
|
|
|
);
|
2020-09-28 15:27:38 +02:00
|
|
|
// es6-promise is already written as an es module,
|
|
|
|
// but it does need to be babelified, and current we don't babelify
|
|
|
|
// anything in node_modules in the build script, so make a bundle that
|
|
|
|
// is conveniently not placed in node_modules rather than symlinking.
|
|
|
|
await fs.mkdir(path.join(libDir, "es6-promise/"));
|
|
|
|
await commonjsToESM(
|
2021-03-08 14:40:21 +01:00
|
|
|
require.resolve('es6-promise/lib/es6-promise/promise.js'),
|
2020-09-28 15:27:38 +02:00
|
|
|
path.join(libDir, "es6-promise/index.js")
|
|
|
|
);
|
2020-08-27 18:45:54 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 19:51:04 +02:00
|
|
|
populateLib();
|