-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: resolve node modules, closes #1
- Loading branch information
Showing
5 changed files
with
251 additions
and
11 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import colors from 'colorette' | ||
|
||
export function handlError(error: any) { | ||
if (error.frame) { | ||
console.error(colors.red(`Error parsing: ${error.loc.file}:${error.loc.line}:${error.loc.column}`)) | ||
console.error(colors.dim(error.frame)) | ||
} else { | ||
console.error(colors.red(error.stack)) | ||
} | ||
process.exitCode = 1 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {builtinModules} from 'module' | ||
import { dirname } from 'path' | ||
import { Plugin } from 'rollup' | ||
import r, { Opts as ResolveOpts } from 'resolve' | ||
|
||
const resolvePackage = (id: string, options: ResolveOpts): Promise<string> => | ||
new Promise((resolve, reject) => { | ||
r(id, options, (err, result) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
resolve(result) | ||
}) | ||
}) | ||
|
||
const PACKAGE_NAME_RE = /^[@a-z]/ | ||
|
||
export const resolvePlugin = (): Plugin => { | ||
return { | ||
name: 'resolve', | ||
|
||
async resolveId(source, importer) { | ||
// Always exclude builtin modules | ||
if (builtinModules.includes(source)) { | ||
return false | ||
} | ||
|
||
const cwd = importer && dirname(importer) | ||
if (cwd && PACKAGE_NAME_RE.test(source)) { | ||
const id = await resolvePackage(source, { basedir: cwd }) | ||
return id | ||
} | ||
return null | ||
}, | ||
} | ||
} |
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