-
Notifications
You must be signed in to change notification settings - Fork 231
Alternative ways of exposing pnp to the end user (that doesn't break the end-user consumption of lib) #88
Comments
I have just added a design goals page to the wiki, so we document things. Only so much time in the day. Have you tested this second format when importing into a new TS project from the npm package? I.e. does this still work:
This was one of the "big" problems that started this entire conversation so want to ensure we maintain that functionality as well. |
@pbjorklund Excellent evaluation, testing, research on this so far! |
My vote is to close this issue and move on. The string replacements work, and we don't have to duplicate everything in pnp.ts. This is getting around a specific limitation in how TypeScript builds the libraries, and was only an issue because the idea that we would use the syntax: |
If you say so. Your library, your call. |
The duplication in the dist above also allows people to build something by refing NPM package without bundling in their own dist of pnp. Now it will transpile to for instance (Taken from discussion on gitter with @derek-smith this weekend) |
Just read through gitter. Not sure I understand 100% - so including the .default enables what behavior? If it helps something then we should include it in our library. |
So here is the thinking. Let's say I start a new project and use webpack to bundle my app. I install the sp-pnp-js library and Now I don't want to bundle the NPM packages /lib into my solution (with something like webpack) since I already have it globally accessible on pages in SharePoint outside of my own app-code so I rely on pnp already being loaded. For instance via a script tag pointing to the /dist (or in future bower) package of pnp.js on my cdn. Calls made in my transpiled source will point to pnp.default which does not exist in the /dist version of pnp.js. index.ts
index.js commonjs
index.js umd
|
Ah, so other things are thinking the default will be there because that is how the TS code is structured. Got it. So the supported TS way to do this is what we did previously with "export =", but we can't because folks want to use the newer import syntax. I am playing a bit more with packaging today to see if I can get the size down some more, I'll take another look at this. Likely we'll just have to duplicate code in pnp.ts to cover all the bases. |
Completed here #110 |
Design goal
First I would like to see if everyone is aware of the design goal of project of making sure that consumption should be done by including the library and then being able to start using it directly through the
pnp
object. I myself did not realise this was set in stone which led to some confusion in the discussions that followed.Usage as shown in server-root/index.html
This should be documented somewhere seeing as it's not going to be possible to change this signature after release.
Creating the pnp object
The problem is that the transpilation gives us an object where the code is accessible through
pnp.defaults
instead ofpnp
directly as the transpilation adds some metadata to the object exposed. (The replace below shows what I mean)The current approach around this is to rewrite the transpiled code by adding the following to the package.js build task (line 84 and 102)
An alternative approach
I noticed that exposing the properties of the PnP class directly through
export const sharepoint = new SharePoint();
would generate a pnp object that is consumable without rewriting the transpiled code with replace.Two approaches that generate the same end-user result
Exporting a class and using .replace() to rewrite the source
Exporting constants without the use of a class in pnp.ts and with no .replace()
The above 2 examples will both generate an object that we can do
pnp.web.lists
on in the browser.Lib output
The class example will generate this code in /lib
The const example will generate this code in /lib
More background
Related discussions that led to this issue can be found in #78 and #80.
The text was updated successfully, but these errors were encountered: