From 7bf2a3929c6cdd64cf5a6abaac3841fcf46f3f84 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 27 Aug 2020 18:45:54 +0200 Subject: [PATCH] add another-json as a dependency also add a postinstall script to turn it into an ES module, so it works with our setup --- package.json | 4 ++- scripts/post-install.mjs | 53 ++++++++++++++++++++++++++++++++++++++++ yarn.lock | 5 ++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 scripts/post-install.mjs diff --git a/package.json b/package.json index 3935c174..a4f4c082 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "scripts": { "test": "node_modules/.bin/impunity --entry-point src/main.js --force-esm", "start": "node scripts/serve-local.js", - "build": "node --experimental-modules scripts/build.mjs" + "build": "node --experimental-modules scripts/build.mjs", + "postinstall": "node ./scripts/post-install.mjs" }, "repository": { "type": "git", @@ -46,6 +47,7 @@ "xxhash": "^0.3.0" }, "dependencies": { + "another-json": "^0.2.0", "olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz" } } diff --git a/scripts/post-install.mjs b/scripts/post-install.mjs new file mode 100644 index 00000000..3b287fd9 --- /dev/null +++ b/scripts/post-install.mjs @@ -0,0 +1,53 @@ +/* +Copyright 2020 Bruno Windels +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. +*/ + +import fsRoot from "fs"; +const fs = fsRoot.promises; +import path from "path"; +import { rollup } from 'rollup'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +// needed to translate commonjs modules to esm +import commonjs from '@rollup/plugin-commonjs'; +// multi-entry plugin so we can add polyfill file to main + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const projectDir = path.join(__dirname, "../"); + +async function commonjsToESM(src, dst) { + // create js bundle + const bundle = await rollup({ + input: src, + plugins: [commonjs()] + }); + const {output} = await bundle.generate({ + format: 'es' + }); + const code = output[0].code; + await fs.writeFile(dst, code, "utf8"); +} + +async function transpile() { + await fs.mkdir(path.join(projectDir, "lib/another-json/")); + await commonjsToESM( + path.join(projectDir, 'node_modules/another-json/another-json.js'), + path.join(projectDir, "lib/another-json/index.js") + ); +} + +transpile(); diff --git a/yarn.lock b/yarn.lock index 9e556624..ea3c6ba5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -907,6 +907,11 @@ dependencies: "@types/node" "*" +another-json@^0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/another-json/-/another-json-0.2.0.tgz#b5f4019c973b6dd5c6506a2d93469cb6d32aeedc" + integrity sha1-tfQBnJc7bdXGUGotk0acttMq7tw= + ansi-styles@^3.2.1: version "3.2.1" resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"