From df97d969096f3bc2ef4e9fb1a2288d0abb335d8c Mon Sep 17 00:00:00 2001 From: Kia King Ishii Date: Wed, 4 Nov 2020 19:05:29 +0900 Subject: [PATCH 1/3] build: handle shared files better (#72) (#114) fix #72 --- .gitignore | 2 ++ package.json | 9 +++++---- scripts/{copy.js => copyClient.js} | 0 scripts/copyShared.js | 7 +++++++ scripts/watchAndCopy.js | 27 +++++++++++++++++++++++---- src/client/tsconfig.json | 10 ++++------ src/node/config.ts | 2 +- src/node/tsconfig.json | 13 ++++++++++--- src/shared/config.ts | 2 +- src/shared/tsconfig.json | 10 +++++----- 10 files changed, 58 insertions(+), 24 deletions(-) rename scripts/{copy.js => copyClient.js} (100%) create mode 100644 scripts/copyShared.js diff --git a/.gitignore b/.gitignore index 6fec63b6..6e9dbd35 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /dist /node_modules +/src/client/shared +/src/node/shared *.log .DS_Store diff --git a/package.json b/package.json index 3f696d9c..cef47a3a 100644 --- a/package.json +++ b/package.json @@ -27,13 +27,14 @@ }, "homepage": "https://github.com/vuejs/vitepress/tree/master/#readme", "scripts": { - "dev": "run-p dev-client dev-client-copy dev-node dev-shared", + "dev": "yarn dev-shared && yarn dev-start", + "dev-start": "run-p dev-client dev-node dev-watch", "dev-client": "tsc -w -p src/client", - "dev-client-copy": "node scripts/watchAndCopy", "dev-node": "tsc -w -p src/node", - "dev-shared": "tsc -w -p src/shared", + "dev-shared": "node scripts/copyShared", + "dev-watch": "node scripts/watchAndCopy", "release": "bash scripts/release.sh", - "build": "rimraf -rf dist && tsc -p src/client && tsc -p src/node && tsc -p src/shared && node scripts/copy", + "build": "rimraf -rf dist && node scripts/copyShared && tsc -p src/client && tsc -p src/node && node scripts/copyClient", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s" }, "engines": { diff --git a/scripts/copy.js b/scripts/copyClient.js similarity index 100% rename from scripts/copy.js rename to scripts/copyClient.js diff --git a/scripts/copyShared.js b/scripts/copyShared.js new file mode 100644 index 00000000..af10bc26 --- /dev/null +++ b/scripts/copyShared.js @@ -0,0 +1,7 @@ +const fs = require('fs-extra') +const glob = require('globby') + +glob.sync('src/shared/**/*.ts').forEach((file) => { + fs.copy(file, file.replace(/^src\//, 'src/node/')) + fs.copy(file, file.replace(/^src\//, 'src/client/')) +}) diff --git a/scripts/watchAndCopy.js b/scripts/watchAndCopy.js index 898b708b..77e89cf4 100644 --- a/scripts/watchAndCopy.js +++ b/scripts/watchAndCopy.js @@ -2,12 +2,31 @@ const fs = require('fs-extra') const chokidar = require('chokidar') -function toDest(file) { +function toClientAndNode(method, file) { + if (method === 'copy') { + fs.copy(file, file.replace(/^src\//, 'src/node/')) + fs.copy(file, file.replace(/^src\//, 'src/client/')) + } else if (method === 'remove') { + fs.remove(file.replace(/^src\//, 'src/node/')) + fs.remove(file.replace(/^src\//, 'src/client/')) + } +} + +function toDist(file) { return file.replace(/^src\//, 'dist/') } +// copy shared files to the client and node directory whenever they change. +chokidar + .watch('src/shared/**/*.ts') + .on('change', (file) => toClientAndNode('copy', file)) + .on('add', (file) => toClientAndNode('copy', file)) + .on('unlink', (file) => toClientAndNode('remove', file)) + +// copy non ts files, such as an html or css, to the dist directory whenever +// they change. chokidar .watch('src/client/**/!(*.ts|tsconfig.json)') - .on('change', (file) => fs.copy(file, toDest(file))) - .on('add', (file) => fs.copy(file, toDest(file))) - .on('unlink', (file) => fs.remove(toDest(file))) + .on('change', (file) => fs.copy(file, toDist(file))) + .on('add', (file) => fs.copy(file, toDist(file))) + .on('unlink', (file) => fs.remove(toDist(file))) diff --git a/src/client/tsconfig.json b/src/client/tsconfig.json index b004aa66..e23f7ed1 100644 --- a/src/client/tsconfig.json +++ b/src/client/tsconfig.json @@ -2,22 +2,20 @@ "extends": "../../tsconfig.base.json", "compilerOptions": { "baseUrl": ".", - "outDir": "../../dist", + "outDir": "../../dist/client", "module": "esnext", "lib": ["ESNext", "DOM"], "types": ["vite"], "paths": { "/@app/*": ["app/*"], "/@theme/*": ["theme-default/*"], - "/@shared/*": ["../shared/*"], + "/@shared/*": ["shared/*"], + "/@types/*": ["../../types/*"], "vitepress": ["app/exports.ts"] } }, "include": [ - ".", + ".", "../../types/shared.d.ts", - ], - "exclude": [ - "../shared" ] } diff --git a/src/node/config.ts b/src/node/config.ts index 3df6646c..963596f7 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -5,7 +5,7 @@ import globby from 'globby' import { createResolver, APP_PATH } from './resolver' import { Resolver } from 'vite' import { SiteData, HeadConfig, LocaleConfig } from '../../types/shared' -export { resolveSiteDataByRoute } from '../shared/config' +export { resolveSiteDataByRoute } from './shared/config' const debug = require('debug')('vitepress:config') diff --git a/src/node/tsconfig.json b/src/node/tsconfig.json index e67dbbc7..4d912460 100644 --- a/src/node/tsconfig.json +++ b/src/node/tsconfig.json @@ -1,10 +1,17 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "outDir": "../../dist", + "baseUrl": ".", + "outDir": "../../dist/node", "module": "commonjs", "lib": ["ESNext", "DOM"], - "sourceMap": true + "sourceMap": true, + "paths": { + "/@types/*": ["../../types/*"] + } }, - "include": [".", "../shared", "../../types/shared.d.ts"] + "include": [ + ".", + "../../types/shared.d.ts" + ] } diff --git a/src/shared/config.ts b/src/shared/config.ts index 2946c23d..378829e1 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -1,4 +1,4 @@ -import { SiteData } from '../../types/shared' +import { SiteData } from '/@types/shared' const inBrowser = typeof window !== 'undefined' diff --git a/src/shared/tsconfig.json b/src/shared/tsconfig.json index 34954d27..04fa6565 100644 --- a/src/shared/tsconfig.json +++ b/src/shared/tsconfig.json @@ -2,12 +2,12 @@ "extends": "../../tsconfig.base.json", "compilerOptions": { "baseUrl": ".", - "outDir": "../../dist/client/shared", - "module": "esnext", - "lib": ["ESNext", "DOM"], + "paths": { + "/@types/*": ["../../types/*"] + } }, "include": [ - ".", - "../../types/shared.d.ts", + ".", + "../../types/shared.d.ts" ] } From f8d20383422e76cba9d7d09f13541ba7622e3462 Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Wed, 4 Nov 2020 19:08:03 +0900 Subject: [PATCH 2/3] chore: organize the scripts order --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index cef47a3a..ae0edd4a 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,9 @@ "dev-node": "tsc -w -p src/node", "dev-shared": "node scripts/copyShared", "dev-watch": "node scripts/watchAndCopy", - "release": "bash scripts/release.sh", "build": "rimraf -rf dist && node scripts/copyShared && tsc -p src/client && tsc -p src/node && node scripts/copyClient", - "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s" + "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", + "release": "bash scripts/release.sh" }, "engines": { "node": ">=10.0.0" From 287ca2f0da61c5161fc9b3bd1e3f34a02455b5c0 Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Wed, 4 Nov 2020 19:08:21 +0900 Subject: [PATCH 3/3] style: remove duplicated comment --- scripts/watchAndCopy.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/watchAndCopy.js b/scripts/watchAndCopy.js index 77e89cf4..d88a1567 100644 --- a/scripts/watchAndCopy.js +++ b/scripts/watchAndCopy.js @@ -1,4 +1,3 @@ -// copy and watch non-ts files in src/client const fs = require('fs-extra') const chokidar = require('chokidar')