@tsmodule/tsmodule 9.5.0 → 10.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/commands/build/index.d.ts +2 -1
- package/dist/commands/build/index.js +3 -3
- package/dist/commands/dev/index.d.ts +1 -0
- package/dist/commands/dev/index.js +3 -0
- package/dist/index.js +2 -2
- package/dist/loader/index.js +1 -1
- package/package.json +8 -4
- package/template/.github/workflows/ci.yml +3 -3
- package/template/package.json +1 -0
package/README.md
CHANGED
@@ -18,6 +18,10 @@ Supports React via TSX/JSX. Ready with zero config:
|
|
18
18
|
- No more polyfilling to CJS or older featuresets
|
19
19
|
- Use latest syntax in source, leave backporting to downstream consumers
|
20
20
|
|
21
|
+
### Dev mode with **`tsmodule dev`**
|
22
|
+
|
23
|
+
- Build and watch for changes
|
24
|
+
|
21
25
|
### Run TypeScript directly with **`tsmodule <file>`**
|
22
26
|
|
23
27
|
- Uses Node module loader to resolve TS at runtime
|
@@ -4,7 +4,8 @@ export declare const bannerLog: (msg: string) => void;
|
|
4
4
|
* could mean many things, all of which is handled by the loader which will
|
5
5
|
* resolve them for us.
|
6
6
|
*/
|
7
|
-
export declare const build: ({ dev, fast }: {
|
7
|
+
export declare const build: ({ files, dev, fast }: {
|
8
|
+
files?: string;
|
8
9
|
dev?: boolean;
|
9
10
|
fast?: boolean;
|
10
11
|
}) => Promise<void>;
|
@@ -1,3 +1,3 @@
|
|
1
|
-
import{build as
|
2
|
-
`}}),
|
3
|
-
This might take a moment.`),
|
1
|
+
import{build as w}from"esbuild";import{existsSync as k,readFileSync as x,writeFileSync as F}from"fs";import{extname as O,isAbsolute as B,resolve as p}from"path";import c from"chalk";import{env as h}from"process";import j from"fs/promises";import D from"fast-glob";import s from"ora";import{createDebugLogger as J,log as u}from"create-debug-logger";import{isTs as d,isTsxOrJsx as m}from"../../utils/index.js";import{emitTsDeclarations as P}from"./lib/emitTsDeclarations.js";import{getPackageJsonFile as C}from"../../utils/pkgJson.js";import{normalizeImportSpecifiers as G}from"../normalize/index.js";const E=t=>{u(c.bgBlue(c.white(` ${t} `)))},W=async({files:t="src/**/*",dev:r=!1,fast:f=!1})=>{h.NODE_ENV=r?"development":"production";const o=J(W);o.log("Building",{files:t,dev:r,fast:f}),E(`${c.bold("TS Module")} [${h.NODE_ENV}]`);const N=await C(),i=process.cwd(),g={absWorkingDir:i,outbase:"src",outdir:"dist",assetNames:"[name].js",logLevel:r?"warning":"error",charset:"utf8",format:"esm",target:"esnext",minify:!r,define:{PACKAGE_JSON:N}},b=p(i,"src"),n=p(i,"dist");if(o.log("Cleaning old output:",{outDir:n}),r&&B(t)){const e=t.replace(b,n).replace(d,".js").replace(m,".js");await j.rm(e,{force:!0})}else await j.rm(n,{force:!0,recursive:!0});console.log();const l=D.sync(t,{cwd:i}).filter(e=>O(e)!==".d.ts").map(e=>p(e)),y=l.filter(e=>d.test(e)).filter(e=>!m.test(e));o.log("Compiling TS files:",{tsFiles:y}),await w({...g,entryPoints:y.filter(e=>!e.endsWith(".d.ts"))}),s("Built TS files.").succeed();const S=l.filter(e=>m.test(e));if(o.log("Compiling TSX files:",{tsxFiles:S}),await w({...g,entryPoints:S.filter(e=>!e.endsWith(".d.ts")),jsxFactory:"createElement",banner:{js:`import { createElement } from "react";
|
2
|
+
`}}),s("Built TSX files.").succeed(),process.env.NO_REWRITES)return;const T=t.replace(b,n).replace(/^(\.\/)?src\//,"dist/").replace(d,".js").replace(m,".js");if(await G(T),s("Normalized import specifiers.").succeed(),console.log(),r||f)return;E("Running post-build setup."),u(`Generating type declarations.
|
3
|
+
This might take a moment.`),P(l),s(`Generated delcarations for ${l.length} files.`).succeed();let a;k("dist/package.json")?a=JSON.parse(x("dist/package.json","utf-8")):a={},a.type="module",F("dist/package.json",JSON.stringify(a,null,2)),s('Forced "type": "module" in output.').succeed(),u(c.green("Build complete."))};export{E as bannerLog,W as build};
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const dev: () => Promise<void>;
|
@@ -0,0 +1,3 @@
|
|
1
|
+
import{relative as a,resolve as m}from"path";import{build as o}from"../build/index.js";import e from"chalk";import{log as l}from"create-debug-logger";import p from"ora";import{watch as d}from"chokidar";const s=()=>{console.clear()},c=r=>{l(`
|
2
|
+
`,e.gray(`Built ${e.bold(r)}.`),`
|
3
|
+
`,e.blue(new Date().toLocaleString()))},D=async()=>{const r=process.cwd();s(),await o({dev:!0}),c("src/**/*"),d(m(r,"src"),{persistent:!0}).on("change",async t=>{s();const i=Date.now();await o({dev:!0,files:t});const n=Date.now()-i;p(e.blueBright(`Dev refresh finished in ${e.bold(`${n}ms`)}.`)).succeed(),c(a(r,t))})};export{D as dev};
|
package/dist/index.js
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
var r="@tsmodule/tsmodule",
|
3
|
-
(default: dist/**/*.js)`).action(async({files:n})=>{await
|
2
|
+
var r="@tsmodule/tsmodule",a="module",m="10.3.0",d="tsmodule/tsmodule",s="TypeScript Module loader and compiler",l="MIT",p={tsmodule:"dist/index.js"},c={".":"./dist/loader/index.js","./*":"./dist/*/index.js","./package.json":"./package.json"},f="dist/types/index.d.ts",u=[{name:"C. Lewis",email:"ctj.lewis@icloud.com",url:"https://ctjlewis.com"}],v=["dist/","template/"],g={node:">=14"},b={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",test:"ava --no-worker-threads"},h={"@types/cross-spawn":"^6.0.2","@types/node":"17.0.8","@types/react":"17.0.38","@typescript-eslint/eslint-plugin":"^5.9.1","@typescript-eslint/parser":"^5.9.1",ava:"^4.0.1","await-shell":"^7.4.0",chalk:"^5.0.0",chokidar:"^3.5.3",commander:"^8.3.0","create-debug-logger":"^1.10.1","cross-spawn":"^7.0.3",esbuild:"^0.14.0",eslint:"^8.6.0","fast-glob":"^3.2.10",ora:"^6.0.1",path:"^0.12.7",react:"^17.0.2",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"]},t={name:r,type:a,version:m,repository:d,description:s,license:l,bin:p,exports:c,types:f,contributors:u,files:v,engines:g,scripts:b,dependencies:h,keywords:S,ava:w};import i from"chalk";import{Command as B}from"commander";import{build as T}from"./commands/build/index.js";import{create as y}from"./commands/create/index.js";import{dev as A}from"./commands/dev/index.js";import{execute as C}from"./commands/execute/index.js";import{normalizeImportSpecifiers as E}from"./commands/normalize/index.js";const{version:j}=t,e=new B;e.name(i.bold(i.blueBright("tsmodule"))).description(i.blueBright("A tool for building TypeScript modules.")).version(String(j)),e.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(C),e.command("dev").description("Build and watch for changes.").action(A),e.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(T),e.command("create <name>").description("Create a new project.").action(y),e.command("normalize [files]").description(`Rewrites import specifiers in files to ESM-compliant paths.
|
3
|
+
(default: dist/**/*.js)`).action(async({files:n})=>{await E(n)}),e.parse(process.argv);
|
package/dist/loader/index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
import{extname as m,isAbsolute as U,join as E,normalize as w,resolve as S}from"path";import{fileURLToPath as H,pathToFileURL as v,URL as L}from"url";import{promises 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 N,isTs as P,MODULE_LOADERS as R}from"../utils/index.js";import{getPackageJsonFile as y}from"../utils/pkgJson.js";
|
1
|
+
import{extname as m,isAbsolute as U,join as E,normalize as w,resolve as S}from"path";import{fileURLToPath as H,pathToFileURL as v,URL as L}from"url";import{createDebugLogger as c}from"create-debug-logger";import{promises 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 N,isTs as P,MODULE_LOADERS as R}from"../utils/index.js";import{getPackageJsonFile as y}from"../utils/pkgJson.js";const F=await y(),A=async(o,r,t)=>{const{parentURL:s}=r,e=c(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(S(w(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(),d=m(n).toLowerCase();if(e.log("Rewriting file extension:",{parentExtension:f,specifierExtension:d}),d){const a=n.substring(0,n.lastIndexOf(d));if(e.log("Re-resolving specifier:",{unresolvedSpecifier:a}),N.test(d)&&P.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 g=k(n);if(g)return e.log("Resolved import URL to file:",{resolvedFile:g}),{url:g};const x=new L(E(n,"index")).href,u=k(x);return u?(e.log("Resolved import URL to index file:",{resolvedIndexFile:u}),{url:u}):t(o,r,t)},J=async(o,r,t)=>{const s=c(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=await G.readFile(i),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=c(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=c(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/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@tsmodule/tsmodule",
|
3
3
|
"type": "module",
|
4
|
-
"version": "
|
4
|
+
"version": "10.3.0",
|
5
5
|
"repository": "tsmodule/tsmodule",
|
6
6
|
"description": "TypeScript Module loader and compiler",
|
7
7
|
"license": "MIT",
|
@@ -30,8 +30,10 @@
|
|
30
30
|
},
|
31
31
|
"scripts": {
|
32
32
|
"bootstrap": "node bootstrap.js",
|
33
|
-
"
|
34
|
-
"
|
33
|
+
"bootload": "node --no-warnings --loader ./dist/loader/index.js src/index.ts build -f",
|
34
|
+
"prebuild": "yarn bootstrap && yarn bootload && yarn link -f",
|
35
|
+
"build": "tsmodule build",
|
36
|
+
"dev": "tsmodule dev",
|
35
37
|
"lint": "eslint --fix src",
|
36
38
|
"prepare": "yarn build -f",
|
37
39
|
"prepublishOnly": "yarn build && yarn test",
|
@@ -44,8 +46,9 @@
|
|
44
46
|
"@typescript-eslint/eslint-plugin": "^5.9.1",
|
45
47
|
"@typescript-eslint/parser": "^5.9.1",
|
46
48
|
"ava": "^4.0.1",
|
47
|
-
"await-shell": "^4.
|
49
|
+
"await-shell": "^7.4.0",
|
48
50
|
"chalk": "^5.0.0",
|
51
|
+
"chokidar": "^3.5.3",
|
49
52
|
"commander": "^8.3.0",
|
50
53
|
"create-debug-logger": "^1.10.1",
|
51
54
|
"cross-spawn": "^7.0.3",
|
@@ -66,6 +69,7 @@
|
|
66
69
|
"experimental-loader"
|
67
70
|
],
|
68
71
|
"ava": {
|
72
|
+
"timeout": "10 minutes",
|
69
73
|
"files": [
|
70
74
|
"test/**/*.test.ts"
|
71
75
|
],
|
@@ -10,12 +10,12 @@ on:
|
|
10
10
|
|
11
11
|
jobs:
|
12
12
|
test:
|
13
|
-
name: Node
|
13
|
+
name: Node ${{ matrix.nodejs }} on ${{ matrix.os }}
|
14
14
|
runs-on: ${{ matrix.os }}
|
15
|
-
timeout-minutes:
|
15
|
+
timeout-minutes: 10
|
16
16
|
strategy:
|
17
17
|
matrix:
|
18
|
-
nodejs: [16]
|
18
|
+
nodejs: [14, 16, "lts/*"]
|
19
19
|
os: [ubuntu-latest, windows-latest, macos-latest]
|
20
20
|
steps:
|
21
21
|
- uses: actions/checkout@v2
|