78 lines
3.2 KiB
TypeScript
78 lines
3.2 KiB
TypeScript
import { readFile } from 'node:fs/promises'
|
|
import { dirname, relative, resolve as resolvePath } from 'node:path'
|
|
import type { UserConfig, TsdownPlugin } from 'tsdown'
|
|
|
|
const PLUGIN_ID = 'dsh-brain'
|
|
const CSS_VIRTUAL_PREFIX = '\0dsh-brain-css:'
|
|
const CSS_VIRTUAL_SUFFIX = '.mjs'
|
|
|
|
function cssHash(value: string): string {
|
|
let hash = 2166136261
|
|
for (let index = 0; index < value.length; index += 1) {
|
|
hash ^= value.charCodeAt(index)
|
|
hash = Math.imul(hash, 16777619)
|
|
}
|
|
return (hash >>> 0).toString(36)
|
|
}
|
|
|
|
/** Minimal CSS-modules inliner: scope class names, inject one <style> tag. */
|
|
function brainCssPlugin(): TsdownPlugin {
|
|
return {
|
|
name: 'dsh-brain-css-modules-inline',
|
|
resolveId(source: string, importer: string | undefined) {
|
|
if (!source.endsWith('.module.css')) return null
|
|
const absolute = importer === undefined ? source : resolvePath(dirname(importer), source)
|
|
const rel = relative(process.cwd(), absolute).replaceAll('\\', '/')
|
|
return CSS_VIRTUAL_PREFIX + rel + CSS_VIRTUAL_SUFFIX
|
|
},
|
|
async load(virtualId: string) {
|
|
if (!virtualId.startsWith(CSS_VIRTUAL_PREFIX)) return null
|
|
const fileId = resolvePath(process.cwd(), virtualId.slice(CSS_VIRTUAL_PREFIX.length, -CSS_VIRTUAL_SUFFIX.length))
|
|
this.addWatchFile(fileId)
|
|
const source = await readFile(fileId, 'utf8')
|
|
const filename = relative(process.cwd(), fileId).replaceAll('\\', '/')
|
|
const classMap: Record<string, string> = {}
|
|
for (const match of source.matchAll(/\.([A-Za-z_][A-Za-z0-9_-]*)/g)) {
|
|
const local = match[1]
|
|
if (classMap[local] === undefined) classMap[local] = 'dshbrain_' + cssHash(filename + ':' + local)
|
|
}
|
|
let css = source
|
|
for (const local of Object.keys(classMap).sort((left, right) => right.length - left.length)) {
|
|
css = css.replace(new RegExp('\\.' + local + '(?![\\w-])', 'g'), '.' + classMap[local])
|
|
}
|
|
return [
|
|
'const css = ' + JSON.stringify(css) + ';',
|
|
'const tagId = ' + JSON.stringify(filename) + ';',
|
|
'if (typeof document !== "undefined" && typeof document.querySelector === "function" && typeof document.createElement === "function") {',
|
|
' let tag = document.querySelector("style[data-plugin-css=" + JSON.stringify(tagId) + "]");',
|
|
' if (!tag) { tag = document.createElement("style"); tag.dataset.pluginCss = tagId; document.head.appendChild(tag); }',
|
|
' if (tag.textContent !== css) tag.textContent = css;',
|
|
'}',
|
|
'export default ' + JSON.stringify(classMap) + ';',
|
|
].join('\n')
|
|
},
|
|
}
|
|
}
|
|
|
|
export default {
|
|
name: PLUGIN_ID + '/client',
|
|
entry: { index: 'src-client/index.ts' },
|
|
outDir: 'client',
|
|
format: 'cjs',
|
|
platform: 'browser',
|
|
target: 'es2022',
|
|
fixedExtension: false,
|
|
outExtensions: () => ({ js: '.js' }),
|
|
dts: false,
|
|
sourcemap: false,
|
|
clean: false,
|
|
deps: { neverBundle: [/^react(?:\/.*)?$/, /^react-dom(?:\/.*)?$/] },
|
|
define: { 'process.env.NODE_ENV': '"production"' },
|
|
plugins: [brainCssPlugin()],
|
|
outputOptions: {
|
|
banner: `window.__ModuleLoader__.load({ id: '${PLUGIN_ID}', factory: (require) => {`,
|
|
intro: 'var module = { exports: {} }; var exports = module.exports;',
|
|
footer: 'return module.exports; } });',
|
|
},
|
|
} satisfies UserConfig
|