nollup icon indicating copy to clipboard operation
nollup copied to clipboard

Line Breaks fix

Open Filyus opened this issue 4 years ago • 6 comments

If you use \r\n instead of \n in main.js, then one extra \r appears in the generated code (before generateFile() call). The buggy code is somewhere else, but this fix is simple and works.

Filyus avatar Aug 19 '20 10:08 Filyus

An example of incorrectly incoming code that produces an error:

                                \r;                                                                                                                               const impørt_mêtä = { hot: __compatNollup__(module) };\n
\r\n
const app = new App({target: document.body});\r\n
\r\n
__e__('default', app);;\r\n
\r\n
// recreate the whole app if an HMR update touches this module\r\n
if (impørt_mêtä.hot) {\r\n
  impørt_mêtä.hot.dispose(() => {\r\n
    app.$destroy();\r\n
  })\r\n
  impørt_mêtä.hot.accept();\r\n
}\r\n

Source code (\r\n after every line):

import App from './App.svelte';

const app = new App({target: document.body});

export default app;

// recreate the whole app if an HMR update touches this module
if (import.meta.hot) {
  import.meta.hot.dispose(() => {
    app.$destroy();
  })
  import.meta.hot.accept();
}

Filyus avatar Aug 19 '20 10:08 Filyus

I thought the reason was the rollup-plugin-hot code, but this function is not even called:

const splitFirstLine = code => {
  const eolIndex = code.indexOf('\n')
  if (eolIndex === -1) {
    return ['', '', code]
  }
  const firstLine = code.slice(0, eolIndex)
  if (/\bimport\.meta\b/.test(firstLine)) {
    return [firstLine, code.slice(eolIndex + 1), '']
  }
  return [firstLine, '', code.slice(eolIndex + 1)]
}

I tried to fix it with this code, but the problem is somewhere else:

const splitFirstLine = code => {
  const regex = /(.*?)\r?\n(.*)/s
  const matches = code.match(regex)
  if (matches) {
    const firstLine = matches[1]
    const restLines = matches[2]
    if (/\bimport\.meta\b/.test(firstLine)) {
      return [firstLine, restLines, '']
    }
    return [firstLine, '', restLines]
  }
  else {
    return ['', '', code]
  }
}

Filyus avatar Aug 19 '20 10:08 Filyus

The example code was taken from here: https://github.com/rixo/svelte-template-hot

Filyus avatar Aug 19 '20 10:08 Filyus

That's strange. I'm not sure what would cause there to be a stray \r. For the purpose of writing a regression test, would be important to figure out what's causing it.

PepsRyuu avatar Aug 19 '20 10:08 PepsRyuu

I was right that the error is in the splitFirstLine() function, but it is in a this file: \node_modules\rollup-plugin-hot-nollup\compat-nollup.js but not that: \node_modules\rollup-plugin-hot\lib\compat-nollup.js

Filyus avatar Aug 19 '20 12:08 Filyus

I will talk about this issue with rixo.

Filyus avatar Aug 19 '20 12:08 Filyus