flow like the river

This commit is contained in:
root 2025-11-07 00:06:12 +01:00
commit 013fe673f3
42435 changed files with 5764238 additions and 0 deletions

View file

@ -0,0 +1,171 @@
const types = require('babel-types');
const template = require('babel-template');
const traverse = require('babel-traverse').default;
const urlJoin = require('../utils/urlJoin');
const isURL = require('../utils/is-url');
const matchesPattern = require('./matches-pattern');
const requireTemplate = template('require("_bundle_loader")');
const argTemplate = template('require.resolve(MODULE)');
const serviceWorkerPattern = ['navigator', 'serviceWorker', 'register'];
module.exports = {
ImportDeclaration(node, asset) {
asset.isES6Module = true;
addDependency(asset, node.source);
},
ExportNamedDeclaration(node, asset) {
asset.isES6Module = true;
if (node.source) {
addDependency(asset, node.source);
}
},
ExportAllDeclaration(node, asset) {
asset.isES6Module = true;
addDependency(asset, node.source);
},
ExportDefaultDeclaration(node, asset) {
asset.isES6Module = true;
},
CallExpression(node, asset, ancestors) {
let {callee, arguments: args} = node;
let isRequire =
types.isIdentifier(callee) &&
callee.name === 'require' &&
args.length === 1 &&
types.isStringLiteral(args[0]) &&
!hasBinding(ancestors, 'require') &&
!isInFalsyBranch(ancestors);
if (isRequire) {
let optional = ancestors.some(a => types.isTryStatement(a)) || undefined;
addDependency(asset, args[0], {optional});
return;
}
let isDynamicImport =
callee.type === 'Import' &&
args.length === 1 &&
types.isStringLiteral(args[0]);
if (isDynamicImport) {
asset.addDependency('_bundle_loader');
addDependency(asset, args[0], {dynamic: true});
node.callee = requireTemplate().expression;
node.arguments[0] = argTemplate({MODULE: args[0]}).expression;
asset.isAstDirty = true;
return;
}
const isRegisterServiceWorker =
types.isStringLiteral(args[0]) &&
matchesPattern(callee, serviceWorkerPattern);
if (isRegisterServiceWorker) {
// Treat service workers as an entry point so filenames remain consistent across builds.
// https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#avoid_changing_the_url_of_your_service_worker_script
addURLDependency(asset, args[0], {entry: true});
return;
}
},
NewExpression(node, asset) {
const {callee, arguments: args} = node;
const isWebWorker =
callee.type === 'Identifier' &&
callee.name === 'Worker' &&
args.length === 1 &&
types.isStringLiteral(args[0]);
if (isWebWorker) {
addURLDependency(asset, args[0]);
return;
}
}
};
function hasBinding(node, name) {
if (Array.isArray(node)) {
return node.some(ancestor => hasBinding(ancestor, name));
} else if (
types.isProgram(node) ||
types.isBlockStatement(node) ||
types.isBlock(node)
) {
return node.body.some(statement => hasBinding(statement, name));
} else if (
types.isFunctionDeclaration(node) ||
types.isFunctionExpression(node) ||
types.isArrowFunctionExpression(node)
) {
return (
(node.id !== null && node.id.name === name) ||
node.params.some(
param => types.isIdentifier(param) && param.name === name
)
);
} else if (types.isVariableDeclaration(node)) {
return node.declarations.some(declaration => declaration.id.name === name);
}
return false;
}
function isInFalsyBranch(ancestors) {
// Check if any ancestors are if statements
return ancestors.some((node, index) => {
if (types.isIfStatement(node)) {
let res = evaluateExpression(node.test);
if (res && res.confident) {
// If the test is truthy, exclude the dep if it is in the alternate branch.
// If the test if falsy, exclude the dep if it is in the consequent branch.
let child = ancestors[index + 1];
return res.value ? child === node.alternate : child === node.consequent;
}
}
});
}
function evaluateExpression(node) {
// Wrap the node in a standalone program so we can traverse it
node = types.file(types.program([types.expressionStatement(node)]));
// Find the first expression and evaluate it.
let res = null;
traverse(node, {
Expression(path) {
res = path.evaluate();
path.stop();
}
});
return res;
}
function addDependency(asset, node, opts = {}) {
if (asset.options.target !== 'browser') {
const isRelativeImport = /^[/~.]/.test(node.value);
if (!isRelativeImport) return;
}
opts.loc = node.loc && node.loc.start;
asset.addDependency(node.value, opts);
}
function addURLDependency(asset, node, opts = {}) {
opts.loc = node.loc && node.loc.start;
let assetPath = asset.addURLDependency(node.value, opts);
if (!isURL(assetPath)) {
assetPath = urlJoin(asset.options.publicURL, assetPath);
}
node.value = assetPath;
asset.isAstDirty = true;
}

View file

@ -0,0 +1,28 @@
const types = require('babel-types');
const matchesPattern = require('./matches-pattern');
module.exports = {
MemberExpression(node, asset) {
// Inline environment variables accessed on process.env
if (matchesPattern(node.object, 'process.env')) {
let key = types.toComputedKey(node);
if (types.isStringLiteral(key)) {
let val = types.valueToNode(process.env[key.value]);
morph(node, val);
asset.isAstDirty = true;
asset.cacheData.env[key.value] = process.env[key.value];
}
}
}
};
// replace object properties
function morph(object, newProperties) {
for (let key in object) {
delete object[key];
}
for (let key in newProperties) {
object[key] = newProperties[key];
}
}

201
VISUALIZACION/node_modules/parcel-bundler/src/visitors/fs.js generated vendored Executable file
View file

@ -0,0 +1,201 @@
const t = require('babel-types');
const Path = require('path');
const fs = require('fs');
const template = require('babel-template');
const logger = require('../Logger');
const bufferTemplate = template('Buffer(CONTENT, ENC)');
module.exports = {
AssignmentExpression(path) {
if (!isRequire(path.node.right, 'fs', 'readFileSync')) {
return;
}
for (let name in path.getBindingIdentifiers()) {
const binding = path.scope.getBinding(name);
if (!binding) continue;
binding.path.setData('__require', path.node);
}
},
CallExpression(path, asset) {
// See https://github.com/defunctzombie/node-browser-resolve#skip
let ignore =
asset.package &&
asset.package.browser &&
asset.package.browser.fs === false;
if (!ignore && referencesImport(path, 'fs', 'readFileSync')) {
let vars = {
__dirname: Path.dirname(asset.name),
__filename: asset.basename
};
let filename, args, res;
try {
[filename, ...args] = path
.get('arguments')
.map(arg => evaluate(arg, vars));
filename = Path.resolve(filename);
res = fs.readFileSync(filename, ...args);
} catch (err) {
if (err instanceof NodeNotEvaluatedError) {
// Warn using a code frame
err.fileName = asset.name;
asset.generateErrorMessage(err);
logger.warn(err);
return;
}
// Add location info so we log a code frame with the error
err.loc =
path.node.arguments.length > 0
? path.node.arguments[0].loc.start
: path.node.loc.start;
throw err;
}
let replacementNode;
if (Buffer.isBuffer(res)) {
replacementNode = bufferTemplate({
CONTENT: t.stringLiteral(res.toString('base64')),
ENC: t.stringLiteral('base64')
});
} else {
replacementNode = t.stringLiteral(res);
}
asset.addDependency(filename, {includedInParent: true});
path.replaceWith(replacementNode);
asset.isAstDirty = true;
}
}
};
function isRequire(node, name, method) {
// e.g. require('fs').readFileSync
if (t.isMemberExpression(node) && node.property.name === method) {
node = node.object;
}
if (!t.isCallExpression(node)) {
return false;
}
let {callee, arguments: args} = node;
let isRequire =
t.isIdentifier(callee) &&
callee.name === 'require' &&
args.length === 1 &&
t.isStringLiteral(args[0]);
if (!isRequire) {
return false;
}
if (name && args[0].value !== name) {
return false;
}
return true;
}
function referencesImport(path, name, method) {
let callee = path.node.callee;
let bindingPath;
// e.g. readFileSync()
if (t.isIdentifier(callee)) {
bindingPath = getBindingPath(path, callee.name);
} else if (t.isMemberExpression(callee)) {
if (callee.property.name !== method) {
return false;
}
// e.g. fs.readFileSync()
if (t.isIdentifier(callee.object)) {
bindingPath = getBindingPath(path, callee.object.name);
// require('fs').readFileSync()
} else if (isRequire(callee.object, name)) {
return true;
}
} else {
return false;
}
if (!bindingPath) {
return;
}
let bindingNode = bindingPath.getData('__require') || bindingPath.node;
let parent = bindingPath.parentPath;
// e.g. import fs from 'fs';
if (parent.isImportDeclaration()) {
if (
bindingPath.isImportSpecifier() &&
bindingPath.node.imported.name !== method
) {
return false;
}
return parent.node.source.value === name;
// e.g. var fs = require('fs');
} else if (
t.isVariableDeclarator(bindingNode) ||
t.isAssignmentExpression(bindingNode)
) {
let left = bindingNode.id || bindingNode.left;
let right = bindingNode.init || bindingNode.right;
// e.g. var {readFileSync} = require('fs');
if (t.isObjectPattern(left)) {
let prop = left.properties.find(p => p.value.name === callee.name);
if (!prop || prop.key.name !== method) {
return false;
}
} else if (!t.isIdentifier(left)) {
return false;
}
return isRequire(right, name, method);
}
return false;
}
function getBindingPath(path, name) {
let binding = path.scope.getBinding(name);
return binding && binding.path;
}
function NodeNotEvaluatedError(node) {
this.message = 'Cannot statically evaluate fs argument';
this.node = node;
this.loc = node.loc.start;
}
function evaluate(path, vars) {
// Inline variables
path.traverse({
Identifier: function(ident) {
let key = ident.node.name;
if (key in vars) {
ident.replaceWith(t.valueToNode(vars[key]));
}
}
});
let res = path.evaluate();
if (!res.confident) {
throw new NodeNotEvaluatedError(path.node);
}
return res.value;
}

View file

@ -0,0 +1,56 @@
const Path = require('path');
const types = require('babel-types');
const VARS = {
process: asset => {
asset.addDependency('process');
return 'var process = require("process");';
},
global: () => 'var global = arguments[3];',
__dirname: asset =>
`var __dirname = ${JSON.stringify(Path.dirname(asset.name))};`,
__filename: asset => `var __filename = ${JSON.stringify(asset.name)};`,
Buffer: asset => {
asset.addDependency('buffer');
return 'var Buffer = require("buffer").Buffer;';
},
// Prevent AMD defines from working when loading UMD bundles.
// Ideally the CommonJS check would come before the AMD check, but many
// existing modules do the checks the opposite way leading to modules
// not exporting anything to Parcel.
define: () => 'var define;'
};
module.exports = {
Identifier(node, asset, ancestors) {
let parent = ancestors[ancestors.length - 2];
if (
VARS.hasOwnProperty(node.name) &&
!asset.globals.has(node.name) &&
types.isReferenced(node, parent)
) {
asset.globals.set(node.name, VARS[node.name](asset));
}
},
Declaration(node, asset, ancestors) {
// If there is a global declaration of one of the variables, remove our declaration
let identifiers = types.getBindingIdentifiers(node);
for (let id in identifiers) {
if (VARS.hasOwnProperty(id) && !inScope(ancestors)) {
// Don't delete entirely, so we don't add it again when the declaration is referenced
asset.globals.set(id, '');
}
}
}
};
function inScope(ancestors) {
for (let i = ancestors.length - 2; i >= 0; i--) {
if (types.isScope(ancestors[i]) && !types.isProgram(ancestors[i])) {
return true;
}
}
return false;
}

View file

@ -0,0 +1,36 @@
const types = require('babel-types');
// from babel-types. remove when we upgrade to babel 7.
// https://github.com/babel/babel/blob/0189b387026c35472dccf45d14d58312d249f799/packages/babel-types/src/index.js#L347
module.exports = function matchesPattern(member, match, allowPartial) {
// not a member expression
if (!types.isMemberExpression(member)) return false;
const parts = Array.isArray(match) ? match : match.split('.');
const nodes = [];
let node;
for (node = member; types.isMemberExpression(node); node = node.object) {
nodes.push(node.property);
}
nodes.push(node);
if (nodes.length < parts.length) return false;
if (!allowPartial && nodes.length > parts.length) return false;
for (let i = 0, j = nodes.length - 1; i < parts.length; i++, j--) {
const node = nodes[j];
let value;
if (types.isIdentifier(node)) {
value = node.name;
} else if (types.isStringLiteral(node)) {
value = node.value;
} else {
return false;
}
if (parts[i] !== value) return false;
}
return true;
};