fossamagna / gas-webpack-plugin

Webpack plugin for Google Apps Script

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GasPlugin cannot be used with decorators

soracore opened this issue · comments

Hi. Thank you for making a great plugin.

If original source code contains decorators,
this plugin does not produce working code...
When I run the transpiled file, I get the following error.

TypeError: (0 , _metadata_MetadataStorage__WEBPACK_IMPORTED_MODULE_1__.getMetadataStorage)(...).addConstraintMetadata is not a function(line 482, file "index")

minimum reproduction repository: https://github.com/soracore/gas-ts-decorator-problem
Simply removing gas-webpack-plugin from webpack config solves this problem,
but then I cannot access the main function from GAS.

Is there any way I could workaround this problem?

@soracore Hi. Thank you for your feedback.

I will address this issue.

Thanks! I appreciate your contribution 🙏

@soracore I found cause of this issue. In case of reproduction repository: https://github.com/soracore/gas-ts-decorator-problem, following two top level functions is generated by gas-webpack-plugin

// ENTRYPOINT
function main(e) {
}
function classValidatorMetadataStorage() {
}
...

main function is expected output. but, classValidatorMetadataStorage function is not. When classValidatorMetadataStorage function is defined in global object, MetadataStorage instance is not created, so addConstraintMetadata is not a function error is happen.

  • dist/index.js:
/**
 * Gets metadata storage.
 * Metadata storage follows the best practices and stores metadata in a global variable. 
 */

function getMetadataStorage() {
  var global = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.getGlobal)();

  if (!global.classValidatorMetadataStorage) {
    global.classValidatorMetadataStorage = new MetadataStorage();
  }

  return global.classValidatorMetadataStorage;
}

When I delete lines of generated top level classValidatorMetadataStorage function definition and run it, I don't get error.
I will add include option to gas-webpack-plugin. include option is definition of path patterns for detecting functions.
I will be able to set it as follows.

new GasPlugin({
  include: ["src/**/*.ts"]
})

@fossamagna
Thank you for your investigation!
Right now, I also confirmed that removing the auto-generated function from the global scope solves the problem.

So far, I'm manually doing this but the suggested include option sounds great!
Looking forward to it 👍

@soracore I published gas-webpack-plugin@2.2.0. You can use include option with this version.

@fossamagna
It works like a charm!
Again I appreciate it a lot!!