-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: normalize loader import path to compiled file
- Loading branch information
Showing
5 changed files
with
124 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { platform } from 'os'; | ||
import { win32 } from 'path'; | ||
|
||
import slash from 'slash'; | ||
|
||
function createLoaderCode(relativePath: string): string { | ||
if (/win32/.test(platform()) && win32.isAbsolute(relativePath)) { | ||
relativePath = exhaustiveReplace(relativePath, /\/\//g, '/'); | ||
relativePath = exhaustiveReplace(relativePath, /\//g, '\\'); | ||
relativePath = exhaustiveReplace(relativePath, /\\\\/g, '\\'); | ||
relativePath = relativePath.replace(/\\/g, '\\\\'); | ||
} else { | ||
relativePath = slash(relativePath); | ||
} | ||
|
||
return ` | ||
require('bytenode'); | ||
require('${relativePath}'); | ||
`; | ||
} | ||
|
||
function exhaustiveReplace(source: string, regex: RegExp, target: string): string { | ||
if (regex.test(source)) { | ||
return exhaustiveReplace(source.replace(regex, target), regex, target); | ||
} | ||
return source; | ||
} | ||
|
||
export { | ||
createLoaderCode, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
describe('createLoaderCode', () => { | ||
|
||
for (const platform of ['darwin', 'linux'] as NodeJS.Platform[]) { | ||
describe(`${platform}`, () => { | ||
|
||
describe('absolute', () => { | ||
test('backwards', async () => { | ||
await mockPlatform(platform); | ||
await testLoaderCode('\\a\\b\\c', '/a/b/c'); | ||
}); | ||
|
||
test('forwards', async () => { | ||
await mockPlatform(platform); | ||
await testLoaderCode('/a/b/c', '/a/b/c'); | ||
}); | ||
}); | ||
|
||
describe('relative', () => { | ||
test('backwards', async () => { | ||
await mockPlatform(platform); | ||
await testLoaderCode('.\\a\\b\\c', './a/b/c'); | ||
}); | ||
|
||
test('forwards', async () => { | ||
await mockPlatform(platform); | ||
await testLoaderCode('./a/b/c', './a/b/c'); | ||
}); | ||
}); | ||
|
||
}); | ||
} | ||
|
||
describe('win32', () => { | ||
|
||
describe('absolute', () => { | ||
test('backwards', async () => { | ||
await mockPlatform('win32'); | ||
await testLoaderCode('A:\\b\\c', 'A:\\\\b\\\\c'); | ||
}); | ||
|
||
test('forwards', async () => { | ||
await mockPlatform('win32'); | ||
await testLoaderCode('A:/b/c', 'A:\\\\b\\\\c'); | ||
}); | ||
}); | ||
|
||
describe('relative', () => { | ||
test('backwards', async () => { | ||
await mockPlatform('win32'); | ||
await testLoaderCode('.\\a\\b\\c', './a/b/c'); | ||
}); | ||
|
||
test('forwards', async () => { | ||
await mockPlatform('win32'); | ||
await testLoaderCode('./a/b/c', './a/b/c'); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
async function mockPlatform(platform: NodeJS.Platform): Promise<void> { | ||
jest.resetModules(); | ||
jest.doMock('os', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const os = jest.requireActual('os'); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return { ...os, platform: () => platform }; | ||
}); | ||
|
||
const os = await import('os'); | ||
expect(os.platform()).toBe(platform); | ||
} | ||
|
||
async function testLoaderCode(from: string, to: string): Promise<void> { | ||
const { createLoaderCode } = await import('../src/loader'); | ||
|
||
expect(createLoaderCode(from)).toBe(` | ||
require('bytenode'); | ||
require('${to}'); | ||
`); | ||
} |