123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- /** prelude
- const serial = require('../util/serial');
- const relateContext = require('../util/relate-context');
- const LocalModule = require('webpack/lib/dependencies/LocalModule');
- function flattenPrototype(obj) {
- if (typeof obj === 'string') {
- return obj;
- }
- const copy = {};
- for (const key in obj) {
- copy[key] = obj[key];
- }
- return copy;
- }
- const assignTruthful = {
- freeze(arg, dependency) {
- return arg;
- },
- thaw(arg, frozen) {
- return arg;
- },
- };
- const assignDefined = {
- freeze(arg, dependency) {
- if (typeof arg !== 'undefined') {
- return arg;
- }
- },
- thaw(arg, frozen) {
- if (typeof arg !== 'undefined') {
- return arg;
- }
- },
- }
- const optional = serial.assigned({
- prepend: assignTruthful,
- replaces: assignTruthful,
- critical: assignTruthful,
- namespaceObjectAsContext: assignDefined,
- callArgs: assignDefined,
- call: assignDefined,
- directImport: assignDefined,
- shorthand: assignDefined,
- optional: assignTruthful,
- loc: {
- freeze(arg, dependency) {
- return flattenPrototype(dependency.loc);
- },
- thaw(arg, frozen) {
- return arg;
- },
- },
- });
- const localModuleAssigned = {
- freeze(_, dependency) {
- if (
- typeof dependency.localModule === 'object' &&
- dependency.localModule !== null
- ) {
- return {
- name: dependency.localModule.name,
- idx: dependency.localModule.idx,
- used: dependency.localModule.used,
- };
- }
- },
- thaw(thawed, localModule, extra) {
- const state = extra.state;
- if (
- typeof localModule === 'object' &&
- localModule !== null
- ) {
- if (!state.localModules) {
- state.localModules = [];
- }
- if (!state.localModules[localModule.idx]) {
- state.localModules[localModule.idx] = new LocalModule(
- extra.module,
- localModule.name,
- localModule.idx,
- );
- state.localModules[localModule.idx].used =
- localModule.used;
- }
- thawed.localModule = state.localModules[localModule.idx];
- }
- return thawed;
- },
- };
- const warnings = {
- freeze(frozen, dependency) {
- if (frozen && dependency.getWarnings) {
- const warnings = dependency.getWarnings();
- if (warnings && warnings.length) {
- return warnings.map(
- ({ stack }) =>
- stack.includes('\n at Object.freeze')
- ? stack.split('\n at Object.freeze')[0]
- : stack.includes('\n at pluginCompat.tap')
- ? stack.split('\n at pluginCompat.tap')[0]
- : stack.split('\n at Compiler.pluginCompat.tap')[0],
- );
- }
- }
- },
- thaw(dependency, warnings) {
- if (dependency && warnings && dependency.getWarnings) {
- const frozenWarnings = warnings;
- const _getWarnings = dependency.getWarnings;
- dependency.getWarnings = function() {
- const warnings = _getWarnings.call(this);
- if (warnings && warnings.length) {
- return warnings.map((warning, i) => {
- const stack = warning.stack.split(
- '\n at Compilation.reportDependencyErrorsAndWarnings',
- )[1];
- warning.stack = `${
- frozenWarnings[i]
- }\n at Compilation.reportDependencyErrorsAndWarnings${stack}`;
- return warning;
- });
- }
- return warnings;
- };
- }
- return dependency;
- },
- };
- **/
- const constructorArguments = {
- /** dependencies
- dependencies: {
- freeze(arg, dependency, extra, methods) {
- return methods.mapFreeze('Dependency', null, arg, extra);
- },
- thaw(arg, frozen, extra, methods) {
- return methods.mapThaw('Dependency', null, arg, extra);
- },
- },
- **/
- /** freeze dependencies
- dependencies: methods.mapFreeze('Dependency', null, dependency.dependencies, extra),
- **/
- /** thaw dependencies
- methods.mapThaw('Dependency', null, frozen.dependencies, extra),
- **/
- /** depsArray
- depsArray: {
- freeze(arg, dependency, extra, methods) {
- return methods.mapFreeze('Dependency', null, arg, extra);
- },
- thaw(arg, frozen, extra, methods) {
- return methods.mapThaw('Dependency', null, arg, extra);
- },
- },
- **/
- /** freeze depsArray
- depsArray: methods.mapFreeze('Dependency', null, dependency.depsArray, extra),
- **/
- /** thaw depsArray
- methods.mapThaw('Dependency', null, frozen.depsArray, extra),
- **/
- /** localModule
- localModule: {
- freeze({ name, idx }, dependency, extra, methods) {
- return {
- name: name,
- idx: idx,
- };
- },
- thaw({ idx, name, used }, frozen, extra, methods) {
- const state = extra.state;
- if (!state.localModules) {
- state.localModules = [];
- }
- if (!state.localModules[idx]) {
- state.localModules[idx] = new LocalModule(extra.module, name, idx);
- state.localModules[idx].used = used;
- }
- return state.localModules[idx];
- },
- },
- **/
- /** freeze localModule
- localModule: {
- name: dependency.localModule.name,
- name: dependency.localModule.idx,
- },
- **/
- /** thaw prep localModule
- if (!extra.state.localModules) {
- extra.state.localModules = [];
- }
- if (!extra.state.localModules[frozen.localModule.idx]) {
- extra.state.localModules[frozen.localModule.idx] = new LocalModule(extra.module, frozen.localModule.name, frozen.localModule.idx);
- extra.state.localModules[frozen.localModule.idx].used = frozen.localModule.used;
- }
- **/
- /** thaw localModule
- extra.state.localModules[frozen.localModule.idx],
- **/
- /** regExp
- regExp: {
- freeze(arg, dependency, extra, methods) {
- return arg ? arg.source : false;
- },
- thaw(arg, frozen, extra, methods) {
- return arg ? new RegExp(arg) : arg;
- },
- },
- **/
- /** freeze regExp
- regExp: dependency.regExp ? dependency.regExp.source : false,
- **/
- /** thaw regExp
- frozen.regExp ? new RegExp(frozen.regExp) : frozen.regExp,
- **/
- /** request
- request: {
- freeze(arg, dependency, extra, methods) {
- return relateContext.relateAbsoluteRequest(extra.module.context, arg);
- },
- thaw(arg, dependency, extra, methods) {
- return arg;
- // return relateContext.contextNormalRequest(extra.compilation.compiler, arg);
- },
- },
- **/
- /** freeze request
- request: relateContext.relateAbsoluteRequest(extra.module.context, dependency.request),
- **/
- /** thaw request
- frozen.request,
- **/
- /** userRequest
- userRequest: {
- freeze(arg, dependency, extra, methods) {
- return relateContext.relateAbsoluteRequest(extra.module.context, arg);
- },
- thaw(arg, dependency, extra, methods) {
- return arg;
- // return relateContext.contextNormalRequest(extra.compilation.compiler, arg);
- },
- },
- **/
- /** freeze userRequest
- userRequest: relateContext.relateAbsoluteRequest(extra.module.context, dependency.userRequest),
- **/
- /** thaw userRequest
- frozen.userRequest,
- **/
- /** block
- block: {
- freeze(arg, dependency, extra, methods) {
- // Dependency nested in a parent. Freezing the block is a loop.
- if (arg.dependencies.includes(dependency)) {
- return;
- }
- return methods.freeze('DependencyBlock', null, arg, extra);
- },
- thaw(arg, frozen, extra, methods) {
- // Not having a block, means it needs to create a cycle and refer to its
- // parent.
- if (!arg) {
- return extra.parent;
- }
- return methods.thaw('DependencyBlock', null, arg, extra);
- },
- },
- **/
- /** freeze block
- block: !dependency.block.dependencies.includes(dependency) ?
- methods.freeze('DependencyBlock', null, dependency.block, extra) :
- undefined,
- **/
- /** thaw block
- !frozen.block ? extra.parent : methods.thaw('DependencyBlock', null, frozen.block, extra),
- **/
- /** importDependency
- importDependency: {
- freeze(arg, dependency, extra, methods) {
- return methods.freeze('Dependency', null, arg, extra);
- },
- thaw(arg, frozen, extra, methods) {
- return methods.thaw('Dependency', null, arg, extra);
- },
- },
- **/
- /** freeze importDependency
- importDependency: methods.freeze('Dependency', null, dependency.importDependency, extra),
- **/
- /** thaw importDependency
- methods.thaw('Dependency', null, frozen.importDependency, extra),
- **/
- /** originModule
- originModule: {
- freeze(arg, dependency, extra, methods) {
- // This will be in extra, generated or found during the process of thawing.
- },
- thaw(arg, frozen, extra, methods) {
- return extra.module;
- },
- },
- **/
- /** freeze originModule
- originModule: null,
- **/
- /** thaw originModule
- extra.module,
- **/
- /** activeExports
- activeExports: {
- freeze(arg, dependency, extra, methods) {
- return null;
- },
- thaw(arg, { name }, { state }, methods) {
- state.activeExports = state.activeExports || new Set();
- if (name) {
- state.activeExports.add(name);
- }
- return state.activeExports;
- },
- },
- **/
- /** freeze activeExports
- activeExports: null,
- **/
- /** thaw prep activeExports
- extra.state.activeExports = extra.state.activeExports || new Set();
- if (frozen.name) {
- extra.state.activeExports.add(frozen.name);
- }
- **/
- /** thaw activeExports
- extra.state.activeExports,
- **/
- /** otherStarExports
- otherStarExports: {
- freeze(arg, dependency, extra, methods) {
- if (arg) {
- // This will be in extra, generated during the process of thawing.
- return 'star';
- }
- return null;
- },
- thaw(arg, frozen, { state }, methods) {
- if (arg === 'star') {
- return state.otherStarExports || [];
- }
- return null;
- },
- },
- **/
- /** freeze otherStarExports
- otherStarExports: dependency.otherStarExports ? 'star' : null,
- **/
- /** thaw otherStarExports
- frozen.otherStarExports === 'star' ?
- (extra.state.otherStarExports || []) :
- null,
- **/
- /** options
- options: {
- freeze(arg, dependency, extra, methods) {
- if (arg.regExp) {
- return Object.assign({}, arg, {
- regExp: arg.regExp.source,
- });
- }
- return arg;
- },
- thaw(arg, frozen, extra, methods) {
- if (arg.regExp) {
- return Object.assign({}, arg, {
- regExp: new RegExp(arg.regExp),
- });
- }
- return arg;
- },
- },
- **/
- /** freeze options
- options: dependency.options.regExp ?
- Object.assign({}, dependency.options, {
- regExp: dependency.options.regExp.source,
- }) :
- dependency.options,
- **/
- /** thaw options
- frozen.options.regExp ?
- Object.assign({}, frozen.options, {
- regExp: new RegExp(frozen.options.regExp),
- }) :
- frozen.options,
- **/
- /** parserScope
- parserScope: {
- freeze(arg, dependencies, extra, methods) {
- return;
- },
- thaw(arg, frozen, { state }, methods) {
- state.harmonyParserScope = state.harmonyParserScope || {};
- return state.harmonyParserScope;
- },
- },
- **/
- /** freeze parserScope
- parserScope: null,
- **/
- /** thaw prep parserScope
- extra.state.harmonyParserScope = extra.state.harmonyParserScope || {};
- **/
- /** thaw parserScope
- extra.state.harmonyParserScope,
- **/
- };
- /** importDependencyState
- importDependency: {
- freeze(frozen) {
- return frozen;
- },
- thaw(thawed, frozen, extra) {
- const state = extra.state;
- const ref = frozen.range.toString();
- if (state.imports[ref]) {
- return state.imports[ref];
- }
- state.imports[ref] = thawed;
- return thawed;
- },
- },
- **/
- /** exportImportedDependencyState
- exportImportedDependency: {
- freeze(frozen) {},
- thaw(thawed, frozen, extra) {
- if (thawed.otherStarExports) {
- extra.state.otherStarExports = (
- extra.state.otherStarExports || []
- ).concat(thawed);
- }
- return thawed;
- },
- },
- **/
- const fs = require('graceful-fs');
- const path = require('path');
- const generateRaw = fs.readFileSync(path.join(__dirname, '_generate.js'), 'utf8');
- const generateBlocks = generateRaw
- .split(/((?:\/\*\*)((?!\*\*\/)[^\r\n]*\r?\n)+)/g)
- .filter(Boolean)
- .filter(str => !str.startsWith('**/'))
- .reduce((carry, item, index) => index % 2 === 0 ? [...carry, item] : carry, []);
- const getBlock = name => {
- let lines = generateBlocks
- .find(block => block.startsWith(`/** ${name}`));
- if (lines) {
- lines = lines.split('\n');
- lines = lines.slice(1, lines.length - 1);
- }
- return lines || [];
- };
- const dependencyInfo = require('./basic-dependency.json');
- let output = getBlock('prelude');
- for (const dependency of dependencyInfo) {
- const DepName = dependency[0];
- const depName = DepName[0].toLowerCase() + DepName.slice(1);
- const DepNameSerial = `${DepName}Serial`;
- output.push(`const ${dependency[0]} = require('webpack/lib/dependencies/${dependency[0]}');`);
- output.push(`const ${DepNameSerial} = serial.serial('${DepName}', {`);
- // output.push(` constructor: serial.constructed(${DepName}, {`);
- // for (const argument of dependency.slice(1)) {
- // let block = getBlock(argument);
- // if (!block.length) {
- // block = [` ${argument}: serial.identity,`];
- // }
- // output.push(...block);
- // }
- // output.push(` }),`);
- output.push(` constructor: {`);
- output.push(` freeze(_, dependency, extra, methods) {`);
- output.push(` return {`);
- for (const argument of dependency.slice(1)) {
- let block = getBlock(`freeze ${argument}`);
- if (!block.length) {
- block = [` ${argument}: dependency.${argument},`];
- }
- output.push(...block);
- }
- output.push(` };`);
- output.push(` },`);
- output.push(` thaw(thawed, frozen, extra, methods) {`);
- for (const argument of dependency.slice(1)) {
- let block = getBlock(`thaw prep ${argument}`);
- output.push(...block);
- }
- output.push(` return new ${DepName}(`);
- for (const argument of dependency.slice(1)) {
- let block = getBlock(`thaw ${argument}`);
- if (!block.length) {
- block = [` frozen.${argument},`];
- }
- output.push(...block);
- }
- output.push(` );`);
- output.push(` },`);
- output.push(` },`);
- output.push(``);
- output.push(` optional,`);
- if (DepName === 'AMDDefineDependency' || DepName === 'LocalModuleDependency') {
- output.push(``);
- output.push(` localModuleAssigned,`);
- }
- output.push(``);
- output.push(` warnings,`);
- if (DepName === 'HarmonyImportDependency') {
- output.push(``);
- output.push(...getBlock('importDependencyState'));
- }
- if (DepName === 'HarmonyExportImportedSpecifierDependency') {
- output.push(``);
- output.push(...getBlock('exportImportedDependencyState'));
- }
- output.push(`});`);
- output.push(``);
- }
- output.push(`exports.map = new Map();`);
- for (const dependency of dependencyInfo) {
- const DepName = dependency[0];
- const DepNameSerial = `${DepName}Serial`;
- output.push(`exports.${DepName} = ${DepNameSerial};`);
- output.push(`exports.map.set(${DepName}, ${DepNameSerial});`);
- }
- output.push(``);
- require('graceful-fs').writeFileSync(require('path').join(__dirname, 'index.js'), output.join('\n'), 'utf8');
|