Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-import

Package Overview
Dependencies
Maintainers
1
Versions
88
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-import - npm Package Compare versions

Comparing version 2.0.6 to 2.0.7

9

dist/index.d.ts

@@ -6,2 +6,7 @@ export interface ICompilerOptions {

}
export interface ICompilationContext {
cwd: string;
tsDir: string;
tsPath: string;
}
/**

@@ -13,3 +18,2 @@ * Compiles TypeScript file to JavaScript, stores it cached and reads js from cache if available.

export declare class Compiler {
static defaultCacheDir: string;
static defaults: {

@@ -25,3 +29,4 @@ cacheDir: string;

*/
compile(tsPath?: string): Promise<any>;
compile(relativeTsPath?: string): Promise<any>;
compileOrFail(ctx: ICompilationContext): Promise<any>;
buildCache(absoluteTsPath: string): Promise<unknown>;

@@ -28,0 +33,0 @@ wasModified(tsFilePath: string, jsFilePath: string): Promise<boolean>;

@@ -73,25 +73,47 @@ "use strict";

*/
Compiler.prototype.compile = function (tsPath) {
if (tsPath === void 0) { tsPath = ""; }
Compiler.prototype.compile = function (relativeTsPath) {
if (relativeTsPath === void 0) { relativeTsPath = ""; }
return __awaiter(this, void 0, void 0, function () {
var logger, absoluteTsPath, absoluteTsDir, tsFileName, jsFileName, cacheDir, cachedFile, cwd, tsWasModified, compiled_1, compiled_2, compiled;
var cwd, tsPath, tsDir, compiled;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logger = this.options.logger;
absoluteTsPath = path.resolve(process.cwd(), tsPath);
cwd = process.cwd();
tsPath = path.resolve(cwd, relativeTsPath);
if (!fs.existsSync(tsPath)) {
throw new Error("File " + tsPath + " not found to compile.");
}
absoluteTsDir = path.dirname(absoluteTsPath);
tsFileName = path.basename(absoluteTsPath);
tsDir = path.dirname(tsPath);
// Switch directory to scripts.ts to resolve node_modules during require.
process.chdir(tsDir);
return [4 /*yield*/, this.compileOrFail({ cwd: cwd, tsDir: tsDir, tsPath: tsPath })["catch"](function (err) {
// Change directory back to cwd to prevent side-effects on error.
process.chdir(cwd);
throw err;
})];
case 1:
compiled = _a.sent();
// Change directory back to cwd and return compiled.
process.chdir(cwd);
return [2 /*return*/, compiled];
}
});
});
};
Compiler.prototype.compileOrFail = function (ctx) {
return __awaiter(this, void 0, void 0, function () {
var logger, tsDir, tsPath, tsFileName, jsFileName, cacheDir, cachedFile, tsWasModified, compiled_1, compiled_2, compiled;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logger = this.options.logger;
tsDir = ctx.tsDir, tsPath = ctx.tsPath;
tsFileName = path.basename(tsPath);
jsFileName = tsFileName.replace(/\.[^/.]+$/, ".js");
cacheDir = path.resolve(this.options.cacheDir, "." + absoluteTsDir);
cacheDir = path.resolve(this.options.cacheDir, "." + tsDir);
cachedFile = path.resolve(cacheDir, jsFileName);
cwd = process.cwd();
process.chdir(absoluteTsDir);
// Check if cached scripts.js exist.
logger === null || logger === void 0 ? void 0 : logger.debug("Looking for cached file at " + cachedFile);
if (!fs.existsSync(cachedFile)) return [3 /*break*/, 6];
return [4 /*yield*/, this.wasModified(absoluteTsPath, cachedFile)];
return [4 /*yield*/, this.wasModified(tsPath, cachedFile)];
case 1:

@@ -104,3 +126,2 @@ tsWasModified = _a.sent();

compiled_1 = _a.sent();
process.chdir(cwd);
return [2 /*return*/, compiled_1];

@@ -110,3 +131,3 @@ case 3:

logger === null || logger === void 0 ? void 0 : logger.debug("File was modified, building and importing.");
return [4 /*yield*/, this.buildCache(absoluteTsPath)];
return [4 /*yield*/, this.buildCache(tsPath)];
case 4:

@@ -117,3 +138,2 @@ _a.sent();

compiled_2 = _a.sent();
process.chdir(cwd);
return [2 /*return*/, compiled_2];

@@ -130,3 +150,3 @@ case 6:

logger === null || logger === void 0 ? void 0 : logger.debug("File was not cached, caching...");
return [4 /*yield*/, this.buildCache(absoluteTsPath)];
return [4 /*yield*/, this.buildCache(tsPath)];
case 7:

@@ -137,3 +157,2 @@ _a.sent();

compiled = _a.sent();
process.chdir(cwd);
return [2 /*return*/, compiled];

@@ -148,3 +167,3 @@ }

return new Promise(function (resolve, reject) {
var compileCommand = "pnpx tsc " + absoluteTsPath + " --rootDir / --outDir " + cacheDir + " " + flags.join(' ');
var compileCommand = "npx -p typescript tsc '" + absoluteTsPath + "' --rootDir / --outDir '" + cacheDir + "' " + flags.join(' ');
logger === null || logger === void 0 ? void 0 : logger.info("Compiling " + absoluteTsPath);

@@ -183,5 +202,4 @@ logger === null || logger === void 0 ? void 0 : logger.debug("Command: " + compileCommand);

};
Compiler.defaultCacheDir = path.resolve(__dirname, "../cache");
Compiler.defaults = {
cacheDir: Compiler.defaultCacheDir,
cacheDir: path.resolve(__dirname, "../cache"),
flags: [

@@ -188,0 +206,0 @@ "--downlevelIteration",

{
"name": "ts-import",
"version": "2.0.6",
"version": "2.0.7",
"description": "Import (compile and cache on the fly) TypeScript files dynamically with ease.",

@@ -21,5 +21,5 @@ "keywords": [

"devDependencies": {
"@radrat-scripts/package": "0.0.79-12",
"@radrat-scripts/readme": "0.0.79-12",
"@radrat/cli": "0.0.79-12",
"@radrat-scripts/package": "0.0.79-14",
"@radrat-scripts/readme": "0.0.79-14",
"@radrat/cli": "0.0.79-15",
"@types/node": "^14.6.0",

@@ -26,0 +26,0 @@ "typescript": "^4.0.2"

<p align="center">
<h1>ts-import</h1>
<div>Import (compile and cache on the fly) TypeScript files dynamically with ease.</div>
<div>Importing TypeScript files dynamically into JavaScript requires additional compilation step, which is troublesome to write for many. Popular **typescript-require** package seems to be obsolete and doesn't allow much customization. Typed alternative to https://github.com/theblacksmith/typescript-require written in TypeScript.</div>
</p>

@@ -8,10 +8,8 @@

1. [Pre Requirements](#pre-requirements)
1. [Getting Started](#getting-started)
2. [Getting Started](#getting-started)
2. [Usage](#usage)
3. [Usage](#usage)
3. [Features](#features)
4. [Features](#features)
## Getting Started

@@ -39,4 +37,4 @@

- **Asynchronous** - uses **import** over **require** therefore is asynchronous.
- **Highly flexible and configurable** - all **tsc** flags are available for customization. By default uses: `--module commonjs`, `--target es2015`, `--downlevelIteration`, `--emitDecoratorMetadata`, `--experimentalDecorators`, `--resolveJsonModule` which allow greatest amount of features.
- **Highly flexible and configurable** - all **tsc** flags are available for customization. By default uses: `--module commonjs`, `--target es2015`, `--downlevelIteration`, `--emitDecoratorMetadata`, `--experimentalDecorators`, `--resolveJsonModule` which allow great amount of features.
- **Compiler class** - allows making multiple instances of compiler with different configurations and overriding default settings to all of them (i.e. logger) via static "defaults" property: `Compiler.defaults = { ...customDefaults }`. **tsImport** object is a default instance of Compiler class suitable for majority of use-cases.
- **No interference** - doesn't interfere with native import, require etc. changing their behavior or impacting their performance.
- **Caches JavaScript** files into directory inside **node_modules/ts-import/cache** (pretty much like **typescript-require**). Removing module removes cache as well.
- **Fast** - I've benchmarked ways to compare detecting file changes with **fs** module and checking mtimeMs turned out to be fastest (https://jsperf.com/fs-stat-mtime-vs-mtimems).
- **Asynchronous** - uses **import** over **require** therefore is asynchronous.
- **Highly flexible and configurable** - all **tsc** flags are available for customization. By default uses: `--module commonjs`, `--target es2015`, `--downlevelIteration`, `--emitDecoratorMetadata`, `--experimentalDecorators`, `--resolveJsonModule` which allow greatest amount of features.
- **Highly flexible and configurable** - all **tsc** flags are available for customization. By default uses: `--module commonjs`, `--target es2015`, `--downlevelIteration`, `--emitDecoratorMetadata`, `--experimentalDecorators`, `--resolveJsonModule` which allow great amount of features.
- **Compiler class** - allows making multiple instances of compiler with different configurations and overriding default settings to all of them (i.e. logger) via static "defaults" property: `Compiler.defaults = { ...customDefaults }`. **tsImport** object is a default instance of Compiler class suitable for majority of use-cases.
- **No interference** - doesn't interfere with native import, require etc. changing their behavior or impacting their performance.

@@ -12,2 +12,8 @@ import * as childProcess from 'child_process';

export interface ICompilationContext {
cwd: string;
tsDir: string;
tsPath: string;
}
/**

@@ -19,5 +25,4 @@ * Compiles TypeScript file to JavaScript, stores it cached and reads js from cache if available.

export class Compiler {
static defaultCacheDir = path.resolve(__dirname, `../cache`);
static defaults = {
cacheDir: Compiler.defaultCacheDir,
cacheDir: path.resolve(__dirname, `../cache`),
flags: [

@@ -43,6 +48,7 @@ `--downlevelIteration`,

*/
async compile(tsPath: string = ``): Promise<any> {
const { logger } = this.options;
async compile(relativeTsPath: string = ``): Promise<any> {
// Check if file exists.
const cwd = process.cwd();
const tsPath = path.resolve(cwd, relativeTsPath);
const absoluteTsPath = path.resolve(process.cwd(), tsPath);
if (!fs.existsSync(tsPath)) {

@@ -52,14 +58,29 @@ throw new Error(`File ${tsPath} not found to compile.`);

const absoluteTsDir = path.dirname(absoluteTsPath);
// Get file name and directory path.
const tsDir = path.dirname(tsPath);
const tsFileName = path.basename(absoluteTsPath);
// Switch directory to scripts.ts to resolve node_modules during require.
process.chdir(tsDir);
const compiled = await this.compileOrFail({ cwd, tsDir, tsPath }).catch((err) => {
// Change directory back to cwd to prevent side-effects on error.
process.chdir(cwd);
throw err;
});
// Change directory back to cwd and return compiled.
process.chdir(cwd);
return compiled;
}
async compileOrFail(ctx: ICompilationContext) {
const { logger } = this.options;
const { tsDir, tsPath } = ctx;
const tsFileName = path.basename(tsPath);
const jsFileName = tsFileName.replace(/\.[^/.]+$/, `.js`);
const cacheDir = path.resolve(this.options.cacheDir, `.${absoluteTsDir}`);
const cacheDir = path.resolve(this.options.cacheDir, `.${tsDir}`);
const cachedFile = path.resolve(cacheDir, jsFileName);
// Switch directory to scripts.ts to resolve node_modules during require.
const cwd = process.cwd();
process.chdir(absoluteTsDir);
// Check if cached scripts.js exist.

@@ -69,7 +90,6 @@ logger?.debug(`Looking for cached file at ${cachedFile}`);

// Cache is correct, do nothing.
const tsWasModified = await this.wasModified(absoluteTsPath, cachedFile);
const tsWasModified = await this.wasModified(tsPath, cachedFile);
if (!tsWasModified) {
logger?.debug(`File was not modified, importing.`);
const compiled = await import(cachedFile);
process.chdir(cwd);
return compiled;

@@ -80,5 +100,4 @@ }

logger?.debug(`File was modified, building and importing.`);
await this.buildCache(absoluteTsPath);
await this.buildCache(tsPath);
const compiled = await import(cachedFile);
process.chdir(cwd);
return compiled;

@@ -97,5 +116,4 @@ }

logger?.debug(`File was not cached, caching...`);
await this.buildCache(absoluteTsPath);
await this.buildCache(tsPath);
const compiled = await import(cachedFile);
process.chdir(cwd);
return compiled;

@@ -109,3 +127,3 @@ }

return new Promise((resolve, reject) => {
const compileCommand = `pnpx tsc ${absoluteTsPath} --rootDir / --outDir ${cacheDir} ${flags.join(' ')}`;
const compileCommand = `npx -p typescript tsc '${absoluteTsPath}' --rootDir / --outDir '${cacheDir}' ${flags.join(' ')}`;
logger?.info(`Compiling ${absoluteTsPath}`);

@@ -112,0 +130,0 @@ logger?.debug(`Command: ${compileCommand}`);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc