aboutsummaryrefslogtreecommitdiff
path: root/node_modules/resolve-dependencies/lib/node-loader.js
blob: 1877de45daa33667cdea7471a64fa1b6657c84a2 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.load = exports.resolveSync = void 0;
const fs = __importStar(require("fs"));
const globby_1 = require("globby");
const path_1 = require("path");
const gather_deps_1 = require("./gather-deps");
const file_1 = require("./file");
const enhanced_resolve_1 = require("enhanced-resolve");
const { readFileSync, lstatSync, statSync, realpathSync } = fs;
const supportedJsExtensions = ['.js', '.cjs', '.mjs'];
const fileSystem = new enhanced_resolve_1.CachedInputFileSystem(fs, 4000), esmResolver = enhanced_resolve_1.ResolverFactory.createResolver({
    extensions: [],
    conditionNames: ['node', 'import', 'require', 'default'],
    useSyncFileSystemCalls: true,
    symlinks: false,
    fileSystem,
}), cjsResolver = enhanced_resolve_1.ResolverFactory.createResolver({
    extensions: ['.js', '.cjs', '.mjs', '.json', '.node'],
    conditionNames: ['node', 'require', 'default'],
    useSyncFileSystemCalls: true,
    symlinks: false,
    fileSystem,
}), defaultOptions = {
    loadContent: true,
    expand: 'none',
    isEntry: false,
    type: 'commonjs',
};
function stripControlCharacters(path) {
    return path && path.replace(/[\u0000-\u001F\u007F-\u009F]/g, '');
}
const emptyContext = {}, emptyResolveContext = {};
function resolveSync(from, request, resolver = cjsResolver) {
    const result = {
        absPath: '',
        pkgPath: '',
        pkg: null,
        warning: '',
    };
    resolver.resolve(emptyContext, from, request, emptyResolveContext, (err, path, data) => {
        if (err) {
            result.warning = err.message;
            return;
        }
        result.absPath = stripControlCharacters(path);
        result.pkgPath = stripControlCharacters(data.descriptionFilePath);
        result.pkg = data.descriptionFileData;
        return;
    });
    return result;
}
exports.resolveSync = resolveSync;
async function expand(file, fileDir, baseDir, globs) {
    const files = globby_1.sync(globs, {
        cwd: baseDir,
        followSymbolicLinks: false,
    });
    files
        .sort() //glob search is width first and not deterministic
        .map((dep) => file_1.ensureDottedRelative(fileDir, path_1.join(baseDir, dep)))
        .filter((relDep) => file.absPath !== path_1.join(baseDir, relDep))
        .forEach((relDep) => {
        file.deps[relDep] = file.deps[relDep] || null;
    });
    const currentDeps = Object.keys(file.deps);
    file.package &&
        file.package.dependencies &&
        Object.keys(file.package.dependencies || {}).forEach((dependency) => {
            if (!currentDeps.some((curDep) => curDep.startsWith(dependency))) {
                file.deps[dependency] = file.deps[dependency] || null;
            }
        });
}
function load(workingDirectory, request, options = defaultOptions) {
    var _a;
    const resolver = options.type === 'module' ? esmResolver : cjsResolver, { absPath, pkg, pkgPath, warning } = resolveSync(workingDirectory, request, resolver);
    if (!absPath) {
        return { warning: warning };
    }
    const file = file_1.createFile(absPath), isJs = options.isEntry || supportedJsExtensions.some((x) => absPath.endsWith(x));
    file.absPath = absPath;
    if (isJs || absPath.endsWith('json')) {
        file.contents = readFileSync(absPath, 'utf-8');
    }
    if (isJs) {
        try {
            const isModule = absPath.endsWith('.mjs') || !file_1.isScript(file.contents);
            const parseResult = gather_deps_1.gatherDependencies(file.contents, isModule);
            Object.assign(file.deps, parseResult.deps);
            file.moduleType = isModule ? 'module' : 'commonjs';
            file.variableImports = parseResult.variable;
        }
        catch (e) {
            return { warning: `Error parsing file: "${file.absPath}"\n${e.stack}` };
        }
    }
    const fileDir = path_1.dirname(file.absPath), expandVariable = Boolean(options.expand === 'variable' && file.variableImports);
    if (file_1.isNodeModule(request) && pkg && pkgPath) {
        file.package = pkg;
        file.deps[file_1.ensureDottedRelative(fileDir, pkgPath)] = null;
        const pkgDir = (file.moduleRoot = path_1.dirname(pkgPath)), expandAll = options.expand === 'all';
        if (expandVariable || expandAll) {
            expand(file, fileDir, pkgDir, file_1.nodeModuleGlobs(file));
            file.contextExpanded = true;
        }
        if (file_1.extraGlobs(file).length) {
            expand(file, fileDir, pkgDir, file_1.extraGlobs(file));
        }
    }
    else if (expandVariable && ((_a = options.context) === null || _a === void 0 ? void 0 : _a.moduleRoot) && !options.context.expanded) {
        expand(file, fileDir, options.context.moduleRoot, file_1.nodeModuleGlobs({ package: { files: options.context.globs } }));
        file.contextExpanded = true;
    }
    if (!options.loadContent) {
        file.contents = null;
    }
    const stats = lstatSync(file.absPath);
    if (stats.isSymbolicLink()) {
        const path = realpathSync(file.absPath);
        const absStat = statSync(file.absPath);
        file.realPath = path;
        file.realSize = absStat.size;
    }
    file.size = stats.size;
    return file;
}
exports.load = load;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibm9kZS1sb2FkZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvbm9kZS1sb2FkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7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