This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix a bug... that I still can't explained (#5)
Major refactoring. Fix false positive in tests. Thanks to @yormi
- Loading branch information
Showing
4 changed files
with
156 additions
and
211 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,90 @@ | ||
import { | ||
normalizeSourceRoot, normalizeFilename, | ||
checkAndRemovePrefix, transformPath | ||
} from './helper' | ||
dirname, | ||
join, | ||
relative, | ||
isAbsolute | ||
} from 'path' | ||
import slash from 'slash' | ||
|
||
export default function() { | ||
export default function () { | ||
return { | ||
visitor: { | ||
ImportDeclaration(path, state) { | ||
const projectPathSuffix = typeof state.opts.projectPathSuffix === 'string' ? | ||
state.opts.projectPathSuffix : | ||
'' | ||
ImportDeclaration (path, state) { | ||
/* | ||
* config: { | ||
* projectRoot: babel option sourceRoot or process.cwd as fallback | ||
* prefix: importPathPrefix provided by the user in the plugin config | ||
* suffix: projectPathSuffix provided by the user in the plugin config | ||
* } | ||
*/ | ||
const config = extractConfig(state) | ||
|
||
const importPathPrefix = typeof state.opts.importPathPrefix === 'string' ? | ||
state.opts.importPathPrefix + '/': | ||
'~/' | ||
// file currently visited | ||
const sourcePath = state.file.opts.filename | ||
|
||
const [sourceRoot, suffixedSourceRoot] = normalizeSourceRoot( | ||
state.file.opts.sourceRoot, projectPathSuffix | ||
) | ||
|
||
const filename = normalizeFilename( | ||
state.file.opts.filename, | ||
sourceRoot, | ||
suffixedSourceRoot | ||
) | ||
if (sourcePath === 'unknown') { | ||
return | ||
} | ||
|
||
if (!filename) return | ||
invariants(config) | ||
unixifyPaths(config) // works on windows too according to slash's doc | ||
|
||
// string in the import statement | ||
const importPath = path.node.source.value | ||
const importPathWithoutPrefix = checkAndRemovePrefix( | ||
importPath, importPathPrefix | ||
) | ||
|
||
if (importPathWithoutPrefix) { | ||
path.node.source.value = transformPath( | ||
importPathWithoutPrefix, filename, sourceRoot | ||
) | ||
|
||
if (isImportPathPrefixed(importPath, config.prefix)) { | ||
const absoluteImportPath = getAbsoluteImportPath(importPath, config) | ||
|
||
const absoluteSourcePath = getAbsoluteSourcePath(config.projectRoot, sourcePath) | ||
const relativeImportPath = relative(dirname(absoluteSourcePath), absoluteImportPath) | ||
|
||
path.node.source.value = './' + slash(relativeImportPath) | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
function extractConfig (state) { | ||
return { | ||
projectRoot: state.file.opts.sourceRoot || process.cwd(), | ||
prefix: state.opts.importPathPrefix || '~/', | ||
suffix: state.opts.projectPathSuffix || '' | ||
} | ||
} | ||
|
||
function unixifyPaths (config) { | ||
config.projectRoot = slash(config.projectRoot) | ||
config.suffix = slash(config.suffix) | ||
} | ||
|
||
function invariants (state) { | ||
if (typeof state.suffix !== 'string') { | ||
throw new Error('The projectPathSuffix provided is not a string') | ||
} | ||
|
||
if (typeof state.prefix !== 'string') { | ||
throw new Error('The projectPathSuffix provided is not a string') | ||
} | ||
} | ||
|
||
function isImportPathPrefixed (targetPath, prefix) { | ||
return (targetPath.lastIndexOf(prefix, 0) === 0) | ||
} | ||
|
||
function getAbsoluteImportPath (importPath, config) { | ||
const importPathWithoutPrefix = importPath.substring(config.prefix.length) | ||
const suffixedProjectPath = join(config.projectRoot, config.suffix) | ||
return join(suffixedProjectPath, importPathWithoutPrefix) | ||
} | ||
|
||
function getAbsoluteSourcePath (projectRoot, sourcePath) { | ||
// Some babel wrappers supply an absolute path already | ||
// so we need to check for that. | ||
if (isAbsolute(sourcePath)) { | ||
return sourcePath | ||
} else { | ||
return join(projectRoot, sourcePath) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.