Office-js: Can not bundle libary because of basepath check

Created on 17 Oct 2018  路  4Comments  路  Source: OfficeDev/office-js

Great news that the CDN is not a requirement anymore (#62 and #260) 馃帀 An internal check on basePath to load translation strings still make it impossible to use bundlers like webpack to require the library though. The following error gets thrown:

Office Web Extension script library file name should be office.js or office.debug.js.

Especially given that we're writing an ES6 (instead of a TS) project. We did try to include @types/office-js with our webpack externals, but @types/office-js as being TS thereby obviously can not get imported.

It seems a base path gets determined based on the script tag (line 246), but bundlers like webpack don't work with script tags anymore. Can the filename check be removed / changed?

Expected Behavior

Bundlers like Webpack should be able to import the office.js library without problems.

Current Behavior

The script relies on a DOM

All 4 comments

@rvanlaak , for now, this is "by design". Office.js is not a bundle-able dependency today: its architecture -- of dynamically loading the right host file and localization files -- is not something that meshes with the bundle concept.

So for the time being, you do need to use a script tag to reference it, and to just reference the root "office.js" file and let it load whatever it needs.

Re. "CDN is not a requirement anymore": it's true that it's not a hard requirement (and never was) for running add-ins, but remember that for add-ins submitted to the Store, they do need to reference the CDN. So the NPM package is meant mostly for offline debugging or for firewall-ed environments.

Thanks for the clarification 馃憤 For Webpack importing is possible by using the externals configuration:

module.exports = {
    externals: {
        '@microsoft/office-js/dist/office': 'Office'
    },
}

This also fixes eslint errors on missing dependencies:

import Office from '@microsoft/office-js/dist/office';

Interesting. When you do that (the externals configuration), you still need to include it in the script tag in your HTML page, though, right?

If so, what are you gaining by doing the import statement. Are you just trying to get IntelliSense (and if so, any reason not to use @types/office-js), or is there something else? Also, if it is for IntelliSense, does it actually pick it up from the dist/office.d.ts?

With the externals section the script indeed manually has to get included. In other words, the babel / webpack compilation does not transpile / bundle these dependencies. The script tag gets handled externally based on the type of environment (dev includes office.debug.js, prod includes office.js).

We make use of WebStorm as IDE, as we want to have a consistent "developer IDE experience" across our team. The project is vanilla ES6, so unfortunately the dist/office.d.ts but also office.js isn't used for autocompletion.

Was this page helpful?
0 / 5 - 0 ratings