Skip to content

Commit

Permalink
use cjs form for webpack4 because it does not support import.meta. …
Browse files Browse the repository at this point in the history
…Instead of feature testing with `this.environment` which was added to webpack 5 relatively recently use `typeof this.addMissingDepdencey === 'function'` which is in most (or all) webpack 5 releases but not included in webpack 4.
  • Loading branch information
gnoff authored and pmmmwh committed Dec 14, 2023
1 parent 981821e commit 9769038
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions loader/utils/getModuleSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ async function getModuleSystem(ModuleFilenameHelpers, options) {
if (/\.mjs$/.test(this.resourcePath)) return 'esm';
if (/\.cjs$/.test(this.resourcePath)) return 'cjs';

if (typeof this.addMissingDependency !== 'function') {
// This is Webpack 4 which does not support `import.meta` and cannot use ESM anwyay. 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 = '';

Expand Down Expand Up @@ -104,10 +110,7 @@ async function getModuleSystem(ModuleFilenameHelpers, options) {
} catch (e) {
// package.json does not exist. We track it as a missing dependency so we can reload if this
// file is added
if (typeof this.addMissingDependency === 'function') {
// Webpack 4 does not have this method
this.addMissingDependency(packageJsonPath);
}
this.addMissingDependency(packageJsonPath);
}

// try again at the next level up
Expand Down

0 comments on commit 9769038

Please sign in to comment.