0

I have a rather complex scenario.

We are building a desktop application with React which is wrapped with Electron, Webpack takes care of the Babel transpilation and chunking.

The application receives configuration data from a cms.

Part of the configuration may be a javascript class that needs to override one that resides in the application. The JS code as specified in the CMS will be vanilla Javascript code (ES6/7/8 same as what we use for the application)

I see 2 problems here:

  1. How to transpile just this one class and

  2. How to replace it runtime in the application

Is this even possible?

Regards

atmd
  • 7,430
  • 2
  • 33
  • 64
Thomas
  • 4,641
  • 13
  • 44
  • 67

1 Answers1

1

If with "The application receives configuration data from a cms." you mean runtime data, then, because Webpack acts at compile time, it cannot help you to transpile/replace your code (Runtime vs Compile time).

if your data from a CMS can be fetched at compile time, then, notice that you can return a promise from webpack.config.js.

module.exports = function webpackConfig(env) {
  const configs = {
    context: __dirname,
    plugins: []
    // etc...
  };

  return CMS
    .fetchConfig()
    .then(cmsConfigs => {
      const vars = {
        replaceClass: JSON.stringify(cmsConfigs.classINeed.toString())
      };

      configs.plugins.push(new webpack.DefinePlugin(vars));
      return configs;
    })
  ;
}
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • Hi. Unfortunately it is case #1. Am I out of luck? – Thomas Oct 16 '17 at 12:35
  • you can modify your CMS to save a `transpiled version` of that `runtime code` and fetch it as a normal script into your application (if you can trust it). – Hitmands Oct 16 '17 at 12:55
  • This can also be an alternative. However, I am not sure if loading the transpiled the version of the class will actually override the one in the app, since webpack uses identifiers to load transpiled code. So, if the original class is refered internally with an id of 800 (for instance), the CMS one is not guaranteed to receive the same id. – Thomas Oct 16 '17 at 13:47
  • yes, but if you know what class could be overwritten, then just write a program that acts as a Proxy. – Hitmands Oct 16 '17 at 14:41
  • My problem is not the implementation pattern. If we pout aside the transpilation issue (assuming I do it and get the transpiled code), the problem is that I cannot just eval the JS string in the application and it would override the class that is already in the app, due to webpack's module system that wraps all exported modules/classes – Thomas Oct 16 '17 at 16:52