Use vite for build

This commit is contained in:
Sidharth Vinod
2022-09-22 15:35:22 +05:30
parent 7bd7bcf4b8
commit aeb31fe1ae
12 changed files with 330 additions and 317 deletions

81
.vite/build.ts Normal file
View File

@@ -0,0 +1,81 @@
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' };
import { OutputOptions } from 'vite/node_modules/rollup';
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 | 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) {
external.push(...Object.keys(dependencies));
output = {
name: 'mermaid',
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: true, core: true }));
}

17
.vite/jisonPlugin.js Normal file
View File

@@ -0,0 +1,17 @@
import { transformJison } from './jisonTransformer.js';
const fileRegex = /\.(jison)$/;
export default function jison() {
return {
name: 'jison',
transform(src, id) {
if (fileRegex.test(id)) {
return {
code: transformJison(src),
map: null, // provide source map if available
};
}
},
};
}

15
.vite/jisonTransformer.js Normal file
View File

@@ -0,0 +1,15 @@
import jison from 'jison';
export const transformJison = (src) => {
const parser = new jison.Generator(src, {
moduleType: 'js',
'token-stack': true,
});
const source = parser.generate({ moduleMain: '() => {}' });
const exporter = `
parser.parser = parser;
export { parser };
export default parser;
`;
return `${source} ${exporter}`;
};

View File

@@ -1,33 +0,0 @@
import express from 'express';
import { createServer as createViteServer } from 'vite';
async function createServer() {
const app = express();
// Create Vite server in middleware mode
// const vite = await createViteServer({
// configFile: './vite.config.cts',
// server: { middlewareMode: true },
// appType: 'custom', // don't include Vite's default HTML handling middlewares
// });
app.use(express.static('dist'));
app.use(express.static('demos'));
app.use(express.static('cypress/platform'));
// Use vite's connect instance as middleware
// app.use(vite.middlewares);
app.use('*', async (req, res) => {
// Since `appType` is `'custom'`, should serve response here.
// Note: if `appType` is `'spa'` or `'mpa'`, Vite includes middlewares to handle
// HTML requests and 404s so user middlewares should be added
// before Vite's middlewares to take effect instead
res.end('Hello world!');
});
app.listen(9000, () => {
console.log(`Listening on http://localhost:9000`);
});
}
createServer();

26
.vite/server.ts Normal file
View File

@@ -0,0 +1,26 @@
import express from 'express';
import { build, 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(express.static('dist'));
app.use(express.static('demos'));
app.use(express.static('cypress/platform'));
app.use(vite.middlewares);
app.listen(9000, () => {
console.log(`Listening on http://localhost:9000`);
});
}
// build(getBuildConfig({ minify: false, watch: true }));
createServer();