How Webpack Works PDF
How Webpack Works PDF
works
Tobias Koppers
About me
• Tobias Koppers
• M. Sc. Computer Science
• Started with Open Source in 2012 as Hobby
• Since 2017: Freelancer (Open Source + Consulting)
• Father of a 2-months old daughter
• Twitter: @wSokra
• Github: @sokra
Agenda
• Walkthrough webpack
• You should get a rough understanding of the implementation of webpack.
• You should know where to look into when customizing.
• We will not go into details of individual optimization algorithms.
• Some unimportant details are omitted for simplicity.
• Some points for customization are highlighted.
• Customization
• You will learn how to write plugins for webpack.
• We won’t list every possible customizable thing.
Plugins
class MyPlugin {
apply(compiler) {
compiler.plugin(“done”, (stats) => {
console.log(stats.toString());
}
}
}
• bin/webpack.js, bin/convert-argv.js
• Can‘t be customized
API / webpack facade
• Validates configuration according to schema
• Applies the node.js environment plugins
• Input/Output/Watch FileSystem for node.js
• Calls WebpackOptionsApply to process configuration
• Creates the Compiler
• May call run or watch on the Compiler
• addEntry addModuleChain
• finish
• report errors from modules
• seal
Compilation.addModuleChain
• input is a Dependency
• get the ModuleFactory for the Dependency
• ModuleFactory.create returns Module
• Compilation.addModule
• Compilation.buildModule
• Calls Module.build
• Compilation.processModuleDependencies
• foreach unique Dependency in Module
Compilation.addModule
• input is a Module from ModuleFactory
• Return if Module is already in the Compilation (same identifier)
• Check if we have module in cache (same identifier)
Disconnect Unbuild
ModuleFactory
NormalModuleFactory ContextModuleFactory
NormalModuleFactory
• Resolve request (using enhanced-resolve)
• Process rules from RuleSet
• Resolve remaining requests i. e. loaders
• Instanciate NomalModule
• Plugins may alter this process
• Returning other kind of Modules (i. e. externals, dll, ignore)
• Overriding requests
Module
Module
NormalModule ExternalModule
ContextModule DelegatedModule
MultiModule
NormalModule
• Build
• Run loaders using loader-runner
• Store returned Code
• Parse using Parser
• ParserPlugins add dependencies to the DependencyBlock/Module
• Get Source (Code Generation)
• Create a ReplaceSource on top of the stored Code
• Get DependencyTemplate for each Dependency
• Apply templates for Dependency on ReplaceSource
• Templates do string replacements/insertings
Source
Source
ConcatSource
RawSource
CachedSource
OriginalSource
PrefixSource
SourceMapSource
ReplaceSource
Compilation.createAssets
• MainTemplate
• ChunkTemplate
• ModuleTemplate
ModuleTemplate
webpack Facade
Parser
Watching
MainTemplate
Compiler
ChunkTemplate NormalModule
Compilation ModuleFactory
EntryPoint Resolver
NormalModuleFactory
Chunk
RuleSet
Module CommonJsRequire
Dependency
Dependency
ModuleDependency
DependenciesBlock CommonJsRequire
AsyncDependenciesBlock ContextDependency DependencyTemplate
More Info
• Source Code
• github.com/webpack/webpack
• github.com/webpack/webpack-sources
• github.com/webpack/enhanced-resolve
• github.com/webpack/loader-runner
• Documentation
• webpack.js.org
Funding
• webpack needs your help. We are funded by donations. Sponsor?