|
| 1 | +const { readFileSync } = require('fs'); |
| 2 | +const { dirname } = require('path'); |
| 3 | +const { sync: resolveSync } = require('resolve'); |
| 4 | +const readFileAsync = require('read-file-async'); |
| 5 | +const resolveAsync = require('resolve-async'); |
| 6 | +const createFile = require('babel-file'); |
| 7 | + |
| 8 | +function getPathFileName(path) { |
| 9 | + return path.hub.file.opts.filename; |
| 10 | +} |
| 11 | + |
| 12 | +function getImportSource(path) { |
| 13 | + return path.node.source.value; |
| 14 | +} |
| 15 | + |
| 16 | +function toResolveOptions(fromPath, resolveOpts) { |
| 17 | + return Object.assign({}, resolveOpts, { basedir: dirname(fromPath) }); |
| 18 | +} |
| 19 | + |
| 20 | +function resolveFilePathAsync(path, filePath, resolveOpts) { |
| 21 | + let fromPath = getPathFileName(path); |
| 22 | + let opts = toResolveOptions(fromPath, resolveOpts); |
| 23 | + return resolveAsync(filePath, opts); |
| 24 | +} |
| 25 | + |
| 26 | +function resolveFilePathSync(path, filePath, resolveOpts) { |
| 27 | + let fromPath = getPathFileName(path); |
| 28 | + let opts = toResolveOptions(fromPath, resolveOpts); |
| 29 | + return resolveSync(filePath, opts); |
| 30 | +} |
| 31 | + |
| 32 | +function resolveImportFilePathAsync(importDeclaration, resolveOpts) { |
| 33 | + let fromPath = getPathFileName(importDeclaration); |
| 34 | + let toPath = getImportSource(importDeclaration); |
| 35 | + let opts = toResolveOptions(fromPath, resolveOpts); |
| 36 | + return resolveAsync(toPath, opts); |
| 37 | +} |
| 38 | + |
| 39 | +function resolveImportFilePathSync(importDeclaration, resolveOpts) { |
| 40 | + let fromPath = getPathFileName(importDeclaration); |
| 41 | + let toPath = getImportSource(importDeclaration); |
| 42 | + let opts = toResolveOptions(fromPath, resolveOpts); |
| 43 | + return resolveSync(toPath, opts); |
| 44 | +} |
| 45 | + |
| 46 | +function loadFileAsync(filePath, parserOpts) { |
| 47 | + return readFileAsync(filePath).then(buffer => |
| 48 | + createFile(buffer.toString(), { filename: filePath, parserOpts }) |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +function loadFileSync(filePath, parserOpts) { |
| 53 | + let buffer = readFileSync(filePath); |
| 54 | + return createFile(buffer.toString(), { filename: filePath, parserOpts }); |
| 55 | +} |
| 56 | + |
| 57 | +function loadImportAsync(importDeclaration, resolveOpts, parserOpts) { |
| 58 | + return resolveImportFilePathAsync(importDeclaration, resolveOpts).then(resolved => |
| 59 | + loadFileAsync(resolved, parserOpts) |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +function loadImportSync(importDeclaration, resolveOpts, parserOpts) { |
| 64 | + const resolved = resolveImportFilePathSync(importDeclaration, resolveOpts); |
| 65 | + return loadFileSync(resolved, parserOpts); |
| 66 | +} |
| 67 | + |
| 68 | +module.exports = { |
| 69 | + resolveFilePathAsync, |
| 70 | + resolveFilePathSync, |
| 71 | + resolveImportFilePathAsync, |
| 72 | + resolveImportFilePathSync, |
| 73 | + loadFileAsync, |
| 74 | + loadFileSync, |
| 75 | + loadImportAsync, |
| 76 | + loadImportSync |
| 77 | +}; |
0 commit comments