Professional Documents
Culture Documents
master
webpack-interview-questions / answers.md
styopdev
Fix grammar
History
1
contributor
Table of Contents
Concepts
Config file
Loaders
Plugins
Development
Optimization
Migration
Advanced-questions
Concepts
Question: What is webpack?
Answer: Webpack is a module bundler, though it is quite often used instead of Gulp or
Grunt task runners. This advanced tool provides developers with control of spliting the
modules, allowing them to adjust builds to particular situations and workaround solutions
that don’t function properly out of the box.
Comparing Webpack vs Grunt, the first of those offers more flexibility and advanced
functionality for modern front-end projects. It comes with a functional core and can be
extended using particular loaders and plugins. Essentially it is used for bundling JavaScript
modules with dependencies into files, but for complex JavaScript applications with lots of
non-code assets (images, fonts, CSS, etc.) it can provide great benefits.
Talking about Webpack vs Gulp vs Grunt performance, the two latter look into a defined
path for files that match your configuration, while the Webpack analyzes the whole project.
It looks through all the dependencies, processes them with loaders and produces a
bundled JS file. (source)
Answer: bundle is the output file generated by webpack. It contains all of the modules
which are used in application. Bundles generation process is regulated by webpack config
file.
Answer: node.js
Answer: The entry object is where webpack looks to start building the bundle, at this point
the application starts executing.
Question: What is a dependency graph and how does webpack build it?
Answer: Any time one file depends on another, webpack treats this as a dependency.
Starting from entry point(s), webpack recursively builds a dependency graph that includes
every module your application needs, using import and require statements, then
packages all of those modules into bundle(s).
Question Which modules design patterns webpack supports out of the box?
Question: Is it possible to have multiple entry points in a singe webpack configuration file?
Answer: Yes
Answer: Yes
Loaders
Question: What is a loader in webpack
Answer: Loaders are transformations that are applied on the source code of a module.
webpack supports modules written in a variety of languages and preprocessors, via
loaders. Loaders describe to webpack how to process non-javaScript modules and include
these dependencies into your bundles.
test: /\.scss$/,
Answer: This code is intended to transform and load sass files as webpack modules.
This
config tells to webpack to search for all files which have .scss extension, then applies the
following loaders on them, from right to left order:
Due to ?sourceMap at the end of css-loader and sass-loader, source maps for all .scss files
will be created.
Question: Name loaders you think are very important and helpful
Plugins
Question: Describe a plugin in webpack
Answer: https://stackoverflow.com/a/38281240/3283209
Answer: CompressionPlugin builds gzip-ed version of bundles. Its possible to simply add
server side compression e.g using nginx or expres compression plugin. Server-side
compression is not recommended because it addes load on CPU and increases response
time.
Question: How to move some data (e.g css code) from a bundle to a separate file in
webpack?
Answer: using ExtractTextWebpackPlugin. It moves all the required *.css modules in entry
chunks into a separate CSS file. So your styles are no longer inlined into the JS bundle, but
in a separate CSS file (styles.css). If your total stylesheet volume is big, it will be faster
because the CSS bundle is loaded in parallel to the JS bundle.
https://github.com/webpack-contrib/extract-text-webpack-plugin
Question: Name some plugins you think are very important and helpful
Answer: Yes, its possible to write your own plugin and use plugins written by community.
Development
Question What are some advantages of using webpack-dev-server over simple http
server or nginx ?
Answer: Using devtool: 'source-map' (there are various other configurations for source
maps, view full list here)
Question: How to automatically build and update bundles in the browser after a change in
source code?
Answer: Using watch: true and devServer: { hot: true } options together.
Question: What is parallel-webpack and how does it affect webpack's build process
Answer: parallel-webpack useful for webpack configurations with multiple entry points. It
allows to run multiple webpack builds in parallel, spreading the work across your
processors and thus helping to significantly speed up build.
Optimization
Question: Briefly describe long-term caching and how to achieve it using webpack?
Answer: Browsers should cache static assets to save traffic and users time. But after each
change or bugfix, browser have to download newer version of files. The most easy way to
achieve this is changing file name. It could be buildId or unique hash in the end of file's
name like
app.js?build=1
app.js?build=2
or
app.js.2a6c1fee4b5b0d2c9285.js
app.js.70b594fe8b07bcedaa98.js
module.exports = {
...
output: {
filename: "[name].[hash].js"
...
Answer: [hash] will generate unique hash for each build and use it for all chunks. Replace
[hash] with [chunkhash] to generate unique hashes for each chunk. This is useful when
you dont want to re-download vendors (dependencies) file but you have changes in your
application code and want to update it.
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
filename: 'common.js',
})
Answer: This code creates separate file: common.js which contains common modules from
home and dashboard chunks.
new webpack.ContextReplacementPlugin(
/moment[\/\\]locale/,
/(en-gb|en-us)\.js/
Answer: This code tells to webpack to include only those files which names match to /(en-
gb|en-us)\.js/ pattern from /moment[\/\\]locale/ path. It used to optimize final bundle
excluding unused locales. So if you need to use e.g french language's locale as well then
you should add it to second regex like this: /(en-gb|en-us|fr)\.js/ .
Question: What analyzing tools you use to inspect your webpack bundle?
Migration
305 lines (184 sloc)
13.7 KB
webpack 1
module.exports = {
eslint: {
webpack 2
module.exports = {
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
eslint: {
})
Question: Do you need to include OccurenceOrderPlugin in the plugins section when use
webpack 2/3?
Answer: webpack 2+
Answer: webpack 2+
Advanced questions
Question: Describe the webpack runtime and manifest.
Answer: https://webpack.js.org/concepts/manifest/
Question: Is it possible to use other languages (except javascript) for the webpack config
file?
Answer: Yes, webpack accepts configuration files written in multiple programming and
data languages, such as typescript, coffeescript, babel and jsx. The list of supported file
extensions can be found here.
Answer: https://webpack.js.org/guides/tree-shaking/
Question: What is difference between tree shaking and dead code elimination.
Answer: https://medium.com/@Rich_Harris/tree-shaking-versus-dead-code-elimination-
d3765df85c80