pnpm workspace+TypeScriptãªmonorepoã§ã
Cloud Functions for Firebaseãéçºãã¦ããã¨ãã«ã
unjs/unbuildã§ãã«ããã¦ã¿ãã¨ãã®åå¿é²(*´Ïï½*)
å°ãªãè¨å®ã§ãã«ããã§ãã¦ä¾¿å©(*´Ïï½*)
unbuildã¨ã¯
ãA unified JavaScript build systemããããã
Viteãããã³ãã¨ã³ãç¨ã®ãã«ãã«å¯¾ãã
unbuildã¯ã©ã¤ãã©ãªãªã©ã«ãããããªå°è±¡
- å°ãªãè¨å®ã§ESM/commonjs+åå®ç¾©ãçæã§ãã
package.json
ãããã«ãè¨å®ãèªåã§åå¾package.json
ã®è¨å®ãdependenciesã®éä¸è¶³ãèªåãã§ãã¯
ãã«ãããæ¹æ³ã¯2ã¤ãããrollupã¨mkdistã使ã£ã¦ãã
- rollup: è¤æ°ã®ãã¡ã¤ã«ã1ã¤ã«ã¾ã¨ãã(ãã³ãã«)
- importãã¦ããã¡ã¤ã«ã1ã¤ã«ã¾ã¨ãã¦ããã
- dependenciesã«æå®ããã¦ãããã®ã¯ãã®ã¾ã¾å©ç¨ãã
- devDependenciesã«æå®ããã¦ããã®ã¯ãã³ãã«ãã¦ããã
- mkdist: ãã³ãã«ããªãã·ã³ãã«ãªãã©ã³ã¹ãã¤ã«
- ãã£ã¬ã¯ããªæ§é ãã®ã¾ã¾ã«ããã¡ã¤ã«ãã¨ã«ãã©ã³ã¹ãã¤ã«ãã¦ããã
ãªã®ã§ãmonorepoã®å
±éããã¸ã§ã¯ããã
rollupã使ã£ã¦ãdevDependenciesã«æå®ãã¦ããã°ã
å
¨é¨ãã³ãã«ãã¦ãããã®ã§ãã ãã¶ä¾¿å©ã«(*´Ïï½*)
ã·ã³ãã«ãªä½¿ãæ¹
READMEã«æ¸ããã¦ããã®ã¯ãããªæãã
src/index.ts
ã«ãã¡ã¤ã«ãç¨æãã¦ã
// src/index.ts export const log = (...args) => { console.log(...args); };
package.json
ã«è¨å®ã追å ãã¦ã
// package.json { "type": "module", "exports": { ".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs" } }, "main": "./dist/index.cjs", "types": "./dist/index.d.ts", "files": ["dist"] }
ã³ãã³ããå®è¡ããã°OK
$ npx unbuild
ããã¨ããããªæãã«ä½æãã¦ãããã
./dist/ âââ index.cjs âââ index.d.cts âââ index.d.mts âââ index.d.ts âââ index.mjs
è¨å®ãã¡ã¤ã«
build.config.ts
ã使ã£ã¦ç´°ããè¨å®ãã§ãã
import { defineBuildConfig } from "unbuild"; export default defineBuildConfig({ // ãã«ããããã¡ã¤ã«ã¯ãentriesã§æå®ãã entries: [ // ãã¡ã¤ã«ãæå®ããã¨ãrollupã§ãã«ã "./src/index", // ãã£ã¬ã¯ããªãæå®ããã¨ãmkdistã§ãã¡ã¤ã«ãã¨ã®ãã©ã³ã¹ãã¤ã« { builder: "mkdist", input: "./src/package/components/", outDir: "./dist/components", }, ], // åå®ç¾©ãã¡ã¤ã«(.d.ts)ãçæããå ´åã¯ãtrueã«ãã declaration: true, // waringã®ã¨ãã§ãã¨ã©ã¼ã«ããªãå ´åã¯ãfalseã«ãã failOnWarn: false, });
entries.builder
ã¯ãç¹ã¯æå®ãªããã°ã
- ãã¡ã¤ã«(æ«å°¾ã"/"ã§ãªãªã)å ´åã¯ãrollup
- ãã£ã¬ã¯ããª(æ«å°¾ã"/")ã®å ´åã¯ãmkdist
ãå©ç¨ããããã
cjsãåºåãã
build.config.ts
ãå©ç¨ããå ´åã
ããã©ã«ãã§ã¯.cjs
ãåºåãããªãã
rollupã¨mkdirã§ã¯è¨å®ã®ä»æ¹ãç°ãªã
rollup
import { defineBuildConfig } from "unbuild"; export default defineBuildConfig({ entries: [ { builder: "rollup" input: "./src/index", outDir: "./dist", }, ], // rollupã®å ´åã¯ãããã§è¨å® rollup: { emitCJS: true, }, });
mkdist
mkdistã®å ´åã¯ãentiresã§ããããæå®ãã
import { defineBuildConfig } from "unbuild"; export default defineBuildConfig({ entries: [ { builder: "mkdist", input: "./src/package/components/", outDir: "./dist/components", }, { builder: "mkdist", input: "./src/package/components/", outDir: "./dist/components", format: "cjs", // commonjså½¢å¼ãæå® ext: "cjs", // æ¡å¼µåãæå®ããªãã¨ãjsã«ãªã }, ], });
minifyãã
ãªã«ããã¦ããªãã¨minifyãããªãã®ã§ã
åå¥ã«æå®ããå¿
è¦ãããã
rollup
import { defineBuildConfig } from "unbuild"; export default defineBuildConfig({ entries: [ { builder: "rollup" input: "./src/index", outDir: "./dist", }, ], // rollupã®å ´åã¯ãããã§è¨å® rollup: { esbuild: { minify: true, } }, });
mkdist
mkdistã®å ´åã¯ãentiresã§æå®ãã
import { defineBuildConfig } from "unbuild"; export default defineBuildConfig({ entries: [ { builder: "mkdist", input: "./src/package/components/", outDir: "./dist/components", esbuild: { minify: true, }, }, ], });
ç¹å®ã®æååãç½®ãæãã
ä¸é¨ã®æååãå¤æ°ãªã©ããã«ãæã«ç½®ãæãããå ´å
rollup
import { defineBuildConfig } from "unbuild"; export default defineBuildConfig({ entries: [ { builder: "rollup" input: "./src/index", outDir: "./dist", }, ], // rollupã®å ´åã¯ãã(@rollup/plugin-replaceã§ç½®ãæã) replace: { "process.env.FOO": `"bar"`, }, // or rollup: { // esbuildã®defineã§ç½®ãæãã esbuild: { define: { "process.env.FOO": `"bar"`, } } }, });
mkdist
mkdistã®å ´åã¯ãentiresã§æå®ãã
import { defineBuildConfig } from "unbuild"; export default defineBuildConfig({ entries: [ { builder: "mkdist", input: "./src/package/components/", outDir: "./dist/components", // esbuildã®defineã§ç½®ãæãã esbuild: { define: { "process.env.FOO": `"bar"`, } }, }, ], });
以ä¸!! å°ãªãè¨å®ã§ãã³ãã«ãã/ãªãã§ãã«ãã§ãã¦ä¾¿å©...(*´Ïï½*)