diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index 866c0af..5b0fd78 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -2,7 +2,6 @@ 'use strict'; var ts = require('typescript'); -var rollupPluginutils = require('rollup-pluginutils'); var fs = require('fs'); var path = require('path'); @@ -38,92 +37,73 @@ function findFile(cwd, filename) { } return null; } +function parseTsConfig() { + var fileName = findFile(process.cwd(), "tsconfig.json"); + var text = ts.sys.readFile(fileName); + var result = ts.parseConfigFileTextToJson(fileName, text); + var configParseResult = ts.parseJsonConfigFileContent(result.config, ts.sys, path.dirname(fileName), undefined, fileName); + return configParseResult; +} function typescript(options) { options = __assign({}, options); - var filter = rollupPluginutils.createFilter(options.include || ["*.ts+(|x)", "**/*.ts+(|x)"], options.exclude || ["*.d.ts", "**/*.d.ts"]); - // Verify that we're targeting ES2015 modules. - if (options.module !== "es2015" && options.module !== "es6") - throw new Error("rollup-plugin-typescript2: The module kind should be 'es2015', found: '" + options.module + "'"); - var cwd = process.cwd(); - var typescript = ts; - var config = typescript.readConfigFile(findFile(cwd, "tsconfig.json"), function (path$$1) { return fs.readFileSync(path$$1, "utf8"); }); - var compilerOptions = config.config.compilerOptions; - var files = {}; + var parsedConfig = parseTsConfig(); + console.log("lib:", parsedConfig.options.target, parsedConfig.options.lib); var servicesHost = { - getScriptFileNames: function () { return _.keys(files); }, + getScriptFileNames: function () { return parsedConfig.fileNames; }, getScriptVersion: function (_fileName) { return "0"; }, getScriptSnapshot: function (fileName) { if (!fs.existsSync(fileName)) return undefined; - return ts.ScriptSnapshot.fromString(fs.readFileSync(fileName).toString()); + return ts.ScriptSnapshot.fromString(ts.sys.readFile(fileName)); }, getCurrentDirectory: function () { return process.cwd(); }, - getCompilationSettings: function () { return compilerOptions; }, + getCompilationSettings: function () { return parsedConfig.options; }, getDefaultLibFileName: function (opts) { return ts.getDefaultLibFilePath(opts); }, }; var services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); + var printDiagnostics = function (diagnostics) { + diagnostics.forEach(function (diagnostic) { + var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); + if (diagnostic.file) { + var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; + console.log(" Error " + diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message); + } + else { + console.log(" Error: " + message); + } + }); + }; return { - /* resolveId(importee: string, importer: string): any - { - if (importee === TSLIB) - return "\0" + TSLIB; - - if (!importer) - return null; - - let result; - - importer = importer.split("\\").join(" / "); - - result = typescript.nodeModuleNameResolver( importee, importer, compilerOptions, resolveHost ); - - if (result.resolvedModule && result.resolvedModule.resolvedFileName) - { - if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) - return null; - - return result.resolvedModule.resolvedFileName; - } - - return null; - }, - */ - load: function (id) { - if (!filter(id)) - return; + load: function (_id) { return ""; // avoiding double loading }, transform: function (_code, id) { - if (!filter(id)) - return; - files[id] = ""; + console.log("transform", id); var output = services.getEmitOutput(id); - if (output.emitSkipped) - throw new Error("failed to transpile " + id); - return { - code: output.outputFiles[0], - map: output.outputFiles[1], - }; - }, - intro: function () { - return; - }, - outro: function () { - _.each(_.keys(files), function (id) { + if (output.emitSkipped) { var allDiagnostics = services .getCompilerOptionsDiagnostics() .concat(services.getSyntacticDiagnostics(id)) .concat(services.getSemanticDiagnostics(id)); - allDiagnostics.forEach(function (diagnostic) { - var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); - if (diagnostic.file) { - var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; - console.log(" Error " + diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message); - } - else { - console.log(" Error: " + message); - } - }); + printDiagnostics(allDiagnostics); + throw new Error("failed to transpile " + id); + } + var code = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".js"); }); + var map = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".map"); }); + console.log("code: " + code.name + ", map: " + map.name); + return { + code: code ? code.text : undefined, + map: map ? map.text : undefined, + }; + }, + outro: function () { + console.log(); + _.each(parsedConfig.fileNames, function (id) { + var allDiagnostics = services + .getCompilerOptionsDiagnostics() + .concat(services.getSyntacticDiagnostics(id)) + .concat(services.getSemanticDiagnostics(id)); + printDiagnostics(allDiagnostics); }); return; }, diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index cf37e45..b89bdf7 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -1,10 +1,9 @@ /* eslint-disable */ -import { ScriptSnapshot, createDocumentRegistry, createLanguageService, flattenDiagnosticMessageText, getDefaultLibFilePath } from 'typescript'; +import { ScriptSnapshot, createDocumentRegistry, createLanguageService, flattenDiagnosticMessageText, getDefaultLibFilePath, parseConfigFileTextToJson, parseJsonConfigFileContent, sys } from 'typescript'; import * as ts from 'typescript'; -import { createFilter } from 'rollup-pluginutils'; -import { existsSync, readFileSync } from 'fs'; +import { existsSync } from 'fs'; import * as fs from 'fs'; -import { sep } from 'path'; +import { dirname, sep } from 'path'; import * as path from 'path'; const __assign = Object.assign || function (target) { @@ -39,92 +38,73 @@ function findFile(cwd, filename) { } return null; } +function parseTsConfig() { + var fileName = findFile(process.cwd(), "tsconfig.json"); + var text = sys.readFile(fileName); + var result = parseConfigFileTextToJson(fileName, text); + var configParseResult = parseJsonConfigFileContent(result.config, sys, dirname(fileName), undefined, fileName); + return configParseResult; +} function typescript(options) { options = __assign({}, options); - var filter = createFilter(options.include || ["*.ts+(|x)", "**/*.ts+(|x)"], options.exclude || ["*.d.ts", "**/*.d.ts"]); - // Verify that we're targeting ES2015 modules. - if (options.module !== "es2015" && options.module !== "es6") - throw new Error("rollup-plugin-typescript2: The module kind should be 'es2015', found: '" + options.module + "'"); - var cwd = process.cwd(); - var typescript = ts; - var config = typescript.readConfigFile(findFile(cwd, "tsconfig.json"), function (path$$1) { return readFileSync(path$$1, "utf8"); }); - var compilerOptions = config.config.compilerOptions; - var files = {}; + var parsedConfig = parseTsConfig(); + console.log("lib:", parsedConfig.options.target, parsedConfig.options.lib); var servicesHost = { - getScriptFileNames: function () { return _.keys(files); }, + getScriptFileNames: function () { return parsedConfig.fileNames; }, getScriptVersion: function (_fileName) { return "0"; }, getScriptSnapshot: function (fileName) { if (!existsSync(fileName)) return undefined; - return ScriptSnapshot.fromString(readFileSync(fileName).toString()); + return ScriptSnapshot.fromString(sys.readFile(fileName)); }, getCurrentDirectory: function () { return process.cwd(); }, - getCompilationSettings: function () { return compilerOptions; }, + getCompilationSettings: function () { return parsedConfig.options; }, getDefaultLibFileName: function (opts) { return getDefaultLibFilePath(opts); }, }; var services = createLanguageService(servicesHost, createDocumentRegistry()); + var printDiagnostics = function (diagnostics) { + diagnostics.forEach(function (diagnostic) { + var message = flattenDiagnosticMessageText(diagnostic.messageText, "\n"); + if (diagnostic.file) { + var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; + console.log(" Error " + diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message); + } + else { + console.log(" Error: " + message); + } + }); + }; return { - /* resolveId(importee: string, importer: string): any - { - if (importee === TSLIB) - return "\0" + TSLIB; - - if (!importer) - return null; - - let result; - - importer = importer.split("\\").join(" / "); - - result = typescript.nodeModuleNameResolver( importee, importer, compilerOptions, resolveHost ); - - if (result.resolvedModule && result.resolvedModule.resolvedFileName) - { - if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) - return null; - - return result.resolvedModule.resolvedFileName; - } - - return null; - }, - */ - load: function (id) { - if (!filter(id)) - return; + load: function (_id) { return ""; // avoiding double loading }, transform: function (_code, id) { - if (!filter(id)) - return; - files[id] = ""; + console.log("transform", id); var output = services.getEmitOutput(id); - if (output.emitSkipped) - throw new Error("failed to transpile " + id); - return { - code: output.outputFiles[0], - map: output.outputFiles[1], - }; - }, - intro: function () { - return; - }, - outro: function () { - _.each(_.keys(files), function (id) { + if (output.emitSkipped) { var allDiagnostics = services .getCompilerOptionsDiagnostics() .concat(services.getSyntacticDiagnostics(id)) .concat(services.getSemanticDiagnostics(id)); - allDiagnostics.forEach(function (diagnostic) { - var message = flattenDiagnosticMessageText(diagnostic.messageText, "\n"); - if (diagnostic.file) { - var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; - console.log(" Error " + diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message); - } - else { - console.log(" Error: " + message); - } - }); + printDiagnostics(allDiagnostics); + throw new Error("failed to transpile " + id); + } + var code = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".js"); }); + var map = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".map"); }); + console.log("code: " + code.name + ", map: " + map.name); + return { + code: code ? code.text : undefined, + map: map ? map.text : undefined, + }; + }, + outro: function () { + console.log(); + _.each(parsedConfig.fileNames, function (id) { + var allDiagnostics = services + .getCompilerOptionsDiagnostics() + .concat(services.getSyntacticDiagnostics(id)) + .concat(services.getSemanticDiagnostics(id)); + printDiagnostics(allDiagnostics); }); return; }, diff --git a/package.json b/package.json index f7da639..d3113ca 100644 --- a/package.json +++ b/package.json @@ -23,22 +23,23 @@ "lint": "tslint -c ./tslint.json src/*.ts", "postinstall": "typings install" }, - "dependencies": { - }, + "dependencies": {}, "peerDependencies": { "typescript": "^2.1.5" }, "devDependencies": { "@alexlur/rollup-plugin-typescript": "^0.8.1", "@types/node": "^6.0.53", + "glob": "^7.1.1", + "glob-fs": "^0.1.6", "lodash": "^4.17.4", "rimraf": "^2.5.4", "rollup": "^0.41.4", + "rollup-pluginutils": "^2.0.1", + "tslib": "^1.5.0", "tslint": "^4.1.1", "typescript": "^2.1.5", - "typings": "^2.1.0", - "rollup-pluginutils": "^2.0.1", - "tslib": "^1.5.0" + "typings": "^2.1.0" }, "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index 86c1f23..10c121f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import * as ts from "typescript"; import { createFilter } from "rollup-pluginutils"; import * as fs from "fs"; import * as path from "path"; -import { existsSync, readFileSync } from "fs"; +import { existsSync } from "fs"; const _ = require("lodash") as lodash; // Gratefully lifted from 'look-up', due to problems using it directly: @@ -33,88 +33,103 @@ function findFile(cwd: string, filename: string) return null; } +function parseTsConfig() +{ + const fileName = findFile(process.cwd(), "tsconfig.json"); + const text = ts.sys.readFile(fileName); + const result = ts.parseConfigFileTextToJson(fileName, text); + const configParseResult = ts.parseJsonConfigFileContent(result.config, ts.sys, path.dirname(fileName), undefined, fileName); + + return configParseResult; +} + export default function typescript (options: any) { options = { ... options }; - const filter = createFilter(options.include || ["*.ts+(|x)", "**/*.ts+(|x)"], options.exclude || ["*.d.ts", "**/*.d.ts"]); + let parsedConfig = parseTsConfig(); - // Verify that we're targeting ES2015 modules. - if ( options.module !== "es2015" && options.module !== "es6" ) - throw new Error( `rollup-plugin-typescript2: The module kind should be 'es2015', found: '${ options.module }'` ); - - const cwd = process.cwd(); - - let typescript = ts; - let config = typescript.readConfigFile(findFile(cwd, "tsconfig.json"), (path) => readFileSync(path, "utf8")); - let compilerOptions = config.config.compilerOptions; - - let files: { [id: string]: string } = {}; + console.log("lib:", parsedConfig.options.target, parsedConfig.options.lib); const servicesHost: ts.LanguageServiceHost = { - getScriptFileNames: () => _.keys(files), + getScriptFileNames: () => parsedConfig.fileNames, getScriptVersion: (_fileName) => "0", getScriptSnapshot: (fileName) => { if (!fs.existsSync(fileName)) return undefined; - return ts.ScriptSnapshot.fromString(fs.readFileSync(fileName).toString()); + return ts.ScriptSnapshot.fromString(ts.sys.readFile(fileName)); }, getCurrentDirectory: () => process.cwd(), - getCompilationSettings: () => compilerOptions, + getCompilationSettings: () => parsedConfig.options, getDefaultLibFileName: (opts) => ts.getDefaultLibFilePath(opts), }; const services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); - return { - load(id: string): any + let printDiagnostics = function(diagnostics: ts.Diagnostic[]) + { + diagnostics.forEach((diagnostic) => { - if (!filter(id)) return; + let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); + if (diagnostic.file) + { + let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); + console.log(` Error ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); + } + else + { + console.log(` Error: ${message}`); + } + }); + }; + return { + load(_id: string): any + { return ""; // avoiding double loading }, transform(_code: string, id: string): any { - if (!filter(id)) return; - - files[id] = ""; + console.log("transform", id); let output = services.getEmitOutput(id); if (output.emitSkipped) - throw new Error(`failed to transpile ${id}`); - - return { - code: output.outputFiles[0], - map: output.outputFiles[1], - }; - }, - - outro(): any - { - _.each(_.keys(files), (id) => { let allDiagnostics = services .getCompilerOptionsDiagnostics() .concat(services.getSyntacticDiagnostics(id)) .concat(services.getSemanticDiagnostics(id)); - allDiagnostics.forEach((diagnostic) => - { - let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); - if (diagnostic.file) - { - let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); - console.log(` Error ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); - } - else - { - console.log(` Error: ${message}`); - } - }); + printDiagnostics(allDiagnostics); + throw new Error(`failed to transpile ${id}`); + } + + const code: ts.OutputFile = _.find(output.outputFiles, (entry: ts.OutputFile) => _.endsWith(entry.name, ".js") ); + const map: ts.OutputFile = _.find(output.outputFiles, (entry: ts.OutputFile) => _.endsWith(entry.name, ".map") ); + + console.log(`code: ${code.name}, map: ${map.name}`); + + return { + code: code ? code.text : undefined, + map: map ? map.text : undefined, + }; + }, + + outro(): any + { + console.log(); + _.each(parsedConfig.fileNames, (id: string) => + { + let allDiagnostics = services + .getCompilerOptionsDiagnostics() + .concat(services.getSyntacticDiagnostics(id)) + .concat(services.getSemanticDiagnostics(id)); + + printDiagnostics(allDiagnostics); }); return;