next-transpile-modules
Advanced tools
Comparing version 5.0.0 to 6.0.0
{ | ||
"name": "next-transpile-modules", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"license": "MIT", | ||
@@ -43,3 +43,4 @@ "author": "Pierre de la Martinière <pierre.de.la.martiniere@gmail.com>", | ||
"dependencies": { | ||
"enhanced-resolve": "^5.3.2" | ||
"enhanced-resolve": "^5.3.2", | ||
"pkg-dir": "^5.0.0" | ||
}, | ||
@@ -46,0 +47,0 @@ "devDependencies": { |
@@ -29,3 +29,3 @@ # Next.js + Transpile `node_modules` | ||
| ----------------- | -------------- | | ||
| Next.js 9.5+ / 10 | 4.x, 5.x | | ||
| Next.js 9.5+ / 10 | 4.x, 5.x, 6.x | | ||
| Next.js 9.2 | 3.x | | ||
@@ -77,5 +77,7 @@ | Next.js 8 / 9 | 2.x | | ||
**note:** please declare `withTM` as your last plugin (the "most nested" one). | ||
**Notes:** | ||
**note 2:** make sure all your packages have [a valid `main` field](https://docs.npmjs.com/cli/v6/configuring-npm/package-json#main). | ||
- please declare `withTM` as your last plugin (the "most nested" one). | ||
- make sure all your packages have [a valid `main` field](https://docs.npmjs.com/cli/v6/configuring-npm/package-json#main). | ||
- there is currently no way to transpile only parts of a package, it's all or nothing | ||
@@ -82,0 +84,0 @@ ### Scoped packages |
const path = require('path'); | ||
const process = require('process'); | ||
const enhancedResolve = require('enhanced-resolve'); | ||
const pkgDir = require('pkg-dir'); | ||
@@ -11,5 +13,7 @@ // Use me when needed | ||
const CWD = process.cwd(); | ||
/** | ||
* We create our own Node.js resolver that can ignore symlinks resolution and | ||
* can support PnP | ||
* Our own Node.js resolver that can ignore symlinks resolution and can support | ||
* PnP | ||
*/ | ||
@@ -41,29 +45,45 @@ const resolve = enhancedResolve.create.sync({ | ||
/** | ||
* Resolve modules to their real paths | ||
* @param {string[]} modules | ||
* Return the root path (package.json directory) of a given module | ||
* @param {string} module | ||
*/ | ||
const generateResolvedModules = (modules) => { | ||
const resolvedModules = modules | ||
.map((module) => { | ||
let resolved; | ||
const getPackageRootDirectory = (module) => { | ||
let packageDirectory; | ||
let packageRootDirectory; | ||
try { | ||
resolved = resolve(process.cwd(), module); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
try { | ||
// Get the module path | ||
packageDirectory = resolve(CWD, module); | ||
if (!resolved) | ||
throw new Error( | ||
`next-transpile-modules: could not resolve module "${module}". Are you sure the name of the module you are trying to transpile is correct?` | ||
); | ||
if (!packageDirectory) { | ||
throw new Error( | ||
`next-transpile-modules - could not resolve module "${module}". Are you sure the name of the module you are trying to transpile is correct?` | ||
); | ||
} | ||
return resolved; | ||
}) | ||
.map(path.dirname); | ||
try { | ||
// Get the location of its package.json | ||
packageRootDirectory = pkgDir.sync(packageDirectory); | ||
} catch (err) { | ||
throw new Error( | ||
`next-transpile-modules - an error happened when trying to get the root directory of "${module}". Is it missing a package.json?\n${err}` | ||
); | ||
} | ||
} catch (err) { | ||
throw new Error(`next-transpile-modules - an unexpected error happened when trying to resolve "${module}"\n${err}`); | ||
} | ||
return resolvedModules; | ||
return packageRootDirectory; | ||
}; | ||
/** | ||
* Resolve modules to their real paths | ||
* @param {string[]} modules | ||
*/ | ||
const generateModulesPaths = (modules) => { | ||
const packagesPaths = modules.map(getPackageRootDirectory); | ||
return packagesPaths; | ||
}; | ||
/** | ||
* Logger for the debug mode | ||
@@ -92,7 +112,7 @@ */ | ||
const resolvedModules = generateResolvedModules(modules); | ||
const modulesPaths = generateModulesPaths(modules); | ||
if (isWebpack5) logger(`WARNING experimental Webpack 5 support enabled`, true); | ||
logger(`the following paths will get transpiled:\n${resolvedModules.map((mod) => ` - ${mod}`).join('\n')}`); | ||
logger(`the following paths will get transpiled:\n${modulesPaths.map((mod) => ` - ${mod}`).join('\n')}`); | ||
@@ -102,5 +122,5 @@ // Generate Webpack condition for the passed modules | ||
const match = (path) => | ||
resolvedModules.some((modulePath) => { | ||
modulesPaths.some((modulePath) => { | ||
const transpiled = path.includes(modulePath); | ||
logger(`${transpiled} ${path}`); | ||
if (transpiled) logger(`transpiled: ${path}`); | ||
return transpiled; | ||
@@ -125,11 +145,11 @@ }); | ||
const hasInclude = (context, request) => { | ||
const test = resolvedModules.some((mod) => { | ||
const test = modulesPaths.some((mod) => { | ||
// If we the code requires/import an absolute path | ||
if (!request.startsWith('.')) { | ||
try { | ||
const resolved = resolve(process.cwd(), request); | ||
const moduleDirectory = getPackageRootDirectory(request); | ||
if (!resolved) return false; | ||
if (!moduleDirectory) return false; | ||
return resolved.includes(mod); | ||
return moduleDirectory.includes(mod); | ||
} catch (err) { | ||
@@ -204,3 +224,3 @@ return false; | ||
} else { | ||
console.warn('next-transpile-modules: could not find default CSS rule, CSS imports may not work'); | ||
console.warn('next-transpile-modules - could not find default CSS rule, CSS imports may not work'); | ||
} | ||
@@ -213,3 +233,3 @@ | ||
} else { | ||
console.warn('next-transpile-modules: could not find default SASS rule, SASS imports may not work'); | ||
console.warn('next-transpile-modules - could not find default SASS rule, SASS imports may not work'); | ||
} | ||
@@ -216,0 +236,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23030
210
289
2
+ Addedpkg-dir@^5.0.0
+ Addedfind-up@5.0.0(transitive)
+ Addedlocate-path@6.0.0(transitive)
+ Addedp-limit@3.1.0(transitive)
+ Addedp-locate@5.0.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedpkg-dir@5.0.0(transitive)
+ Addedyocto-queue@0.1.0(transitive)