Merge pull request #5838 from bollwyvl/gh-5747-relative-paths

fix 5747 replace $root with relative paths
This commit is contained in:
Sidharth Vinod
2024-09-09 11:47:58 +00:00
committed by GitHub
49 changed files with 287 additions and 156 deletions

View File

@@ -0,0 +1,5 @@
---
'mermaid': patch
---
fix: Replace $root with relative paths

View File

@@ -38,6 +38,10 @@ jobs:
run: |
pnpm exec vitest run ./packages/mermaid/src/diagrams/gantt/ganttDb.spec.ts --coverage
- name: Verify out-of-tree build with TypeScript
run: |
pnpm test:check:tsc
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0
# Run step only pushes to develop and pull_requests

View File

@@ -42,6 +42,7 @@
"test": "pnpm lint && vitest run",
"test:watch": "vitest --watch",
"test:coverage": "vitest --coverage",
"test:check:tsc": "tsx scripts/tsc-check.ts",
"prepare": "husky && pnpm build",
"pre-commit": "lint-staged"
},

View File

@@ -1,4 +1,4 @@
import { unknownIcon } from '$root/rendering-util/icons.js';
import { unknownIcon } from '../../rendering-util/icons.js';
import type { IconifyJSON } from '@iconify/types';
const wrapIcon = (icon: string) => {

View File

@@ -1,4 +1,4 @@
import { registerIconPacks } from '$root/rendering-util/icons.js';
import { registerIconPacks } from '../../rendering-util/icons.js';
import type { Position } from 'cytoscape';
import cytoscape from 'cytoscape';
import type { FcoseLayoutOptions } from 'cytoscape-fcose';

View File

@@ -1,4 +1,4 @@
import { getIconSVG } from '$root/rendering-util/icons.js';
import { getIconSVG } from '../../rendering-util/icons.js';
import type cytoscape from 'cytoscape';
import { getConfig } from '../../diagram-api/diagramAPI.js';
import { createText } from '../../rendering-util/createText.js';

View File

@@ -2,7 +2,7 @@
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid
* functionality and to render the diagrams to svg code!
*/
import { registerIconPacks } from '$root/rendering-util/icons.js';
import { registerIconPacks } from './rendering-util/icons.js';
import { dedent } from 'ts-dedent';
import type { MermaidConfig } from './config.type.js';
import { detectType, registerLazyLoadedDiagrams } from './diagram-api/detectType.js';

View File

@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// @ts-nocheck TODO: Fix types
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import common, { hasKatex, renderKatex } from '$root/diagrams/common/common.js';
import { getConfig } from '../diagram-api/diagramAPI.js';
import common, { hasKatex, renderKatex } from '../diagrams/common/common.js';
import { select } from 'd3';
import type { MermaidConfig } from '../config.type.js';
import type { SVGGroup } from '../diagram-api/types.js';

View File

@@ -1,4 +1,4 @@
import { log } from '$root/logger.js';
import { log } from '../logger.js';
import type { ExtendedIconifyIcon, IconifyIcon, IconifyJSON } from '@iconify/types';
import type { IconifyIconCustomisations } from '@iconify/utils';
import { getIconData, iconToHTML, iconToSVG, replaceIDs, stringToIcon } from '@iconify/utils';

View File

@@ -23,7 +23,7 @@ import {
insertEdge,
clear as clearEdges,
} from '../../rendering-elements/edges.js';
import { log } from '$root/logger.js';
import { log } from '../../../logger.js';
import { getSubGraphTitleMargins } from '../../../utils/subGraphTitleMargins.js';
import { getConfig } from '../../../diagram-api/diagramAPI.js';

View File

@@ -1,5 +1,5 @@
/** Decorates with functions required by mermaids dagre-wrapper. */
import { log } from '$root/logger.js';
import { log } from '../../../logger.js';
import * as graphlib from 'dagre-d3-es/src/graphlib/index.js';
import * as graphlibJson from 'dagre-d3-es/src/graphlib/json.js';

View File

@@ -6,7 +6,7 @@ import {
extractDescendants,
sortNodesByHierarchy,
} from './mermaid-graphlib.js';
import { setLogLevel, log } from '$root/logger.js';
import { setLogLevel, log } from '../../../logger.js';
describe('Graphlib decorations', () => {
let g;

View File

@@ -1,7 +1,7 @@
import type { SVG } from '$root/diagram-api/types.js';
import type { InternalHelpers } from '$root/internals.js';
import { internalHelpers } from '$root/internals.js';
import { log } from '$root/logger.js';
import type { SVG } from '../diagram-api/types.js';
import type { InternalHelpers } from '../internals.js';
import { internalHelpers } from '../internals.js';
import { log } from '../logger.js';
import type { LayoutData } from './types.js';
export interface RenderOptions {

View File

@@ -1,17 +1,14 @@
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import { evaluate } from '$root/diagrams/common/common.js';
import { log } from '$root/logger.js';
import { getSubGraphTitleMargins } from '$root/utils/subGraphTitleMargins.js';
import { getConfig } from '../../diagram-api/diagramAPI.js';
import { evaluate } from '../../diagrams/common/common.js';
import { log } from '../../logger.js';
import { getSubGraphTitleMargins } from '../../utils/subGraphTitleMargins.js';
import { select } from 'd3';
import rough from 'roughjs';
import { createText } from '../createText.ts';
import intersectRect from '../rendering-elements/intersect/intersect-rect.js';
import createLabel from './createLabel.js';
import { createRoundedRectPathD } from './shapes/roundedRectPath.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import { styles2String, userNodeOverrides } from './shapes/handDrawnShapeStyles.js';
const rect = async (parent, node) => {
log.info('Creating subgraph rect for ', node.id, node);

View File

@@ -1,8 +1,8 @@
import { select } from 'd3';
import { log } from '$root/logger.js';
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import common, { evaluate, renderKatex, hasKatex } from '$root/diagrams/common/common.js';
import { decodeEntities } from '$root/utils.js';
import { log } from '../../logger.js';
import { getConfig } from '../../diagram-api/diagramAPI.js';
import common, { evaluate, renderKatex, hasKatex } from '../../diagrams/common/common.js';
import { decodeEntities } from '../../utils.js';
/**
* @param dom

View File

@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/unbound-method */
import type { SVG } from '$root/diagram-api/types.js';
import type { SVG } from '../../diagram-api/types.js';
import type { Mocked } from 'vitest';
import { addEdgeMarkers } from './edgeMarker.js';

View File

@@ -1,6 +1,6 @@
import type { SVG } from '$root/diagram-api/types.js';
import { log } from '$root/logger.js';
import type { EdgeData } from '$root/types.js';
import type { SVG } from '../../diagram-api/types.js';
import { log } from '../../logger.js';
import type { EdgeData } from '../../types.js';
/**
* Adds SVG markers to a path element based on the arrow types specified in the edge.
*

View File

@@ -1,10 +1,10 @@
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import { evaluate } from '$root/diagrams/common/common.js';
import { log } from '$root/logger.js';
import { createText } from '$root/rendering-util/createText.ts';
import utils from '$root/utils.js';
import { getLineFunctionsWithOffset } from '$root/utils/lineWithOffset.js';
import { getSubGraphTitleMargins } from '$root/utils/subGraphTitleMargins.js';
import { getConfig } from '../../diagram-api/diagramAPI.js';
import { evaluate } from '../../diagrams/common/common.js';
import { log } from '../../logger.js';
import { createText } from '../createText.js';
import utils from '../../utils.js';
import { getLineFunctionsWithOffset } from '../../utils/lineWithOffset.js';
import { getSubGraphTitleMargins } from '../../utils/subGraphTitleMargins.js';
import { curveBasis, line, select } from 'd3';
import rough from 'roughjs';
import createLabel from './createLabel.js';

View File

@@ -1,5 +1,5 @@
/** Setup arrow head and define the marker. The result is appended to the svg. */
import { log } from '$root/logger.js';
import { log } from '../../logger.js';
// Only add the number of markers that the diagram needs
const insertMarkers = (elem, markerArray, type, id) => {

View File

@@ -1,4 +1,4 @@
import { log } from '$root/logger.js';
import { log } from '../../logger.js';
import { state } from './shapes/state.ts';
import { roundedRect } from './shapes/roundedRect.ts';
import { squareRect } from './shapes/squareRect.ts';
@@ -9,7 +9,7 @@ import { choice } from './shapes/choice.ts';
import { note } from './shapes/note.ts';
import { stadium } from './shapes/stadium.js';
import { rectWithTitle } from './shapes/rectWithTitle.js';
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import { getConfig } from '../../diagram-api/diagramAPI.js';
import { subroutine } from './shapes/subroutine.js';
import { cylinder } from './shapes/cylinder.js';
import { circle } from './shapes/circle.js';

View File

@@ -1,10 +1,10 @@
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import type { SVG } from '$root/diagram-api/types.js';
import type { Node } from '../../types.js';
import type { SVG } from '../../../diagram-api/types.js';
// @ts-ignore TODO: Fix rough typings
import rough from 'roughjs';
import { solidStateFill, styles2String } from './handDrawnShapeStyles.js';
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import { getConfig } from '../../../diagram-api/diagramAPI.js';
export const choice = (parent: SVG, node: Node) => {
const { labelStyles, nodeStyles } = styles2String(node);

View File

@@ -1,11 +1,8 @@
import { log } from '$root/logger.js';
import { log } from '../../../logger.js';
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
export const circle = async (parent: SVGAElement, node: Node): Promise<SVGAElement> => {

View File

@@ -1,10 +1,7 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
export const createCylinderPathD = (

View File

@@ -1,11 +1,8 @@
import { log } from '$root/logger.js';
import { log } from '../../../logger.js';
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
export const doublecircle = async (parent: SVGAElement, node: Node): Promise<SVGAElement> => {

View File

@@ -1,11 +1,8 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node, RectOptions } from '$root/rendering-util/types.d.ts';
import type { Node, RectOptions } from '../../types.js';
import { createRoundedRectPathD } from './roundedRectPath.js';
import {
userNodeOverrides,
styles2String,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import { userNodeOverrides, styles2String } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
export const drawRect = async (parent: SVGAElement, node: Node, options: RectOptions) => {

View File

@@ -1,10 +1,10 @@
import { updateNodeBounds } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import type { SVG } from '$root/diagram-api/types.js';
import type { Node } from '../../types.js';
import type { SVG } from '../../../diagram-api/types.js';
import rough from 'roughjs';
import { solidStateFill } from './handDrawnShapeStyles.js';
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import { getConfig } from '../../../diagram-api/diagramAPI.js';
export const forkJoin = (parent: SVG, node: Node, dir: string) => {
const { themeVariables } = getConfig();

View File

@@ -1,5 +1,5 @@
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import { getConfig } from '../../../diagram-api/diagramAPI.js';
import type { Node } from '../../types.js';
// Striped fill like start or fork nodes in state diagrams
export const solidStateFill = (color: string) => {

View File

@@ -1,10 +1,7 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { insertPolygonShape } from './insertPolygonShape.js';

View File

@@ -1,10 +1,7 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { insertPolygonShape } from './insertPolygonShape.js';

View File

@@ -1,4 +1,4 @@
import type { Node, RectOptions } from '$root/rendering-util/types.d.ts';
import type { Node, RectOptions } from '../../types.js';
import { drawRect } from './drawRect.js';
import { labelHelper, updateNodeBounds } from './util.js';
import intersect from '../intersect/index.js';

View File

@@ -1,10 +1,7 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { insertPolygonShape } from './insertPolygonShape.js';

View File

@@ -1,10 +1,7 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { insertPolygonShape } from './insertPolygonShape.js';

View File

@@ -1,8 +1,8 @@
import { log } from '$root/logger.js';
import { log } from '../../../logger.js';
import { labelHelper, updateNodeBounds } from './util.js';
import intersect from '../intersect/index.js';
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import { getConfig } from '../../../diagram-api/diagramAPI.js';
import type { Node } from '../../types.js';
import rough from 'roughjs';
export const note = async (parent: SVGAElement, node: Node) => {

View File

@@ -1,11 +1,8 @@
import { log } from '$root/logger.js';
import { log } from '../../../logger.js';
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { insertPolygonShape } from './insertPolygonShape.js';

View File

@@ -1,10 +1,7 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { insertPolygonShape } from './insertPolygonShape.js';

View File

@@ -1,17 +1,14 @@
import type { Node } from '$root/rendering-util/types.d.ts';
import type { Node } from '../../types.js';
import { select } from 'd3';
import { evaluate } from '$root/diagrams/common/common.js';
import { evaluate } from '../../../diagrams/common/common.js';
import { updateNodeBounds } from './util.js';
import createLabel from '../createLabel.js';
import intersect from '../intersect/index.js';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import { getConfig } from '../../../diagram-api/diagramAPI.js';
import { createRoundedRectPathD } from './roundedRectPath.js';
import { log } from '$root/logger.js';
import { log } from '../../../logger.js';
export const rectWithTitle = async (parent: SVGElement, node: Node) => {
const { labelStyles, nodeStyles } = styles2String(node);

View File

@@ -1,4 +1,4 @@
import type { Node, RectOptions } from '$root/rendering-util/types.d.ts';
import type { Node, RectOptions } from '../../types.js';
import { drawRect } from './drawRect.js';
export const roundedRect = async (parent: SVGAElement, node: Node) => {

View File

@@ -1,4 +1,4 @@
import type { Node, RectOptions } from '$root/rendering-util/types.d.ts';
import type { Node, RectOptions } from '../../types.js';
import { drawRect } from './drawRect.js';
export const squareRect = async (parent: SVGAElement, node: Node) => {

View File

@@ -1,10 +1,7 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { createRoundedRectPathD } from './roundedRectPath.js';

View File

@@ -1,4 +1,4 @@
import type { Node, RectOptions } from '$root/rendering-util/types.d.ts';
import type { Node, RectOptions } from '../../types.js';
import { drawRect } from './drawRect.js';
export const state = async (parent: SVGAElement, node: Node) => {

View File

@@ -1,10 +1,10 @@
import { updateNodeBounds } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import type { SVG } from '$root/diagram-api/types.js';
import type { Node } from '../../types.js';
import type { SVG } from '../../../diagram-api/types.js';
import rough from 'roughjs';
import { solidStateFill } from './handDrawnShapeStyles.js';
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import { getConfig } from '../../../diagram-api/diagramAPI.js';
export const stateEnd = (parent: SVG, node: Node) => {
const { themeVariables } = getConfig();

View File

@@ -1,10 +1,10 @@
import { updateNodeBounds } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import type { SVG } from '$root/diagram-api/types.js';
import type { Node } from '../../types.js';
import type { SVG } from '../../../diagram-api/types.js';
import rough from 'roughjs';
import { solidStateFill } from './handDrawnShapeStyles.js';
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import { getConfig } from '../../../diagram-api/diagramAPI.js';
export const stateStart = (parent: SVG, node: Node) => {
const { themeVariables } = getConfig();

View File

@@ -1,10 +1,7 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { insertPolygonShape } from './insertPolygonShape.js';

View File

@@ -1,10 +1,7 @@
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '$root/rendering-util/types.d.ts';
import {
styles2String,
userNodeOverrides,
} from '$root/rendering-util/rendering-elements/shapes/handDrawnShapeStyles.js';
import type { Node } from '../../types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs';
import { insertPolygonShape } from './insertPolygonShape.js';

View File

@@ -1,8 +1,8 @@
import { createText } from '$root/rendering-util/createText.ts';
import { getConfig } from '$root/diagram-api/diagramAPI.js';
import { createText } from '../../createText.js';
import { getConfig } from '../../../diagram-api/diagramAPI.js';
import { select } from 'd3';
import { evaluate, sanitizeText } from '$root/diagrams/common/common.js';
import { decodeEntities } from '$root/utils.js';
import { evaluate, sanitizeText } from '../../../diagrams/common/common.js';
import { decodeEntities } from '../../../utils.js';
export const labelHelper = async (parent, node, _classes) => {
let cssClasses;

View File

@@ -1,6 +1,6 @@
import { configureSvgSize } from '$root/setupGraphViewbox.js';
import type { SVG } from '$root/diagram-api/types.js';
import { log } from '$root/logger.js';
import { configureSvgSize } from '../setupGraphViewbox.js';
import type { SVG } from '../diagram-api/types.js';
import { log } from '../logger.js';
export const setupViewPortForSVG = (
svg: SVG,

View File

@@ -3,11 +3,7 @@
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"types": ["vitest/importMeta", "vitest/globals"],
"baseUrl": ".", // This must be set if "paths" is set
"paths": {
"$root/*": ["src/*"]
}
"types": ["vitest/importMeta", "vitest/globals"]
},
"include": [
"./src/**/*.ts",

175
scripts/tsc-check.ts Normal file
View File

@@ -0,0 +1,175 @@
/**
* Verify the as-built tarballs can be imported into a fresh, out-of-tree TypeScript project.
*/
/* eslint-disable no-console */
import { mkdtemp, mkdir, writeFile, readFile, readdir, copyFile, rm } from 'node:fs/promises';
import { execFileSync } from 'child_process';
import * as path from 'path';
import { fileURLToPath } from 'url';
import { tmpdir } from 'node:os';
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
/**
* Packages to build and import
*/
const PACKAGES = {
mermaid: 'mermaid',
'@mermaid-js/layout-elk': 'mermaid-layout-elk',
// TODO: these don't import cleanly yet due to exotic tsconfig.json requirements
// '@mermaid-js/mermaid-zenuml': 'mermaid-zenuml',
// '@mermaid-js/parser': 'parser',
};
/**
* Files to create in the temporary package.
*/
const SRC = {
// a minimal description of a buildable package
'package.json': (tarballs: Record<string, string>) =>
JSON.stringify(
{
dependencies: tarballs,
scripts: { build: 'tsc -b --verbose' },
devDependencies: {
// these are somewhat-unexpectedly required, and a downstream would need
// to match the real `package.json` values
'type-fest': '*',
'@types/d3': '^7.4.3',
'@types/dompurify': '^3.0.5',
typescript: '*',
},
},
null,
2
),
// a fairly strict TypeScript configuration
'tsconfig.json': () =>
JSON.stringify(
{
compilerOptions: {
allowSyntheticDefaultImports: true,
composite: true,
declaration: true,
esModuleInterop: true,
incremental: true,
lib: ['dom', 'es2020'],
module: 'esnext',
moduleResolution: 'node',
noEmitOnError: true,
noImplicitAny: true,
noUnusedLocals: true,
sourceMap: true,
target: 'es2020',
rootDir: './src',
outDir: './lib',
strict: true,
tsBuildInfoFile: 'lib/.tsbuildinfo',
},
},
null,
2
),
// the simplest possible script: will everything even import?
'src/index.ts': (tarballs) => {
const imports: string[] = [];
const outputs: string[] = [];
let i = 0;
for (const pkg of Object.keys(tarballs)) {
imports.push(`import * as pkg_${i} from '${pkg}';`);
outputs.push(`console.log(pkg_${i});`);
i++;
}
return [...imports, ...outputs].join('\n');
},
};
/**
* Commands to run after source files are created.
*
* `npm` is used to detect any unwanted `pnpm`-specific runtime "features".
*/
const COMMANDS = [
['npm', 'install'],
['npm', 'run', 'build'],
];
/**
* Built files to expect after commands are executed.
*/
const LIB = ['lib/index.js', 'lib/index.js.map', 'lib/index.d.ts', 'lib/.tsbuildinfo'];
/**
* Run a small out-of-tree build.
*/
async function main() {
console.warn('Checking out-of-tree TypeScript build using', Object.keys(PACKAGES).join('\n'));
const cwd = await mkdtemp(path.join(tmpdir(), 'mermaid-tsc-check-'));
console.warn('... creating temporary folder', cwd);
const tarballs = await buildTarballs(cwd);
for (const [filename, generate] of Object.entries(SRC)) {
const dest = path.join(cwd, filename);
await mkdir(path.dirname(dest), { recursive: true });
console.warn('... creating', dest);
const text = generate(tarballs);
await writeFile(dest, text);
console.info(text);
}
for (const argv of COMMANDS) {
console.warn('... in', cwd);
console.warn('>>>', ...argv);
execFileSync(argv[0], argv.slice(1), { cwd });
}
for (const lib of LIB) {
const checkLib = path.join(cwd, lib);
console.warn('... checking built file', checkLib);
await readFile(checkLib, 'utf-8');
}
console.warn('... deleting', cwd);
await rm(cwd, { recursive: true, force: true });
console.warn('... tsc-check OK for\n', Object.keys(PACKAGES).join('\n'));
}
/** Build all the tarballs. */
async function buildTarballs(tmp: string): Promise<Record<string, string>> {
const dist = path.join(tmp, 'dist');
await mkdir(dist);
const promises: Promise<void>[] = [];
const tarballs: Record<string, string> = {};
for (const [pkg, srcPath] of Object.entries(PACKAGES)) {
promises.push(buildOneTarball(pkg, srcPath, dist, tarballs));
}
await Promise.all(promises);
return tarballs;
}
/** Build a single tarball. */
async function buildOneTarball(
pkg: string,
srcPath: string,
dist: string,
tarballs: Record<string, string>
): Promise<void> {
const cwd = await mkdtemp(path.join(dist, 'pack-'));
const pkgDir = path.join(__dirname, '../packages', srcPath);
const argv = ['pnpm', 'pack', '--pack-destination', cwd];
console.warn('>>>', ...argv);
execFileSync(argv[0], argv.slice(1), { cwd: pkgDir });
const built = await readdir(cwd);
const dest = path.join(dist, built[0]);
await copyFile(path.join(cwd, built[0]), dest);
await rm(cwd, { recursive: true, force: true });
tarballs[pkg] = dest;
}
void main().catch((err) => {
console.error(err);
console.error('!!! tsc-check FAIL: temp folder left in place. see logs above for failure notes');
process.exit(1);
});

View File

@@ -2,15 +2,10 @@ import jison from './.vite/jisonPlugin.js';
import jsonSchemaPlugin from './.vite/jsonSchemaPlugin.js';
import typescript from '@rollup/plugin-typescript';
import { defaultExclude, defineConfig } from 'vitest/config';
import path from 'path';
export default defineConfig({
resolve: {
extensions: ['.js'],
alias: {
// Define your alias here
$root: path.resolve(__dirname, 'packages/mermaid/src'),
},
},
plugins: [
jison(),