Skip to content

Commit

Permalink
breaking: Make typescript an optional dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Feb 8, 2021
1 parent 89f8294 commit 22bdcf0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 22 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"@types/react": "^16.9.56",
"@types/rimraf": "^3.0.0",
"@types/semver": "^7.3.4",
"chokidar": "^3.5.1"
"chokidar": "^3.5.1",
"typescript": "^4.1.3"
},
"dependencies": {
"@babel/core": "^7.12.13",
Expand Down Expand Up @@ -104,10 +105,10 @@
"rollup-plugin-polyfill-node": "^0.5.0",
"rollup-plugin-visualizer": "^4.2.0",
"semver": "^7.3.4",
"spdx-license-list": "^6.4.0",
"typescript": "^4.1.3"
"spdx-license-list": "^6.4.0"
},
"optionalDependencies": {
"chokidar": "^3.5.1"
"chokidar": "^3.5.1",
"typescript": "^4.1.3"
}
}
7 changes: 6 additions & 1 deletion src/Package.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable require-atomic-updates, no-param-reassign, @typescript-eslint/member-ordering */

import fs from 'fs-extra';
import ts from 'typescript';
import { Memoize, optimal, Path, toArray } from '@boost/common';
import { createDebugger, Debugger } from '@boost/debug';
import Artifact from './Artifact';
import { FORMATS_BROWSER, FORMATS_NATIVE, FORMATS_NODE } from './constants';
import { loadModule } from './helpers/loadModule';
import Project from './Project';
import { packemonBlueprint } from './schemas';
import {
Expand Down Expand Up @@ -232,6 +232,11 @@ export default class Package {
return undefined;
}

const ts = loadModule(
'typescript',
'TypeScript is required for config loading.',
) as typeof import('typescript');

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { config, error } = ts.readConfigFile(tsconfigJsonPath.path(), (name) =>
fs.readFileSync(name, 'utf8'),
Expand Down
5 changes: 2 additions & 3 deletions src/TypesArtifact.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import path from 'path';
import glob from 'fast-glob';
import fs from 'fs-extra';
import { ParsedCommandLine } from 'typescript';
import { Path } from '@boost/common';
import { createDebugger, Debugger } from '@boost/debug';
import { Extractor, ExtractorConfig } from '@microsoft/api-extractor';
import Artifact from './Artifact';
import { APIExtractorStructure, DeclarationType, TypesBuild } from './types';
import { APIExtractorStructure, DeclarationType, TSConfigStructure, TypesBuild } from './types';

// eslint-disable-next-line
const extractorConfig = require(path.join(__dirname, '../api-extractor.json')) as {
Expand Down Expand Up @@ -167,7 +166,7 @@ export default class TypesArtifact extends Artifact<TypesBuild> {

// This method only exists so that we can mock in tests.
// istanbul ignore next
protected loadTsconfigJson(): ParsedCommandLine | undefined {
protected loadTsconfigJson(): TSConfigStructure | undefined {
return this.package.tsconfigJson;
}

Expand Down
17 changes: 5 additions & 12 deletions src/commands/Watch.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { applyStyle, Arg, Config } from '@boost/cli';
import { Bind, formatMs } from '@boost/common';
import { loadModule } from '../helpers/loadModule';
import Package from '../Package';
import { BaseCommand } from './Base';

Expand All @@ -26,7 +27,10 @@ export class WatchCommand extends BaseCommand<WatchOptions> {

async run() {
const { packemon } = this;
const chokidar = this.loadChokidar() as typeof import('chokidar');
const chokidar = loadModule(
'chokidar',
'Chokidar is required for file watching.',
) as typeof import('chokidar');

packemon.debug('Starting `watch` process');

Expand Down Expand Up @@ -70,17 +74,6 @@ export class WatchCommand extends BaseCommand<WatchOptions> {
}
}

loadChokidar(): unknown {
try {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
return require('chokidar');
} catch {
throw new Error(
'Chokidar is required for file watching. Please install with `yarn add --dev chokidar`.',
);
}
}

triggerRebuilds() {
if (this.rebuildTimer) {
clearTimeout(this.rebuildTimer);
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/loadModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function loadModule(name: string, message: string): unknown {
try {
// eslint-disable-next-line
return require(name);
} catch {
throw new Error(`${message} Please install with \`yarn add --dev ${name}\`.`);
}
}
10 changes: 8 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-underscore-dangle */

import ts from 'typescript';
import { PackageStructure } from '@boost/common';

// Platform = The runtime or operating system the code will run in.
Expand Down Expand Up @@ -145,7 +144,14 @@ declare module 'rollup' {

export type Awaitable = Promise<void> | void;

export type TSConfigStructure = ts.ParsedCommandLine;
export interface TSConfigStructure {
options: {
declarationDir?: string;
experimentalDecorators?: boolean;
outDir?: string;
strict?: boolean;
};
}

export interface APIExtractorStructure {
projectFolder: string;
Expand Down
13 changes: 13 additions & 0 deletions tests/helpers/loadModule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { loadModule } from '../../src/helpers/loadModule';

describe('loadModule()', () => {
it('doesnt error if module exists', () => {
expect(() => loadModule('typescript', '')).not.toThrow();
});

it('errors if module does not exist', () => {
expect(() => loadModule('unknown-module', 'Fake module!')).toThrow(
'Fake module! Please install with `yarn add --dev unknown-module`.',
);
});
});

0 comments on commit 22bdcf0

Please sign in to comment.