import path from 'path';
import { defineConfig, PluginOption } from 'vite';
import escapeStringRegexp from 'escape-string-regexp';
import tsconfigPaths from 'vite-jsconfig-paths';
import dts from 'vite-plugin-dts';
import packageJson from '../package.json';
import { viteStaticCopy } from 'vite-plugin-static-copy';
export default defineConfig(({ mode }) => {
return {
root: path.resolve(__dirname, './'),
plugins: [
{
name: 'fix-component-styles-replace-backtick-with-single-quote',
apply: 'build',
enforce: 'pre',
generateBundle(_, bundle) {
for (const [fileName, chunk] of Object.entries(bundle)) {
if (chunk.type === 'chunk' && chunk.code) {
chunk.code = chunk.code.replace(/styles: \[\`(.*?)\s\`\]/gs, "styles: ['$1']");
}
}
},
},
tsconfigPaths({
projects: ['./tsconfig.json'],
}) as PluginOption,
dts({
rollupTypes: true,
tsconfigPath: path.resolve(__dirname, './tsconfig.json'),
compilerOptions: {
paths: {
'../dist/renderer': ['../../dist/renderer/index.d.ts']
},
},
bundledPackages: ['@opentiny/tiny-schema-renderer-ng', '@opentiny/genui-sdk-core', 'jsondiffpatch', '@dmsnell/diff-match-patch'],
}),
viteStaticCopy({
targets: [
{
src: '../dist/renderer/package.json',
dest: '../',
transform: (content) => {
const newPackageJson = JSON.parse(content);
newPackageJson.exports = {
'.': {
...newPackageJson.exports['.'],
'types': './dist/index.d.ts',
'default': './dist/index.mjs',
},
};
newPackageJson.module = './dist/index.mjs';
newPackageJson.typings = './dist/index.d.ts';
newPackageJson.version = packageJson.version;
return JSON.stringify(newPackageJson, null, 2);
},
},
{
src: '../README.md',
dest: '../',
}
],
}),
],
build: {
lib: {
entry: path.resolve(__dirname, './index.ts'),
formats: ['es'],
fileName: `index`,
},
outDir: path.resolve(__dirname, '../output/dist'),
emptyOutDir: true,
sourcemap: false,
terserOptions: {
mangle: {
toplevel: true,
},
format: {
comments: false
},
},
rollupOptions: {
external: [...Object.keys(packageJson.dependencies || {}).map(name => new RegExp(`^${escapeStringRegexp(name)}(/|$)`))],
},
},
};
});