From afca694cbff8b5a2565685eff8db783b4433ca8e Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 30 Nov 2017 15:24:28 +0100 Subject: [PATCH] feat: An ability to create context in Sparky (#962) --- package.json | 2 +- src/Utils.ts | 11 +++++++++++ src/index.ts | 1 + src/sparky/Sparky.ts | 10 +++++++++- src/sparky/SparkyContext.ts | 23 +++++++++++++++++++++++ src/sparky/index.ts | 7 +++++++ 6 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/sparky/SparkyContext.ts create mode 100644 src/sparky/index.ts diff --git a/package.json b/package.json index c01eff20b..03c6d56dd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fuse-box", - "version": "3.0.0-next.26", + "version": "3.0.0-next.27", "description": "Fuse-Box a bundler that does it right", "typings": "index.d.ts", "main": "index.js", diff --git a/src/Utils.ts b/src/Utils.ts index 37c6a8ecc..4b4164003 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -189,6 +189,17 @@ export function hashString(text: string) { let data = hash >>> 0; return data.toString(16); } +export function isClass(obj) { + const isCtorClass = obj.constructor + && obj.constructor.toString().substring(0, 5) === 'class' + if (obj.prototype === undefined) { + return isCtorClass + } + const isPrototypeCtorClass = obj.prototype.constructor + && obj.prototype.constructor.toString + && obj.prototype.constructor.toString().substring(0, 5) === 'class' + return isCtorClass || isPrototypeCtorClass +} export function fastHash(text: string) { let hash = 0; diff --git a/src/index.ts b/src/index.ts index ddddd8c8a..908ec4221 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,6 +43,7 @@ export { OptimizeJSPlugin } from "./plugins/OptimizeJSPlugin"; export { Fluent } from "./arithmetic/Fluent"; export { FuseBox } from "./core/FuseBox"; export { Sparky } from "./sparky/Sparky"; +export { SparkyContext } from './sparky/SparkyContext'; export { CLI } from "./cli/Cli"; export { CSSModules } from "./plugins/stylesheet/CSSModules"; export { CopyPlugin } from "./plugins/CopyPlugin"; diff --git a/src/sparky/Sparky.ts b/src/sparky/Sparky.ts index 479619d6f..3ce34c5f3 100644 --- a/src/sparky/Sparky.ts +++ b/src/sparky/Sparky.ts @@ -4,6 +4,7 @@ import { SparkyFilePatternOptions } from "./SparkyFilePattern"; import { each } from "realm-utils"; import { WorkFlowContext } from "../core/WorkflowContext"; import { Log } from "../Log"; +import { SparkyContext, SparkyContextClass, SparkyCurrentContext } from './SparkyContext'; const context = new WorkFlowContext(); context.doLog = process.env.SPARKY_LOG !== 'false'; @@ -39,6 +40,13 @@ export class Sparky { return this; } + public static context(target: + () => { [key: string]: any } | + (new () => any) | + { [key: string]: any }): SparkyContextClass { + return SparkyContext(target); + } + public static src(glob: string | string[], opts?: SparkyFilePatternOptions): SparkFlow { const flow = new SparkFlow(); let globs = Array.isArray(glob) ? glob : [glob] @@ -66,7 +74,7 @@ export class Sparky { // resolve waterfal dependencies each(task.waterfallDependencies, name => this.resolve(name)) ]).then(() => { - return this.execute(task.fn()); + return this.execute(task.fn(SparkyCurrentContext)); }); } diff --git a/src/sparky/SparkyContext.ts b/src/sparky/SparkyContext.ts new file mode 100644 index 000000000..5307aaab9 --- /dev/null +++ b/src/sparky/SparkyContext.ts @@ -0,0 +1,23 @@ +import { utils } from "realm-utils"; +import { isClass } from '../Utils'; +export let SparkyCurrentContext; + +export class SparkyContextClass { + constructor(public target: any) {} +} +export function SparkyContext( + target: + () => { [key: string]: any } | + (new () => any) | + { [key: string]: any } +) { + if (utils.isPlainObject(target)) { + SparkyCurrentContext = target; + } else if (isClass(target)) { + const Class: any = target; + SparkyCurrentContext = new Class(); + } else if (utils.isFunction(target)) { + SparkyCurrentContext = target(); + } + return new SparkyContextClass(SparkyCurrentContext); +} \ No newline at end of file diff --git a/src/sparky/index.ts b/src/sparky/index.ts new file mode 100644 index 000000000..f7dd89f1a --- /dev/null +++ b/src/sparky/index.ts @@ -0,0 +1,7 @@ +import { Sparky } from './Sparky'; +import { SparkyContext } from './SparkyContext'; + +export const src = Sparky.src; +export const watch = Sparky.watch; +export const task = Sparky.task; +export const context = SparkyContext; \ No newline at end of file