javascript icon indicating copy to clipboard operation
javascript copied to clipboard

[guide] Non-code issues: keep them out of your code files.

Open mk-pmb opened this issue 6 years ago • 3 comments

Summary of #1640. I extrapolated the reasoning to other transport layer packaging, hope it's the right conclusion.

mk-pmb avatar Nov 22 '17 12:11 mk-pmb

rebased on current master.

mk-pmb avatar Feb 09 '18 00:02 mk-pmb

For clearer dos and don'ts about the BOM, we could use code blocks that contain just explanatory comments. Would at least keep the visual style consistent:

/* <- bad:  Invisible BOM at start of code file, meant to
            fix your editor's charset.
      good: Set the charset in your editor config.
            If you need per-project config and your editor
            doesn't support it, get a better one.           */
/* <- bad:  Invisible BOM at start of code file, meant to
            fix some browser's charset guessing.
      good: Use your webserver or some part of your toolchain
            to add the charset info.                        */

mk-pmb avatar Feb 10 '18 14:02 mk-pmb

Draft of example for omitting potential UMD wrappers:

// bad: Bloat project code with AMD/UMD export.
(function unifiedExport(e) {
  var d = ((typeof define === 'function') && define),
    m = ((typeof module === 'object') && module);
  if (d && d.amd) { d(function () { return e; }); }
  if (m && m.exports) { m.exports = e; }
}(api));

// good: 
// Just omit it. Use your toolchain to add compatibility
// mechanisms only in the dist version.

Edit: Not a good example though, since an export statement would prevent old browsers that need AMD/UMD from parsing the script at all. Overlaps with rule 10.1.

mk-pmb avatar Feb 10 '18 14:02 mk-pmb