Skip to content

Commit

Permalink
Fixed single outFile generation (#1) (#42)
Browse files Browse the repository at this point in the history
* Fixed single outFile generation (#1)

 - All input files now point to the same output file
 - Added `AllowJS` compiler flag, as this is useful for projects using outFile
 - Added `typingsFile` option to allow typings files to be written for older JS code being included in the single file output

* Added scripted test for single-outfile fixes
  • Loading branch information
DomBlack authored and BrandonArp committed Aug 15, 2016
1 parent da4a89a commit 2500372
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 27 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sbt-typescript
sbt-typescript
==============

<a href="https://raw.githubusercontent.com/ArpNetworking/sbt-typescript/master/LICENSE">
Expand All @@ -14,7 +14,7 @@ sbt-typescript
alt="Maven Artifact">
</a>

Allows TypeScript to be used from within sbt. Leverages the functionality of com.typesafe.sbt:js-engine to run the
Allows TypeScript to be used from within sbt. Leverages the functionality of com.typesafe.sbt:js-engine to run the
typescript compiler.

To use this plugin use the addSbtPlugin command within your project's plugins.sbt (or as a global setting) i.e.:
Expand All @@ -30,22 +30,24 @@ The options provided mimic the arguments to the tsc command line compiler.

Option | Description
-----------------------|------------
allowJS | Allow JavaScript files to be compiled.
declaration | Generates corresponding '.d.ts' file.
sourceMap | Generates source maps for input files.
sourceRoot | Specifies the location where the compiler should locate TypeScript files instead of the source locations.
sourceRoot | Specifies the location where the compiler should locate TypeScript files instead of the source locations on the webserver. (Note this is require if using `outFile` to compile into a single file)
mapRoot | Specifies the location where the compiler should locate map files instead of generated locations.
target | ECMA script target version. Should be "ES3" or "ES5" (default).
noImplicitAny | Warn on expressions and declarations with an implied "any" type.
moduleKind | Specifies module code generation. Should be "" (default), "None", "CommonJS", "AMD", "UMD", "System", "ES6" and "ES2015".
outFile | Concatenate and emit output to a single file.
outFile | Concatenate and emit output to a single file.
outDir | Destination directory for output files.
removeComments | Removes comments from the generated source.
jsx | Specifices JSX-mode for .tsx files. Should be "None", "Preserve" (default) to generate .jsx or "React" to generate .js files.
experimentalDecorators | Experimental decorators support. Set this to true if you have an angular2 project.
emitDecoratorMetadata | This will write decorator metadata to your js files. Set this to true if you have an angular2 project.
moduleResolutionKind | "NodeJs" or "Classic" module resolution.
moduleResolutionKind | "NodeJs" or "Classic" module resolution.
typingsFile | A file that refers to typings that the build needs. Default None, but would normally be "/typings/index.d.ts"



By default, all typescript files (*.ts and *.tsx) are included in the compilation and will generate corresponding javascript
files. To change this, supply an includeFilter in the TypescriptKeys.typescript task configuration.

Expand Down
71 changes: 55 additions & 16 deletions src/main/resources/typescriptc.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
var compSettings = new typescript.getDefaultCompilerOptions();
logger.debug("instantiated");

// If the out put exists and is not an absolute path, assume it's meant to go into
// the output directory rather than in the root of the SBT project!
var outFile = options.outFile;
if (outFile && outFile[0] !== '/') {
outFile = options.outDir + '/' + outFile;
}

compSettings.declaration = options.declaration;
compSettings.sourceMap = options.sourceMap;
compSettings.sourceRoot = options.sourceRoot;
Expand All @@ -87,13 +94,14 @@
compSettings.module = typescript.ModuleKind[options.moduleKind];
compSettings.target = typescript.ScriptTarget[options.target];
compSettings.jsx = typescript.JsxEmit[options.jsx];
compSettings.out = options.outFile;
compSettings.out = outFile;
compSettings.outDir = options.outDir;
compSettings.removeComments = options.removeComments;
compSettings.rootDir = options.rootDir;
compSettings.baseUrl = options.baseUrl;
compSettings.traceResolution = options.traceResolution;
compSettings.paths = options.paths;
compSettings.allowJs = options.allowJs;
logger.debug("settings created");

return compSettings;
Expand All @@ -104,7 +112,12 @@
return file.substring(0, file.length - oldExt.length) + ext;
}

function fixSourceMapFile(file){
function fixSourceMapFile(opt, file){
if (opt.sourceRoot) {
// If we have a defined sourceRoot, we don't need to "fix" anything
return;
}

/*
All source .ts files are copied to public folder and reside there side by side with generated .js and js.map files.
It means that source maps root at runtime is always '.' and 'sources' array should contain only file name.
Expand All @@ -120,6 +133,7 @@
function compile(args) {
var sourceMaps = args.sourceFileMappings;
var inputFiles = [];
var filesToCompile = inputFiles;
var outputFiles = [];
var opt = args.options;

Expand All @@ -135,15 +149,40 @@
));
});

if (opt.typingsFile) {
filesToCompile = inputFiles.concat(opt.typingsFile);
}

logger.debug("starting compilation of " + inputFiles);
opt.outDir = args.target;
var options = createCompilerSettings(opt);
logger.debug("options = " + JSON.stringify(options));
var compilerHost = typescript.createCompilerHost(options);
var program = typescript.createProgram(inputFiles, options, compilerHost);
var program = typescript.createProgram(filesToCompile, options, compilerHost);

var output = {results: [], problems: []};

var createFilesWrittenArray = function(outputFile) {
var filesWritten = [];

var outputFileMap = outputFile + ".map";

filesWritten.push(outputFile);

if (options.declaration) {
var outputFileDeclaration = replaceFileExtension(outputFile, ".d.ts");
filesWritten.push(outputFileDeclaration);
}

if(options.sourceMap){
// alter source map file to change a thing
fixSourceMapFile(opt, outputFileMap);
filesWritten.push(outputFileMap);
}

return filesWritten;
};


var recordDiagnostic = function (d) {
logger.debug("recording diagnostic");
Expand Down Expand Up @@ -242,20 +281,9 @@
}

var outputFile = outputFiles[index];
var outputFileMap = outputFile + ".map";

var filesWritten = [outputFile];

if (options.declaration) {
var outputFileDeclaration = replaceFileExtension(outputFile, ".d.ts");
filesWritten.push(outputFileDeclaration);
}

if(options.sourceMap){
// alter source map file to change a thing
fixSourceMapFile(outputFileMap);
filesWritten.push(outputFileMap);
}
// Only record this written files if we are not creating a single output file
var filesWritten = (options.out) ? [] : createFilesWrittenArray(outputFile);

var result = {
source: file,
Expand All @@ -266,6 +294,17 @@
};
output.results.push(result);
});

// If we are outputting a single file create the array of written files
// and map it against all input files
if (options.out) {
var filesWritten = createFilesWrittenArray(options.out);

output.results.forEach(function(file) {
file.result.filesWritten = filesWritten;
});
}

logger.debug("output=" + JSON.stringify(output));
return output;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import sbt._
import com.typesafe.sbt.jse.SbtJsTask
import com.typesafe.sbt.web.SbtWeb
import sbt.Keys._
import spray.json.{JsString, JsBoolean, JsObject, JsArray}
import spray.json.{JsValue, JsNull, JsString, JsBoolean, JsObject, JsArray}

object Import {

Expand All @@ -37,7 +37,7 @@ object Import {
val mapRoot = SettingKey[String]("typescript-map-root", "Specifies the location where debugger should locate map files instead of generated locations.")
val target = SettingKey[String]("typescript-target", "ECMAScript target version: 'ES3', 'ES5' (default), 'ES6', 'ES2015', 'Latest'.")
val noImplicitAny = SettingKey[Boolean]("typescript-no-implicit-any", "Warn on expressions and declarations with an implied 'any' type.")
val moduleKind = SettingKey[String]("typescript-module", "Specify module code generation: 'commonjs' or 'amd'.")
val moduleKind = SettingKey[String]("typescript-module", "Specify module code generation: 'Commonjs', 'AMD' or 'System'.")
val outFile = SettingKey[String]("typescript-output-file", "Concatenate and emit output to a single file.")
val outDir = SettingKey[String]("typescript-output-directory", "Redirect output structure to the directory.")
val jsx = SettingKey[String]("typescript-jsx-mode", "Specify JSX mode for .tsx files: 'Preserve' (default) 'React' or 'None'.")
Expand All @@ -49,6 +49,8 @@ object Import {
val baseUrl = SettingKey[String]("--baseUrl", "A directory where the compiler should look for modules")
val traceResolution = SettingKey[Boolean]("--traceResolution", "Offers a handy way to understand how modules have been resolved by the compiler")
val paths = SettingKey[Map[String, Seq[String]]]("paths", "Will map one path to another")
val allowJS = SettingKey[Boolean]("typescript-allow-js", "Allow JavaScript files to be compiled")
val typingsFile = SettingKey[Option[File]]("typescript-typings-file", "A file that refers to typings that the build needs. Default None.")
}
}

Expand All @@ -67,7 +69,13 @@ object SbtTypescript extends AutoPlugin {

val typescriptUnscopedSettings = Seq(

includeFilter in typescript := GlobFilter("*.ts") | GlobFilter("*.tsx"),
includeFilter in typescript := (
if (allowJS.value) {
GlobFilter("*.ts") | GlobFilter("*.tsx") | GlobFilter("*.js") | GlobFilter("*.jsx")
} else {
GlobFilter("*.ts") | GlobFilter("*.tsx")
}
),
excludeFilter in typescript := GlobFilter("*.d.ts"),

sources in typescript := (sourceDirectories.value ** ((includeFilter in typescript).value -- (excludeFilter in typescript).value)).get,
Expand All @@ -92,7 +100,9 @@ object SbtTypescript extends AutoPlugin {
"rootDir" -> JsString(rootDir.value),
"baseUrl" -> JsString(baseUrl.value),
"traceResolution" -> JsBoolean(traceResolution.value),
"paths" -> JsObject(mapToJs(paths.value))
"paths" -> JsObject(mapToJs(paths.value)),
"allowJs" JsBoolean(allowJS.value),
"typingsFile" typingsFile.value.fold[JsValue](JsNull)(f JsString(f.absolutePath))
).toString()
)

Expand All @@ -117,7 +127,9 @@ object SbtTypescript extends AutoPlugin {
rootDir := (sourceDirectory in Assets).value.absolutePath,
baseUrl := ((webJarsDirectory in Assets).value / "lib").absolutePath,
traceResolution := false,
paths := Map()
paths := Map(),
allowJS := false,
typingsFile := None
) ++ inTask(typescript)(
SbtJsTask.jsTaskSpecificUnscopedSettings ++
inConfig(Assets)(typescriptUnscopedSettings) ++
Expand Down
28 changes: 28 additions & 0 deletions src/sbt-test/sbt-typescript-plugin/single-outfile/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.nio.file.Files

import com.arpnetworking.sbt.typescript.Import.TypescriptKeys._
import sbt.complete.DefaultParsers._

lazy val root = (project in file(".")).enablePlugins(SbtWeb).settings(
JsEngineKeys.engineType := JsEngineKeys.EngineType.Node,
outFile := "app.js", // Create a single output file
moduleKind := "AMD",
sourceMap := true,
sourceRoot := "/assets/", // Map the root folder to a URL on the server
allowJS := true, // Allow "legacy" JS files to be included and compiled into the single output file
typingsFile := Some(file("typings/index.d.ts")) // Include the typings root file
)

lazy val assertMatches = inputKey[Unit]("")

assertMatches := {
spaceDelimited("<arg>").parsed.map(file) match {
case Seq(actual, expected)
assert(actual.exists, s"Generated file ${actual.getName} does not exist")
assert(expected.exists, s"Expected file ${expected.getName} does not exist")
assert(readAllText(actual) == readAllText(expected), s"Generated file ${actual.getName} does not match expected")
}
}

def readAllText(file: File) = new String(Files.readAllBytes(file.toPath), "UTF-8")

20 changes: 20 additions & 0 deletions src/sbt-test/sbt-typescript-plugin/single-outfile/expected/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2014 Groupon.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

sbt.version=0.13.5
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Copyright 2014 Groupon.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//


addSbtPlugin("com.arpnetworking" % "sbt-typescript" % sys.props("project.version"))

resolvers ++= Seq(
Resolver.mavenLocal,
Resolver.url("sbt snapshot plugins", url("http://repo.scala-sbt.org/scalasbt/sbt-plugin-snapshots"))(Resolver.ivyStylePatterns),
Resolver.sonatypeRepo("snapshots"),
"Typesafe Snapshots Repository" at "http://repo.typesafe.com/typesafe/snapshots/"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {upperCase} from './util/string'; // TS file in another folder (testing source maps)
import {log} from './util/logger'; // Legacy JS file

// Legacy is a "global" object which typescript will complain about without the typings file
log(Legacy.someFunc(upperCase("Hello World")));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function log(str) {
console.log(str);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function upperCase(str: string): string {
return str.toUpperCase();
}
6 changes: 6 additions & 0 deletions src/sbt-test/sbt-typescript-plugin/single-outfile/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Compile a typescript file

> assets
> assertMatches target/web/public/main/app.js expected/app.js
> assertMatches target/web/public/main/app.js.map expected/app.js.map

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

declare namespace __LegacyGlobal {
function someFunc(str: string): string;
}

import Legacy = __LegacyGlobal;

0 comments on commit 2500372

Please sign in to comment.