-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3503 from mermaid-js/sidv/viteVitest
Build mermaid using Vite
- Loading branch information
Showing
22 changed files
with
1,132 additions
and
1,594 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { build, InlineConfig } from 'vite'; | ||
import { resolve } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import jisonPlugin from './jisonPlugin.js'; | ||
import pkg from '../package.json' assert { type: 'json' }; | ||
type OutputOptions = Exclude< | ||
Exclude<InlineConfig['build'], undefined>['rollupOptions'], | ||
undefined | ||
>['output']; | ||
|
||
const { dependencies } = pkg; | ||
const watch = process.argv.includes('--watch'); | ||
const __dirname = fileURLToPath(new URL('.', import.meta.url)); | ||
|
||
interface BuildOptions { | ||
minify: boolean | 'esbuild'; | ||
core?: boolean; | ||
watch?: boolean; | ||
} | ||
|
||
export const getBuildConfig = ({ minify, core, watch }: BuildOptions): InlineConfig => { | ||
const external = ['require', 'fs', 'path']; | ||
let output: OutputOptions = [ | ||
{ | ||
name: 'mermaid', | ||
format: 'esm', | ||
sourcemap: true, | ||
entryFileNames: `[name].esm${minify ? '.min' : ''}.mjs`, | ||
}, | ||
{ | ||
name: 'mermaid', | ||
format: 'umd', | ||
sourcemap: true, | ||
entryFileNames: `[name]${minify ? '.min' : ''}.js`, | ||
}, | ||
]; | ||
|
||
if (core) { | ||
// Core build is used to generate file without bundled dependencies. | ||
// This is used by downstream projects to bundle dependencies themselves. | ||
external.push(...Object.keys(dependencies)); | ||
// This needs to be an array. Otherwise vite will build esm & umd with same name and overwrite esm with umd. | ||
output = [ | ||
{ | ||
format: 'esm', | ||
sourcemap: true, | ||
entryFileNames: `[name].core.mjs`, | ||
}, | ||
]; | ||
} | ||
|
||
const config: InlineConfig = { | ||
configFile: false, | ||
build: { | ||
emptyOutDir: false, | ||
lib: { | ||
entry: resolve(__dirname, '../src/mermaid.ts'), | ||
name: 'mermaid', | ||
// the proper extensions will be added | ||
fileName: 'mermaid', | ||
}, | ||
minify, | ||
rollupOptions: { | ||
external, | ||
output, | ||
}, | ||
}, | ||
resolve: { | ||
extensions: ['.jison', '.js', '.ts', '.json'], | ||
}, | ||
plugins: [jisonPlugin()], | ||
}; | ||
|
||
if (watch && config.build) { | ||
config.build.watch = { | ||
include: 'src/**', | ||
}; | ||
} | ||
|
||
return config; | ||
}; | ||
|
||
if (watch) { | ||
build(getBuildConfig({ minify: false, watch })); | ||
} else { | ||
build(getBuildConfig({ minify: false })); | ||
build(getBuildConfig({ minify: 'esbuild' })); | ||
build(getBuildConfig({ minify: false, core: true })); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { transformJison } from './jisonTransformer.js'; | ||
const fileRegex = /\.(jison)$/; | ||
|
||
export default function jison() { | ||
return { | ||
name: 'jison', | ||
|
||
transform(src: string, id: string) { | ||
if (fileRegex.test(id)) { | ||
return { | ||
code: transformJison(src), | ||
map: null, // provide source map if available | ||
}; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import express from 'express'; | ||
import { createServer as createViteServer } from 'vite'; | ||
// import { getBuildConfig } from './build'; | ||
|
||
async function createServer() { | ||
const app = express(); | ||
|
||
// Create Vite server in middleware mode | ||
const vite = await createViteServer({ | ||
configFile: './vite.config.ts', | ||
server: { middlewareMode: true }, | ||
appType: 'custom', // don't include Vite's default HTML handling middlewares | ||
}); | ||
|
||
app.use(vite.middlewares); | ||
app.use(express.static('dist')); | ||
app.use(express.static('demos')); | ||
app.use(express.static('cypress/platform')); | ||
|
||
app.listen(9000, () => { | ||
console.log(`Listening on http://localhost:9000`); | ||
}); | ||
} | ||
|
||
// build(getBuildConfig({ minify: false, watch: true })); | ||
createServer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"module": "ES2022" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.