@tsmodule/tsmodule 22.2.0 → 23.0.0

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