Skip to content

Line Breaks fix#134

Open
Filyus wants to merge 1 commit into
PepsRyuu:masterfrom
Filyus:master
Open

Line Breaks fix#134
Filyus wants to merge 1 commit into
PepsRyuu:masterfrom
Filyus:master

Conversation

@Filyus

@Filyus Filyus commented Aug 19, 2020

Copy link
Copy Markdown

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

Filyus commented Aug 19, 2020

Copy link
Copy Markdown
Author

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

Filyus commented Aug 19, 2020

Copy link
Copy Markdown
Author

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

Filyus commented Aug 19, 2020

Copy link
Copy Markdown
Author

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

@PepsRyuu

Copy link
Copy Markdown
Owner

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.

@Filyus

Filyus commented Aug 19, 2020

Copy link
Copy Markdown
Author

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

Filyus commented Aug 19, 2020

Copy link
Copy Markdown
Author

I will talk about this issue with rixo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants