question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Master issue for webpack/loadAddon issues

See original GitHub issue

I’ve seen a bunch of issues recently come in around how loadAddon works and I wonder if we’re over complicating things. In today’s world of bundlers and source maps, it appears very difficult to get this right in a way that will work across all build systems.

Goals

  • We need an easy way to load addons without forcing the user to download and parse/run the script. The core should be as slim as possible, leaving out optional components.
  • It should work with webpack, browserify and vscode’s module system (and more?)
  • It should work nicely with TypeScript

Current Situation

  • We wrap addons in UMD which allows handling of the addon script in a plan browser environment, CommonJS environment and require.js environment. A problem here is that we needed to reorder the UMD definition as VS Code defines the define global, messing with the typical format and introducing issues for people eg. https://github.com/sourcelair/xterm.js/pull/994
  • In a CommonJS environment the addon script attempts to load in the Terminal object via require, this causes issues for webpack https://github.com/sourcelair/xterm.js/issues/927
  • Terminal inside addons is not typed currently as the definition for Terminal lies outside of the addon folder

Potential Solutions

I’m not attempting to solve the problem right now, here are some ideas though:

  • Have loadAddon pull in the script addon object and apply it to Terminal, as opposed to leaving it up to the addon to load Terminal (see https://github.com/sourcelair/xterm.js/issues/927#issuecomment-325210992)

    public static loadAddon(addon: string, callback?: Function): boolean | any {
        // TODO: Improve return type and documentation
        if (typeof exports === 'object' && typeof module === 'object') {
          // CommonJS
          return require('./addons/' + addon + '/' + addon)(Terminal);
        } else if (typeof define === 'function') {
    ...
    
  • Require bundling or concatenation when using addons?

If anyone has more insights into how this sort of dynamic loading at runtime issue is typically handled, I’d love to hear it 😃

CCing people from related issues: @parisk @mofux @ficristo @dgriffen @ebertmi @rdicroce @jclangst @LucaT1 @jan0e @KagamiChan @vincentwoo

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:25 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
jgillichcommented, Oct 2, 2017

Why not just get rid of all the various module loader hacks and let the consumer deal with it? Maybe I’m missing something, but for me the easiest solution would be:

addon:

export function myAddon(terminal: Terminal) {
    terminal.prototype.doStuff = function() {}
}

loadAddon:

loadAddon(fn: (terminal: Terminal) => any) {
    return fn(Terminal)
}

consumer:

import {myAddon} from 'anywhere'
// or
let myAddon = require('anywhere')
// ...

xterm.loadAddon(myAddon)
1reaction
reemcommented, Oct 24, 2017

I am seeing source maps fail to parse as well, here is a full example error:

WARNING in ./~/xterm/lib/addons/search/SearchHelper.js.map
Module parse failed: /Users/reem/code/udacity/web-terminal-client/node_modules/xterm/lib/addons/search/SearchHelper.js.map Unexpected token (1:10)
You may need an appropriate loader to handle this file type.
| {"version":3,"sources":["../../../src/addons/search/SearchHelper.ts"],"names":[],"mappings":";;AAgBA;IACE,sBAAoB,SAAc,EAAU,4BAAiC;QAAzD,cAAS,GAAT,SAAS,CAAK;QAAU,iCAA4B,GAA5B,4BAA4B,CAAK;IAK7E,CAAC;IAQM,+BAAQ,GAAf,UAAgB,IAAY;QAC1B,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;YAC/B,MAAM,CAAC,KAAK,CAAC;QACf,CAAC;QAED,IAAI,MAAqB,CAAC;QAE1B,IAAI,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC;QAC3C,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,YAAY,CAAC,CAAC,CAAC;YAEjD,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;QAC7D,CAAC;QAGD,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,QAAQ,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;YACtF,MAAM,GA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SAAS,CAAC,gBAAgB,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACzF,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAC3E,MAAM,CAAC,IAAI,CAAC;IACd,CAAC;IACH,mBAAC;AAAD,CA3HA,AA2HC,IAAA;AA3HY,oCAAY","file":"SearchHelper.js","sourceRoot":"."}
 @ ./~/xterm/lib/addons ^\.\/.*$
 @ ./~/xterm/lib/xterm.js
 @ ./src/js/components/terminal/index.js
 @ ./src/js/lib.js
 @ ./examples/web-terminal.js
 @ ./examples/index.jsx
 @ multi webpack-hot-middleware/client?reload=true babel-polyfill ./examples/index.jsx

haven’t been able to get webpack to parse these files.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found