All files / src index.ts

0% Statements 0/45
0% Branches 0/16
0% Functions 0/8
0% Lines 0/42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                                                                                                                                                                                                     
import * as path from 'path';
import * as ts_module from 'typescript/lib/tsserverlibrary';
import { isCSS as _isCSS, isRelativeCSS } from './helpers/cssExtensions';
import { getDtsSnapshot } from './helpers/cssSnapshots';
 
function init({ typescript: ts }: { typescript: typeof ts_module }) {
  let isCSS = _isCSS;
  function create(info: ts.server.PluginCreateInfo) {
    // User options for plugin.
    const options: IOptions = info.config.options || {};
 
    // Allow custom matchers to be used, handling bad matcher patterns;
    try {
      const { customMatcher } = options;
      if (customMatcher) {
        isCSS = (fileName) => new RegExp(customMatcher).test(fileName);
      }
    } catch (e) {
      // TODO: Provide error/warning to user.
    }
 
    // Creates new virtual source files for the CSS modules.
    const _createLanguageServiceSourceFile = ts.createLanguageServiceSourceFile;
    ts.createLanguageServiceSourceFile = (
      fileName,
      scriptSnapshot,
      ...rest
    ): ts.SourceFile => {
      if (isCSS(fileName)) {
        scriptSnapshot = getDtsSnapshot(ts, scriptSnapshot, options);
      }
      const sourceFile = _createLanguageServiceSourceFile(
        fileName,
        scriptSnapshot,
        ...rest,
      );
      if (isCSS(fileName)) {
        sourceFile.isDeclarationFile = true;
      }
      return sourceFile;
    };
 
    // Updates virtual source files as files update.
    const _updateLanguageServiceSourceFile = ts.updateLanguageServiceSourceFile;
    ts.updateLanguageServiceSourceFile = (
      sourceFile,
      scriptSnapshot,
      ...rest
    ): ts.SourceFile => {
      if (isCSS(sourceFile.fileName)) {
        scriptSnapshot = getDtsSnapshot(ts, scriptSnapshot, options);
      }
      sourceFile = _updateLanguageServiceSourceFile(
        sourceFile,
        scriptSnapshot,
        ...rest,
      );
      if (isCSS(sourceFile.fileName)) {
        sourceFile.isDeclarationFile = true;
      }
      return sourceFile;
    };
 
    if (info.languageServiceHost.resolveModuleNames) {
      const _resolveModuleNames = info.languageServiceHost.resolveModuleNames.bind(
        info.languageServiceHost,
      );
 
      info.languageServiceHost.resolveModuleNames = (
        moduleNames,
        containingFile,
        reusedNames,
      ) => {
        const resolvedCSS: ts.ResolvedModuleFull[] = [];
 
        return _resolveModuleNames(
          moduleNames.filter((moduleName) => {
            if (isRelativeCSS(moduleName)) {
              resolvedCSS.push({
                extension: ts_module.Extension.Dts,
                resolvedFileName: path.resolve(
                  path.dirname(containingFile),
                  moduleName,
                ),
              });
              return false;
            }
            return true;
          }),
          containingFile,
          reusedNames,
        ).concat(resolvedCSS);
      };
    }
 
    return info.languageService;
  }
 
  function getExternalFiles(project: ts_module.server.ConfiguredProject) {
    return project.getFileNames().filter(isCSS);
  }
 
  return { create, getExternalFiles };
}
 
export = init;