create-questpie-app
Version:
Bootstrap a new QUESTPIE Studio application with your choice of template and packages
119 lines (108 loc) • 4.22 kB
text/typescript
import { fi } from 'date-fns/locale' // <- Unused import, should be removed
import { glob } from 'glob'
import { copyFile, mkdir } from 'node:fs/promises'
import { dirname } from 'node:path'
import { posix } from 'node:path' // <- Add this import
import scss from 'rollup-plugin-scss'
import { defineConfig } from 'tsdown'
export default defineConfig({
entry: ['src/**/*.{ts,tsx,js,jsx,json}', '!src/**/migrations/snapshots/*.json'],
outDir: 'dist',
external: () => true,
unbundle: true,
tsconfig: 'tsconfig.json',
onSuccess: async () => {
try {
// Copy CSS/SCSS files
const cssFiles = await glob('src/**/*.{css,scss}')
for (const file of cssFiles) {
const normalizedFile = file.replace(/\\/g, '/')
const destPath = normalizedFile.replace('src/', 'dist/')
const destDir = dirname(destPath)
await mkdir(destDir, { recursive: true })
await copyFile(file, destPath)
}
// Copy migration snapshots
const snapshotFiles = await glob('src/**/migrations/snapshots/*.json')
for (const file of snapshotFiles) {
const normalizedFile = file.replace(/\\/g, '/')
const destPath = normalizedFile.replace('src/', 'dist/')
const destDir = dirname(destPath)
await mkdir(destDir, { recursive: true })
await copyFile(file, destPath)
}
} catch (error) {
if ((error as any).code !== 'ENOENT') {
console.warn('Failed to copy files:', (error as any).message)
}
}
},
plugins: [scss()],
inputOptions: { transform: { jsx: { runtime: 'automatic' } } },
exports: {
customExports: async (exports, opts) => {
try {
const cssFiles = await glob('src/**/*.{css,scss}')
const cssExports: Record<string, any> = {}
for (const file of cssFiles) {
// Normalize all paths to use forward slashes consistently
const normalizedFile = file.replace(/\\/g, '/')
const exportKey = `./${normalizedFile.replace('src/', '')}`
const distPath = opts.isPublish
? `./${normalizedFile.replace('src/', 'dist/')}`
: `./${normalizedFile}`
cssExports[exportKey] = distPath
}
// Add snapshot exports
const snapshotFiles = await glob('src/**/migrations/snapshots/*.json')
const snapshotExports: Record<string, any> = {}
for (const file of snapshotFiles) {
const normalizedFile = file.replace(/\\/g, '/')
const exportKey = `./${normalizedFile.replace('src/', '')}`
const distPath = opts.isPublish
? `./${normalizedFile.replace('src/', 'dist/')}`
: `./${normalizedFile}`
snapshotExports[exportKey] = distPath
}
const indexExports: Record<string, any> = {}
for (const [exportKey, exportValue] of Object.entries(exports)) {
// Normalize paths consistently
const normalizedKey = exportKey.replace(/\\/g, '/')
const normalizedValue = exportValue.replace(/\\/g, '/')
exports[normalizedKey] = normalizedValue
if (normalizedKey !== exportKey) {
delete exports[exportKey]
}
if (
typeof normalizedValue === 'string' &&
(normalizedValue.endsWith('index.js') ||
normalizedValue.endsWith('index.d.ts') ||
normalizedValue.endsWith('index.ts') ||
normalizedValue.endsWith('index.tsx') ||
normalizedValue.endsWith('index.jsx'))
) {
const indexExportKey = normalizedKey.endsWith('/index')
? normalizedKey
: `${normalizedKey}/index`
const dirExportKey = normalizedKey.endsWith('/index')
? normalizedKey.replace('/index', '')
: normalizedKey
indexExports[indexExportKey] = normalizedValue
indexExports[dirExportKey] = normalizedValue
}
}
return {
...exports,
...indexExports,
...cssExports,
...snapshotExports,
}
} catch (error) {
return {
...exports,
}
}
},
devExports: true,
},
})