-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added experimental TypeScript support
caveats: * special-casing (cf. inline comment) complicates `generateConfig` * there's barely any abstraction here (cf. inline comment); why bother? * `generateTranspilerConfig` now actually only takes care of Babel configuration, which is likely confusing; we'll probably wanna move these details into transpiler-specific modules * no tests yet; these are essential for mainline integration * this anticipates a faucet-pipeline-typescript meta-package * the sample seems pretty silly Co-authored-by: FND <[email protected]>
- Loading branch information
Showing
8 changed files
with
73 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/node_modules | ||
/.eslintcache | ||
|
||
/samples/dist | ||
/samples/*/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,30 +24,40 @@ module.exports = generateConfig; | |
// * `moduleName` determines the global variable to hold the entry point's | ||
// exports (if any) | ||
// * `transpiler.features` determines the language features to be supported | ||
// within source code (e.g. `["es2015", "jsx"]`) | ||
// within source code (e.g. `["es2015", "typescript", "jsx"]`) | ||
// * `transpiler.typescript` are TypeScript-specific options | ||
// * `transpiler.jsx` are JSX-specific options (e.g. `{ pragma: "createElement" }`) | ||
// * `transpiler.exclude` is a list of modules for which to skip transpilation | ||
// (e.g. `["jquery"]`, perhaps due to an already optimized ES5 distribution) | ||
function generateConfig({ extensions = [], // eslint-disable-next-line indent | ||
externals, format, compact, moduleName, transpiler }) { | ||
let plugins = []; | ||
if(transpiler) { | ||
let { features } = transpiler; | ||
if(features && features.includes("jsx")) { | ||
extensions = [".jsx"].concat(extensions); | ||
} | ||
let { features = [] } = transpiler; | ||
if(features.includes("typescript")) { // XXX: special-casing | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tillsc
Author
|
||
let ts = requireOptional("rollup-plugin-typescript2", | ||
"failed to activate TypeScript", "faucet-pipeline-typescript"); | ||
// TODO: provide defaults and abstractions for low-level options | ||
let { typescript } = transpiler; | ||
plugins.push(typescript ? ts(typescript) : ts()); | ||
} else { | ||
if(features.includes("jsx")) { | ||
extensions = [".jsx"].concat(extensions); | ||
} | ||
|
||
let babel = requireOptional("rollup-plugin-babel", | ||
"failed to activate transpiler", "faucet-pipeline-esnext"); | ||
let settings = generateTranspilerConfig(transpiler); | ||
transpiler = babel(settings); | ||
let babel = requireOptional("rollup-plugin-babel", | ||
"failed to activate transpiler", "faucet-pipeline-esnext"); | ||
let settings = generateTranspilerConfig(transpiler); | ||
plugins.push(babel(settings)); | ||
} | ||
} | ||
|
||
let resolve = { jsnext: true }; | ||
if(extensions.length) { | ||
resolve.extensions = [".js"].concat(extensions); | ||
} | ||
|
||
let plugins = (transpiler ? [transpiler] : []).concat([ | ||
plugins = plugins.concat([ | ||
nodeResolve(resolve), | ||
commonjs({ include: "node_modules/**" }) | ||
]); | ||
|
@@ -81,7 +91,7 @@ function generateConfig({ extensions = [], // eslint-disable-next-line indent | |
}); | ||
} | ||
|
||
function generateTranspilerConfig({ features, jsx = {}, exclude }) { | ||
function generateTranspilerConfig({ features, jsx, exclude }) { | ||
let settings = {}; | ||
let plugins = []; | ||
|
||
|
@@ -102,7 +112,7 @@ function generateTranspilerConfig({ features, jsx = {}, exclude }) { | |
} | ||
|
||
if(features.includes("jsx")) { | ||
plugins.push(["transform-react-jsx", jsx]); | ||
plugins.push(["transform-react-jsx", jsx || {}]); | ||
} | ||
|
||
if(plugins.length) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
manifest: { | ||
file: "./dist/manifest.json" | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
js: [{ | ||
source: "./index.ts", | ||
target: "./dist/bundle.js", | ||
transpiler: { | ||
features: ["typescript"] | ||
} | ||
}] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
maybe we should add "tsx" for convenience too. this could result in a option for https://github.com/ezolenko/rollup-plugin-typescript2 (
tsconfigDefaults.compilerOptions.jsx = 'React'
)