@pmmmwh/react-refresh-webpack-plugin
Advanced tools
Comparing version 0.5.12 to 0.5.13
@@ -65,7 +65,3 @@ // This is a patch for mozilla/source-map#349 - | ||
/** @type {'esm' | 'cjs'} */ | ||
let moduleSystem = 'cjs'; | ||
// Only try to resolve the module system if the environment is known to support ES syntax | ||
if (this.environment != null) { | ||
moduleSystem = await getModuleSystem.call(this, ModuleFilenameHelpers, options); | ||
} | ||
const moduleSystem = await getModuleSystem.call(this, ModuleFilenameHelpers, options); | ||
@@ -72,0 +68,0 @@ const RefreshSetupRuntime = RefreshSetupRuntimes[moduleSystem]; |
const { promises: fsPromises } = require('fs'); | ||
const path = require('path'); | ||
const commonPathPrefix = require('common-path-prefix'); | ||
const findUp = require('find-up'); | ||
@@ -46,25 +44,79 @@ /** @type {Map<string, string | undefined>} */ | ||
// Load users' `package.json` - | ||
// We will cache the results in a global variable so it will only be parsed once. | ||
let packageJsonType = packageJsonTypeMap.get(this.rootContext); | ||
if (!packageJsonType) { | ||
if (typeof this.addMissingDependency !== 'function') { | ||
// This is Webpack 4 which does not support `import.meta`. | ||
// We assume `.js` files are CommonJS because the output cannot be ESM anyway. | ||
return 'cjs'; | ||
} | ||
// We will assume CommonJS if we cannot determine otherwise | ||
let packageJsonType = ''; | ||
// We begin our search for relevant `package.json` files, | ||
// at the directory of the resource being loaded. | ||
// These paths should already be resolved, | ||
// but we resolve them again to ensure we are dealing with an aboslute path. | ||
const resourceContext = path.dirname(this.resourcePath); | ||
let searchPath = resourceContext; | ||
let previousSearchPath = ''; | ||
// We start our search just above the root context of the webpack compilation | ||
const stopPath = path.dirname(this.rootContext); | ||
// If the module context is a resolved symlink outside the `rootContext` path, | ||
// then we will never find the `stopPath` - so we also halt when we hit the root. | ||
// Note that there is a potential that the wrong `package.json` is found in some pathalogical cases, | ||
// such as a folder that is conceptually a package + does not have an ancestor `package.json`, | ||
// but there exists a `package.json` higher up. | ||
// This might happen if you have a folder of utility JS files that you symlink but did not organize as a package. | ||
// We consider this an unsupported edge case for now. | ||
while (searchPath !== stopPath && searchPath !== previousSearchPath) { | ||
// If we have already determined the `package.json` type for this path we can stop searching. | ||
// We do however still need to cache the found value, | ||
// from the `resourcePath` folder up to the matching `searchPath`, | ||
// to avoid retracing these steps when processing sibling resources. | ||
if (packageJsonTypeMap.has(searchPath)) { | ||
packageJsonType = packageJsonTypeMap.get(searchPath); | ||
let currentPath = resourceContext; | ||
while (currentPath !== searchPath) { | ||
// We set the found type at least level from `resourcePath` folder up to the matching `searchPath` | ||
packageJsonTypeMap.set(currentPath, packageJsonType); | ||
currentPath = path.dirname(currentPath); | ||
} | ||
break; | ||
} | ||
let packageJsonPath = path.join(searchPath, 'package.json'); | ||
try { | ||
const commonPath = commonPathPrefix([this.rootContext, this.resourcePath], '/'); | ||
const stopPath = path.resolve(commonPath, '..'); | ||
const packageSource = await fsPromises.readFile(packageJsonPath, 'utf-8'); | ||
try { | ||
const packageObject = JSON.parse(packageSource); | ||
const packageJsonPath = await findUp( | ||
(dir) => { | ||
if (dir === stopPath) return findUp.stop; | ||
return 'package.json'; | ||
}, | ||
{ cwd: path.dirname(this.resourcePath) } | ||
); | ||
// Any package.json is sufficient as long as it can be parsed. | ||
// If it does not explicitly have a `type: "module"` it will be assumed to be CommonJS. | ||
packageJsonType = typeof packageObject.type === 'string' ? packageObject.type : ''; | ||
packageJsonTypeMap.set(searchPath, packageJsonType); | ||
const buffer = await fsPromises.readFile(packageJsonPath, { encoding: 'utf-8' }); | ||
const rawPackageJson = buffer.toString('utf-8'); | ||
({ type: packageJsonType } = JSON.parse(rawPackageJson)); | ||
packageJsonTypeMap.set(this.rootContext, packageJsonType); | ||
// We set the type in the cache for all paths from the `resourcePath` folder, | ||
// up to the matching `searchPath` to avoid retracing these steps when processing sibling resources. | ||
let currentPath = resourceContext; | ||
while (currentPath !== searchPath) { | ||
packageJsonTypeMap.set(currentPath, packageJsonType); | ||
currentPath = path.dirname(currentPath); | ||
} | ||
} catch (e) { | ||
// `package.json` exists but could not be parsed. | ||
// We track it as a dependency so we can reload if this file changes. | ||
} | ||
this.addDependency(packageJsonPath); | ||
break; | ||
} catch (e) { | ||
// Failed to parse `package.json`, do nothing. | ||
// `package.json` does not exist. | ||
// We track it as a missing dependency so we can reload if this file is added. | ||
this.addMissingDependency(packageJsonPath); | ||
} | ||
// Try again at the next level up | ||
previousSearchPath = searchPath; | ||
searchPath = path.dirname(searchPath); | ||
} | ||
@@ -71,0 +123,0 @@ |
{ | ||
"name": "@pmmmwh/react-refresh-webpack-plugin", | ||
"version": "0.5.12", | ||
"version": "0.5.13", | ||
"description": "An **EXPERIMENTAL** Webpack plugin to enable \"Fast Refresh\" (also previously known as _Hot Reloading_) for React components.", | ||
@@ -60,6 +60,4 @@ "keywords": [ | ||
"ansi-html-community": "^0.0.8", | ||
"common-path-prefix": "^3.0.0", | ||
"core-js-pure": "^3.23.3", | ||
"error-stack-parser": "^2.0.6", | ||
"find-up": "^5.0.0", | ||
"html-entities": "^2.1.0", | ||
@@ -66,0 +64,0 @@ "loader-utils": "^2.0.4", |
15
3188
133242
58
+ Addedarray-union@2.1.0(transitive)
- Removedcommon-path-prefix@^3.0.0
- Removedfind-up@^5.0.0
- Removedcommon-path-prefix@3.0.0(transitive)
- Removedfind-up@5.0.0(transitive)
- Removedlocate-path@6.0.0(transitive)
- Removedp-limit@3.1.0(transitive)
- Removedp-locate@5.0.0(transitive)
- Removedpath-exists@4.0.0(transitive)
- Removedyocto-queue@0.1.0(transitive)