bundle-utils

0.0.2 • Public • Published

Bundle-utils

How it works

.browsers.json

This is an optional config to configure multiple bundles concept. Just pass all requirements using browserslist notation in array. For each item new bundle will be generated with specific id. Then you can serve it with any static server.

.browsers.json

[
  "not dead",
  "last 5 chrome versions, last 5 safari versions, last 5 firefox versions",
  "last 1 chrome version, last 1 safari version, last 1 firefox version",
]

Bundler side

Currently it supports webpack and parcel.

Webpack

Since webpack supports multiple configs with array type and creates new bundle for each one, we can just wrap our usual webpack config with mapConfigToTargets wrapper and pass here function which expects your webpack config to be returned, but also passes browsers and id arguments. This 2 arguments implements the core idea of this package.

Wrap your config with mapConfigToTargets and pass browsers to babel-preset-env's targets. With id you can configure your bundle location. It will be synced with server-side part, so you can easily get it and serve in future.

With the help of getBundleLocationWithId we can better resolve bundle location for both server and bundler side.

webpack.config.js

const path = require('path');
const { mapConfigToTargets, getBundleLocationWithId } = require('./utils/env-bundles');
 
module.exports = mapConfigToTargets(({ browsers, id }) => {
  return {
    entry: [path.join(__dirname, 'app/main.js')],
    output: {
      path: getBundleLocationWithId(`${__dirname}/dist/`, id), // generate your output location with specific bundle id.
      filename: '[name].js',
      publicPath: '/',
    },
    module: {
      rules: [
        {
          test: /\.js/,
          exclude: /node_modules/,
          loader: 'babel-loader',
          query: {
            presets: [
              [
                '@babel/preset-env',
                {
                  targets: browsers, // pass browsers as a targets for current bundle.
                },
              ],
            ],
          },
        }
      ],
    },
  };
});

Node.js

Here we just need to read configs and understand the sizes of bundles. So we could serve smallest bundle possble for every target. For example, chrome will receive the smallest bundle, but IE - possibly the biggest.

server.js

const BUNDLE_DIRNAME = 'dist';
const bundlesRoot = path.join(__dirname, BUNDLE_DIRNAME);
const { getBundleIdByRequest } = initializeBundleGetter({ bundlesRoot });
 
app.use(express.static(`${__dirname}/dist`));
app.get('*', (req, res) => {
  const bundleId = getBundleIdByRequest(req);
  const fullPath = path.join(__dirname, BUNDLE_DIRNAME, bundleId, req.url);
  res.write(fs.readFileSync(path.join(fullPath)));
  res.end();
});

License

MIT License

Copyright (c) 2017-2018 all contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i bundle-utils

Weekly Downloads

2

Version

0.0.2

License

MIT

Unpacked Size

272 kB

Total Files

28

Last publish

Collaborators

  • aqson