Loaders do the pre-processing transformation of virtually any file format when you use sth like
require("my-loader!./my-awesome-module") in your code. Compared to plugins, they are quite simple as they (a) expose only one single function to webpack and (b) are not able to influence the actual build process.
Plugins on the other hand can deeply integrate into webpack because they can register hooks within webpacks build system and access (and modify) the compiler, and how it works, as well as the compilation. Therefore, they are more powerful, but also harder to maintain.
At its core, webpack is just a file bundler. Considering a very simple scenario (no code splitting), this could mean just the following actions(on a high level):
- find the entry file and load its contents into memory
- match certain text within the content and evaluate those (for e.g. @import)
- find the dependencies based on previous evaluation and do the same with them
- stitch them all into a bundle in memory
- write the results to file system
When you examine the above steps closely, this resonates with what a Java compiler(or any compiler) does. There are differences of course but those don't matter to understand loaders and plugins.
are here because webpack promises to bundle together any file type.
Since webpack at its core is only capable enough to bundle js files, this promise meant that the webpack core team had to incorporate build flows which allowed external code to transform a particular file type in a way that webpack could consume.
These external code are called loaders and they typically run during step 1 and 3 above. Thus, since the stage at which these loaders need to run is obvious, they don't require hooks and neither do they influence the build process(since the build or bundle only happens at step 4).
So Loaders prepare the stage for compilation and they sort of extend the flexibility of the webpack compiler.
are here because even though webpack doesn't directly promise variable output, the world wants it and webpack does allow it.
Since webpack at its core is just a bundler and yet goes through several steps and sub-steps in doing so, these steps can be utilised to build in additional functionality.
The production build process(minifying and writing to file system), which is a native capability of webpack compiler, for e.g., can be treated as an extension of its core capability(which is just bundling) and can be treated like a native plugin. Had they not provided it, someone else would have done it.
Looking at the native plugin above, it appears as if the webpack bundling or compilation can be broken down into core bundling process, plus a lot of native plugin processes which we can turn off or customise or extend. This meant allowing external code to join in the bundling process at specific points that they can choose from (called hooks).
Plugins therefore influence the output and sort of extend the capability of webpack compiler.
Adding complementary and simpler answer.
Loaders work at the individual file level during or before the bundle is generated.
Plugins work at bundle or chunk level and usually work at the end of the bundle generation process. Plugins can also modify how the bundles themselves are created. Plugins have more powerful control than loaders.
Just for an example you can clearly see in below image where loaders are working and where plugins are working -
- Webpack loaders vs plugins; what's the difference?
- Whats the difference between type babel and jsx
- Whats the difference between ForwardRefExoticComponent and ForwardRefRenderFunction in react?
- Whats is the difference between index.js and _app.js in NextJs?
- Whats the difference between Array.fill and a for loop to create Array
- Whats the difference between toBeInTheDocument and getBy* in @testing-library/react
- What is the difference between React Native and React?
- What is the difference between state and props in React?
- What is the difference between using constructor vs getInitialState in React / React Native?
- What's the difference between "super()" and "super(props)" in React when using es6 classes?
- What's the difference between `useRef` and `createRef`?
- How to make the webpack dev server run on port 80 and on 0.0.0.0 to make it publicly accessible?
- What is the difference between HashRouter and BrowserRouter in React?
- What's the difference between useCallback and useMemo in practice?
- Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema
- Can anyone explain the difference between Reacts one-way data binding and Angular's two-way data binding
- What is the difference between .ts and .tsx extensions. Both are used as extensions for typescript files in react. So where should we use them?
- In React, what's the difference between onChange and onInput?
- In useEffect, what's the difference between providing no dependency array and an empty one?
- What's the difference between hydrate() and render() in React 16?
- Whats the best way to update an object in an array in ReactJS?
- What is the difference between redux-thunk and redux-promise?
- What is the difference between componentWillMount and componentDidMount in ReactJS?
- What is the difference between NextJs and Create React App
- What is the difference between jest.fn() and jest.spyOn() methods in jest?
- what's the difference between getDerivedStateFromError and componentDidCatch
- What is the technical difference between .jsx and .js files
- What the difference of this.state and this.setstate in ReactJS?
- react custom hooks vs normal functions, what is the difference
- What is the difference between hashHistory and browserHistory in react router?
More Query from same tag
- Conditional form input non-responsive
- Reactjs : How to route to another form on submit and send submitted data to the new page
- React-ReduxForm Password Confirmation not working?
- React ReferenceError: document is not defined
- Hide an element and it's space and notify the app so it displays other content with React
- gatsbyjs and expressjs Error 400 Bad Request while fetch
- React/Redux - Passing JSON data from one API call to another Redux action using Thunk
- React - Accessing updated values from nested components
- Use a local tile layer with leaflet
- Reactjs Nominatim API-Cors
- Connection to signalr hub blocked by content security policy
- Global Variables not being accessed by Mocha tests
- New browser tab doesn't load styled components
- How to make a file with variables of all project's text?
- Inserting axios result using hooks
- Getting [ object object ] response when trying to fetch data from my api
- React not applying css styles
- Reactjs: Unknown why function re-run second time
- Why state changes only on second click?
- Plot polygon using react map gl
- How would I turn a graphQL query into a react component?
- Material-UI Table select all per page pagination
- In Reactjs,is it possible to update the state of a component only for those items that satisfy a particular condition
- How to access headers in react.js frontend built by rails backend?
- React setState race conditions
- React-- why do looped state updates precede recursive state updates?
- How to retrieve Gitlab environments variables to be used in gatsby-*.js files
- What type do I specify for react components?
- Mapping components with variable props not rendering from JSX to DOM, React