Skip to content

Commit

Permalink
fix(libdef): handle dir aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
pismenskiy authored Feb 4, 2020
1 parent 4384728 commit fe163e8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"rimraf": "^3.0.1",
"semantic-release": "^17.0.2",
"terser": "^4.6.3",
"ts-jest": "^25.1.0",
"ts-jest": "^25.2.0",
"typedoc": "0.16.9",
"typedoc-plugin-external-module-name": "^3.0.0",
"typescript": "^3.7.5",
Expand Down
22 changes: 19 additions & 3 deletions scripts/js/libdef-fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,26 @@ const IMPORT_MAIN_LINE_PATTERN = /^\timport main = require\('(.+)'\);$/
const BROKEN_MODULE_NAME = /(declare module '.+\/target\/es5\/)[^/]*\/src\/main\/index'.+/
const IMPORT = /(import|export) .+ from '(.+)'/g
const REFERENCE = /\/\/\/.+/
const EOF = /$/

assert(!!dts, ' `dts` file path should be specified')

const dtsFile = readFileSync(DTS, 'utf-8')
const declaredModules = (dtsFile.match(/declare module '.*'/g) || []).map(v => v.slice(16, -1))

// Modules should be reachable by `dir/index` and `dir` names
const makeAliases = (modules) => modules.reduce((r, name) => {
const [from, to] = /\/index$/.test(name)
? [name, name.slice(0,-6)]
: [name, name + '/index']

return `${r}
declare module '${to}' {
export * from '${from}'
}`
}, '')

console.log('declaredModules=', declaredModules)

// console.log(dtsFile)
Expand All @@ -29,7 +43,8 @@ const options = {
BROKEN_MODULE_NAME,
REFERENCE,
/^\s*[\r\n]/gm,
IMPORT
IMPORT,
EOF
],
to: [
'',
Expand All @@ -47,12 +62,13 @@ const options = {

const re = /^(.+from ')([^']+)('.*)$/
const [, pre, module, post] = line.match(re)
const name = declaredModules.includes(module)
const name = declaredModules.includes(module) || declaredModules.includes(module + '/index')
? module
: module.replace(prefix + '/', '')

return `${pre}${name}${post}`
}
},
line => makeAliases(declaredModules)
],
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10334,10 +10334,10 @@ trim-repeated@^1.0.0:
dependencies:
escape-string-regexp "^1.0.2"

ts-jest@^25.1.0:
version "25.1.0"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-25.1.0.tgz#06e776c4cce8a4da8eec4945f36a5823d0c0f9ba"
integrity sha512-1Lf576ulKhbxX5og+tG8udVg/5cgcMLPBxp1iCqbbf6VvUK4gEsgAtzMjl8u98izhLrzKMPB0LxCBKEZ5l19Hw==
ts-jest@^25.2.0:
version "25.2.0"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-25.2.0.tgz#dfd87c2b71ef4867f5a0a44f40cb9c67e02991ac"
integrity sha512-VaRdb0da46eorLfuHEFf0G3d+jeREcV+Wb/SvW71S4y9Oe8SHWU+m1WY/3RaMknrBsnvmVH0/rRjT8dkgeffNQ==
dependencies:
bs-logger "0.x"
buffer-from "1.x"
Expand Down

0 comments on commit fe163e8

Please sign in to comment.