- Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
enhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomers
Description
Currently we only support relative paths in import(), but the only reason for this is because I'm lazy. Since it happens at build time, let's just throw a package at it.
The path resolution happens here (just before readFileSync) and it's just some regular Node.js functions, nothing fancy. Also importeeRelativePath is not necessarily relative, it's whatever we find as the first argument of import().
use-imported-hook/src/read-importee.js
Lines 74 to 83 in 1fbe423
| function readImportee(importerAbsolutePath, importeeRelativePath, state, parentPath) { | |
| const directory = nodePath.dirname(importerAbsolutePath) | |
| const fileAbsolutePath = nodePath.join(directory, importeeRelativePath) | |
| const codeString = fs.readFileSync(fileAbsolutePath, {encoding: 'utf8'}) | |
| const ast = parser.parse(codeString, { | |
| sourceType: 'module', | |
| plugins: ['jsx'], | |
| }) | |
| traverse(ast, ExtractHooksFromImporteeVisitor, undefined, state, parentPath); | |
| } |
Maybe look into this babel-plugin-module-resolver package?
Basic of paths we need to support:
-
./folder/file.jsxrelative -
/folder/file.jsxabsolute from project root -
./folder/namewithout extension, can mean name.js, name.jsx, name/index.js, ...
And also aliased modules:
-
some-module/file.jsxaliased from node_modules -
@alias/file.jsxaliased from project config
If we're to support aliased imports, we'll need to get a pointer to a config file passed as an option, like a webpack.config.js or a .babelrc.js
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomers