I know its probably more advanced then what most p...
# sdf
f
I know its probably more advanced then what most people need. But in my case I try to pack a reactjs single page app as a clientscript and had to spend quiet some time to find a working workaround
j
FYI what I’ve done to work around this issue is have a standard client script that loads my webpack’d script as a module
f
Interesting. I've actual created a workaround too with webpack. Its based on the wrapper plugin but heavily modified to support imports of Netsuite modules
j
Ah yes I used the wrapper plugin as well to prepend a JSDoc that specifies
NAPIVersion
and
NModuleScope
. That plus a updating the webpack config so that it knows that all of the N/* modules are externally available over amd
Copy code
{externals: {"N/record": {amd: "N/record"}}}
And so on and so forth
I’m using the
@hitc/netsuite-types
npm module with TypeScript so this snippet does it for me:
Copy code
const externals = {};
    fs.readdirSync(path.resolve(context, "node_modules/@hitc/netsuite-types/N")).forEach(function (file) {
      if (file === '.' || file === '..') return;
      const module = 'N/' + file.replace('.d.ts', '');
      externals[module] = {amd: module};
    });
f
My trick is
Copy code
plugins: [
        new WrapperPlugin({
            header: function (fileName, externalDependencies) {
                return `
/**
 * @preserve
 *
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */
define([${Object.keys(externalDependencies).map(d => `"${d}"`).join(",")}], function(${Object.values(externalDependencies).join(",")}) {
    ${NETSUITE_GLOBAL_SHIMS.map(shim => `
    if (typeof(${shim}) === "undefined") {
        ${shim} = {};
    }
    `).join('')}
                `;
            },
            footer: function () {
                return `
    return {
        pageInit: NetSuiteSSP.pageInit,
    };
});
                `
            }
        })
    ]
And I compile the project as
Copy code
output: {
        filename: '[name].bundle.js',
        library: 'NetSuiteSSP',
        path: path.resolve(__dirname, 'dist')
    }
And I modified the existing
WrapperPlugin
a bit to give me a list of external dependencies and define externals like you did for all netsuite dependencies
j
Oh gotcha. Yeah I can see how that works. Cool!
f
The cool thing is I can add a import to any netsuite dependency anywhere in my codebase and its going to work correctly
while not having to use custom AMD modules or anything like that
j
Yeah totally. I have mine defining all of the external modules in case they come up so I don’t need to worry about it either ;)