Skip to content

Commit

Permalink
added experimental TypeScript support
Browse files Browse the repository at this point in the history
caveats:
* there's barely any abstraction here (cf. inline comment), so the only
  reason not to use TypeScript directly is module bundling via Rollup
* this plugin is getting a little bloaty, but that seems inevitable
  unless we wanna make some separate TypeScript plugin fork it or
  include hooks for a `generateTranspiler` equivalent; YAGNI for now
* more significantly, stuffing everything into the `js` config section
  seems a little risky WRT avoiding breaking changes in future evolution
* the test's sample code seems pretty silly

based on earlier efforts by @tillsc
  • Loading branch information
FND committed Feb 19, 2018
1 parent 8cab7ec commit b4b8690
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/bundle/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ module.exports = generateConfig;
// * `jsx.pragma` determines the function to use for JSX expressions
// (e.g. `jsx: { pragma: "createElement" }`)
// * additionally accepts the same options as `esnext`
// * `typescript`, if truthy, activates TypeScript transpilation - anything
// other than `true` will be passed through as TypeScript compiler options
// * `compact`, if truthy, compresses the bundle's code while retaining the
// source code's original structure
function generateConfig({ extensions = [], // eslint-disable-next-line indent
externals, format, moduleName, esnext, jsx, compact }, { browsers }) {
externals, format, moduleName, esnext, jsx, typescript, compact }, { browsers }) {
let plugins = [];
if(esnext || jsx) {
let transpiler = Object.assign({}, esnext, jsx);
Expand All @@ -54,6 +56,13 @@ function generateConfig({ extensions = [], // eslint-disable-next-line indent
extensions = ext.concat(extensions);
plugins.push(plugin);
}
if(typescript) {
let ts = requireOptional("rollup-plugin-typescript2",
"failed to activate TypeScript", "faucet-pipeline-typescript");
extensions.push(".ts");
// TODO: provide defaults and abstractions for low-level options?
plugins.push(typescript === true ? ts() : ts(typescript));
}

let resolve = { jsnext: true };
if(extensions.length) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"eslint-config-fnd-jsx": "^1.1.0",
"faucet-pipeline-esnext": "file:pkg/faucet-pipeline-esnext",
"faucet-pipeline-jsx": "file:pkg/faucet-pipeline-jsx",
"faucet-pipeline-typescript": "file:pkg/faucet-pipeline-typescript",
"json-diff": "^0.5.2",
"mocha": "^5.0.1",
"npm-run-all": "^4.1.2",
Expand Down
23 changes: 23 additions & 0 deletions pkg/faucet-pipeline-typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "faucet-pipeline-typescript",
"version": "1.0.0-beta.1",
"description": "TypeScript for faucet-pipeline",
"author": "FND",
"contributors": [
"Till Schulte-Coerne <npmjs@trsnet.de>"
],
"license": "Apache-2.0",
"homepage": "https://github.com/faucet-pipeline/faucet-pipeline-js",
"repository": {
"type": "git",
"url": "https://github.com/faucet-pipeline/faucet-pipeline-js.git"
},
"bugs": {
"url": "https://github.com/faucet-pipeline/faucet-pipeline-js/issues"
},
"dependencies": {
"faucet-pipeline-js": "1.0.0-beta.1",
"rollup-plugin-typescript2": "^0.11.1",
"typescript": "^2.7.2"
}
}
6 changes: 6 additions & 0 deletions test/cli/run
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ begin "$root/test_jsx"
assert_identical "./dist/bundle.js" "./expected.js"
end

begin "$root/test_typescript"
faucet
assert_identical "./dist/bundle.js" "./expected.js"
rm -r "./.rpt2_cache"
end

begin "$root/test_browserslist"
faucet
assert_identical "./dist/bundle.js" "./expected.js"
Expand Down
39 changes: 39 additions & 0 deletions test/cli/test_typescript/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(function () {
'use strict';

if(typeof global === "undefined" && typeof window !== "undefined") {
window.global = window;
}

var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["Debug"] = 0] = "Debug";
LogLevel[LogLevel["Info"] = 1] = "Info";
LogLevel[LogLevel["Critical"] = 2] = "Critical";
})(LogLevel || (LogLevel = {}));
function log(level, msg) {
if (level === LogLevel.Critical) {
console.error(msg);
}
else {
console.log(msg);
}
}

var generateArticle = function (params) {
var title = params.title, authors = params.authors;
if (typeof title !== "string") {
log(LogLevel.Debug, "auto-generating title");
title = title.main + ": " + title.sub;
}
return title + "\n" + authors.join(", ");
};
generateArticle({
title: {
main: "Hello World",
sub: "sup"
},
authors: ["foo", "bar"]
});

}());
14 changes: 14 additions & 0 deletions test/cli/test_typescript/faucet.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

let path = require("path");

module.exports = {
js: [{
source: "./src/index.ts",
target: "./dist/bundle.js",
typescript: true
}],
plugins: {
js: path.resolve(__dirname, "../../..")
}
};
28 changes: 28 additions & 0 deletions test/cli/test_typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { log, LogLevel } from "./util";

interface ComplexTitle {
main: string;
sub: string;
}

interface ArticleInterface {
title: string | ComplexTitle;
authors: string[];
}

let generateArticle = (params: ArticleInterface) => {
let { title, authors } = params;
if(typeof title !== "string") {
log(LogLevel.Debug, "auto-generating title");
title = `${title.main}: ${title.sub}`;
}
return title + "\n" + authors.join(", ");
};

generateArticle({
title: {
main: "Hello World",
sub: "sup"
},
authors: ["foo", "bar"]
});
9 changes: 9 additions & 0 deletions test/cli/test_typescript/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum LogLevel { Debug, Info, Critical }

export function log(level: LogLevel, msg: string) {
if(level === LogLevel.Critical) {
console.error(msg);
} else {
console.log(msg);
}
}

0 comments on commit b4b8690

Please sign in to comment.