-
Notifications
You must be signed in to change notification settings - Fork 204
Feat: warn when the package from resolved alias is not available #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
501b839
fa481cd
3e15eec
b42ceb2
c55a2d0
fda055b
024dcb1
81d528d
da1fcd9
1b4f87f
c59bcee
040bb1c
7c71d3e
ce36607
5b1b900
3caaaa1
c0baaa0
66bc9c6
dc01d6d
8e55566
647ce41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // This module exists only for abstracting logging away and making testing easier | ||
|
|
||
| // eslint-disable-next-line import/prefer-default-export | ||
| export function warn(...args) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn(...args); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import path from 'path'; | |
|
|
||
| import findBabelConfig from 'find-babel-config'; | ||
| import glob from 'glob'; | ||
| import { warn } from './log'; | ||
|
|
||
|
|
||
| const defaultExtensions = ['.js', '.jsx', '.es', '.es6']; | ||
|
|
@@ -51,29 +52,46 @@ function normalizeRoot(opts) { | |
| } | ||
| } | ||
|
|
||
| function normalizeAlias(opts) { | ||
| opts.regExps = []; | ||
| function getAliasPair(key, value) { | ||
| const parts = value.split('\\\\'); | ||
|
|
||
| function substitute(execResult) { | ||
| return parts | ||
| .map(part => | ||
| part.replace(/\\\d+/g, number => execResult[number.slice(1)] || ''), | ||
| ) | ||
| .join('\\'); | ||
| } | ||
|
|
||
| return [new RegExp(key), substitute]; | ||
| } | ||
|
|
||
| function normalizeAlias(opts) { | ||
| if (opts.alias) { | ||
| Object.keys(opts.alias) | ||
| .filter(isRegExp) | ||
| .forEach((key) => { | ||
| const parts = opts.alias[key].split('\\\\'); | ||
|
|
||
| function substitute(execResult) { | ||
| return parts | ||
| .map(part => | ||
| part.replace(/\\\d+/g, number => execResult[number.slice(1)] || ''), | ||
| ) | ||
| .join('\\'); | ||
| const { alias } = opts; | ||
| const aliasKeys = Object.keys(alias); | ||
| let warnedAboutNpmPrefix = false; | ||
|
|
||
| const nonRegExpAliases = aliasKeys | ||
| .filter(key => !isRegExp(key)) | ||
| .map((key) => { | ||
| let value = alias[key]; | ||
|
|
||
| if (value.startsWith('npm:') && !warnedAboutNpmPrefix) { | ||
| warnedAboutNpmPrefix = true; | ||
| warn('The "npm:" prefix in an alias is deprecated and will be removed in the next major version release.'); | ||
| value = value.slice('npm:'.length); | ||
| } | ||
| return getAliasPair(`^${key}((?:/|).*)`, `${value}\\1`); | ||
| }); | ||
|
|
||
| opts.regExps.push([new RegExp(key), substitute]); | ||
| const regExpAliases = aliasKeys | ||
| .filter(isRegExp) | ||
| .map(key => getAliasPair(key, alias[key])); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be best to do both case in the same
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, will join those cases. |
||
|
|
||
| delete opts.alias[key]; | ||
| }); | ||
| opts.alias = [...nonRegExpAliases, ...regExpAliases]; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of this? opts.alias = aliasKeys.reduce((acc, key) => {
acc.push(
isRegExp(key)
? getAliasPair(key, alias[key])
: getAliasPair(`^${key}((?:/|).*)`, `${alias[key]}\\1`)
);
return acc;
}, []);
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to preserve the order: standard aliases before regexps. At this point I think it could go either way, as the order should be taken from the object. Note: according to the spec, the order of enumerating the object keys is not guaranteed to be the same as in the literal, so in the future it may be needed to allow an array-type declaration. We're currently relying on V8.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, one or the other, we'll have an issue if a user expects the order to be kept. With an object, it's not guaranteed as you say. We'll figure out a way later if really needed. For now, it doesn't seem like users need it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll go with the single array then.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (btw.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. |
||
| } else { | ||
| opts.alias = {}; | ||
| opts.alias = []; | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets drop this for good :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you say so :) I'm just worried about breaking some more configs :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be a 3.0 so no problem removing the deprecated things :) Especially since there's a way to do it without using the prefix.