Update - After restructuring the JavaScript code to separate views from components, as per the great article by Brad Westfall, I found that the local custom declaration file no longer satisfied the compiler. The reason is that the views and container components are placed in separate folders, which breaks the relative module path references. For the sake of expediency, I have resorted to moving the custom declaration file into the SPScript node_module folder. Not great, I know, but it works and so will do in the meantime.
If you are needing to hand-craft your own d.ts files, have a look at Complex Typescript Definitions Made Easy by Steve Fenton. You'll be glad you did!
The JavaScript library SPScript simplifies REST calls to SharePoint from the client by wrapping the AJAX requests in higher level abstractions. Seemed a useful library for my trials with TypeScript, React and D3 in presenting SharePoint data, so I added it to my source code in Visual Studio Code, and imported the file into my module App.ts.
If you are needing to hand-craft your own d.ts files, have a look at Complex Typescript Definitions Made Easy by Steve Fenton. You'll be glad you did!
Issue - Cannot Find Module SPScript
Its not that simple in Typescript. First issue to correct is the red squiggly that appeared under SPScript in the following statement:1 | import SPScript from "SPScript" ; |
To correct this, it was necessary to add a file at one of those locations. Rather than make any adjustments to the SPScript node module (risky!), I added the declaration file SPScript.d.ts in the same folder as the App.ts file, and added an export statement to that new file to set this file as a module.
A note on Visual Studio Code - I have noticed that VS Code is sometimes slow to indicate that errors are corrected. In this case, the red squiggly did not disappear, even with the new file in place. If you close and reopen the file with the issue (in my case, App.ts) then the error was gone when the file reopened!
Issue - What to Include in the Declaration File?
The call I wanted to make using SPScript is as follows:1 | const dao = new SPScript.RestDao(_spPageContextInfo.webAbsoluteUrl); |
1 2 3 4 5 | export module SPScript { export class RestDao { constructor(webUrl: string); } } |
So, all ready to go? Nope - looking at the code in the compiled JS file, that statement had become
1 2 3 | var SPScript_1 = ( typeof window !== "undefined" ? window[ 'SPScript' ] : typeof global !== "undefined" ? global[ 'SPScript' ] : null ); //... other statements var dao1 = new SPScript_1.SPScript.RestDao(_spPageContextInfo.webAbsoluteUrl); |
Plenty of investigation, trial and error lead to the use of this in the declaration file:
1 2 3 | export class RestDao { constructor(webUrl: string); } |
1 | import { RestDao } from "./SPScript" ; |
1 2 3 | var SPScript_1 = ( typeof window !== "undefined" ? window[ 'SPScript' ] : typeof global !== "undefined" ? global[ 'SPScript' ] : null ); //... other statements var dao = new SPScript_1.RestDao(_spPageContextInfo.webAbsoluteUrl); |
A note on the Global Name and Browserify-Shim
The ES5 JavaScript file is being created from the TypeScript modules with the help of Browserify within a Gulp task1 2 3 4 5 6 7 8 9 10 11 12 13 | gulp.task( 'package' , [ 'includeLibs' , 'compile' ], () => { let bundler = browserify({ entries: config.rootJS, debug: true //This provides sourcemapping }) .external([ './SPScript' ]); bundler.bundle() //start buindling .on( 'error' , console.error.bind(console)) .pipe(exorcist(config.distOutputPath + '/' + config.bundleFile + '.map' )) //Extract the sourcemap to a separate file .pipe(source(config.bundleFile)) // Pass desired output file name to vinyl-source-stream .pipe(gulp.dest(config.distOutputPath)); // Destination for the bundle }) |
1 2 3 4 5 6 7 8 | "browserify" : { "transform" : [ "browserify-shim" ] }, "browserify-shim" : { "./SPScript" : "global:SPScript" } |
No comments:
Post a Comment