当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Project.createSourceFile方法代码示例

本文整理汇总了TypeScript中ts-simple-ast.Project.createSourceFile方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Project.createSourceFile方法的具体用法?TypeScript Project.createSourceFile怎么用?TypeScript Project.createSourceFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ts-simple-ast.Project的用法示例。


在下文中一共展示了Project.createSourceFile方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: setupFixtures

/** setups and returns the fixtures for testing */
function setupFixtures() {
  const basePath = process.cwd();
  const buildPath = `${basePath}/tools/ts_library_builder/testdata`;
  const outputFile = `${buildPath}/lib.output.d.ts`;
  const inputProject = new Project({
    compilerOptions: {
      baseUrl: basePath,
      declaration: true,
      emitDeclarationOnly: true,
      module: ModuleKind.AMD,
      moduleResolution: ModuleResolutionKind.NodeJs,
      strict: true,
      stripInternal: true,
      target: ScriptTarget.ESNext
    }
  });
  inputProject.addExistingSourceFiles([
    `${buildPath}/globals.ts`,
    `${buildPath}/api.ts`
  ]);
  const declarationProject = new Project({
    compilerOptions: {},
    useVirtualFileSystem: true
  });
  loadDtsFiles(declarationProject);
  for (const { filePath, text } of inputProject.emitToMemory().getFiles()) {
    declarationProject.createSourceFile(filePath, text);
  }
  const outputProject = new Project({
    compilerOptions: {},
    useVirtualFileSystem: true
  });
  loadDtsFiles(outputProject);
  const outputSourceFile = outputProject.createSourceFile(outputFile);
  const debug = true;

  return {
    basePath,
    buildPath,
    inputProject,
    outputFile,
    declarationProject,
    outputProject,
    outputSourceFile,
    debug
  };
}
开发者ID:F001,项目名称:deno,代码行数:48,代码来源:test.ts

示例2: createDeclarationFile

export function createDeclarationFile(project: Project) {
    const mainFile = project.getSourceFileOrThrow("src/main.ts");
    const outputFiles = mainFile.getEmitOutput({ emitOnlyDtsFiles: true }).getOutputFiles();
    if (outputFiles.length !== 1)
        throw new Error(`Expected 1 file when emitting, but had ${outputFiles.length}`);

    const declarationFile = project.createSourceFile("ts-nameof.d.ts", outputFiles[0].getText(), { overwrite: true });

    removePreceedingCommentReference();
    commentExternalTypes();
    removeTypeScriptImport();
    wrapInGlobalModule();
    addGlobalDeclarations();

    function removePreceedingCommentReference() {
        const firstChild = declarationFile.getFirstChildOrThrow();
        declarationFile.removeText(0, firstChild.getStart());
    }

    function commentExternalTypes() {
        // these types are made to be any so that this library will work when included in
        // web projects and NodeJS does not exist. See issue #22.
        const typesToComment = [
            "ts.TransformerFactory<ts.SourceFile>",
            "NodeJS.ErrnoException"
        ];
        declarationFile.forEachDescendant(descendant => {
            if (typesToComment.indexOf(descendant.getText()) >= 0)
                descendant.replaceWithText(`any /* ${descendant.getText()} */`);
        });
    }

    function removeTypeScriptImport() {
        declarationFile.getImportDeclarationOrThrow("typescript").remove();
    }

    function wrapInGlobalModule() {
        const fileText = declarationFile.getText();
        declarationFile.removeText();
        const apiModule = declarationFile.addNamespace({
            hasDeclareKeyword: true,
            declarationKind: NamespaceDeclarationKind.Module,
            name: `"ts-nameof"`
        });
        apiModule.setBodyText(fileText);
        apiModule.getVariableStatementOrThrow(s => s.getDeclarations().some(d => d.getName() === "api"))
            .setHasDeclareKeyword(false);
    }

    function addGlobalDeclarations() {
        const globalFile = project.addExistingSourceFile("../../shared/lib/global.d.ts");
        declarationFile.addStatements(writer => {
            writer.newLine();
            writer.write(globalFile.getText().replace(/\r?\n$/, ""));
        });
    }
}
开发者ID:dsherret,项目名称:ts-nameof,代码行数:57,代码来源:createDeclarationFile.ts

示例3: createDeclarationFile

export function createDeclarationFile(project: Project) {
    const globalFile = project.addExistingSourceFile("../../shared/lib/global.d.ts");
    const declarationFile = project.createSourceFile("ts-nameof.macro.d.ts", "", { overwrite: true });
    const namespaceDec = declarationFile.addNamespace({
        name: `"ts-nameof.macro"`,
        declarationKind: NamespaceDeclarationKind.Module,
        hasDeclareKeyword: true
    });

    namespaceDec.setBodyText(globalFile.getFullText());

    for (const statement of namespaceDec.getStatements()) {
        if (TypeGuards.isAmbientableNode(statement))
            statement.setHasDeclareKeyword(false);
    }

    namespaceDec.addExportAssignment({
        expression: "nameof",
        isExportEquals: false
    });
}
开发者ID:dsherret,项目名称:ts-nameof,代码行数:21,代码来源:createDeclarationFile.ts

示例4: createProject

export function createProject(files: { fileName: string, content: string }[]): Project {
  if (!project) {
    project = new Project({
      useVirtualFileSystem: true,
      compilerOptions: {
        target: ts.ScriptTarget.ES2016,
        moduleResolution: ModuleResolutionKind.NodeJs,
        // module: ModuleKind.CommonJS,
        // noEmit: true,
        // strict: true,
        jsx: JsxEmit.React,
        jsxFactory: 'JSXAlone.createElement',
        libs: ["es2015", "dom"],
        // typeRoots: ['node_modules/@types'],
      },
    })


    const fs: FileSystemHost = project.getFileSystem();
    // fs.writeFileSync(`node_modules/typescript/lib/lib.d.ts`, lib_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.d.ts`, lib_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es5.d.ts`, lib_es5_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.core.d.ts`, lib_es2015_core_d_ts);
    fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.symbol.d.ts`, lib_es2015_symbol_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.iterable.d.ts`, lib_es2015_iterable_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.collection.d.ts`, lib_es2015_collection_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.d.ts`, lib_es2015_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.promise.d.ts`, lib_es2015_promise_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.proxy.d.ts`, lib_es2015_proxy_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.reflect.d.ts`, lib_es2015_reflect_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts`, lib_es2015_symbol_wellknown_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.scripthost.d.ts`, lib_scripthost_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.webworker.d.ts`, lib_webworker_d_ts);
    // fs.writeFileSync(`node_modules/typescript/lib/lib.webworker.importscripts.d.ts`, lib_webworker_importscripts_d_ts);

    // fs.writeFileSync(`node_modules/typescript/lib/lib.dom.d.ts`, lib_dom_d_ts);

    // fs.writeFileSync(`node_modules/jsx-alone/index.d.ts`, jsx_alone_core_d_ts);

    // project.createSourceFile('lib.d.ts', lib_d_ts)
    project.createSourceFile('lib.es5.d.ts', lib_es5_d_ts)
    project.createSourceFile('lib.es2015.core.d.ts', lib_es2015_core_d_ts)
    // project.createSourceFile('lib.es2015.symbol.d.ts', lib_es2015_symbol_d_ts)
    project.createSourceFile('lib.es2015.iterable.d.ts', lib_es2015_iterable_d_ts)
    project.createSourceFile('lib.dom.d.ts', lib_dom_d_ts)
    // fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.iterable.d.ts`, lib_es2015_iterable_d_ts);

    // project.createSourceFile('node_modules/typescript/lib/lib.es5.d.ts', lib_es5_d_ts)
    // project.createSourceFile('node_modules/typescript/lib/lib.es2015.core.d.ts', lib_es2015_core_d_ts)
    // project.createSourceFile('node_modules/typescript/lib/lib.es2015.symbol.d.ts', lib_es2015_symbol_d_ts)
    // project.createSourceFile('node_modules/typescript/lib/lib.es2015.iterable.d.ts', lib_es2015_iterable_d_ts)
    // project.createSourceFile('node_modules/typescript/lib/lib.dom.d.ts', lib_dom_d_ts)

    project.createSourceFile('index.d.ts', jsx_alone_core_d_ts)



    files.forEach(f => project!.createSourceFile(f.fileName, f.content, { overwrite: true }))
  }
  else {
    files.forEach(f => {
      let sf = project!.getSourceFile(f.fileName)
      if (!sf) {
        // this only happens in tests
        sf = project!.createSourceFile(f.fileName, f.content)
      }
      else if (sf!.getText() !== f.content) {
        sf.replaceWithText(f.content)
      }
    })
  }
  project.saveSync()
  return project
}
开发者ID:cancerberoSgx,项目名称:javascript-sample-projects,代码行数:74,代码来源:ts-simple-ast.ts

示例5: main

export function main({
  basePath,
  buildPath,
  inline,
  debug,
  outFile,
  silent
}: BuildLibraryOptions): void {
  if (!silent) {
    console.log("-----");
    console.log("build_lib");
    console.log();
    console.log(`basePath: "${basePath}"`);
    console.log(`buildPath: "${buildPath}"`);
    if (inline && inline.length) {
      console.log(`inline:`);
      for (const filename of inline) {
        console.log(`  "${filename}"`);
      }
    }
    console.log(`debug: ${!!debug}`);
    console.log(`outFile: "${outFile}"`);
    console.log();
  }

  // the inputProject will take in the TypeScript files that are internal
  // to Deno to be used to generate the library
  const inputProject = new Project({
    compilerOptions: {
      baseUrl: basePath,
      declaration: true,
      emitDeclarationOnly: true,
      lib: [],
      module: ModuleKind.AMD,
      moduleResolution: ModuleResolutionKind.NodeJs,
      noLib: true,
      paths: {
        "*": ["*", `${buildPath}/*`]
      },
      preserveConstEnums: true,
      strict: true,
      stripInternal: true,
      target: ScriptTarget.ESNext
    }
  });

  // Add the input files we will need to generate the declarations, `globals`
  // plus any modules that are importable in the runtime need to be added here
  // plus the `lib.esnext` which is used as the base library
  inputProject.addExistingSourceFiles([
    `${basePath}/node_modules/typescript/lib/lib.esnext.d.ts`,
    `${basePath}/js/deno.ts`,
    `${basePath}/js/globals.ts`
  ]);

  // emit the project, which will be only the declaration files
  const inputEmitResult = inputProject.emitToMemory();

  const inputDiagnostics = inputEmitResult
    .getDiagnostics()
    .map(d => d.compilerObject);
  logDiagnostics(inputDiagnostics);
  if (inputDiagnostics.length) {
    process.exit(1);
  }

  // the declaration project will be the target for the emitted files from
  // the input project, these will be used to transfer information over to
  // the final library file
  const declarationProject = new Project({
    compilerOptions: {
      baseUrl: basePath,
      moduleResolution: ModuleResolutionKind.NodeJs,
      noLib: true,
      paths: {
        "*": ["*", `${buildPath}/*`]
      },
      strict: true,
      target: ScriptTarget.ESNext
    },
    useVirtualFileSystem: true
  });

  // we don't want to add to the declaration project any of the original
  // `.ts` source files, so we need to filter those out
  const jsPath = normalizeSlashes(`${basePath}/js`);
  const inputProjectFiles = inputProject
    .getSourceFiles()
    .map(sourceFile => sourceFile.getFilePath())
    .filter(filePath => !filePath.startsWith(jsPath));
  loadFiles(declarationProject, inputProjectFiles);

  // now we add the emitted declaration files from the input project
  for (const { filePath, text } of inputEmitResult.getFiles()) {
    declarationProject.createSourceFile(filePath, text);
  }

  // the outputProject will contain the final library file we are looking to
  // build
  const outputProject = new Project({
//.........这里部分代码省略.........
开发者ID:F001,项目名称:deno,代码行数:101,代码来源:build_library.ts


注:本文中的ts-simple-ast.Project.createSourceFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。