@tsmodule/tsmodule 23.0.3 → 23.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
- import{dirname as T,extname as W,isAbsolute as G,resolve as b,resolve as g}from"path";import{build as D}from"esbuild";import{existsSync as P,mkdirSync as M,readFileSync as u,rmSync as x,writeFileSync as N}from"fs";import m from"chalk";import{env as O}from"process";import _ from"fast-glob";import p from"ora";import{createDebugLogger as A,log as y}from"create-debug-logger";import{getPackageJson as B,getPackageJsonFile as I}from"../../utils/pkgJson.js";import{isJsOrTs as L,isTs as f,isTsxOrJsx as c}from"../../utils/index.js";import{createShell as z}from"await-shell";import{emitTsDeclarations as V}from"./lib/emitTsDeclarations.js";import{normalizeImportSpecifiers as X}from"../normalize/index.js";const C=s=>{const l=` ${s} `;y(m.bgBlue(m.bold(m.white(l))))},K=()=>{let s;if(P("dist/package.json")?s=JSON.parse(u("dist/package.json","utf-8")):s={},s?.module==="module")return!0;s.type="module",N("dist/package.json",JSON.stringify(s,null,2))},U=async({files:s="src/**/*",styles:l="src/styles/components/index.css",bundle:w=!1,dev:r=!1,runtimeOnly:S=!1})=>{O.NODE_ENV=r?"development":"production";const E=z(),t=A(U);t.log("Building",{files:s,dev:r,runtimeOnly:S}),C(`${m.bold("TS Module")} [${O.NODE_ENV}]`);const F=await I(),$=await B(),a=process.cwd(),h={absWorkingDir:a,bundle:w,treeShaking:w,outbase:"src",outdir:"dist",assetNames:"[name].js",logLevel:r?"warning":"error",charset:"utf8",format:"esm",target:"esnext",platform:$?.platform??"node",minify:!r,define:{PACKAGE_JSON:F}},d=g(a,"src"),n=g(a,"dist"),o=_.sync(s,{cwd:a}).filter(e=>W(e)!==".d.ts").map(e=>g(e));if(G(s)){o.length||o.push(s);const e=s.replace(d,n).replace(f,".js").replace(c,".js");t.log("Cleaning emitted file:",{outfile:e}),x(e,{force:!0})}else t.log("Cleaning old output:",{outDir:n}),x(n,{force:!0,recursive:!0});console.log();const j=o.filter(e=>f.test(e)).filter(e=>!c.test(e));t.log("Compiling TS files:",{tsFiles:j}),await D({...h,entryPoints:j.filter(e=>!e.endsWith(".d.ts"))}),p("Built TS files.").succeed();const k=o.filter(e=>c.test(e));t.log("Compiling TSX files:",{tsxFiles:k});const R=k.filter(e=>!e.endsWith(".d.ts"));await Promise.all(R.map(async e=>{const i=`
1
+ import{dirname as D,extname as v,isAbsolute as W,resolve as N,resolve as u}from"path";import{build as O}from"esbuild";import{existsSync as x,mkdirSync as G,readFileSync as g,rmSync as B,writeFileSync as C}from"fs";import m from"chalk";import{env as E}from"process";import M from"fast-glob";import p from"ora";import{createDebugLogger as _,log as y}from"create-debug-logger";import{isJsOrTs as A,isTs as f,isTsxOrJsx as l}from"../../utils/index.js";import{createShell as I}from"await-shell";import{emitTsDeclarations as L}from"./lib/emitTsDeclarations.js";import{getPackageJsonFile as z}from"../../utils/pkgJson.js";import{normalizeImportSpecifiers as V}from"../normalize/index.js";const P=s=>{const c=` ${s} `;y(m.bgBlue(m.bold(m.white(c))))},X=()=>{let s;if(x("dist/package.json")?s=JSON.parse(g("dist/package.json","utf-8")):s={},s?.module==="module")return!0;s.type="module",C("dist/package.json",JSON.stringify(s,null,2))},K=async({files:s="src/**/*",styles:c="src/styles/components/index.css",bundle:S=!1,dev:i=!1,runtimeOnly:w=!1})=>{E.NODE_ENV=i?"development":"production";const F=I(),t=_(K);t.log("Building",{files:s,dev:i,runtimeOnly:w}),P(`${m.bold("TS Module")} [${E.NODE_ENV}]`);const h=await z(),j=JSON.parse(h),a=process.cwd(),J={absWorkingDir:a,bundle:S,treeShaking:S,outbase:"src",outdir:"dist",assetNames:"[name].js",logLevel:i?"warning":"error",charset:"utf8",format:"esm",target:"esnext",platform:j?.platform??"node",minify:!i,define:{PACKAGE_JSON:h}},d=u(a,"src"),r=u(a,"dist"),o=M.sync(s,{cwd:a}).filter(e=>v(e)!==".d.ts").map(e=>u(e));if(W(s)){o.length||o.push(s);const e=s.replace(d,r).replace(f,".js").replace(l,".js");t.log("Cleaning emitted file:",{outfile:e}),B(e,{force:!0})}else t.log("Cleaning old output:",{outDir:r}),B(r,{force:!0,recursive:!0});console.log();const T=o.filter(e=>f.test(e)).filter(e=>!l.test(e));t.log("Compiling TS files:",{tsFiles:T}),await O({...J,entryPoints:T.filter(e=>!e.endsWith(".d.ts"))}),p("Built TS files.").succeed();const b=o.filter(e=>l.test(e));t.log("Compiling TSX files:",{tsxFiles:b});const $=b.filter(e=>!e.endsWith(".d.ts"));await Promise.all($.map(async e=>{const n=`
2
2
  import React from "react";
3
3
  import ReactDOM from "react-dom";
4
- ${u(e,"utf-8")}
5
- `;await D({...h,stdin:{contents:i,sourcefile:e,resolveDir:T(e),loader:"tsx"},outdir:void 0,outfile:e.replace(c,".js").replace(d,n),jsxFactory:"React.createElement"})})),p("Built TSX files.").succeed();const J=o.filter(e=>!L.test(e));if(t.log("Copying non-JS/TS files.",{allFiles:o,nonJsTsFiles:J}),await Promise.all(J.map(async e=>{const i=b(a,e).replace(d,n).replace(f,".js").replace(c,".js");t.log("Copying non-source file:",{file:e,outfile:i}),M(T(i),{recursive:!0}),N(i,u(e),{encoding:"binary",flag:"w"})})),!process.env.NO_REWRITES){const e=s.replace(d,n).replace(/^(\.\/)?src\//,"dist/").replace(f,".js").replace(c,".js");await X(e.endsWith(".js")?e:`${e}.js`),p("Normalized import specifiers.").succeed()}if(K()&&p('Forced "type": "module" in output.').succeed(),console.log(),!(r||S)){if(P(b(l))){t.log("Building styles for production.");const e=await B(),{style:i="./dist/styles.css"}=e,v=["npx tailwindcss",r?"":"-m","--postcss postcss.config.js",`-i ${l}`,"-o",i];await E.run(v.join(" "))}else t.log("Styles not found.",{styles:l});C("Running post-build setup."),y(`Generating type declarations.
6
- This might take a moment.`),V(o),p(`Generated delcarations for ${o.length} files.`).succeed(),y(m.green("Build complete."))}};export{C as bannerLog,U as build};
4
+ ${g(e,"utf-8")}
5
+ `;await O({...J,stdin:{contents:n,sourcefile:e,resolveDir:D(e),loader:"tsx"},outdir:void 0,outfile:e.replace(l,".js").replace(d,r),jsxFactory:"React.createElement"})})),p("Built TSX files.").succeed();const k=o.filter(e=>!A.test(e));if(t.log("Copying non-JS/TS files.",{allFiles:o,nonJsTsFiles:k}),await Promise.all(k.map(async e=>{const n=N(a,e).replace(d,r).replace(f,".js").replace(l,".js");t.log("Copying non-source file:",{file:e,outfile:n}),G(D(n),{recursive:!0}),C(n,g(e),{encoding:"binary",flag:"w"})})),!process.env.NO_REWRITES){const e=s.replace(d,r).replace(/^(\.\/)?src\//,"dist/").replace(f,".js").replace(l,".js");await V(e.endsWith(".js")?e:`${e}.js`),p("Normalized import specifiers.").succeed()}if(X()&&p('Forced "type": "module" in output.').succeed(),console.log(),!(i||w)){if(x(N(c))){t.log("Building styles for production.");const{style:e="./dist/styles.css"}=j,R=["npx tailwindcss",i?"":"-m","--postcss postcss.config.js",`-i ${c}`,"-o",e];await F.run(R.join(" "))}else t.log("Styles not found.",{styles:c});P("Running post-build setup."),y(`Generating type declarations.
6
+ This might take a moment.`),L(o),p(`Generated delcarations for ${o.length} files.`).succeed(),y(m.green("Build complete."))}};export{P as bannerLog,K as build};
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- var r="@tsmodule/tsmodule",s="node",d="module",m="23.0.3",l="tsmodule/tsmodule",a="TypeScript Module loader and compiler",c="MIT",p={tsmodule:"dist/index.js"},u={".":"./dist/loader/index.js","./*":"./dist/*/index.js","./package.json":"./package.json"},f="dist/types/index.d.ts",b=[{name:"C. Lewis",email:"ctj.lewis@icloud.com",url:"https://ctjlewis.com"}],g=["dist/","templates/"],h={node:">=14"},v={bootstrap:"node bootstrap.js",bootload:"node --no-warnings --loader ./dist/loader/index.js src/index.ts build -r",prebuild:"yarn bootstrap && yarn bootload && yarn link -f",build:"tsmodule build",dev:"tsmodule dev",lint:"eslint --fix src",prepare:"yarn build -r",prepublishOnly:"yarn build && yarn test",pretest:"yarn build -r",test:"ava --no-worker-threads"},y={"@types/glob":"^7.2.0","@types/node":"^17.0.21","@typescript-eslint/eslint-plugin":"^5.14.0","@typescript-eslint/parser":"^5.14.0",ava:"^4.1.0","await-shell":"^31.0.0",chalk:"^5.0.1",commander:"^9.0.0","create-debug-logger":"^3.0.0",esbuild:"^0.14.25",eslint:"^8.10.0","fast-glob":"^3.2.10","fs-extra":"^10.0.1",glob:"^7.2.0","node-watch":"^0.7.3",ora:"^6.1.0",path:"^0.12.7",typescript:"^4.6.2"},S=["esm","loader","typescript","loader hook","require hook","experimental-loader"],w={timeout:"10 minutes",files:["test/**/*.test.ts"],extensions:{ts:"module"},nodeArguments:["--no-warnings","--loader=@tsmodule/tsmodule"]},T={"@types/fs-extra":"^9.0.13"},t={name:r,platform:s,type:d,version:m,repository:l,description:a,license:c,bin:p,exports:u,types:f,contributors:b,files:g,engines:h,scripts:v,dependencies:y,keywords:S,ava:w,devDependencies:T};import o from"chalk";import{Command as B}from"commander";import{build as x}from"./commands/build/index.js";import{create as C}from"./commands/create/index.js";import{dev as R}from"./commands/dev/index.js";import{execute as j}from"./commands/execute/index.js";import{normalizeImportSpecifiers as A}from"./commands/normalize/index.js";const{version:E}=t,i=new B;i.name(o.white(o.bold("tsmodule"))).usage(o.white(o.bold("<file | command> [options]"))).description(o.blueBright(`A tool for building TypeScript modules.
2
+ var r="@tsmodule/tsmodule",s="node",d="module",m="23.1.0",l="tsmodule/tsmodule",a="TypeScript Module loader and compiler",c="MIT",p={tsmodule:"dist/index.js"},u={".":"./dist/loader/index.js","./*":"./dist/*/index.js","./package.json":"./package.json"},f="dist/types/index.d.ts",b=[{name:"C. Lewis",email:"ctj.lewis@icloud.com",url:"https://ctjlewis.com"}],g=["dist/","templates/"],h={node:">=14"},v={bootstrap:"node bootstrap.js",bootload:"node --no-warnings --loader ./dist/loader/index.js src/index.ts build -r",prebuild:"yarn bootstrap && yarn bootload && yarn link -f",build:"tsmodule build",dev:"tsmodule dev",lint:"eslint --fix src",prepare:"yarn build -r",prepublishOnly:"yarn build && yarn test",pretest:"yarn build -r",test:"ava --no-worker-threads"},y={"@types/glob":"^7.2.0","@types/node":"^17.0.21","@typescript-eslint/eslint-plugin":"^5.14.0","@typescript-eslint/parser":"^5.14.0",ava:"^4.1.0","await-shell":"^31.0.0",chalk:"^5.0.1",commander:"^9.0.0","create-debug-logger":"^3.0.0",esbuild:"^0.14.25",eslint:"^8.10.0","fast-glob":"^3.2.10","fs-extra":"^10.0.1",glob:"^7.2.0","node-watch":"^0.7.3",ora:"^6.1.0",path:"^0.12.7",typescript:"^4.6.2"},S=["esm","loader","typescript","loader hook","require hook","experimental-loader"],w={timeout:"10 minutes",files:["test/**/*.test.ts"],extensions:{ts:"module"},nodeArguments:["--no-warnings","--loader=@tsmodule/tsmodule"]},T={"@types/fs-extra":"^9.0.13"},t={name:r,platform:s,type:d,version:m,repository:l,description:a,license:c,bin:p,exports:u,types:f,contributors:b,files:g,engines:h,scripts:v,dependencies:y,keywords:S,ava:w,devDependencies:T};import o from"chalk";import{Command as B}from"commander";import{build as x}from"./commands/build/index.js";import{create as C}from"./commands/create/index.js";import{dev as R}from"./commands/dev/index.js";import{execute as j}from"./commands/execute/index.js";import{normalizeImportSpecifiers as A}from"./commands/normalize/index.js";const{version:E}=t,i=new B;i.name(o.white(o.bold("tsmodule"))).usage(o.white(o.bold("<file | command> [options]"))).description(o.blueBright(`A tool for building TypeScript modules.
3
3
 
4
4
  Run TS directly: ${o.bold("tsmodule src/index.ts")}
5
5
  Use a command: ${o.bold("tsmodule build")}`)).version(String(E)),i.command("dev").description("Build and watch for changes.").action(R),i.command("build").option("--files <files>","Files to build","src/**/*").option("--styles <styles>","The styles to build","src/styles/components/index.css").option("-b, --bundle","Bundle dependencies").option("-d, --dev","Build development runtime").option("-r, --runtime-only","Do not emit type declarations, only build JS runtime").description("Builds TS files to output in dist/.").action(x),i.command("create <name>").option("--react","Create React component library with Next.js").description("Create a new project.").action(C),i.command("normalize [files]").description(`Rewrites import specifiers in files to ESM-compliant paths.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsmodule/tsmodule",
3
3
  "platform": "node",
4
4
  "type": "module",
5
- "version": "23.0.3",
5
+ "version": "23.1.0",
6
6
  "repository": "tsmodule/tsmodule",
7
7
  "description": "TypeScript Module loader and compiler",
8
8
  "license": "MIT",