-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split exports for installation from NPM and CDN
Prior to this change, the default exported value, the global singleton, was exported along with the error classes. This caused the global singleton plug to be available at `window.croct.default` instead of `croct`. With this change, the Javascript code generated for the browser will export only the singleton plug as a global variable `croct` and the NPM package will export an object as a module with the error classes and the singleton plug as its properties. Co-authored-by: Marcos Passos <[email protected]>
- Loading branch information
1 parent
2c41229
commit 9c9923f
Showing
8 changed files
with
172 additions
and
141 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,45 +1,31 @@ | ||
import resolve from 'rollup-plugin-node-resolve'; | ||
import typescript from 'rollup-plugin-typescript2'; | ||
import tempDir from 'temp-dir'; | ||
import {uglify} from 'rollup-plugin-uglify'; | ||
import dts from 'rollup-plugin-dts'; | ||
import commonjs from 'rollup-plugin-commonjs'; | ||
|
||
export default () => { | ||
const minify = process.env.minify || false; | ||
|
||
return [ | ||
{ | ||
input: 'src/index.ts', | ||
output: { | ||
file: minify ? 'build/index.min.js' : 'build/index.js', | ||
name: 'croct', | ||
format: 'iife', | ||
file: 'build/index.js', | ||
format: 'commonjs', | ||
sourcemap: true, | ||
}, | ||
treeshake: { | ||
propertyReadSideEffects: false | ||
}, | ||
plugins: [ | ||
resolve(), | ||
commonjs(), | ||
typescript({ | ||
cacheRoot: `${tempDir}/.rpt2_cache`, | ||
useTsconfigDeclarationDir: true | ||
useTsconfigDeclarationDir: true, | ||
}), | ||
minify ? | ||
uglify({ | ||
compress: { | ||
unused: true, | ||
dead_code: true, | ||
} | ||
}) : {}, | ||
] | ||
], | ||
}, | ||
{ | ||
input: './build/declarations/index.d.ts', | ||
output: [{file: 'build/index.d.ts', format: 'es'}], | ||
output: [{file: 'build/index.d.ts', format: 'commonjs'}], | ||
plugins: [dts({respectExternal: true})], | ||
}, | ||
]; | ||
}; | ||
}; |
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,36 @@ | ||
import resolve from 'rollup-plugin-node-resolve'; | ||
import typescript from 'rollup-plugin-typescript2'; | ||
import tempDir from 'temp-dir'; | ||
import {uglify} from 'rollup-plugin-uglify'; | ||
import commonjs from 'rollup-plugin-commonjs'; | ||
|
||
export default () => { | ||
return [ | ||
{ | ||
input: 'src/plug.ts', | ||
output: { | ||
file: 'build/plug.min.js', | ||
name: 'croct', | ||
format: 'iife', | ||
sourcemap: false, | ||
}, | ||
treeshake: { | ||
propertyReadSideEffects: false, | ||
}, | ||
plugins: [ | ||
resolve(), | ||
commonjs(), | ||
typescript({ | ||
cacheRoot: `${tempDir}/.rpt2_cache`, | ||
useTsconfigDeclarationDir: true, | ||
}), | ||
uglify({ | ||
compress: { | ||
unused: true, | ||
dead_code: true, | ||
}, | ||
}), | ||
], | ||
}, | ||
]; | ||
}; |
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
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,124 @@ | ||
import { | ||
JsonValue, | ||
SdkFacade, | ||
SdkFacadeConfiguration as Configuration, | ||
EvaluationFacadeOptions as EvaluationOptions, | ||
SessionFacade, | ||
TrackerFacade, | ||
UserFacade, | ||
ExternalEvent, | ||
ExternalEventPayload, | ||
ExternalEventType, | ||
} from '@croct-tech/sdk'; | ||
|
||
export interface Plug { | ||
readonly tracker: TrackerFacade; | ||
readonly user: UserFacade; | ||
readonly session: SessionFacade; | ||
|
||
plug(configuration: Configuration): void; | ||
|
||
isAnonymous(): boolean; | ||
|
||
getUserId(): string | null; | ||
|
||
identify(userId: string): void; | ||
|
||
anonymize(): void; | ||
|
||
setToken(token: string): void; | ||
|
||
unsetToken(): void; | ||
|
||
track<T extends ExternalEventType>(type: T, payload: ExternalEventPayload<T>): Promise<ExternalEvent<T>>; | ||
|
||
evaluate(expression: string, options?: EvaluationOptions): Promise<JsonValue>; | ||
|
||
unplug(): Promise<void>; | ||
} | ||
|
||
class GlobalPlug implements Plug { | ||
private facade?: SdkFacade; | ||
|
||
public plug(configuration: Configuration): void { | ||
if (this.facade !== undefined) { | ||
const logger = this.facade.getLogger(); | ||
|
||
logger.info('Croct is already plugged in.'); | ||
|
||
return; | ||
} | ||
|
||
this.facade = SdkFacade.init(configuration); | ||
} | ||
|
||
private get instance(): SdkFacade { | ||
if (this.facade === undefined) { | ||
throw new Error('Croct is not plugged in.'); | ||
} | ||
|
||
return this.facade; | ||
} | ||
|
||
public get tracker(): TrackerFacade { | ||
return this.instance.tracker; | ||
} | ||
|
||
public get user(): UserFacade { | ||
return this.instance.user; | ||
} | ||
|
||
public get session(): SessionFacade { | ||
return this.instance.session; | ||
} | ||
|
||
public isAnonymous(): boolean { | ||
return this.instance.context.isAnonymous(); | ||
} | ||
|
||
public getUserId(): string | null { | ||
return this.instance.context.getUser(); | ||
} | ||
|
||
public identify(userId: string): void { | ||
this.instance.identify(userId); | ||
} | ||
|
||
public anonymize(): void { | ||
this.instance.anonymize(); | ||
} | ||
|
||
public setToken(token: string): void { | ||
this.instance.setToken(token); | ||
} | ||
|
||
public unsetToken(): void { | ||
this.instance.unsetToken(); | ||
} | ||
|
||
public track<T extends ExternalEventType>(type: T, payload: ExternalEventPayload<T>): Promise<ExternalEvent<T>> { | ||
return this.instance.track(type, payload); | ||
} | ||
|
||
public evaluate(expression: string, options: EvaluationOptions = {}): Promise<JsonValue> { | ||
return this.instance.evaluate(expression, options); | ||
} | ||
|
||
public async unplug(): Promise<void> { | ||
if (this.facade === undefined) { | ||
return; | ||
} | ||
|
||
const logger = this.instance.getLogger(); | ||
|
||
try { | ||
await this.facade.close(); | ||
} finally { | ||
delete this.facade; | ||
|
||
logger.info('🔌 Croct has been unplugged.'); | ||
} | ||
} | ||
} | ||
|
||
export default new GlobalPlug(); |
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