2020-08-27 22:15:54 +05:30
|
|
|
/*
|
|
|
|
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 19:10:21 +05:30
|
|
|
const fsRoot = require("fs");
|
2020-08-27 22:15:54 +05:30
|
|
|
const fs = fsRoot.promises;
|
2021-03-08 19:10:21 +05:30
|
|
|
const path = require("path");
|
|
|
|
const { rollup } = require('rollup');
|
|
|
|
const { fileURLToPath } = require('url');
|
|
|
|
const { dirname } = require('path');
|
2020-08-27 22:15:54 +05:30
|
|
|
// needed to translate commonjs modules to esm
|
2021-03-08 19:10:21 +05:30
|
|
|
const commonjs = require('@rollup/plugin-commonjs');
|
2021-07-08 00:51:10 +05:30
|
|
|
const json = require('@rollup/plugin-json');
|
2021-03-08 19:10:21 +05:30
|
|
|
const { nodeResolve } = require('@rollup/plugin-node-resolve');
|
2020-08-27 22:15:54 +05:30
|
|
|
|
|
|
|
const projectDir = path.join(__dirname, "../");
|
|
|
|
|
2021-03-08 19:10:21 +05:30
|
|
|
async function removeDirIfExists(targetDir) {
|
|
|
|
try {
|
|
|
|
await fs.rmdir(targetDir, {recursive: true});
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== "ENOENT") {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 20:28:35 +05:30
|
|
|
/** 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 22:15:54 +05:30
|
|
|
async function commonjsToESM(src, dst) {
|
|
|
|
// create js bundle
|
|
|
|
const bundle = await rollup({
|
2021-06-02 16:02:03 +05:30
|
|
|
treeshake: {moduleSideEffects: false},
|
2020-08-27 22:15:54 +05:30
|
|
|
input: src,
|
2021-07-08 00:51:10 +05:30
|
|
|
plugins: [commonjs(), json(), nodeResolve({
|
2020-09-16 20:28:35 +05:30
|
|
|
browser: true,
|
|
|
|
preferBuiltins: false,
|
|
|
|
customResolveOptions: {packageIterator}
|
|
|
|
})]
|
2020-08-27 22:15:54 +05:30
|
|
|
});
|
|
|
|
const {output} = await bundle.generate({
|
|
|
|
format: 'es'
|
|
|
|
});
|
|
|
|
const code = output[0].code;
|
|
|
|
await fs.writeFile(dst, code, "utf8");
|
|
|
|
}
|
|
|
|
|
2020-08-27 23:21:04 +05:30
|
|
|
async function populateLib() {
|
|
|
|
const libDir = path.join(projectDir, "lib/");
|
|
|
|
await removeDirIfExists(libDir);
|
|
|
|
await fs.mkdir(libDir);
|
2021-06-08 20:48:16 +05:30
|
|
|
const olmSrcDir = path.dirname(require.resolve("@matrix-org/olm"));
|
2020-08-27 23:21:04 +05:30
|
|
|
const olmDstDir = path.join(libDir, "olm/");
|
|
|
|
await fs.mkdir(olmDstDir);
|
|
|
|
for (const file of ["olm.js", "olm.wasm", "olm_legacy.js"]) {
|
2021-08-07 01:16:41 +05:30
|
|
|
await fs.copyFile(path.join(olmSrcDir, file), path.join(olmDstDir, file));
|
2020-08-27 23:21:04 +05:30
|
|
|
}
|
2021-07-10 01:36:24 +05:30
|
|
|
// transpile node-html-parser to esm
|
2021-07-08 00:51:10 +05:30
|
|
|
await fs.mkdir(path.join(libDir, "node-html-parser/"));
|
|
|
|
await commonjsToESM(
|
|
|
|
require.resolve('node-html-parser/dist/index.js'),
|
|
|
|
path.join(libDir, "node-html-parser/index.js")
|
|
|
|
);
|
|
|
|
// transpile another-json to esm
|
2020-08-27 23:21:04 +05:30
|
|
|
await fs.mkdir(path.join(libDir, "another-json/"));
|
2020-08-27 22:15:54 +05:30
|
|
|
await commonjsToESM(
|
2021-03-08 19:10:21 +05:30
|
|
|
require.resolve('another-json/another-json.js'),
|
2020-08-27 23:21:04 +05:30
|
|
|
path.join(libDir, "another-json/index.js")
|
2020-08-27 22:15:54 +05:30
|
|
|
);
|
2020-09-17 21:25:35 +05:30
|
|
|
// transpile bs58 to esm
|
|
|
|
await fs.mkdir(path.join(libDir, "bs58/"));
|
2020-09-16 20:28:35 +05:30
|
|
|
await commonjsToESM(
|
2021-03-08 19:10:21 +05:30
|
|
|
require.resolve('bs58/index.js'),
|
2020-09-17 21:25:35 +05:30
|
|
|
path.join(libDir, "bs58/index.js")
|
|
|
|
);
|
|
|
|
// transpile base64-arraybuffer to esm
|
|
|
|
await fs.mkdir(path.join(libDir, "base64-arraybuffer/"));
|
|
|
|
await commonjsToESM(
|
2021-03-08 19:10:21 +05:30
|
|
|
require.resolve('base64-arraybuffer/lib/base64-arraybuffer.js'),
|
2020-09-17 21:25:35 +05:30
|
|
|
path.join(libDir, "base64-arraybuffer/index.js")
|
2020-09-16 20:28:35 +05:30
|
|
|
);
|
2020-09-18 15:34:22 +05:30
|
|
|
// 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 21:50:39 +05:30
|
|
|
// transpile aesjs to esm
|
|
|
|
await fs.mkdir(path.join(libDir, "aes-js/"));
|
|
|
|
await commonjsToESM(
|
2021-03-08 19:10:21 +05:30
|
|
|
require.resolve('aes-js/index.js'),
|
2020-09-16 21:50:39 +05:30
|
|
|
path.join(libDir, "aes-js/index.js")
|
|
|
|
);
|
2020-09-28 18:57:38 +05:30
|
|
|
// 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 19:10:21 +05:30
|
|
|
require.resolve('es6-promise/lib/es6-promise/promise.js'),
|
2020-09-28 18:57:38 +05:30
|
|
|
path.join(libDir, "es6-promise/index.js")
|
|
|
|
);
|
2021-06-02 16:02:03 +05:30
|
|
|
// fake-indexeddb, used for tests (but unresolvable bare imports also makes the build complain)
|
|
|
|
// and might want to use it for in-memory storage too, although we probably do ts->es6 with esm
|
|
|
|
// directly rather than ts->es5->es6 as we do now. The bundle is 240K currently.
|
|
|
|
await fs.mkdir(path.join(libDir, "fake-indexeddb/"));
|
|
|
|
await commonjsToESM(
|
|
|
|
path.join(projectDir, "/scripts/package-overrides/fake-indexeddb.js"),
|
|
|
|
path.join(libDir, "fake-indexeddb/index.js")
|
|
|
|
);
|
2020-08-27 22:15:54 +05:30
|
|
|
}
|
|
|
|
|
2020-08-27 23:21:04 +05:30
|
|
|
populateLib();
|