In order to make websites efficient and portable, a lot of transpilation is needed.
Webpack is like a magic hydra that can eat any type of file and bundle it into a single output: .js, .ts, .ccs, .scss, .jsx, .tsx, require, import, import css from .js, it doesn't matter at all, it just digests all into the same dump.
When it works, you are just left in awe and with a single Js file. When it doesn't, you're fucked and have to debug for several hours.
Demos under: webpack/. To run all of them by default:
cd webpack/min
npm install
npm run build
xdg-open index.html
To easily make changes and reload the .js output live let this run on a terminal:
npx webpack watch
Examples:
webpack/template contains a reasonable starter template.
This will produce, under dist/ the following minimized files:
You can also run this test with the development server on localhost:9000:
npm start
which uses unminimized outptus, and automatically push reloads the page whenever you change any of the input files!
This example shows how to use @cirosantilli/_file/webpack/webpack/sass.
To make things simple, it generates a completely separate dist/index.js and dist/main.css which are manually included from index.html, and does not do any type of injection (neither Js into HTML nor CSS in Js).
This example shows how you could manually include the dist/index.js that is output from webpack into your index.html.
This is generally not what you want to do, because what you actually want to do is to use a Js output name with a hash in it, so that browsers only need to refetch when the name changes.
And to do that, we have to let webpack dynamically inject that unpredictable hash as done in webpack/template with:
new HtmlWebpackPlugin({
  filename: 'index.html',
  // Inject the include to our hashed filename,
  // since it is not deterministic due to the hash.
  inject: true,
  template: path.resolve(__dirname, 'index.html'),
}),
This shows how to produce a minimized fully embedded CSS file with webpack from a sass:
cd webpack/sass
npm install
npm run build
xdg-open index.html
That example produces a dist/main.css file which is a compresesd combination of:

Articles by others on the same topic (0)

There are currently no matching articles.