@tsmodule/tsmodule 19.0.0 → 20.0.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/dist/commands/build/index.d.ts +12 -0
- package/dist/commands/build/index.js +3 -3
- package/dist/commands/build/lib/emitTsDeclarations.d.ts +1 -0
- package/dist/commands/create/index.d.ts +3 -0
- package/dist/commands/create/lib/createTemplate.d.ts +1 -0
- package/dist/commands/create/lib/rewritePkgJson.d.ts +1 -0
- package/dist/commands/dev/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 +4 -4
- package/dist/loader/index.d.ts +11 -0
- package/dist/loader/types.d.ts +22 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/utils/index.d.ts +28 -0
- package/dist/utils/pkgJson.d.ts +2 -0
- package/package.json +4 -4
- package/templates/default/package.json +1 -1
@@ -0,0 +1,12 @@
|
|
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: ({ files, styles, dev, fast }: {
|
8
|
+
files?: string;
|
9
|
+
styles?: string;
|
10
|
+
dev?: boolean;
|
11
|
+
fast?: boolean;
|
12
|
+
}) => Promise<void>;
|
@@ -1,3 +1,3 @@
|
|
1
|
-
import{dirname as
|
2
|
-
`}}),
|
3
|
-
This might take a moment.`),
|
1
|
+
import{dirname as C,extname as D,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,fast:y=!1})=>{E.NODE_ENV=i?"development":"production";const N=L(),t=G(V);t.log("Building",{files:s,dev:i,fast:y}),F(`${a.bold("TS Module")} [${E.NODE_ENV}]`);const O=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:O}},g=u(n,"src"),l=u(n,"dist"),o=v.sync(s,{cwd:n}).filter(e=>D(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(C(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,B=["npx tailwindcss",i?"":"-m","--postcss postcss.config.js",`-i ${r}`,"-o",c];await N.run(B.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};
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const emitTsDeclarations: (files: string[]) => void;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const createTemplate: (template: string, projectName: string) => Promise<void>;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const rewritePkgJson: (projectName: string) => Promise<void>;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const dev: () => Promise<void>;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const execute: () => Promise<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,6 +1,6 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
var r="@tsmodule/tsmodule",
|
2
|
+
var r="@tsmodule/tsmodule",s="module",m="20.0.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.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"},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.
|
3
3
|
|
4
|
-
Run TS directly: ${
|
5
|
-
Use a command: ${
|
6
|
-
(default: dist/**/*.js)`).action(async({files:n})=>{await
|
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.
|
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);
|
@@ -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;
|
@@ -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,28 @@
|
|
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 isJsOrTs: RegExp;
|
6
|
+
export declare const BASE_CONFIG: {
|
7
|
+
format: string;
|
8
|
+
charset: string;
|
9
|
+
sourcemap: string;
|
10
|
+
target: string;
|
11
|
+
minify: boolean;
|
12
|
+
};
|
13
|
+
export declare type ModuleLoaders = {
|
14
|
+
[extension: string]: {
|
15
|
+
[configKey: string]: unknown;
|
16
|
+
};
|
17
|
+
};
|
18
|
+
export declare const MODULE_LOADERS: ModuleLoaders;
|
19
|
+
export declare const POSSIBLE_EXTENSIONS: string[];
|
20
|
+
/**
|
21
|
+
* Force a Unix-like path.
|
22
|
+
*/
|
23
|
+
export declare const normalizeSpecifier: (specifier: string) => string;
|
24
|
+
export declare const fileExists: (fileUrl: string) => string | void;
|
25
|
+
export declare const fileExistsAny: (fileUrls: string[]) => string | void;
|
26
|
+
export declare const checkTsExtensions: (specifier: string) => string | void;
|
27
|
+
export declare const checkJsExtension: (specifier: string) => string | void;
|
28
|
+
export declare const checkExtensions: (specifier: string) => string;
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@tsmodule/tsmodule",
|
3
3
|
"type": "module",
|
4
|
-
"version": "
|
4
|
+
"version": "20.0.0",
|
5
5
|
"repository": "tsmodule/tsmodule",
|
6
6
|
"description": "TypeScript Module loader and compiler",
|
7
7
|
"license": "MIT",
|
@@ -30,14 +30,14 @@
|
|
30
30
|
},
|
31
31
|
"scripts": {
|
32
32
|
"bootstrap": "node bootstrap.js",
|
33
|
-
"bootload": "node --no-warnings --loader ./dist/loader/index.js src/index.ts build -
|
33
|
+
"bootload": "node --no-warnings --loader ./dist/loader/index.js src/index.ts build -r",
|
34
34
|
"prebuild": "yarn bootstrap && yarn bootload && yarn link -f",
|
35
35
|
"build": "tsmodule build",
|
36
36
|
"dev": "tsmodule dev",
|
37
37
|
"lint": "eslint --fix src",
|
38
|
-
"prepare": "yarn build -
|
38
|
+
"prepare": "yarn build -r",
|
39
39
|
"prepublishOnly": "yarn build && yarn test",
|
40
|
-
"pretest": "yarn build -
|
40
|
+
"pretest": "yarn build -r",
|
41
41
|
"test": "ava --no-worker-threads"
|
42
42
|
},
|
43
43
|
"dependencies": {
|