Coder Social home page Coder Social logo

webpack-from-scratch's Introduction

Webpack from scratch

@johannesstricker

Source: https://javascript.plainenglish.io/getting-started-with-loaders-and-plugins-in-webpack

Learn more: https://survivejs.com/webpack/foreword/


Understanding how webpack works by configuring it step by step.

Initial setup

mkdir webpack-from-scratch
cd webpack-from-scratch
mkdir src
echo "console.log('It works')" > src/index.js
yarn add -D webpack webpack-cli

Bundling manually

yarn webpack bundle

By convention, the default webpack config will look for src/index.js as the entrypoint and output it to dist/main.js.

Loading stylesheets

If we add a src/styles.css and import it into src/index.js, running yarn webpack bundle will give the following error:

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

Loaders

When webpack sees an import statement, it loads its file content into a UTF-8 string. A loader is just a function that takes this string as an argument, applies transformations to it and returns a result.

module.exports = (source) => {
  return source.replace("a", "b");
};

By default, webpack only knows how to import Javascript files, but it can be configured with different loaders for other types. Loaders can even be chained. They can transform the input in however they want, but the output must be a valid Javascript module.

Plugins

A plugin is a Javascript class with an apply method. It receives a compiler instance as an argument, which exposes hooks that we can tap into to add extra work into its steps.

webpack-from-scratch's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.