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,341 @@
'use strict';
let babelTransform = (() => {
var _ref = _asyncToGenerator(function* (asset) {
let config = yield getConfig(asset);
if (!config) {
return;
}
yield asset.parseIfNeeded();
// If this is an internally generated config, use our internal babel-core,
// otherwise require a local version from the package we're compiling.
let babel = config.internal ? require('babel-core') : yield localRequire('babel-core', asset.name);
// TODO: support other versions of babel
if (parseInt(babel.version, 10) !== 6) {
throw new Error(`Unsupported babel version: ${babel.version}`);
}
let res = babel.transformFromAst(asset.ast, asset.contents, config);
if (!res.ignored) {
asset.ast = res.ast;
asset.isAstDirty = true;
}
});
return function babelTransform(_x) {
return _ref.apply(this, arguments);
};
})();
let getConfig = (() => {
var _ref2 = _asyncToGenerator(function* (asset) {
let config = yield getBabelConfig(asset);
if (config) {
config.code = false;
config.filename = asset.name;
config.babelrc = false;
// Hide the internal property from babel
let internal = config.internal;
delete config.internal;
Object.defineProperty(config, 'internal', {
value: internal,
configurable: true
});
}
return config;
});
return function getConfig(_x2) {
return _ref2.apply(this, arguments);
};
})();
let getBabelConfig = (() => {
var _ref3 = _asyncToGenerator(function* (asset) {
// If asset is marked as an ES6 modules, this is a second pass after dependencies are extracted.
// Just compile modules to CommonJS.
if (asset.isES6Module) {
return {
internal: true,
plugins: [require('babel-plugin-transform-es2015-modules-commonjs')]
};
}
if (asset.babelConfig) {
return asset.babelConfig;
}
// Consider the module source code rather than precompiled if the resolver
// used the `source` field, or it is not in node_modules.
let isSource = !!(asset.package && asset.package.source) || !asset.name.includes(NODE_MODULES);
// Try to resolve a .babelrc file. If one is found, consider the module source code.
let babelrc = yield getBabelRc(asset, isSource);
isSource = isSource || !!babelrc;
let envConfig = yield getEnvConfig(asset, isSource);
let jsxConfig = getJSXConfig(asset, isSource);
// Merge the babel-preset-env config and the babelrc if needed
if (babelrc && !shouldIgnoreBabelrc(asset.name, babelrc)) {
if (envConfig) {
// Filter out presets that are already applied by babel-preset-env
if (Array.isArray(babelrc.presets)) {
babelrc.presets = babelrc.presets.filter(function (preset) {
return !ENV_PRESETS[getPluginName(preset)];
});
}
// Filter out plugins that are already applied by babel-preset-env
if (Array.isArray(babelrc.plugins)) {
babelrc.plugins = babelrc.plugins.filter(function (plugin) {
return !ENV_PLUGINS[getPluginName(plugin)];
});
}
// Add plugins generated by babel-preset-env to get to the app's target engines.
mergeConfigs(babelrc, envConfig);
}
// Add JSX config if it isn't already specified in the babelrc
let hasReact = hasPlugin(babelrc.presets, 'react') || hasPlugin(babelrc.plugins, 'transform-react-jsx');
if (!hasReact) {
mergeConfigs(babelrc, jsxConfig);
}
return babelrc;
}
// If there is a babel-preset-env config, and it isn't empty use that
if (envConfig && (envConfig.plugins.length > 0 || jsxConfig)) {
mergeConfigs(envConfig, jsxConfig);
return envConfig;
}
// If there is a JSX config, return that
if (jsxConfig) {
return jsxConfig;
}
// Otherwise, don't run babel at all
return null;
});
return function getBabelConfig(_x3) {
return _ref3.apply(this, arguments);
};
})();
/**
* Finds a .babelrc for an asset. By default, .babelrc files inside node_modules are not used.
* However, there are some exceptions:
* - if `browserify.transforms` includes "babelify" in package.json (for legacy module compat)
* - the `source` field in package.json is used by the resolver
*/
let getBabelRc = (() => {
var _ref4 = _asyncToGenerator(function* (asset, isSource) {
// Support legacy browserify packages
let browserify = asset.package && asset.package.browserify;
if (browserify && Array.isArray(browserify.transform)) {
// Look for babelify in the browserify transform list
let babelify = browserify.transform.find(function (t) {
return (Array.isArray(t) ? t[0] : t) === 'babelify';
});
// If specified as an array, override the config with the one specified
if (Array.isArray(babelify) && babelify[1]) {
return babelify[1];
}
// Otherwise, return the .babelrc if babelify was found
return babelify ? yield findBabelRc(asset) : null;
}
// If this asset is not in node_modules, always use the .babelrc
if (isSource) {
return yield findBabelRc(asset);
}
// Otherwise, don't load .babelrc for node_modules.
// See https://github.com/parcel-bundler/parcel/issues/13.
return null;
});
return function getBabelRc(_x4, _x5) {
return _ref4.apply(this, arguments);
};
})();
let findBabelRc = (() => {
var _ref5 = _asyncToGenerator(function* (asset) {
if (asset.package && asset.package.babel) {
return asset.package.babel;
}
return yield asset.getConfig(['.babelrc', '.babelrc.js']);
});
return function findBabelRc(_x6) {
return _ref5.apply(this, arguments);
};
})();
/**
* Generates a babel-preset-env config for an asset.
* This is done by finding the source module's target engines, and the app's
* target engines, and doing a diff to include only the necessary plugins.
*/
let getEnvConfig = (() => {
var _ref6 = _asyncToGenerator(function* (asset, isSourceModule) {
// Load the target engines for the app and generate a babel-preset-env config
let targetEngines = yield getTargetEngines(asset, true);
let targetEnv = yield getEnvPlugins(targetEngines, true);
if (!targetEnv) {
return null;
}
// If this is the app module, the source and target will be the same, so just compile everything.
// Otherwise, load the source engines and generate a babel-present-env config.
if (!isSourceModule) {
let sourceEngines = yield getTargetEngines(asset, false);
let sourceEnv = (yield getEnvPlugins(sourceEngines, false)) || targetEnv;
// Do a diff of the returned plugins. We only need to process the remaining plugins to get to the app target.
let sourcePlugins = new Set(sourceEnv.map(function (p) {
return p[0];
}));
targetEnv = targetEnv.filter(function (plugin) {
return !sourcePlugins.has(plugin[0]);
});
}
return { plugins: targetEnv, internal: true };
});
return function getEnvConfig(_x7, _x8) {
return _ref6.apply(this, arguments);
};
})();
let getEnvPlugins = (() => {
var _ref7 = _asyncToGenerator(function* (targets, useBuiltIns = false) {
if (!targets) {
return null;
}
let key = JSON.stringify(targets);
if (envCache.has(key)) {
return envCache.get(key);
}
let plugins = presetEnv.default({}, { targets, modules: false, useBuiltIns: useBuiltIns ? 'entry' : false }).plugins;
envCache.set(key, plugins);
return plugins;
});
return function getEnvPlugins(_x9) {
return _ref7.apply(this, arguments);
};
})();
/**
* Generates a babel config for JSX. Attempts to detect react or react-like libraries
* and changes the pragma accordingly.
*/
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const presetEnv = require('babel-preset-env');
const getTargetEngines = require('../utils/getTargetEngines');
const localRequire = require('../utils/localRequire');
const path = require('path');
var _require = require('babel-core');
const babelUtils = _require.util;
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
const ENV_PLUGINS = require('babel-preset-env/data/plugins');
const ENV_PRESETS = {
es2015: true,
es2016: true,
es2017: true,
latest: true,
env: true
};
const JSX_EXTENSIONS = {
'.jsx': true,
'.tsx': true
};
const JSX_PRAGMA = {
react: 'React.createElement',
preact: 'h',
nervjs: 'Nerv.createElement',
hyperapp: 'h'
};
module.exports = babelTransform;
babelTransform.getConfig = getConfig;
function mergeConfigs(a, b) {
if (b) {
a.presets = (a.presets || []).concat(b.presets || []);
a.plugins = (a.plugins || []).concat(b.plugins || []);
}
return a;
}
function hasPlugin(arr, plugin) {
return Array.isArray(arr) && arr.some(p => getPluginName(p) === plugin);
}
function getPluginName(p) {
return Array.isArray(p) ? p[0] : p;
}
function shouldIgnoreBabelrc(filename, babelrc) {
// Determine if we should ignore this babelrc file. We do this here instead of
// letting babel-core handle it because this config might be merged with our
// autogenerated one later which shouldn't be ignored.
let ignore = babelUtils.arrayify(babelrc.ignore, babelUtils.regexify);
let only = babelrc.only && babelUtils.arrayify(babelrc.only, babelUtils.regexify);
return babelUtils.shouldIgnore(filename, ignore, only);
}
const envCache = new Map();
function getJSXConfig(asset, isSourceModule) {
// Don't enable JSX in node_modules
if (!isSourceModule) {
return null;
}
// Find a dependency that we can map to a JSX pragma
let pragma = null;
for (let dep in JSX_PRAGMA) {
let pkg = asset.package;
if (pkg && (pkg.dependencies && pkg.dependencies[dep] || pkg.devDependencies && pkg.devDependencies[dep])) {
pragma = JSX_PRAGMA[dep];
break;
}
}
if (pragma || JSX_EXTENSIONS[path.extname(asset.name)]) {
return {
plugins: [[require('babel-plugin-transform-react-jsx'), { pragma }]],
internal: true
};
}
}

View file

@ -0,0 +1,218 @@
'use strict';
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
const babylon = require('babylon');
const t = require('babel-types');
const traverse = require('babel-traverse').default;
const generate = require('babel-generator').default;
const EXPORTS_RE = /^\$([\d]+)\$exports$/;
const EXPORT_RE = /^\$([\d]+)\$export\$(.+)$/;
// TODO: minify
// TODO: source-map
module.exports = (code, exports, moduleMap, wildcards) => {
let ast = babylon.parse(code);
let addedExports = new Set();
let commentedBindings = new Set();
let resolveModule = (id, name) => {
let module = moduleMap.get(id);
return module.depAssets.get(module.dependencies.get(name)).id;
};
function replaceExportNode(id, name, path) {
path = getOuterStatement(path);
let node = tail(id, id => `$${id}$export$${name}`);
if (!node) {
// if there is no named export then lookup for a CommonJS export
node = tail(id, id => `$${id}$exports`) || t.identifier(`$${id}$exports`);
// if there is a CommonJS export return $id$exports.name
if (node) {
return t.memberExpression(node, t.identifier(name));
}
}
return node;
function tail(id, symbol) {
let computedSymbol = symbol(id);
// if the symbol is in the scope there is not need to remap it
if (path.scope.hasBinding(computedSymbol)) {
return t.identifier(computedSymbol);
}
if (exports.has(computedSymbol)) {
return t.identifier(exports.get(computedSymbol));
}
// if there is a wildcard for the module
// default exports are excluded from wildcard exports
if (wildcards.has(id) && name !== 'default') {
/* recursively lookup the symbol
* this is needed when there is deep export wildcards, like in the following:
* - a.js
* > export * from './b'
* - b.js
* > export * from './c'
* - c.js in es6
* > export * from 'lodash'
* - c.js in cjs
* > module.exports = require('lodash')
*/
let node = null;
wildcards.get(id).find(name => node = tail(resolveModule(id, name), symbol));
return node;
}
return null;
}
}
traverse(ast, {
CallExpression(path) {
var _path$node = path.node;
let args = _path$node.arguments,
callee = _path$node.callee;
// each require('module') call gets replaced with $parcel$require(id, 'module')
if (t.isIdentifier(callee, { name: '$parcel$require' })) {
var _args = _slicedToArray(args, 2);
let id = _args[0],
name = _args[1];
if (args.length !== 2 || !t.isNumericLiteral(id) || !t.isStringLiteral(name)) {
throw new Error('invariant: invalid signature, expected : $parcel$require(number, string)');
}
const mod = resolveModule(id.value, name.value);
if (typeof mod === 'undefined') {
throw new Error(`Cannot find module "${name.value}"`);
}
path.replaceWith(t.identifier(`$${mod}$exports`));
}
},
MemberExpression(path) {
if (!path.isReferenced()) {
return;
}
var _path$node2 = path.node;
let object = _path$node2.object,
property = _path$node2.property;
if (!t.isIdentifier(object) || !t.isIdentifier(property)) {
return;
}
let match = object.name.match(EXPORTS_RE);
if (match) {
let exportName = '$' + match[1] + '$export$' + property.name;
if (path.scope.hasBinding(exportName)) {
path.replaceWith(t.identifier(exportName));
}
}
},
Identifier(path) {
let name = path.node.name;
if (typeof name !== 'string') {
return;
}
let match = name.match(EXPORTS_RE);
if (match) {
if (!path.scope.hasBinding(name) && !addedExports.has(name)) {
let moduleExports = moduleMap.get(+match[1]).cacheData.exports;
addedExports.add(name);
getOuterStatement(path).insertBefore(t.variableDeclaration('var', [t.variableDeclarator(t.identifier(name), t.objectExpression(Object.keys(moduleExports).map(key => {
let binding = path.scope.getBinding(exports.get(key) || key);
if (!binding) {
return null;
}
let exportName = key.match(EXPORT_RE)[2];
let expr = replaceExportNode(+match[1], exportName, path);
if (expr === null) {
return null;
}
if (binding.constant) {
return t.objectProperty(t.identifier(exportName), expr);
} else {
if (!commentedBindings.has(binding)) {
commentedBindings.add(binding);
binding.constantViolations.forEach(path => path.getFunctionParent().addComment('leading', ` bailout: mutates ${generate(expr).code}`, '\n'));
}
return t.objectMethod('get', t.identifier(exportName), [], t.blockStatement([t.returnStatement(expr)]));
}
}).filter(property => property !== null)))]));
}
return;
}
match = name.match(EXPORT_RE);
if (match && !path.scope.hasBinding(name) && !addedExports.has(name)) {
let id = Number(match[1]);
let exportName = match[2];
let node = replaceExportNode(id, exportName, path);
addedExports.add(name);
if (node) {
path.replaceWith(node);
}
}
}
});
return generate(ast, code).code;
};
function getOuterStatement(path) {
if (validate(path)) {
return path;
}
return path.findParent(validate);
function validate(path) {
if (!t.isStatement(path.node) || t.isBlockStatement(path.node)) {
return false;
}
// TODO: use scope?
let outerBlocks = 0;
path.findParent(parent => {
if (t.isBlockStatement(parent.node)) {
outerBlocks++;
}
return false;
});
return outerBlocks === 1;
}
}

View file

@ -0,0 +1,25 @@
'use strict';
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const posthtml = require('posthtml');
const htmlnano = require('htmlnano');
module.exports = (() => {
var _ref = _asyncToGenerator(function* (asset) {
yield asset.parseIfNeeded();
const htmlNanoConfig = asset.package.htmlnano || (yield asset.getConfig(['.htmlnanorc', '.htmlnanorc.js'])) || {};
let res = yield posthtml([htmlnano(htmlNanoConfig)]).process(asset.ast, {
skipParse: true
});
asset.ast = res.tree;
asset.isAstDirty = true;
});
return function (_x) {
return _ref.apply(this, arguments);
};
})();

View file

@ -0,0 +1,76 @@
'use strict';
let getConfig = (() => {
var _ref2 = _asyncToGenerator(function* (asset) {
let config = asset.package.postcss || (yield asset.getConfig(['.postcssrc', '.postcssrc.js', 'postcss.config.js']));
let enableModules = asset.options.rendition && asset.options.rendition.modules;
if (!config && !asset.options.minify && !enableModules) {
return;
}
config = Object.assign({}, config);
let postcssModulesConfig = {
getJSON: function getJSON(filename, json) {
return asset.cssModules = json;
}
};
if (config.plugins && config.plugins['postcss-modules']) {
postcssModulesConfig = Object.assign(config.plugins['postcss-modules'], postcssModulesConfig);
delete config.plugins['postcss-modules'];
}
config.plugins = yield loadPlugins(config.plugins, asset.name);
if (config.modules || enableModules) {
let postcssModules = yield localRequire('postcss-modules', asset.name);
config.plugins.push(postcssModules(postcssModulesConfig));
}
if (asset.options.minify) {
config.plugins.push(cssnano((yield asset.getConfig(['cssnano.config.js'])) || {
// Only enable safe css transforms by default.
// See: https://github.com/parcel-bundler/parcel/issues/698
// Note: Remove when upgrading cssnano to v4
// See: https://github.com/ben-eb/cssnano/releases/tag/v4.0.0-rc.0
safe: true
}));
}
config.from = asset.name;
config.to = asset.name;
return config;
});
return function getConfig(_x2) {
return _ref2.apply(this, arguments);
};
})();
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const localRequire = require('../utils/localRequire');
const loadPlugins = require('../utils/loadPlugins');
const postcss = require('postcss');
const cssnano = require('cssnano');
module.exports = (() => {
var _ref = _asyncToGenerator(function* (asset) {
let config = yield getConfig(asset);
if (!config) {
return;
}
yield asset.parseIfNeeded();
let res = yield postcss(config.plugins).process(asset.getCSSAst(), config);
asset.ast.css = res.css;
asset.ast.dirty = false;
});
return function (_x) {
return _ref.apply(this, arguments);
};
})();

View file

@ -0,0 +1,43 @@
'use strict';
let getConfig = (() => {
var _ref2 = _asyncToGenerator(function* (asset) {
let config = asset.package.posthtml || (yield asset.getConfig(['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js']));
if (!config && !asset.options.minify) {
return;
}
config = Object.assign({}, config);
config.plugins = yield loadPlugins(config.plugins, asset.name);
config.skipParse = true;
return config;
});
return function getConfig(_x2) {
return _ref2.apply(this, arguments);
};
})();
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const loadPlugins = require('../utils/loadPlugins');
const posthtml = require('posthtml');
module.exports = (() => {
var _ref = _asyncToGenerator(function* (asset) {
let config = yield getConfig(asset);
if (!config) {
return;
}
yield asset.parseIfNeeded();
let res = yield posthtml(config.plugins).process(asset.ast, config);
asset.ast = res.tree;
asset.isAstDirty = true;
});
return function (_x) {
return _ref.apply(this, arguments);
};
})();

View file

@ -0,0 +1,76 @@
'use strict';
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
var _require = require('uglify-es');
const minify = _require.minify;
const SourceMap = require('../SourceMap');
module.exports = (() => {
var _ref = _asyncToGenerator(function* (asset) {
yield asset.parseIfNeeded();
// Convert AST into JS
let source = (yield asset.generate()).js;
let customConfig = yield asset.getConfig(['.uglifyrc']);
let options = {
warnings: true,
mangle: {
toplevel: true
}
};
let sourceMap;
if (asset.options.sourceMap) {
sourceMap = new SourceMap();
options.output = {
source_map: {
add(source, gen_line, gen_col, orig_line, orig_col, name) {
sourceMap.addMapping({
source,
name,
original: {
line: orig_line,
column: orig_col
},
generated: {
line: gen_line,
column: gen_col
}
});
}
}
};
}
if (customConfig) {
options = Object.assign(options, customConfig);
}
let result = minify(source, options);
if (result.error) {
throw result.error;
}
if (sourceMap) {
if (asset.sourceMap) {
asset.sourceMap = yield new SourceMap().extendSourceMap(asset.sourceMap, sourceMap);
} else {
asset.sourceMap = sourceMap;
}
}
// babel-generator did our code generation for us, so remove the old AST
asset.ast = null;
asset.outputCode = result.code;
asset.isAstDirty = false;
});
return function (_x) {
return _ref.apply(this, arguments);
};
})();