From eaa1602b1ab3a2a5c3610f95efdc8ddb5fce1c29 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Fri, 23 Sep 2022 15:42:41 +0530 Subject: [PATCH] fix: json import, js-base64 Co-authored-by: Alois Klink --- .vite/build.ts | 11 ++++------- .vite/tsconfig.json | 6 ++++++ cypress/helpers/util.js | 6 ++++-- cypress/platform/viewer.js | 9 ++++++--- package.json | 8 +++----- yarn.lock | 7 +------ 6 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 .vite/tsconfig.json diff --git a/.vite/build.ts b/.vite/build.ts index c4678bbff..da53ff664 100644 --- a/.vite/build.ts +++ b/.vite/build.ts @@ -2,18 +2,15 @@ import { build, InlineConfig } from 'vite'; import { resolve } from 'path'; import { fileURLToPath } from 'url'; import jisonPlugin from './jisonPlugin.js'; -import { readFileSync } from 'node:fs'; - +import pkg from '../package.json' assert { type: 'json' }; type OutputOptions = Exclude< Exclude['rollupOptions'], undefined >['output']; -const __dirname = fileURLToPath(new URL('.', import.meta.url)); -const { dependencies } = JSON.parse( - readFileSync(resolve(__dirname, '../package.json'), { encoding: 'utf8' }) -); +const { dependencies } = pkg; const watch = process.argv.includes('--watch'); +const __dirname = fileURLToPath(new URL('.', import.meta.url)); interface BuildOptions { minify: boolean | 'esbuild'; @@ -23,7 +20,7 @@ interface BuildOptions { export const getBuildConfig = ({ minify, core, watch }: BuildOptions): InlineConfig => { const external = ['require', 'fs', 'path']; - let output: OutputOptions = [ + let output: OutputOptions | OutputOptions[] = [ { name: 'mermaid', format: 'esm', diff --git a/.vite/tsconfig.json b/.vite/tsconfig.json new file mode 100644 index 000000000..915436da1 --- /dev/null +++ b/.vite/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "module": "ES2022" + } +} diff --git a/cypress/helpers/util.js b/cypress/helpers/util.js index d138dfcfe..bee9a59f0 100644 --- a/cypress/helpers/util.js +++ b/cypress/helpers/util.js @@ -1,4 +1,6 @@ -import { Base64 } from 'js-base64'; +const utf8ToB64 = (str) => { + return window.btoa(unescape(encodeURIComponent(str))); +}; export const mermaidUrl = (graphStr, options, api) => { const obj = { @@ -6,7 +8,7 @@ export const mermaidUrl = (graphStr, options, api) => { mermaid: options, }; const objStr = JSON.stringify(obj); - let url = 'http://localhost:9000/e2e.html?graph=' + Base64.encodeURI(objStr); + let url = 'http://localhost:9000/e2e.html?graph=' + utf8ToB64(objStr); if (api) { url = 'http://localhost:9000/xss.html?graph=' + graphStr; } diff --git a/cypress/platform/viewer.js b/cypress/platform/viewer.js index 01c1a96f6..aea2b0f0a 100644 --- a/cypress/platform/viewer.js +++ b/cypress/platform/viewer.js @@ -1,6 +1,9 @@ -import { Base64 } from '../../node_modules/js-base64'; import mermaid2 from '../../src/mermaid'; +function b64ToUtf8(str) { + return decodeURIComponent(escape(window.atob(str))); +} + /** * ##contentLoaded Callback function that is called when page is loaded. This functions fetches * configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the @@ -11,7 +14,7 @@ const contentLoaded = function () { if (pos > 0) { pos = pos + 7; const graphBase64 = document.location.href.substr(pos); - const graphObj = JSON.parse(Base64.decode(graphBase64)); + const graphObj = JSON.parse(b64ToUtf8(graphBase64)); if (graphObj.mermaid && graphObj.mermaid.theme === 'dark') { document.body.style.background = '#3f3f3f'; } @@ -67,7 +70,7 @@ const contentLoadedApi = function () { if (pos > 0) { pos = pos + 7; const graphBase64 = document.location.href.substr(pos); - const graphObj = JSON.parse(Base64.decode(graphBase64)); + const graphObj = JSON.parse(window.btoa(graphBase64)); // const graph = 'hello' if (Array.isArray(graphObj.code)) { const numCodes = graphObj.code.length; diff --git a/package.json b/package.json index 591b30325..d645b964e 100644 --- a/package.json +++ b/package.json @@ -25,18 +25,18 @@ ], "scripts": { "clean": "rimraf dist", - "build:vite": "ts-node-esm .vite/build.ts", + "build:vite": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts", "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly", "build:watch": "yarn build:code --watch", "build": "yarn clean; concurrently \"yarn build:vite\" \"yarn build:types\"", "dev": "concurrently \"yarn build:vite --watch\" \"ts-node-esm .vite/server\"", - "docs:build": "ts-node-esm src/docs.mts", + "docs:build": "ts-node-esm --transpileOnly src/docs.mts", "docs:verify": "yarn docs:build --verify", "postbuild": "documentation build src/mermaidAPI.ts src/config.ts src/defaultConfig.ts --shallow -f md --markdown-toc false > src/docs/Setup.md && prettier --write src/docs/Setup.md", "release": "yarn build", "lint": "eslint --cache --ignore-path .gitignore . && yarn lint:jison && prettier --check .", "lint:fix": "eslint --fix --ignore-path .gitignore . && prettier --write .", - "lint:jison": "ts-node-esm src/jison/lint.mts", + "lint:jison": "ts-node-esm --transpileOnly src/jison/lint.mts", "cypress": "cypress run", "cypress:open": "cypress open", "e2e": "start-server-and-test dev http://localhost:9000/ cypress", @@ -98,7 +98,6 @@ "cypress": "^10.0.0", "cypress-image-snapshot": "^4.0.1", "documentation": "13.2.0", - "esbuild": "^0.15.8", "eslint": "^8.23.1", "eslint-config-prettier": "^8.5.0", "eslint-plugin-cypress": "^2.12.1", @@ -112,7 +111,6 @@ "husky": "^8.0.0", "identity-obj-proxy": "^3.0.0", "jison": "^0.4.18", - "js-base64": "3.7.2", "jsdom": "^20.0.0", "lint-staged": "^13.0.0", "moment": "^2.23.0", diff --git a/yarn.lock b/yarn.lock index 45ca7ba18..f21b8ba5b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4601,7 +4601,7 @@ esbuild-windows-arm64@0.15.8: resolved "https://registry.yarnpkg.com/esbuild-windows-arm64/-/esbuild-windows-arm64-0.15.8.tgz#1d75235290bf23a111e6c0b03febd324af115cb1" integrity sha512-dbpAb0VyPaUs9mgw65KRfQ9rqiWCHpNzrJusoPu+LpEoswosjt/tFxN7cd2l68AT4qWdBkzAjDLRon7uqMeWcg== -esbuild@^0.15.6, esbuild@^0.15.8: +esbuild@^0.15.6: version "0.15.8" resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.15.8.tgz#75daa25d03f6dd9cc9355030eba2b93555b42cd4" integrity sha512-Remsk2dmr1Ia65sU+QasE6svJbsHe62lzR+CnjpUvbZ+uSYo1SitiOWPRfZQkCu82YWZBBKXiD/j0i//XWMZ+Q== @@ -6448,11 +6448,6 @@ jpeg-js@0.4.4: resolved "https://registry.yarnpkg.com/jpeg-js/-/jpeg-js-0.4.4.tgz#a9f1c6f1f9f0fa80cdb3484ed9635054d28936aa" integrity sha512-WZzeDOEtTOBK4Mdsar0IqEU5sMr3vSV2RqkAIzUEV2BHnUfKGyswWFPFwK5EeDo93K3FohSHbLAjj0s1Wzd+dg== -js-base64@3.7.2: - version "3.7.2" - resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-3.7.2.tgz#816d11d81a8aff241603d19ce5761e13e41d7745" - integrity sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ== - js-sdsl@^4.1.4: version "4.1.4" resolved "https://registry.yarnpkg.com/js-sdsl/-/js-sdsl-4.1.4.tgz#78793c90f80e8430b7d8dc94515b6c77d98a26a6"