@tsmodule/tsmodule 9.0.0 → 9.0.5
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 +2 -2
- package/dist/commands/build/index.d.ts +10 -0
- package/dist/commands/build/index.js +1 -1
- package/dist/commands/build/lib/emitTsDeclarations.d.ts +1 -0
- package/dist/commands/create/index.d.ts +1 -0
- package/dist/commands/execute/index.d.ts +1 -0
- package/dist/commands/normalize/index.d.ts +19 -0
- package/dist/commands/normalize/lib/typescriptApi.d.ts +13 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/loader/index.d.ts +11 -0
- package/dist/loader/index.js +1 -1
- package/dist/loader/types.d.ts +22 -0
- package/dist/package.json +3 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/utils/index.d.ts +27 -0
- package/dist/utils/pkgJson.d.ts +1 -0
- package/package.json +3 -2
package/README.md
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
export declare const bannerLog: (msg: string) => void;
|
2
|
+
/**
|
3
|
+
* Build TS to JS. This will contain incomplete specifiers like `./foo` which
|
4
|
+
* could mean many things, all of which is handled by the loader which will
|
5
|
+
* resolve them for us.
|
6
|
+
*/
|
7
|
+
export declare const build: ({ dev, fast }: {
|
8
|
+
dev?: boolean;
|
9
|
+
fast?: boolean;
|
10
|
+
}) => Promise<void>;
|
@@ -1,3 +1,3 @@
|
|
1
|
-
import{build as d}from"esbuild";import{existsSync as h,readFileSync as k,writeFileSync as w}from"fs";import{extname as x,resolve as u}from"path";import l from"chalk";import{env as F}from"process";import T from"fast-glob";import i from"ora";import{rm as B}from"fs/promises";import{createDebugLogger as E,log as a}from"create-debug-logger";import{isTs as N,isTsxOrJsx as g}from"../../utils/index.js";import{emitTsDeclarations as O}from"./lib/emitTsDeclarations.js";import{getPackageJsonFile as J}from"../../utils/pkgJson.js";import{normalizeImportSpecifiers as j}from"../normalize/index.js";const y=t=>{a(l.bgBlue(l.white(` ${t} `)))},D=async({dev:t=!1,fast:b=!1})=>{t&&(F.NODE_ENV="development");const r=E(D);t||y("Building for production.");const n=process.cwd(),S=await J(),c={absWorkingDir:n,outbase:"src",outdir:"dist",assetNames:"[name].js",logLevel:t?"debug":"error",charset:"utf8",format:"esm",target:"esnext",minify:!t,define:{PACKAGE_JSON:S}},m=u(n,"dist");r.log("Cleaning old output:",{distDir:m}),await B(m,{force:!0,recursive:!0}),console.log();const s=T.sync("src/**/*",{cwd:n}).filter(e=>x(e)!==".d.ts").map(e=>u(e)),f=s.filter(e=>N.test(e)).filter(e=>!g.test(e));r.log("Compiling TS files:",{tsFiles:f}),await d({...c,entryPoints:f.filter(e=>!e.endsWith(".d.ts"))}),i("Built TS files.").succeed();const p=s.filter(e=>g.test(e));if(r.log("Compiling TSX files:",{tsxFiles:p}),await d({...c,entryPoints:p.filter(e=>!e.endsWith(".d.ts")),jsxFactory:"createElement",banner:{js:`import { createElement } from
|
1
|
+
import{build as d}from"esbuild";import{existsSync as h,readFileSync as k,writeFileSync as w}from"fs";import{extname as x,resolve as u}from"path";import l from"chalk";import{env as F}from"process";import T from"fast-glob";import i from"ora";import{rm as B}from"fs/promises";import{createDebugLogger as E,log as a}from"create-debug-logger";import{isTs as N,isTsxOrJsx as g}from"../../utils/index.js";import{emitTsDeclarations as O}from"./lib/emitTsDeclarations.js";import{getPackageJsonFile as J}from"../../utils/pkgJson.js";import{normalizeImportSpecifiers as j}from"../normalize/index.js";const y=t=>{a(l.bgBlue(l.white(` ${t} `)))},D=async({dev:t=!1,fast:b=!1})=>{t&&(F.NODE_ENV="development");const r=E(D);t||y("Building for production.");const n=process.cwd(),S=await J(),c={absWorkingDir:n,outbase:"src",outdir:"dist",assetNames:"[name].js",logLevel:t?"debug":"error",charset:"utf8",format:"esm",target:"esnext",minify:!t,define:{PACKAGE_JSON:S}},m=u(n,"dist");r.log("Cleaning old output:",{distDir:m}),await B(m,{force:!0,recursive:!0}),console.log();const s=T.sync("src/**/*",{cwd:n}).filter(e=>x(e)!==".d.ts").map(e=>u(e)),f=s.filter(e=>N.test(e)).filter(e=>!g.test(e));r.log("Compiling TS files:",{tsFiles:f}),await d({...c,entryPoints:f.filter(e=>!e.endsWith(".d.ts"))}),i("Built TS files.").succeed();const p=s.filter(e=>g.test(e));if(r.log("Compiling TSX files:",{tsxFiles:p}),await d({...c,entryPoints:p.filter(e=>!e.endsWith(".d.ts")),jsxFactory:"createElement",banner:{js:`import { createElement } from "react";
|
2
2
|
`}}),i("Built TSX files.").succeed(),process.env.NO_REWRITES||(await j(),i("Normalized import specifiers.").succeed(),console.log(),t||b))return;y("Running post-build setup."),a(`Generating type declarations.
|
3
3
|
This might take a moment.`),O(s),i(`Generated delcarations for ${s.length} files.`).succeed();let o;h("dist/package.json")?o=JSON.parse(k("dist/package.json","utf-8")):o={},o.type="module",w("dist/package.json",JSON.stringify(o,null,2)),i('Forced "type": "module" in output.').succeed(),a(l.green("Build complete."))};export{y as bannerLog,D as build};
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const emitTsDeclarations: (files: string[]) => void;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const create: (name: string) => Promise<void>;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const execute: () => void;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview
|
3
|
+
* This module contains the logic for normalizing import specifiers. It must use
|
4
|
+
* fully-specified filepaths here, since the bootstrap script will compile it
|
5
|
+
* with esbuild and then use it to normalize emitted output.
|
6
|
+
*/
|
7
|
+
/**
|
8
|
+
* Matches a complete import statement, including the import keyword, as well as
|
9
|
+
* dynamic imports, requires, and export statements.
|
10
|
+
*/
|
11
|
+
export declare const generateImportPattern: (importSource: string) => RegExp;
|
12
|
+
/**
|
13
|
+
* Rewrite an import/export/require statement.
|
14
|
+
*/
|
15
|
+
export declare const rewriteImportStatement: (importStatement: string, specifierToReplace: string, specifierReplacement: string) => string;
|
16
|
+
/**
|
17
|
+
* Rewrite imports in the emitted JS to ESM-compliant paths.
|
18
|
+
*/
|
19
|
+
export declare const normalizeImportSpecifiers: (files?: string) => Promise<any>;
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import ts from "typescript";
|
2
|
+
export declare const TS_CONFIG: ts.CompilerOptions;
|
3
|
+
export declare const compilerHost: ts.CompilerHost;
|
4
|
+
interface SpecifierReplacement {
|
5
|
+
specifierToReplace: string;
|
6
|
+
specifierReplacement: string;
|
7
|
+
}
|
8
|
+
/**
|
9
|
+
* Get the rewritten specifiers for a given module. Import/export specifiers
|
10
|
+
* will be resolved ahead-of-time by the TypeScript compiler and returned.
|
11
|
+
*/
|
12
|
+
export declare const getRewrittenSpecifiers: (modulePath: string) => SpecifierReplacement[];
|
13
|
+
export {};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
var r="@tsmodule/tsmodule",m="module",a="9.0.
|
2
|
+
var r="@tsmodule/tsmodule",m="module",a="9.0.5",s="tsmodule/tsmodule",d="TypeScript Module loader and compiler",p="MIT",l={tsmodule:"dist/index.js"},c={".":"./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"}],g=["dist/","template/"],v={node:">=14"},b={bootstrap:"node bootstrap.js",prebuild:"yarn bootstrap",build:"node --no-warnings --loader ./dist/loader/index.js src/index.ts build",lint:"eslint --fix src",prepare:"yarn build -f",prepublishOnly:"yarn build && yarn test",test:"ava --no-worker-threads"},S={"await-shell":"^2.7.0",chalk:"^5.0.0",commander:"^8.3.0","create-debug-logger":"^1.10.1",esbuild:"^0.14.0","fast-glob":"^3.2.10",ora:"^6.0.1",path:"^0.12.7",react:"^17.0.2",rollup:"^2.63.0","rollup-plugin-preserve-shebang":"^1.0.1",typescript:"^4.5.5"},h={"@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",eslint:"^8.6.0"},w=["esm","loader","typescript","loader hook","require hook","experimental-loader"],y={files:["test/**/*.test.ts"],extensions:{ts:"module"},nodeArguments:["--no-warnings","--loader=@tsmodule/tsmodule"]},t={name:r,type:m,version:a,repository:s,description:d,license:p,bin:l,exports:c,types:u,contributors:f,files:g,engines:v,scripts:b,dependencies:S,devDependencies:h,keywords:w,ava:y};import i from"chalk";import{Command as B}from"commander";import{build as T}from"./commands/build/index.js";import{create as A}from"./commands/create/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("build").option("-d, --dev","Build development version (default: production)").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(A),e.command("normalize [files]").description(`Rewrites import specifiers in files to ESM-compliant paths.
|
3
3
|
(default: dist/**/*.js)`).action(async({files:n})=>{await E(n)}),e.parse(process.argv);
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import type { GetFormatHook as ModuleGetFormatHook, LoadHook as ModuleLoadHook, ResolveHook as ModuleResolveHook, TransformHook as ModuleTransformSourceHook } from "./types";
|
2
|
+
export declare const resolve: ModuleResolveHook;
|
3
|
+
export declare const load: ModuleLoadHook;
|
4
|
+
/**
|
5
|
+
* @deprecated As of Node 17.
|
6
|
+
*/
|
7
|
+
export declare const getFormat: ModuleGetFormatHook;
|
8
|
+
/**
|
9
|
+
* @deprecated As of Node 17.
|
10
|
+
*/
|
11
|
+
export declare const transformSource: ModuleTransformSourceHook;
|
package/dist/loader/index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
import{extname as m,isAbsolute as U,join as
|
1
|
+
import{extname as m,isAbsolute as U,join as h,normalize as x,resolve as w}from"path";import{fileURLToPath as H,pathToFileURL as v,URL as L}from"url";import{promises as G}from"fs";import{transform as k}from"esbuild";import{sep as M}from"path/posix";import{sep as T}from"path/win32";import{checkExtensions as F,checkTsExtensions as D,fileExists as b,isJs as N,isTs as y,MODULE_LOADERS as R}from"../utils/index.js";import{getPackageJsonFile as A}from"../utils/pkgJson.js";import{createDebugLogger as c}from"create-debug-logger";const E=await A(),J=async(o,r,t)=>{const{parentURL:s}=r,e=c(J);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(x(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)&&y.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=F(n);if(g)return e.log("Resolved import URL to file:",{resolvedFile:g}),{url:g};const S=new L(h(n,"index")).href,u=F(S);return u?(e.log("Resolved import URL to index file:",{resolvedIndexFile:u}),{url:u}):t(o,r,t)},O=async(o,r,t)=>{const s=c(O);if(s.log("Loading source file:",{url:o}),!o.includes(T)&&!o.includes(M))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 k(n.toString(),{...l,sourcefile:i,format:"esm",define:{PACKAGE_JSON:E}});return{format:"module",source:f.code}},P=async(o,r,t)=>{const s=c(P);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 k(o.toString(),{...i,logLevel:"info",charset:"utf8",target:"esnext",sourcefile:r.url,format:r.format==="module"?"esm":"cjs",define:{PACKAGE_JSON:E}})).code}:(s.log("No loader found, using default transformer:",{url:e}),t(o,r,t))};export{P as getFormat,O as load,J as resolve,B as transformSource};
|
@@ -0,0 +1,22 @@
|
|
1
|
+
export declare type Promisable<T> = Promise<T> | T;
|
2
|
+
export declare type ModuleSource = string | SharedArrayBuffer | Uint8Array;
|
3
|
+
export declare type ModuleFormat = "builtin" | "commonjs" | "json" | "module" | "wasm";
|
4
|
+
export declare type ResolveHook = (specifier: string, context: {
|
5
|
+
conditions: string[];
|
6
|
+
parentURL?: string;
|
7
|
+
}, fallback: ResolveHook) => Promisable<{
|
8
|
+
url: string;
|
9
|
+
format?: ModuleFormat;
|
10
|
+
}>;
|
11
|
+
export declare type GetFormatHook = (url: string, context: object, fallback: GetFormatHook) => Promisable<{
|
12
|
+
format: ModuleFormat;
|
13
|
+
}>;
|
14
|
+
export declare type TransformHook = (source: ModuleSource, context: Record<"url" | "format", string>, fallback: TransformHook) => Promisable<{
|
15
|
+
source: ModuleSource;
|
16
|
+
}>;
|
17
|
+
export declare type LoadHook = (url: string, context: {
|
18
|
+
format?: ModuleFormat;
|
19
|
+
}, fallback: LoadHook) => Promisable<{
|
20
|
+
format: ModuleFormat;
|
21
|
+
source: ModuleSource;
|
22
|
+
}>;
|
@@ -0,0 +1,27 @@
|
|
1
|
+
export declare const DEVELOPMENT_MODE: boolean;
|
2
|
+
export declare const isTs: RegExp;
|
3
|
+
export declare const isJs: RegExp;
|
4
|
+
export declare const isTsxOrJsx: RegExp;
|
5
|
+
export declare const BASE_CONFIG: {
|
6
|
+
format: string;
|
7
|
+
charset: string;
|
8
|
+
sourcemap: string;
|
9
|
+
target: string;
|
10
|
+
minify: boolean;
|
11
|
+
};
|
12
|
+
export declare type ModuleLoaders = {
|
13
|
+
[extension: string]: {
|
14
|
+
[configKey: string]: unknown;
|
15
|
+
};
|
16
|
+
};
|
17
|
+
export declare const MODULE_LOADERS: ModuleLoaders;
|
18
|
+
export declare const POSSIBLE_EXTENSIONS: string[];
|
19
|
+
/**
|
20
|
+
* Force a Unix-like path.
|
21
|
+
*/
|
22
|
+
export declare const normalizeSpecifier: (path: string) => string;
|
23
|
+
export declare const fileExists: (fileUrl: string) => string | void;
|
24
|
+
export declare const fileExistsAny: (fileUrls: string[]) => string | void;
|
25
|
+
export declare const checkTsExtensions: (specifier: string) => string | void;
|
26
|
+
export declare const checkJsExtension: (specifier: string) => string | void;
|
27
|
+
export declare const checkExtensions: (specifier: string) => string;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const getPackageJsonFile: () => Promise<string>;
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@tsmodule/tsmodule",
|
3
3
|
"type": "module",
|
4
|
-
"version": "9.0.
|
4
|
+
"version": "9.0.5",
|
5
5
|
"repository": "tsmodule/tsmodule",
|
6
6
|
"description": "TypeScript Module loader and compiler",
|
7
7
|
"license": "MIT",
|
@@ -33,7 +33,8 @@
|
|
33
33
|
"prebuild": "yarn bootstrap",
|
34
34
|
"build": "node --no-warnings --loader ./dist/loader/index.js src/index.ts build",
|
35
35
|
"lint": "eslint --fix src",
|
36
|
-
"prepare": "yarn build
|
36
|
+
"prepare": "yarn build -f",
|
37
|
+
"prepublishOnly": "yarn build && yarn test",
|
37
38
|
"test": "ava --no-worker-threads"
|
38
39
|
},
|
39
40
|
"dependencies": {
|