Allow paper core import in TypeScript ()

* Allow paper core import in TypeScript

Typings were missing when importing paper core version with:
`import * as paper from 'paper/dist/paper-core'` syntax.
This changes the generated TypeScript definition so that it exports two
modules: `paper` and `paper/dist/paper-core`. In the same logic,
`paper-core.d.ts` file is added to make sure that the corresponding
definition is automatically loaded.
This also takes care of the fact that `PaperScript` class is not
available in paper core version, by removing it from the corresponding
TypeScript definition.
Finally, this also simplifies existing definition by directly exporting
a `PaperScope` instance as the module instead of duplicating all
`PaperScope` properties and methods on the module itself.
Closes 
This commit is contained in:
Samuel Asensi 2019-11-07 12:12:14 +01:00 committed by Jürg Lehni
parent f0b8799c95
commit 43bbb249ab
4 changed files with 16 additions and 28 deletions

1
dist/paper-core.d.ts vendored Normal file
View file

@ -0,0 +1 @@
import './paper';

View file

@ -23,6 +23,7 @@ gulp.task('zip', ['clean:zip', 'dist'], function() {
'dist/paper-full*.js',
'dist/paper-core*.js',
'dist/paper.d.ts',
'dist/paper-core.d.ts',
'dist/node/**/*',
'LICENSE.txt',
'examples/**/*',

View file

@ -91,21 +91,12 @@ classes.forEach(cls => {
// PaperScope class needs to be handled slightly differently because it "owns"
// all the other classes as properties. Eg. we can do `new paperScope.Path()`.
// So we add a `classesPointers` property that the template will use.
const paperScopeClass = classes.find(_ => _.className === 'PaperScope');
paperScopeClass.classesPointers = classes.filter(_ => _.className !== 'PaperScope').map(_ => ({ name: _.className }));
// Since paper.js module is at the same time a PaperScope instance, we need to
// duplicate PaperScope instance properties and methods in the module scope.
// For that, we expose a special variable to the template.
const paperInstance = { ...paperScopeClass };
// We filter static properties and methods for module scope.
paperInstance.properties = paperInstance.properties.filter(_ => !_.static);
paperInstance.methods = paperInstance.methods.filter(_ => !_.static && _.name !== 'constructor');
const paperScopeClass = classes.find(it => it.className === 'PaperScope');
paperScopeClass.classesPointers = classes.map(it => ({ name: it.className }));
// Format data trough a mustache template.
// Prepare data for the template.
const context = {
paperInstance: paperInstance,
classes: classes,
version: data.version,
date: data.date,

View file

@ -14,21 +14,7 @@
* This is an auto-generated type definition.
*/
declare module paper {
{{#paperInstance}}
{{#properties}}
{{#doc}}4{{/doc}}
let {{name}}{{type}}
{{/properties}}
{{#methods}}
{{#doc}}4{{/doc}}
function {{name}}({{params}}){{type}}
{{/methods}}
{{/paperInstance}}
declare namespace paper {
{{#classes}}
{{#doc}}4{{/doc}}
@ -65,6 +51,15 @@ declare module paper {
{{/classes}}
}
declare module 'paper' {
export = paper
declare module 'paper/dist/paper-core'
{
const paperCore: Pick<paper.PaperScope, Exclude<keyof paper.PaperScope, 'PaperScript'>>;
export = paperCore
}
declare module 'paper'
{
const paperFull: paper.PaperScope;
export = paperFull
}