screepers / screeps-typescript-starter

Starter kit for TypeScript-based Screeps AI codes.
https://screepers.gitbook.io/screeps-typescript-starter/
The Unlicense
440 stars 315 forks source link

Missing exports in project dependencies #77

Closed Hopobcn closed 6 years ago

Hopobcn commented 6 years ago

I've found an issue (and a possible solution) when loading screps-profiler to a project. I add this issue in case it's useful for others.

Use case:

Actual behaviour:

$ yarn push-develop
yarn run v1.3.2
$ rollup -c --dest develop

src/main.ts → dist/main.js...
(!) Missing exports
https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module
src/main.ts
enable is not exported by node_modules/screeps-profiler/screeps-profiler.js
5: // Screeps system reloads your script.
6: if (Config.USE_PROFILER) {
7:     Profiler.enable();
                ^
8: }
9: /*
src/main.ts
wrap is not exported by node_modules/screeps-profiler/screeps-profiler.js
29: function ploop() {
30:     if (Config.USE_PROFILER) {
31:         Profiler.wrap(mloop);
                     ^
32:     }
33:     else {

And dist/main.js has some undefined calls:

function ploop() {
    if (USE_PROFILER) {
        undefined(mloop);
    }
    else {
        mloop();
    }
}

In game console:

[10:07:40]TypeError: undefined is not a function
    at main:2356:5
    at eval:2398:4
    at Object.<anonymous>:2:143116
    at Object.r.runCode:2:143673

Possible Solution:

In rollup.config.js, add a namedExprots section to commonjs plugin to manually export enable and wrap functions.

commonjs({
      namedExports: {
        'node_modules/screeps-profiler/screeps-profiler.js': ['enable', 'wrap']
      }
    }),

Maybe screeps-typescript-starter/docs/in-depth/module-bundling.md could be modified to specify the rollup.config.js modifications required.

Does anyone have a better solution than this?

resir014 commented 6 years ago

Good catch! I'll go ahead and update those docs now.

resir014 commented 6 years ago

@Hopobcn and to answer your question, unfortunately no. Rollup has first-class support for bundling ES6 modules, but screeps-profiler is a CommonJS module, which means the module along with its named exports need to be resolved using rollup-plugin-commonjs.

Webpack gets around this by making everything a CommonJS module and then reimplementing the CommonJS require() system client-side, which gets around the namedExports issue at the expense of larger bundle size (which explains the seemingly boilerplate code it generates).

I could be wrong though, @apemanzilla can probably correct me.

dmarcuse commented 6 years ago

Yeah, this is an issue with the way screeps-profiler is written and a limitation of rollup unfortunately.

resir014 commented 6 years ago

Anyways, the docs have been updated. This can be closed now.

roswell67 commented 5 years ago

Still confused on how people got the "Profiler" object without any import after naming the export..... any help here? even the doc isn't clear.