Skip to content

Commit

Permalink
build: tsdv
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Jun 5, 2022
1 parent 053018d commit 1e99e25
Show file tree
Hide file tree
Showing 39 changed files with 430 additions and 2,377 deletions.
183 changes: 0 additions & 183 deletions norm.config.mjs

This file was deleted.

15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,46 @@
"version": "1.15.0",
"private": true,
"scripts": {
"build": "norm build bytemd '@bytemd/*'",
"dev": "norm serve playground",
"build": "tsc --build && pnpm --filter './packages/**' build",
"dev": "pnpm --filter playground dev",
"docs:build": "npx vitepress build docs",
"docs:dev": "npx vitepress dev docs",
"docs:serve": "npx vitepress serve docs",
"format": "norm format",
"format": "prettier --write '**/*.{ts,tsx,json,md,svelte}' && sort-package-json package.json 'packages/*/package.json'",
"postinstall": "node scripts/postinstall.mjs && npm run format && sort-json packages/*/locales/*.json",
"pub": "npm run build && norm version && pnpm --recursive --registry https://registry.npmjs.org/ publish --access public",
"test": "norm test bytemd"
},
"prettier": {
"pluginSearchDirs": [
"."
],
"proseWrap": "never",
"semi": false,
"singleQuote": true
},
"devDependencies": {
"@norm/cli": "^0.4.43",
"@sveltejs/vite-plugin-svelte": "1.0.0-next.47",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/svelte": "^3.1.2",
"@types/fs-extra": "^9.0.13",
"@types/lodash-es": "^4.17.6",
"fast-glob": "^3.2.11",
"fs-extra": "^10.1.0",
"jsdom": "^19.0.0",
"local-pkg": "^0.4.1",
"lodash-es": "^4.17.21",
"mustache": "^4.2.0",
"prettier": "^2.6.2",
"prettier-plugin-svelte": "^2.7.0",
"sass": "^1.52.2",
"sort-json": "^2.0.1",
"sort-package-json": "^1.57.0",
"svelte": "^3.48.0",
"svelte-preprocess": "^4.10.6",
"svelte2tsx": "^0.5.10",
"typescript": "^4.7.3",
"vite": "^2.9.9"
"vite": "3.0.0-alpha.9"
},
"packageManager": "[email protected]",
"bundlewatch": {
Expand Down
80 changes: 80 additions & 0 deletions packages/bytemd/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// @ts-check
import fs from 'fs-extra'
import path from 'path'
import { preprocess } from 'svelte/compiler'
import glob from 'fast-glob'
import { build } from 'vite'
import { resolveModule } from 'local-pkg'
import sveltePreprocess from 'svelte-preprocess'

export const sveltePreprocessor = sveltePreprocess({
typescript: true,
// https://github.com/sveltejs/svelte/issues/189#issuecomment-586142198
replace: [
[/(>)[\s]*([<{])/g, '$1$2'],
[/({[/:][a-z]+})[\s]*([<{])/g, '$1$2'],
[/({[#:][a-z]+ .+?})[\s]*([<{])/g, '$1$2'],
[/([>}])[\s]+(<|{[/#:][a-z][^}]*})/g, '$1$2'],
],
})

const pkgName = 'decode-named-character-reference'
const resolveOptions = {
alias: {
// do not resolve `browser` field to make it work at SSR
// https://github.com/vitejs/vite/issues/4405
[pkgName]: resolveModule(pkgName),
},
}

async function buildFilesForSvelte() {
console.log('building svelte entry and helpers...')
for (const [src, dest] of Object.entries({
'src/helpers.ts': 'helpers.js',
'src/index.ts': 'svelte-entry.js',
})) {
await build({
build: {
emptyOutDir: false,
lib: {
entry: src,
formats: ['es'],
fileName(format) {
if (format === 'es') return dest
throw new Error('should not be here')
},
},
rollupOptions: {
external: ['./helpers', /\.svelte$/],
},
},
resolve: resolveOptions,
})
}

console.log('processing svelte files...')
const files = await glob('src/*.svelte')
for (let file of files) {
const dest = file.replace('src/', 'dist/')
await fs.ensureDir(path.dirname(dest))

if (fs.statSync(file).isDirectory()) return

if (file.endsWith('.svelte')) {
const source = await fs.readFile(file, 'utf8')
const item = await preprocess(source, sveltePreprocessor, {
filename: file,
})
await fs.writeFile(
dest,
item.code.replace('<script lang="ts">', '<script>')
)
}
}

console.log('processing style files (backward compatibility)...')
await fs.move('dist/style.css', 'dist/index.css')
await fs.copy('dist/index.css', 'dist/index.min.css')
}

buildFilesForSvelte()
8 changes: 6 additions & 2 deletions packages/bytemd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@
"./dist/index.min.css": "./dist/index.min.css"
},
"main": "./dist/index.js",
"svelte": "./dist/svelte-entry.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist",
"locales"
],
"scripts": {
"build": "tsdv build",
"dev": "tsdv watch"
},
"dependencies": {
"@types/codemirror": "^5.60.5"
},
Expand All @@ -56,6 +61,5 @@
"unist-util-visit": "^4.1.0",
"vfile": "^5.3.2",
"word-count": "^0.2.2"
},
"svelte": "./dist/svelte-entry.js"
}
}
16 changes: 16 additions & 0 deletions packages/bytemd/tsdv.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-check
import { defineConfig } from 'tsdv'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { sveltePreprocessor } from './build.mjs'

// nullish coalescing in es2020
// TODO: 'decode-named-character-reference'
export default defineConfig({
target: 'es2019',
tsc: false,
plugins: [
svelte({
preprocess: [sveltePreprocessor],
}),
],
})
4 changes: 4 additions & 0 deletions packages/plugin-breaks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"dist",
"locales"
],
"scripts": {
"build": "tsdv build",
"dev": "tsdv watch"
},
"devDependencies": {
"bytemd": "workspace:*",
"remark-breaks": "^3.0.2"
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-breaks/tsdv.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @ts-check
import { defineConfig } from 'tsdv'

export default defineConfig({
target: 'es2019',
tsc: false,
})
4 changes: 4 additions & 0 deletions packages/plugin-frontmatter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"dist",
"locales"
],
"scripts": {
"build": "tsdv build",
"dev": "tsdv watch"
},
"devDependencies": {
"@types/js-yaml": "^4.0.5",
"bytemd": "workspace:*",
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-frontmatter/tsdv.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @ts-check
import { defineConfig } from 'tsdv'

export default defineConfig({
target: 'es2019',
tsc: false,
})
Loading

0 comments on commit 1e99e25

Please sign in to comment.