Skip to content

Commit

Permalink
chore: add module path replacers
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 4, 2020
1 parent 7ba1560 commit 2507392
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/ts/replacer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {IReplacer} from './interface'

export const replaceExportMain: IReplacer = {
from: '\texport = main;',
to: ''
}

export const replaceImportMain: IReplacer = {
from: /\timport main = require\('(.+)'\);/g,
to: (line: string) => {
const [, name] = /^\timport main = require\('(.+)'\);$/.exec(line) || []
return ` export * from '${name}';`
}
}

export const replaceBrokenModulePrefix: IReplacer = (pattern => ({
from: pattern,
to: (line: string) => {
const [, module] = pattern.exec(line) || []
return `${module}index' {`
}
}))(/(declare module '.+\/target\/es5\/)[^/]*\/src\/main\/index'.+/)
40 changes: 40 additions & 0 deletions src/test/ts/replacer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {IReplacer} from '../../main/ts/interface'
import {
replaceExportMain,
replaceImportMain,
replaceBrokenModulePrefix,
} from '../../main/ts/replacer'

describe('replacer', () => {
const assertReplacement = ({from, to}: IReplacer, input: string, output: string) => {
if (from instanceof RegExp) {
expect(from.test(input)).toBeTruthy()
}

if (typeof from === 'string') {
expect(input.includes(from)).toBeTruthy()
}

if (typeof to === 'function') {
expect(to(input, '')).toBe(output)
}
}

it('#replaceExportMain', () => {
assertReplacement(replaceExportMain, '\texport = main;', 'bar')
})

it('#replaceImportMain', () => {
assertReplacement(
replaceImportMain,
' import main = require(\'foo\');',
' export * from \'foo\';')
})

it('#replaceBrokenModulePrefix', () => {
assertReplacement(
replaceBrokenModulePrefix,
'declare module \'@qiwi/foo/target/es5//src/main/index\' {',
'declare module \'@qiwi/foo/target/es5/index\' {')
})
})

0 comments on commit 2507392

Please sign in to comment.