Line Breaks fix#134
Conversation
|
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\nSource 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();
} |
|
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]
}
} |
|
The example code was taken from here: |
|
That's strange. I'm not sure what would cause there to be a stray |
|
I was right that the error is in the splitFirstLine() function, but it is in a this file: |
|
I will talk about this issue with rixo. |
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.