@tsmodule/tsmodule 23.2.1 → 25.0.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 k,extname as R,isAbsolute as W,resolve as O,resolve as u}from"path";import{build as D}from"esbuild";import{existsSync as E,mkdirSync as _,readFileSync as g,rmSync as x,writeFileSync as B}from"fs";import p from"chalk";import{env as C}from"process";import G from"fast-glob";import m from"ora";import{createDebugLogger as M,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 V}from"../../utils/pkgJson.js";import{normalizeImportSpecifiers as z}from"../normalize/index.js";const P=s=>{const c=` ${s} `;y(p.bgBlue(p.bold(p.white(c))))},X=()=>{let s;if(E("dist/package.json")?s=JSON.parse(g("dist/package.json","utf-8")):s={},s?.module==="module")return!0;s.type="module",B("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})=>{C.NODE_ENV=i?"development":"production";const F=I(),t=M(K);t.log("Building",{files:s,dev:i,runtimeOnly:w}),P(`${p.bold("TS Module")} [${C.NODE_ENV}]`);const J=await V(),h=JSON.parse(J),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:h?.platform??"node",minify:!i,define:{PACKAGE_JSON:J,"process.env.NODE_ENV":i?JSON.stringify("development"):JSON.stringify("production")}},d=u(a,"src"),r=u(a,"dist"),o=G.sync(s,{cwd:a}).filter(e=>R(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}),x(e,{force:!0})}else t.log("Cleaning old output:",{outDir:r}),x(r,{force:!0,recursive:!0});console.log();const N=o.filter(e=>f.test(e)).filter(e=>!l.test(e));t.log("Compiling TS files:",{tsFiles:N}),await D({...j,entryPoints:N.filter(e=>!e.endsWith(".d.ts"))}),m("Built TS files.").succeed();const T=o.filter(e=>l.test(e));t.log("Compiling TSX files:",{tsxFiles:T});const $=T.filter(e=>!e.endsWith(".d.ts"));await Promise.all($.map(async e=>{const n=`
1
+ import{dirname as E,extname as I,isAbsolute as V,resolve as C,resolve as B}from"path";import{build as J}from"esbuild";import{existsSync as P,mkdirSync as z,readFileSync as O,rmSync as R,writeFileSync as F}from"fs";import l from"chalk";import{env as $}from"process";import X from"fast-glob";import p from"ora";import{createDebugLogger as K,log as d}from"create-debug-logger";import{isJsOrTs as v,isTs as u,isTsxOrJsx as m}from"../../utils/index.js";import{createShell as U}from"await-shell";import{emitTsDeclarations as q}from"./lib/emitTsDeclarations.js";import{normalizeImportSpecifiers as H}from"../normalize/index.js";import{getPackageJsonFile as Q}from"../../utils/pkgJson.js";import{getWorkingDirs as W}from"../../utils/cwd.js";import{readStdin as Y}from"../../utils/stdin.js";const L=t=>{const s=` ${t} `;d(l.bgBlue(l.bold(l.white(s))))},Z=()=>{let t;if(P("dist/package.json")?t=JSON.parse(O("dist/package.json","utf-8")):t={},t?.module==="module")return!0;t.type="module",F("dist/package.json",JSON.stringify(t,null,2))},_=(t,s,f)=>{const{srcDir:i,outDir:g}=W();return s=B(s),{stdin:{contents:t,sourcefile:s,resolveDir:E(s),loader:f},outdir:void 0,outfile:s.replace(v,".js").replace(i,g)}},ee=async({files:t="src/**/*",styles:s="src/styles/components/index.css",bundle:f=!1,dev:i=!1,runtimeOnly:g=!1,stdin:n="",stdinFile:y=""})=>{$.NODE_ENV=i?"development":"production";const o=K(ee),G=U(),{cwd:S,srcDir:w,outDir:a}=W(),b=await Q(),k=JSON.parse(b),h={absWorkingDir:S,bundle:f,treeShaking:f,outbase:"src",outdir:"dist",assetNames:"[name].js",logLevel:i?"warning":"error",charset:"utf8",format:"esm",target:"esnext",platform:k?.platform??"node",minify:!i,define:{PACKAGE_JSON:b,"process.env.NODE_ENV":i?JSON.stringify("development"):JSON.stringify("production")}};let j="";if(n){o.log("Building file from stdin",{stdin:n,stdinFile:y}),y||(d(l.red("ERROR: --stdin-file must be specified when using stdin.")),process.exit(1)),typeof n=="string"&&n.length?j=n:j=await Y();const e=_(j,y,"tsx");await J({...h,...e});return}o.log("Building",{files:t,dev:i,runtimeOnly:g}),L(`${l.bold("TS Module")} [${$.NODE_ENV}]`);const r=X.sync(t,{cwd:S}).filter(e=>I(e)!==".d.ts").map(e=>B(e));if(V(t)){r.length||r.push(t);const e=t.replace(w,a).replace(u,".js").replace(m,".js");o.log("Cleaning emitted file:",{outfile:e}),R(e,{force:!0})}else o.log("Cleaning old output:",{outDir:a}),R(a,{force:!0,recursive:!0});console.log();const x=r.filter(e=>u.test(e)).filter(e=>!m.test(e));o.log("Compiling TS files:",{tsFiles:x}),await J({...h,entryPoints:x.filter(e=>!e.endsWith(".d.ts"))}),p("Built TS files.").succeed();const D=r.filter(e=>m.test(e));o.log("Compiling TSX files:",{tsxFiles:D});const M=D.filter(e=>!e.endsWith(".d.ts"));await Promise.all(M.map(async e=>{const c=`
2
2
  import React from "react";
3
3
  import ReactDOM from "react-dom";
4
- ${g(e,"utf-8")}
5
- `;await D({...j,stdin:{contents:n,sourcefile:e,resolveDir:k(e),loader:"tsx"},outdir:void 0,outfile:e.replace(l,".js").replace(d,r),jsxFactory:"React.createElement"})})),m("Built TSX files.").succeed();const b=o.filter(e=>!A.test(e));if(t.log("Copying non-JS/TS files.",{allFiles:o,nonJsTsFiles:b}),await Promise.all(b.map(async e=>{const n=O(a,e).replace(d,r).replace(f,".js").replace(l,".js");t.log("Copying non-source file:",{file:e,outfile:n}),_(k(n),{recursive:!0}),B(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 z(e.endsWith(".js")?e:`${e}.js`),m("Normalized import specifiers.").succeed()}if(X()&&m('Forced "type": "module" in output.').succeed(),console.log(),!(i||w)){if(E(O(c))){t.log("Building styles for production.");const{style:e="./dist/styles.css"}=h,v=["npx tailwindcss",i?"":"-m","--postcss postcss.config.js",`-i ${c}`,"-o",e];await F.run(v.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),m(`Generated delcarations for ${o.length} files.`).succeed(),y(p.green("Build complete."))}};export{P as bannerLog,K as build};
4
+ ${O(e,"utf-8")}
5
+ `,T=_(c,e,"tsx");await J({...h,...T,jsxFactory:"React.createElement"})})),p("Built TSX files.").succeed();const N=r.filter(e=>!v.test(e));if(o.log("Copying non-JS/TS files.",{allFiles:r,nonJsTsFiles:N}),await Promise.all(N.map(async e=>{const c=C(S,e).replace(w,a).replace(u,".js").replace(m,".js");o.log("Copying non-source file:",{file:e,outfile:c}),z(E(c),{recursive:!0}),F(c,O(e),{encoding:"binary",flag:"w"})})),!process.env.NO_REWRITES){const e=t.replace(w,a).replace(/^(\.\/)?src\//,"dist/").replace(u,".js").replace(m,".js");await H(e.endsWith(".js")?e:`${e}.js`),p("Normalized import specifiers.").succeed()}if(Z()&&p('Forced "type": "module" in output.').succeed(),console.log(),!(i||g)){if(P(C(s))){o.log("Building styles for production.");const{style:e="./dist/styles.css"}=k,A=["npx tailwindcss",i?"":"-m","--postcss postcss.config.js",`-i ${s}`,"-o",e];await G.run(A.join(" "))}else o.log("Styles not found.",{styles:s});L("Running post-build setup."),d(`Generating type declarations.
6
+ This might take a moment.`),q(r),p(`Generated delcarations for ${r.length} files.`).succeed(),d(l.green("Build complete."))}};export{L as bannerLog,ee as build};
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- var r="@tsmodule/tsmodule",s="node",d="module",m="23.2.1",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",l="25.0.0",m="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",retest:"SKIP_TEST_SETUP=1 yarn test"},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:l,repository:m,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 R}from"./commands/build/index.js";import{create as x}from"./commands/create/index.js";import{dev as C}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
- 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.
5
+ Use a command: ${o.bold("tsmodule build")}`)).version(String(E)),i.command("dev").description("Build and watch for changes.").action(C),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",!1).option("-d, --dev","Build development runtime",!1).option("-r, --runtime-only","Do not emit type declarations, only build JS runtime",!1).option("--stdin [source]","Read from a string or stdin.","").option("--stdin-file [file]","File path to mock for stdin.","").description("Builds TS files to output in dist/.").action(R),i.command("create <name>").option("--react","Create React component library with Next.js").description("Create a new project.").action(x),i.command("normalize [files]").description(`Rewrites import specifiers in files to ESM-compliant paths.
6
6
  (default: dist/**/*.js)`).action(async({files:n})=>{await A(n)}),i.command("run",{isDefault:!0}).argument("<file>","The file to run.").option("--d, --dev","Enable development mode").description("Run the given TS program, analogous to `node <file>`.").action(j),i.parse(process.argv);
@@ -0,0 +1 @@
1
+ import{resolve as o}from"path";const i=()=>{const r=process.cwd(),s=o(r,"src"),t=o(r,"dist");return{cwd:r,srcDir:s,outDir:t}};export{i as getWorkingDirs};
@@ -0,0 +1 @@
1
+ import{createDebugLogger as c,log as d}from"create-debug-logger";import a from"chalk";import{readFileSync as m}from"fs";function g(n){return new Promise((e,r)=>{const t=[];function i(o){t.push(o)}function s(){u(),e(Buffer.concat(t))}function f(o){u(),r(o)}function u(){n.removeListener("data",i),n.removeListener("end",s),n.removeListener("error",f)}n.on("data",i),n.on("end",s),n.on("error",f)})}const p=async()=>{const n=c(p);n.log("Reading stdin");try{const r=m(0).toString();if(r)return n.log("Found stdin",{stdin:r}),r}catch(r){n.log("Could not read stdin",{e:r})}return d(a.gray("Type your source code. Press Ctrl+D to finish.")),(await g(process.stdin)).toString()};export{p as readStdin};
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsmodule/tsmodule",
3
3
  "platform": "node",
4
4
  "type": "module",
5
- "version": "23.2.1",
5
+ "version": "25.0.0",
6
6
  "repository": "tsmodule/tsmodule",
7
7
  "description": "TypeScript Module loader and compiler",
8
8
  "license": "MIT",
@@ -39,7 +39,8 @@
39
39
  "prepare": "yarn build -r",
40
40
  "prepublishOnly": "yarn build && yarn test",
41
41
  "pretest": "yarn build -r",
42
- "test": "ava --no-worker-threads"
42
+ "test": "ava --no-worker-threads",
43
+ "retest": "SKIP_TEST_SETUP=1 yarn test"
43
44
  },
44
45
  "dependencies": {
45
46
  "@types/glob": "^7.2.0",