rollup-plugin-typescript2/src/get-options-overrides.ts
Anton Gilgur b08f272efb
refactor: sort all top-level imports (#337)
- basically, general format is:
  ```ts
  import x from "external-dep"

  import y from "./internal-dep"
  ```
  - so external deps, new line, then internal/local deps
  - with some further sorting within there, like trying to keep Node
    built-ins (e.g. `path`) at the top half of externals, then core deps
    like `typescript`, then any other external deps
    - and similar for internal deps -- core internals at the top half of
      internals, then any other internal deps
    - just to keep things consistent between files -- makes the top
      easier to read through when it's similar between files
    - also makes it easier for contributors to understand where to put
      imports, as there's a sorting already there

- this is how I generally sort my imports and how I wrote most of the
  unit test suite's imports as well

- there is automation for this that we should probably add once TSLint
  is replaced here; some previous art:
  - https://github.com/trivago/prettier-plugin-sort-imports
  - https://github.com/lydell/eslint-plugin-simple-import-sort/
  - Older:
    - https://github.com/renke/import-sort/tree/master/packages/import-sort-style-module
    - https://github.com/mcdougal/js-isort
    - inspired by Python's `isort` ofc
2022-06-01 14:40:54 -06:00

74 lines
2.6 KiB
TypeScript

import * as path from "path";
import * as tsTypes from "typescript";
import { createFilter as createRollupFilter, normalizePath as normalize } from "@rollup/pluginutils";
import { tsModule } from "./tsproxy";
import { IOptions } from "./ioptions";
import { IContext } from "./context";
export function getOptionsOverrides({ useTsconfigDeclarationDir, cacheRoot }: IOptions, preParsedTsconfig?: tsTypes.ParsedCommandLine): tsTypes.CompilerOptions
{
const overrides: tsTypes.CompilerOptions = {
noEmitHelpers: false,
importHelpers: true,
noResolve: false,
noEmit: false,
inlineSourceMap: false,
outDir: normalize(`${cacheRoot}/placeholder`), // need an outdir that is different from source or tsconfig parsing trips up. https://github.com/Microsoft/TypeScript/issues/24715
moduleResolution: tsModule.ModuleResolutionKind.NodeJs,
allowNonTsExtensions: true,
};
if (preParsedTsconfig)
{
if (preParsedTsconfig.options.module === undefined)
overrides.module = tsModule.ModuleKind.ES2015;
// only set declarationDir if useTsconfigDeclarationDir is enabled
if (!useTsconfigDeclarationDir)
overrides.declarationDir = undefined;
// unsetting sourceRoot if sourceMap is not enabled (in case original tsconfig had inlineSourceMap set that is being unset and would cause TS5051)
const sourceMap = preParsedTsconfig.options.sourceMap;
if (!sourceMap)
overrides.sourceRoot = undefined;
}
return overrides;
}
function expandIncludeWithDirs(include: string | string[], dirs: string[])
{
const newDirs: string[] = [];
dirs.forEach(root => {
if (include instanceof Array)
include.forEach(x => newDirs.push(normalize(path.join(root, x))));
else
newDirs.push(normalize(path.join(root, include)));
});
return newDirs;
}
export function createFilter(context: IContext, pluginOptions: IOptions, parsedConfig: tsTypes.ParsedCommandLine)
{
let included = pluginOptions.include;
let excluded = pluginOptions.exclude;
if (parsedConfig.options.rootDirs)
{
included = expandIncludeWithDirs(included, parsedConfig.options.rootDirs);
excluded = expandIncludeWithDirs(excluded, parsedConfig.options.rootDirs);
}
if (parsedConfig.projectReferences)
{
included = expandIncludeWithDirs(included, parsedConfig.projectReferences.map((x) => x.path)).concat(included);
excluded = expandIncludeWithDirs(excluded, parsedConfig.projectReferences.map((x) => x.path)).concat(excluded);
}
context.debug(() => `included:\n${JSON.stringify(included, undefined, 4)}`);
context.debug(() => `excluded:\n${JSON.stringify(excluded, undefined, 4)}`);
return createRollupFilter(included, excluded, { resolve: parsedConfig.options.rootDir });
}