gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// returns the base-level package folder based on detecting "node_modules"
// package name boundaries
const pkgNameRegEx = /^(@[^\\\/]+[\\\/])?[^\\\/]+/;
function getPackageBase(id) {
  const pkgIndex = id.lastIndexOf('node_modules');
  if (pkgIndex !== -1 &&
      (id[pkgIndex - 1] === '/' || id[pkgIndex - 1] === '\\') &&
      (id[pkgIndex + 12] === '/' || id[pkgIndex + 12] === '\\')) {
    const pkgNameMatch = id.substr(pkgIndex + 13).match(pkgNameRegEx);
    if (pkgNameMatch)
      return id.substr(0, pkgIndex + 13 + pkgNameMatch[0].length);
  }
}
 
const emptyModules = { 'uglify-js': true, 'uglify-es': true };
 
module.exports = function (input, map) {
  const id = this.resourcePath;
  const pkgBase = getPackageBase(id);
  if (pkgBase) {
    const baseParts = pkgBase.split('/');
    if (baseParts[baseParts.length - 2] === 'node_modules') {
      const pkgName = baseParts[baseParts.length - 1];
      if (pkgName in emptyModules) {
        console.warn(`ncc: Ignoring build of ${pkgName}, as it is not statically analyzable. Build with "--external ${pkgName}" if this package is needed.`);
        return '';
      }
    }
  }
  this.callback(null, input, map);
};