diff --git a/src/main/ts/replacer.ts b/src/main/ts/replacer.ts new file mode 100644 index 0000000..1169ce1 --- /dev/null +++ b/src/main/ts/replacer.ts @@ -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'.+/) diff --git a/src/test/ts/replacer.ts b/src/test/ts/replacer.ts new file mode 100644 index 0000000..0f14e6e --- /dev/null +++ b/src/test/ts/replacer.ts @@ -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\' {') + }) +})