Last active
February 23, 2021 11:09
-
-
Save Randy808/5c375cd3dc8a2b0b7e4af98452d4277c to your computer and use it in GitHub Desktop.
compilation-addEntry.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string} context context path for entry | |
* @param {Dependency} entry entry dependency that should be followed | |
* @param {string | EntryOptions} optionsOrName options or deprecated name of entry | |
* @param {ModuleCallback} callback callback function | |
* @returns {void} returns | |
*/ | |
addEntry(context, entry, optionsOrName, callback) { | |
// TODO webpack 6 remove | |
const options = | |
typeof optionsOrName === "object" ? optionsOrName : { name: optionsOrName }; | |
this._addEntryItem(context, entry, "dependencies", options, callback); | |
} | |
/** | |
* @param {string} context context path for entry | |
* @param {Dependency} entry entry dependency that should be followed | |
* @param {"dependencies" | "includeDependencies"} target type of entry | |
* @param {EntryOptions} options options | |
* @param {ModuleCallback} callback callback function | |
* @returns {void} returns | |
*/ | |
_addEntryItem(context, entry, target, options, callback) { | |
//'name' should just be set to the default 'main' since | |
//we passed in an empty options object to the top-level 'webpack' | |
const { name } = options; | |
let entryData = | |
name !== undefined ? this.entries.get(name) : this.globalEntry; | |
if (entryData === undefined) { | |
//We fall into this case where we have to construct our own entryData | |
entryData = { | |
dependencies: [], | |
includeDependencies: [], | |
options: { | |
name: undefined, | |
...options, | |
}, | |
}; | |
//I'm not quite sure how entryData will be used later on but at | |
//least we know it has the original 'entry' parameter in 'dependencies' | |
entryData[target].push(entry); | |
this.entries.set(name, entryData); | |
} else { | |
/***** | |
I deleted what was in this 'else' since we're only | |
focused on our example of single file bundling for now | |
******/ | |
} | |
//I don't think this hook is used by the core webpack | |
//library so let's ignore it for now and re-visit it later on | |
this.hooks.addEntry.call(entry, options); | |
//If you look closely this call only needs the 'context', | |
//'entry', and 'callback' parameter values to move on (dropping the 'options'). | |
//I wonder how the 'this.entries' populated above is used further in the pipeline | |
this.addModuleChain(context, entry, (err, module) => { | |
if (err) { | |
this.hooks.failedEntry.call(entry, options, err); | |
return callback(err); | |
} | |
this.hooks.succeedEntry.call(entry, options, module); | |
return callback(null, module); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment