-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers.js
62 lines (56 loc) · 1.87 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import path from 'path'
/**
* Formats an output file, which re-exports items from a list of paths.
* @param {*} config the rollup-plugin-flow-entry config object.
* @param {string} outDir the output directory.
* @param {string} fileName the output file name.
* @param {string[]} paths an array of absolute paths to export types from.
*/
export function buildEntry(config, outDir, fileName, paths) {
const { mode, types } = config
// Handle path overrides:
if (typeof types === 'string') {
paths = [types]
} else if (Array.isArray(types)) {
paths = types
} else if (typeof types === 'object' && types != null) {
const ourTypes = types[fileName]
if (typeof ourTypes === 'string') {
paths = [ourTypes]
} else if (Array.isArray(ourTypes)) {
paths = ourTypes
} else if (ourTypes === false) {
return
}
}
// Set up the path resolution logic:
const here = path.dirname(path.resolve(outDir, fileName))
function escapePath(id) {
const out = path
.relative(here, id)
.replace(/\\+/g, '/')
.replace(/[']/g, "\\'")
return /^[/.]/.test(out) ? out : `./${out}`
}
// Build the source code:
let source = mode != null ? `// @flow ${mode}\n` : '// @flow\n'
source += '// Generated by rollup-plugin-flow-entry\n\n'
for (let i = 0; i < paths.length; ++i) {
source += `export * from '${escapePath(paths[i])}'\n`
}
return { type: 'asset', fileName: fileName + '.flow', source }
}
/**
* Extracts the original entry points from the mulit-entry output.
* @param {string} outDir the output directory.
*/
export function parseMultiEntry(outDir, code) {
const paths = []
const lines = code.split('\n')
for (const line of lines) {
const quoted = line.replace(/^export \* from (".*");?/, '$1')
if (quoted === line) continue
paths.push(path.resolve(outDir, JSON.parse(quoted)))
}
return paths.sort()
}