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.

Byte order marks included in output file

See original GitHub issue

Bug report

When running on Windows and when the webpack mode=development byte order marks from the source files are copied to the output file. This results in broken Javascript.

This was reported a few years ago here https://github.com/webpack/webpack/issues/2868 and was never resolved.

If the current behavior is a bug, please provide the steps to reproduce.

  1. On a Windows 10 machine with node.js installed.
  2. Clone this repo https://github.com/Bikeman868/frag/tree/Webpack_issue_2021_06_26
  3. From a command prompt in the root directory of the repo type npm install
  4. In the command prompt window type npx webpack
  5. Look at the generated dist/frag.js file and see that it contains weird characters at the start of each module.

This is a very simple solution with the following Webpack config:

const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: 'frag.js',
    path: path.resolve(__dirname, 'dist')
  },
  devtool: 'source-map',
  watchOptions: {
    ignored: '**/node_modules'
  }
};

What is the expected behavior?

Byte-order-marks in Windows files are there to indicate whether the file is little-endian or big-endian. These are written into files automatically by most Windows software, and ignored when the file is opened in any application.

Webpack is copying the byte-order-mark bytes from source files into the output file resulting in broken Javascript. It seems to do this only when the webpack mode is ‘development’. I have not seen this when the mode is ‘production’.

Other relevant information: webpack version: 5.38.1 Node.js version: 15.14.0 Operating System: Windows 10 Additional tools: Windows Terminal + WSL + Ubuntu

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Banner-Keithcommented, Jul 22, 2021

I agree that BOM should be removed in development.

1reaction
Bikeman868commented, Jul 8, 2021

Setting up the linter does not fix the problem, it just tells you that the problem exists. This is an improvement, but actually it’s quite easy to see already, the browser refuses to parse the JavaScript and stops at line 1 character 1. I recognized it right away as BOM.

To anyone reading this that wants to know how to fix the problem, you need to open each source file in VS Code, then on the bottom toolbar there is a button that will say “UTF-8 with BOM”. Click this button and choose “Save with encoding” then choose “UTF-8”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The byte-order mark (BOM) in HTML - W3C
The name BYTE ORDER MARK is an alias for the original character name ZERO WIDTH NO-BREAK SPACE (ZWNBSP). With the introduction of U+2060...
Read more >
Byte order mark - Globalization - Microsoft Learn
Byte Order Mark (BOM) is used to indicate how a processor places serialized text into a sequence of bytes.
Read more >
Byte order mark - Wikipedia
The BOM is encoded in the same scheme as the rest of the document and becomes a noncharacter Unicode code point if its...
Read more >
Using Byte Order Marks (BOMs) - Oracle Help Center
Data files that use a Unicode encoding (UTF-16 or UTF-8) may contain a byte-order mark (BOM) in the first few bytes of the...
Read more >
How to write a Byte Order Mark using the FileSystem Connector
Answer. To add the BOM to the output file when 'Write header' is not enabled: 1. Create a Resources > Scripts > New ......
Read more >

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