screepers / screeps-typescript-starter

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

Mark 'lodash' module as external dependency in rollup.config #131

Open stachu3478 opened 4 years ago

stachu3478 commented 4 years ago

I have gone in the problem where i have found a lot of unused bundled code (lodash) and found easy way to solve that.

As in Screeps the lodash module is included by default, i think it should not be bundled one more time. So changing:

// rollup.config.js
export default {
  input: "src/main.ts",
  output: {
    file: "dist/main.js",
    format: "cjs",
    sourcemap: true
  },

  plugins: [
    clear({ targets: ["dist"] }),
    resolve(),
    commonjs(),
    typescript({tsconfig: "./tsconfig.json"}),
    screeps({config: cfg, dryRun: cfg == null})
  ]
}
// UPDATE TO
// rollup.config.js
export default {
  input: "src/main.ts",
  output: {
    file: "dist/main.js",
    format: "cjs",
    sourcemap: true
  },
  external: ['lodash'],

  plugins: [
    clear({ targets: ["dist"] }),
    resolve(),
    commonjs(),
    typescript({tsconfig: "./tsconfig.json"}),
    screeps({config: cfg, dryRun: cfg == null})
  ]
}

... would save a lot of code bundled and improve smart bundling effect. However, there might be an issue with incompatible lodash version so it is still only an option.

pyrodogg commented 4 years ago

Lodash only gets bundled if you explicitly import it into files in your project. This shouldn't be necessary if using the games version of lodash. You should be able to write code assuming a global instance of lodash exists bound to _ without import.

A recent update to @types/lodash (v3.10.3) broke the ability to transparently assume a global lodash, so I added a shim to types.d.ts as a workaround.

If you happened to pull this repo within the past week, and you got compile errors regarding lodash which suggested importing it, try pulling the latest changes.

If you prefer explicitly importing (the games's version of) lodash, then yes you will need this external module definition.

If you're explicitly importing a different version of lodash, then it must be bundled.

pyrodogg commented 4 years ago

It's been pointed out that my hotfix doesn't work / introduces other issues. Back to investigating how to handle lodash.

folke commented 3 years ago

You can add the below to types.d.ts:

declare const _ = typeof lodash