Unverified Commit 0713ba4e authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Fix wildcard paths causing infinite resolving (#12318)

parent 61acc194
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
*/ */
import { ResolvePlugin } from 'webpack' import { ResolvePlugin } from 'webpack'
import { join } from 'path' import { join } from 'path'
import { debug } from 'next/dist/compiled/debug'
const log = debug('next:jsconfig-paths-plugin')
export interface Pattern { export interface Pattern {
prefix: string prefix: string
...@@ -28,6 +31,13 @@ export function hasZeroOrOneAsteriskCharacter(str: string): boolean { ...@@ -28,6 +31,13 @@ export function hasZeroOrOneAsteriskCharacter(str: string): boolean {
return true return true
} }
/**
* Determines whether a path starts with a relative path component (i.e. `.` or `..`).
*/
export function pathIsRelative(path: string): boolean {
return /^\.\.?($|[\\/])/.test(path)
}
export function tryParsePattern(pattern: string): Pattern | undefined { export function tryParsePattern(pattern: string): Pattern | undefined {
// This should be verified outside of here and a proper error thrown. // This should be verified outside of here and a proper error thrown.
const indexOfStar = pattern.indexOf('*') const indexOfStar = pattern.indexOf('*')
...@@ -131,8 +141,9 @@ export class JsConfigPathsPlugin implements ResolvePlugin { ...@@ -131,8 +141,9 @@ export class JsConfigPathsPlugin implements ResolvePlugin {
resolvedBaseUrl: string resolvedBaseUrl: string
constructor(paths: Paths, resolvedBaseUrl: string) { constructor(paths: Paths, resolvedBaseUrl: string) {
this.paths = paths this.paths = paths
this.resolvedBaseUrl = resolvedBaseUrl this.resolvedBaseUrl = resolvedBaseUrl
log('tsconfig.json or jsconfig.json paths: %O', paths)
log('resolved baseUrl: %s', resolvedBaseUrl)
} }
apply(resolver: any) { apply(resolver: any) {
const paths = this.paths const paths = this.paths
...@@ -140,6 +151,7 @@ export class JsConfigPathsPlugin implements ResolvePlugin { ...@@ -140,6 +151,7 @@ export class JsConfigPathsPlugin implements ResolvePlugin {
// If no aliases are added bail out // If no aliases are added bail out
if (pathsKeys.length === 0) { if (pathsKeys.length === 0) {
log('paths are empty, bailing out')
return return
} }
...@@ -150,16 +162,30 @@ export class JsConfigPathsPlugin implements ResolvePlugin { ...@@ -150,16 +162,30 @@ export class JsConfigPathsPlugin implements ResolvePlugin {
.tapPromise( .tapPromise(
'JsConfigPathsPlugin', 'JsConfigPathsPlugin',
async (request: any, resolveContext: any) => { async (request: any, resolveContext: any) => {
const moduleName = request.request
// Exclude node_modules from paths support (speeds up resolving) // Exclude node_modules from paths support (speeds up resolving)
if (request.path.match(NODE_MODULES_REGEX)) { if (request.path.match(NODE_MODULES_REGEX)) {
log('skipping request as it is inside node_modules %s', moduleName)
return return
} }
const moduleName = request.request if (moduleName.startsWith('/')) {
log('skipping request as it is an absolute path %s', moduleName)
return
}
if (pathIsRelative(moduleName)) {
log('skipping request as it is a relative path %s', moduleName)
return
}
// log('starting to resolve request %s', moduleName)
// If the module name does not match any of the patterns in `paths` we hand off resolving to webpack // If the module name does not match any of the patterns in `paths` we hand off resolving to webpack
const matchedPattern = matchPatternOrExact(pathsKeys, moduleName) const matchedPattern = matchPatternOrExact(pathsKeys, moduleName)
if (!matchedPattern) { if (!matchedPattern) {
log('moduleName did not match any paths pattern %s', moduleName)
return return
} }
...@@ -181,7 +207,7 @@ export class JsConfigPathsPlugin implements ResolvePlugin { ...@@ -181,7 +207,7 @@ export class JsConfigPathsPlugin implements ResolvePlugin {
} }
const candidate = join(baseDirectory, path) const candidate = join(baseDirectory, path)
const [err, result] = await new Promise((resolve, reject) => { const [err, result] = await new Promise(resolve => {
const obj = Object.assign({}, request, { const obj = Object.assign({}, request, {
request: candidate, request: candidate,
}) })
......
This source diff could not be displayed because it is too large. You can view the blob instead.
(The MIT License)
Copyright (c) 2014 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the 'Software'), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This diff is collapsed.
{"name":"debug","main":"index.js","author":"TJ Holowaychuk <tj@vision-media.ca>","license":"MIT"}
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -121,6 +121,7 @@ ...@@ -121,6 +121,7 @@
"@types/content-type": "1.1.3", "@types/content-type": "1.1.3",
"@types/cookie": "0.3.3", "@types/cookie": "0.3.3",
"@types/cross-spawn": "6.0.0", "@types/cross-spawn": "6.0.0",
"@types/debug": "4.1.5",
"@types/dotenv": "8.2.0", "@types/dotenv": "8.2.0",
"@types/etag": "1.8.0", "@types/etag": "1.8.0",
"@types/fresh": "0.5.0", "@types/fresh": "0.5.0",
...@@ -161,6 +162,7 @@ ...@@ -161,6 +162,7 @@
"content-type": "1.0.4", "content-type": "1.0.4",
"cookie": "0.4.0", "cookie": "0.4.0",
"cssnano-simple": "1.0.1", "cssnano-simple": "1.0.1",
"debug": "4.1.1",
"devalue": "2.0.1", "devalue": "2.0.1",
"dotenv": "8.2.0", "dotenv": "8.2.0",
"dotenv-expand": "5.1.0", "dotenv-expand": "5.1.0",
......
...@@ -187,6 +187,14 @@ export async function ncc_cssnano_simple(task, opts) { ...@@ -187,6 +187,14 @@ export async function ncc_cssnano_simple(task, opts) {
.target('compiled/cssnano-simple') .target('compiled/cssnano-simple')
} }
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
externals['debug'] = 'next/dist/compiled/debug'
export async function ncc_debug(task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('debug')))
.ncc({ packageName: 'debug', externals })
.target('compiled/debug')
}
// eslint-disable-next-line camelcase
externals['devalue'] = 'next/dist/compiled/devalue' externals['devalue'] = 'next/dist/compiled/devalue'
export async function ncc_devalue(task, opts) { export async function ncc_devalue(task, opts) {
await task await task
...@@ -558,6 +566,7 @@ export async function ncc(task) { ...@@ -558,6 +566,7 @@ export async function ncc(task) {
'ncc_content_type', 'ncc_content_type',
'ncc_cookie', 'ncc_cookie',
'ncc_cssnano_simple', 'ncc_cssnano_simple',
'ncc_debug',
'ncc_devalue', 'ncc_devalue',
'ncc_dotenv', 'ncc_dotenv',
'ncc_dotenv_expand', 'ncc_dotenv_expand',
......
...@@ -75,6 +75,10 @@ declare module 'next/dist/compiled/cssnano-simple' { ...@@ -75,6 +75,10 @@ declare module 'next/dist/compiled/cssnano-simple' {
import m from 'cssnano-simple' import m from 'cssnano-simple'
export = m export = m
} }
declare module 'next/dist/compiled/debug' {
import m from 'debug'
export = m
}
declare module 'next/dist/compiled/devalue' { declare module 'next/dist/compiled/devalue' {
import m from 'devalue' import m from 'devalue'
export = m export = m
......
!node_modules
\ No newline at end of file
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
"paths": { "paths": {
"@c/*": ["components/*"], "@c/*": ["components/*"],
"@lib/*": ["lib/a/*", "lib/b/*"], "@lib/*": ["lib/a/*", "lib/b/*"],
"@mycomponent": ["components/hello.js"] "@mycomponent": ["components/hello.js"],
"*": ["node_modules/*"]
} }
} }
} }
import myfile from 'mypackage/myfile'
export default function WildcardAlias() {
return <div>{myfile.hello}</div>
}
...@@ -59,6 +59,11 @@ describe('TypeScript Features', () => { ...@@ -59,6 +59,11 @@ describe('TypeScript Features', () => {
expect($('body').text()).toMatch(/Hello/) expect($('body').text()).toMatch(/Hello/)
}) })
it('should resolve a single matching alias', async () => {
const $ = await get$('/wildcard-alias')
expect($('body').text()).toMatch(/world/)
})
it('should have correct module not found error', async () => { it('should have correct module not found error', async () => {
const basicPage = join(appDir, 'pages/basic-alias.js') const basicPage = join(appDir, 'pages/basic-alias.js')
const contents = await fs.readFile(basicPage, 'utf8') const contents = await fs.readFile(basicPage, 'utf8')
......
...@@ -2395,6 +2395,11 @@ ...@@ -2395,6 +2395,11 @@
dependencies: dependencies:
"@types/node" "*" "@types/node" "*"
"@types/debug@4.1.5":
version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
integrity sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==
"@types/dotenv@8.2.0": "@types/dotenv@8.2.0":
version "8.2.0" version "8.2.0"
resolved "https://registry.yarnpkg.com/@types/dotenv/-/dotenv-8.2.0.tgz#5cd64710c3c98e82d9d15844375a33bf1b45d053" resolved "https://registry.yarnpkg.com/@types/dotenv/-/dotenv-8.2.0.tgz#5cd64710c3c98e82d9d15844375a33bf1b45d053"
...@@ -5779,6 +5784,13 @@ debug@3.1.0: ...@@ -5779,6 +5784,13 @@ debug@3.1.0:
dependencies: dependencies:
ms "2.0.0" ms "2.0.0"
debug@4.1.1, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
dependencies:
ms "^2.1.1"
debug@^3.0.0, debug@^3.1.0, debug@^3.2.6: debug@^3.0.0, debug@^3.1.0, debug@^3.2.6:
version "3.2.6" version "3.2.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b" resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
...@@ -5786,13 +5798,6 @@ debug@^3.0.0, debug@^3.1.0, debug@^3.2.6: ...@@ -5786,13 +5798,6 @@ debug@^3.0.0, debug@^3.1.0, debug@^3.2.6:
dependencies: dependencies:
ms "^2.1.1" ms "^2.1.1"
debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
dependencies:
ms "^2.1.1"
debug@~0.8.1: debug@~0.8.1:
version "0.8.1" version "0.8.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-0.8.1.tgz#20ff4d26f5e422cb68a1bacbbb61039ad8c1c130" resolved "https://registry.yarnpkg.com/debug/-/debug-0.8.1.tgz#20ff4d26f5e422cb68a1bacbbb61039ad8c1c130"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment