@tsmodule/tsmodule 15.2.0 → 17.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/commands/build/index.js +3 -3
- package/dist/commands/create/index.js +1 -1
- package/dist/commands/dev/index.js +3 -3
- package/dist/commands/execute/index.js +1 -1
- package/dist/commands/normalize/index.js +2 -2
- package/dist/index.js +1 -1
- package/dist/loader/index.js +1 -1
- package/dist/package.json +3 -0
- package/dist/utils/pkgJson.js +1 -1
- package/package.json +4 -4
@@ -1,3 +1,3 @@
|
|
1
|
-
import{build as T}from"esbuild";import{
|
2
|
-
`}}),
|
3
|
-
This might take a moment.`)
|
1
|
+
import{build as T}from"esbuild";import{extname as E,isAbsolute as F,resolve as N,resolve as u}from"path";import{existsSync as O,readFileSync as x,rmSync as h,writeFileSync as D}from"fs";import l from"chalk";import{copy as P}from"fs-extra";import{env as w}from"process";import B from"fast-glob";import c from"ora";import{createDebugLogger as C,log as d}from"create-debug-logger";import{isJsOrTs as W,isTs as m,isTsxOrJsx as a}from"../../utils/index.js";import{emitTsDeclarations as $}from"./lib/emitTsDeclarations.js";import{getPackageJsonFile as v}from"../../utils/pkgJson.js";import{normalizeImportSpecifiers as G}from"../normalize/index.js";const J=t=>{const s=` ${t} `;d(l.bgBlue(l.bold(l.white(s))))},_=()=>{let t;if(O("dist/package.json")?t=JSON.parse(x("dist/package.json","utf-8")):t={},t?.module==="module")return!0;t.type="module",D("dist/package.json",JSON.stringify(t,null,2))},A=async({files:t="src/**/*",dev:s=!1,fast:g=!1})=>{w.NODE_ENV=s?"development":"production";const r=C(A);r.log("Building",{files:t,dev:s,fast:g}),J(`${l.bold("TS Module")} [${w.NODE_ENV}]`);const k=await v(),i=process.cwd(),f={absWorkingDir:i,outbase:"src",outdir:"dist",assetNames:"[name].js",logLevel:s?"warning":"error",charset:"utf8",format:"esm",target:"esnext",minify:!s,define:{PACKAGE_JSON:k}},p=u(i,"src"),n=u(i,"dist"),o=B.sync(t,{cwd:i}).filter(e=>E(e)!==".d.ts").map(e=>u(e));if(F(t)){o.length||o.push(t);const e=t.replace(p,n).replace(m,".js").replace(a,".js");r.log("Cleaning emitted file:",{outfile:e}),h(e,{force:!0})}else r.log("Cleaning old output:",{outDir:n}),h(n,{force:!0,recursive:!0});console.log();const y=o.filter(e=>m.test(e)).filter(e=>!a.test(e));r.log("Compiling TS files:",{tsFiles:y}),await T({...f,entryPoints:y.filter(e=>!e.endsWith(".d.ts"))}),c("Built TS files.").succeed();const S=o.filter(e=>a.test(e));r.log("Compiling TSX files:",{tsxFiles:S}),await T({...f,entryPoints:S.filter(e=>!e.endsWith(".d.ts")),jsxFactory:"createElement",banner:{js:`import { createElement } from "react";
|
2
|
+
`}}),c("Built TSX files.").succeed();const b=o.filter(e=>!W.test(e));if(r.log("Copying non-JS/TS files.",{allFiles:o,nonJsTsFiles:b}),await Promise.all(b.map(async e=>{const j=N(i,e).replace(p,n).replace(m,".js").replace(a,".js");r.log("Copying non-source file:",{file:e,outfile:j}),await P(e,j)})),!process.env.NO_REWRITES){const e=t.replace(p,n).replace(/^(\.\/)?src\//,"dist/").replace(m,".js").replace(a,".js");await G(e.endsWith(".js")?e:`${e}.js`),c("Normalized import specifiers.").succeed()}_()&&c('Forced "type": "module" in output.').succeed(),console.log(),!(s||g)&&(J("Running post-build setup."),d(`Generating type declarations.
|
3
|
+
This might take a moment.`),$(o),c(`Generated delcarations for ${o.length} files.`).succeed(),d(l.green("Build complete.")))};export{J as bannerLog,A as build};
|
@@ -1 +1 @@
|
|
1
|
-
import o from"chalk";import c
|
1
|
+
import o from"chalk";import{createShell as c}from"await-shell";import d from"ora";import{createTemplate as a}from"./lib/createTemplate.js";import{rewritePkgJson as l}from"./lib/rewritePkgJson.js";globalThis.SHELL_OPTIONS={stdio:["ignore","ignore","inherit"]};const w=async(e,{react:s=!1})=>{const i=c(),t=d(`Creating new module ${o.blueBright(e)}.`).start();await a("default",e),s&&await a("react",e),await l(e),t.succeed("Project created. Installing dependencies."),process.chdir(e);const n=[],r=["@tsmodule/tsmodule"];s&&(n.push("react","react-dom"),r.push("next","eslint-config-next","tailwindcss","postcss","autoprefixer")),n.length&&await i.run(`yarn add ${n.join(" ")}`),r.length&&await i.run(`yarn add -D ${r.join(" ")}`),t.succeed("Dependencies installed. Initializing git."),await i.run("git init"),t.succeed("Git initialized.")};export{w as create};
|
@@ -1,3 +1,3 @@
|
|
1
|
-
import{relative as a,resolve as m}from"path";import{build as
|
2
|
-
`,
|
3
|
-
`,
|
1
|
+
import{relative as a,resolve as m}from"path";import{build as s}from"../build/index.js";import r from"chalk";import{createShell as p}from"await-shell";import{existsSync as u,lstatSync as f}from"fs";import{log as d}from"create-debug-logger";import w from"ora";import g from"node-watch";const o=()=>{console.clear()},i=t=>{d(`
|
2
|
+
`,r.gray(`Built ${r.bold(t)}.`),`
|
3
|
+
`,r.blue(new Date().toLocaleString()))},L=async()=>{const t=process.cwd(),n=p();process.platform!=="win32"&&await n.run("clear"),o(),await s({dev:!0}),i("src/**/*"),g(m(t,"src"),{recursive:!0,persistent:!0}).on("change",async(v,e)=>{if(!u(e)||!f(e).isFile())return;o();const c=Date.now();await s({dev:!0,files:e});const l=Date.now()-c;w(r.blueBright(`Dev refresh finished in ${r.bold(`${l}ms`)}.`)).succeed(),i(a(t,e))})};export{L as dev};
|
@@ -1 +1 @@
|
|
1
|
-
import{URL as n}from"url";import{
|
1
|
+
import{URL as n}from"url";import{createShell as s}from"await-shell";const i=async()=>{const e=import.meta.url,o=new n("../../../dist/loader/index.js",e),r=["--no-warnings","--loader",o.href,...process.argv.slice(2)];await s().run(`node ${r.join(" ")}`)};export{i as execute};
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import{
|
2
|
-
\r;]*`,o=t.replace(".","\\.").replace("/","\\/"),
|
1
|
+
import{readFileSync as w,writeFileSync as $}from"fs";import y from"fast-glob";import{pathToFileURL as S}from"url";import{resolve as x}from"path";import{createDebugLogger as g}from"create-debug-logger";import{getRewrittenSpecifiers as E}from"./lib/typescriptApi.js";const I=t=>{const r=`[^
|
2
|
+
\r;]*`,o=t.replace(".","\\.").replace("/","\\/"),s=`${r}["']${o}["']${r}`,e=`(import${r}from)`,i=`(import|require)${r}\\(`,n=`(export${r}from)`;return new RegExp(`(${e}|${i}|${n})${s}`,"g")},d=(t,r,o)=>{g(d).log("Rewriting import",{importStatement:t,specifierToReplace:r,specifierReplacement:o});const[,e]=t.split(/from|\(/),i=e.replace(r,o).trim();return t.replace(e,i)},P=async(t="dist/**/*.js")=>{const r=g(P),o=await y(t,{cwd:process.cwd()});r.log("Normalizing import/require specifiers:",{filesToNormalize:o});for(const s of o){const e=x(s),i=S(e).href,n=E(e);if(!n)return null;r.log("TypeScript API yielded specifiers to rewrite:",{rewrites:n});let c=w(e,"utf8");r.group();for(const{specifierToReplace:p,specifierReplacement:l}of n){const u=I(p),a=c.match(u)??[];r.log("Replacing import statements.",{entryPointURL:i,specifierToReplace:p,specifierReplacement:l,importStatements:a});for(const m of a){r.group();const f=d(m,p,l);r.log("Performing specifier rewrite.",{entryPointURL:i,importStatement:m,rewrittenImportStatement:f}),c=c.replace(m,f),$(e,c),r.log("Wrote output file.",{resolvedEntryPoint:e}),r.groupEnd()}}r.groupEnd()}};export{I as generateImportPattern,P as normalizeImportSpecifiers,d as rewriteImportStatement};
|
package/dist/index.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
var r="@tsmodule/tsmodule",a="module",m="
|
2
|
+
var r="@tsmodule/tsmodule",a="module",m="17.0.0",d="tsmodule/tsmodule",s="TypeScript Module loader and compiler",l="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/","template/"],v={node:">=14"},g={bootstrap:"node bootstrap.js",bootload:"node --no-warnings --loader ./dist/loader/index.js src/index.ts build -f",prebuild:"yarn bootstrap && yarn bootload && yarn link -f",build:"tsmodule build",dev:"tsmodule dev",lint:"eslint --fix src",prepare:"yarn build -f",prepublishOnly:"yarn build && yarn test",pretest:"yarn build -f",test:"ava --no-worker-threads"},h={"@types/glob":"^7.2.0","@types/node":"^17.0.15","@typescript-eslint/eslint-plugin":"^5.9.1","@typescript-eslint/parser":"^5.9.1",ava:"^4.0.1","await-shell":"^29.0.1-0",chalk:"^5.0.0",commander:"^8.3.0","create-debug-logger":"^1.10.1",esbuild:"^0.14.0",eslint:"^8.8.0","fast-glob":"^3.2.10","fs-extra":"^10.0.0",glob:"^7.2.0","node-watch":"^0.7.3",ora:"^6.0.1",path:"^0.12.7",typescript:"^4.5.5"},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"]},y={"@types/fs-extra":"^9.0.13"},t={name:r,type:a,version:m,repository:d,description:s,license:l,bin:c,exports:p,types:u,contributors:f,files:b,engines:v,scripts:g,dependencies:h,keywords:S,ava:w,devDependencies:y};import e from"chalk";import{Command as T}from"commander";import{build as B}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 x}from"./commands/normalize/index.js";const{version:A}=t,i=new T;i.name(e.white(e.bold("tsmodule"))).usage(e.white(e.bold("<file | command> [options]"))).description(e.blueBright(`A tool for building TypeScript modules.
|
3
3
|
|
4
4
|
Run TS directly: ${e.bold("tsmodule src/index.ts")}
|
5
5
|
Use a command: ${e.bold("tsmodule build")}`)).version(String(A)),i.command("dev").description("Build and watch for changes.").action(R),i.command("build").option("--files <files>","The files to build (default: all)").option("-d, --dev","Build development version").option("-f, --fast","Do not emit type declarations, only transform to JS").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(C),i.command("normalize [files]").description(`Rewrites import specifiers in files to ESM-compliant paths.
|
package/dist/loader/index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
import{extname as m,isAbsolute as U,join as E,normalize as
|
1
|
+
import{extname as m,isAbsolute as U,join as E,normalize as S,resolve as w}from"path";import{fileURLToPath as H,pathToFileURL as v,URL as L}from"url";import{createDebugLogger as d}from"create-debug-logger";import{readFileSync as G}from"fs";import{transform as h}from"esbuild";import{posix as M}from"path";import{win32 as T}from"path";import{checkExtensions as k,checkTsExtensions as D,fileExists as b,isJs as y,isTs as N,MODULE_LOADERS as R}from"../utils/index.js";import{getPackageJsonFile as P}from"../utils/pkgJson.js";const F=await P(),A=async(o,r,t)=>{const{parentURL:s}=r,e=d(A);if(e.log("Resolving specifier:",{importedFromURL:s,specifier:o}),!o.startsWith(".")&&!U(o))return e.log("Using defaultResolve for named module:",{specifier:o}),t(o,r,t);const{href:l}=v(process.cwd()),{href:i}=new L(s||l);e.log("Finding import URL for",{specifier:o,baseURL:i});let n=o;o.startsWith("file://")||(U(o)?(e.log("Setting import URL to absolute specifier."),n=v(w(S(o))).href):(e.log("Setting import URL relative to baseURL."),n=new L(o,i).href),e.log("Resolved import URL:",{importedFileURL:n,importedFromURL:s}));const f=m(s??"").toLowerCase(),c=m(n).toLowerCase();if(e.log("Rewriting file extension:",{parentExtension:f,specifierExtension:c}),c){const a=n.substring(0,n.lastIndexOf(c));if(e.log("Re-resolving specifier:",{unresolvedSpecifier:a}),y.test(c)&&N.test(f)){const p=D(a);if(p)return e.log("Found JS import in TS:",{unresolvedSpecifier:a,resolvedTsSourceFile:p}),{url:p}}return b(a)?(e.log("Found file at unresolved specifier:",{unresolvedSpecifier:a}),{url:a}):t(o,r,t)}e.log("Resolving incomplete URL import to file:",{specifier:o});const u=k(n);if(u)return e.log("Resolved import URL to file:",{resolvedFile:u}),{url:u};const x=new L(E(n,"index")).href,g=k(x);return g?(e.log("Resolved import URL to index file:",{resolvedIndexFile:g}),{url:g}):t(o,r,t)},J=async(o,r,t)=>{const s=d(J);if(s.log("Loading source file:",{url:o}),!o.includes(T.sep)&&!o.includes(M.sep))return s.log("Using defaultLoad for named module:",{url:o}),t(o,r,t);const e=m(o),l=R[e];if(!l)return s.log("No loader found, using defaultLoad:",{url:o}),t(o,r,t);const i=H(o),n=G(i,"utf8"),f=await h(n.toString(),{...l,sourcefile:i,format:"esm",define:{PACKAGE_JSON:F}});return{format:"module",source:f.code}},O=async(o,r,t)=>{const s=d(O);s.log("Getting format for source file:",{url:o});const e=m(o);return R[e]?{format:"module"}:(s.log("No loader found, using default format:",{url:o}),t(o,r,t))},B=async(o,r,t)=>{const s=d(B);s.log("Transforming source from context:",{context:r});const{url:e}=r,l=m(e),i=R[l];return i?{source:(await h(o.toString(),{...i,logLevel:"info",charset:"utf8",target:"esnext",sourcefile:r.url,format:r.format==="module"?"esm":"cjs",define:{PACKAGE_JSON:F}})).code}:(s.log("No loader found, using default transformer:",{url:e}),t(o,r,t))};export{O as getFormat,J as load,A as resolve,B as transformSource};
|
package/dist/utils/pkgJson.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
import{
|
1
|
+
import{readFileSync as c}from"fs";import{resolve as r}from"path";const a=async()=>{const o=process.cwd(),e=r(o,"package.json");return c(e,"utf-8")};export{a as getPackageJsonFile};
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@tsmodule/tsmodule",
|
3
3
|
"type": "module",
|
4
|
-
"version": "
|
4
|
+
"version": "17.0.0",
|
5
5
|
"repository": "tsmodule/tsmodule",
|
6
6
|
"description": "TypeScript Module loader and compiler",
|
7
7
|
"license": "MIT",
|
@@ -41,13 +41,12 @@
|
|
41
41
|
"test": "ava --no-worker-threads"
|
42
42
|
},
|
43
43
|
"dependencies": {
|
44
|
+
"@types/glob": "^7.2.0",
|
44
45
|
"@types/node": "^17.0.15",
|
45
|
-
"@types/react": "^17.0.39",
|
46
|
-
"@types/react-dom": "^17.0.11",
|
47
46
|
"@typescript-eslint/eslint-plugin": "^5.9.1",
|
48
47
|
"@typescript-eslint/parser": "^5.9.1",
|
49
48
|
"ava": "^4.0.1",
|
50
|
-
"await-shell": "^
|
49
|
+
"await-shell": "^29.0.1-0",
|
51
50
|
"chalk": "^5.0.0",
|
52
51
|
"commander": "^8.3.0",
|
53
52
|
"create-debug-logger": "^1.10.1",
|
@@ -55,6 +54,7 @@
|
|
55
54
|
"eslint": "^8.8.0",
|
56
55
|
"fast-glob": "^3.2.10",
|
57
56
|
"fs-extra": "^10.0.0",
|
57
|
+
"glob": "^7.2.0",
|
58
58
|
"node-watch": "^0.7.3",
|
59
59
|
"ora": "^6.0.1",
|
60
60
|
"path": "^0.12.7",
|