2.0.1 β€’ Published 8 months ago

@battis/webpack v2.0.1

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
8 months ago

@battis/webpack

npm version Module type: CJS

Reusable, extensible webpack configurations for different needs;

  1. Single Page App: a generic single-page web app with manifest, favicons, etc. Supports TypeScript and SASS.
  2. Vanilla JS: develop in TypeScript and SASS, compile to minified, uglified vanilla JavaScript and CSS.
  3. Bookmarklet: build a bookmarklet in TypeScript and SASS, autogenerate JavaScript executable and documentation.

Install

npm i -D @battis/webpack

if working in a monorepo or using pnpm, you may want to explore add-peer-dependencies.

Usage

package.json

{
  "scripts": {
    "serve": "webpack serve",
    "build": "webpack"
  }
}

tsconfig.json:

{
  "extends": "@battis/webpack/tsconfig.json",
  "include": ["./src"]
}

webpack.config.mjs

import bundle from `@battis/webpack`;

export default bundle.fromTS.toVanillaJS({
  root: import.meta.dirname
});

See Choose Build below.

Common Options

Optional unless otherwise indicated.

Script configuration
ParameterDescription
root (required)Path to project root, where package.json, webpack.config.mjs, etc. reside. In general, import.meta.dirname is the right answer.
templatePath to template directory (if needed) relative to root, from which to draw static web templates that will be updated during the build. Defaults to 'template'
bundleName of the module to bundle. Defaults to 'main'
productionWhether this is a production or development build (which includes a great deal more debugging information and takes up a a lot more space). Defaults to true
overrideAn object indicating which of the below Webpak configurations override, rather than extend the default configurationof the script. By default, all overrides are false. Possible overrides include resolveExtensions,moduleRules,externals,plugins,optimization
Webpack configuration
ParameterDescription
entryPath to the entrypoint to the app relative to root. Defaults to 'src/index.ts'
output.pathPath to the desired output directory for the bundle, relative to root. Default varies depending on build, usually 'build' or 'dist'
output.filenameNaming scheme for the bundle output. Default varies depending on build, usually '[name].[contenthash]'
resolve.extensionsAn array of strings listing file extensions to be resolved by Webpack. Default varies depending on build
module.rulesAn array of Webpack rules for processing file types. Defeault varies depending on build
externalsAn object defining modules that Webpack can externalize from the actual bundle (e.g. JQuery, lodash, etc.). Defaults to none.
pluginsAn array of Webpack plugin instances to be applied to the bundle. Defaults vary depending on build, but always include clean-webpack-plugin
optimizationWebpack optmization rules. Default varies depending on build
terserOptionsConfiguration options for the Terser Plugin in the optimization configuration. Defaults vary depending on build

Choose build

Single Page App

webpack.config.mjs

import bundle from '@battis/webpack';

export default bundle.fromTS.toSPA({
  root: import.meta.dirname
});

Meant to build a single page app, including a manifest, favicons, etc.

Image compression is also supported, but requires optional peer dependencies:

npm i -D image-minimizer-webpack-plugin imagemin imagemin-gifsicle imagemin-jpegtran imagemin-optipng imagemin-svgo

Additional configuration options

ParameterDescription
faviconPath to favicon assets folder relative to root. Defaults is none. If defined, favicon resources will be processed.
appNameDisplay name for the app. Defaults to bundle value
output.publicPathPublic path to the web app

SPA assets folder

πŸ“‚favicon
 ∟ πŸ“„ logo.svg
 ∟ πŸ“„ manifest.json
 ∟ πŸ“„ mask.svg
 ∟ πŸ“„ startup.svg

Vanilla JS

Meant to generate vanilla JavaScript for re-use in a browser.

webpack.confg.mjs

import bundle from '@battis/webpack';

export default bundle.fromTS.toVanillaJS({
  root: import.meta.dirname
});

Additional configuration options

ParameterDescription
targetWebpack target value. Defaults to 'web', but 'node' is useful for compiling node apps and libraries.
extractCSSBoolean value determining whether CSS is extracted as a separate file or embedded in the JS bundle. Defaults to true

package.json

If, in fact, the resulting code is meant to be deployed in a browser, add the following to your package:

{
  ...
  "eslintConfig": {
    ...
    "env": {
      "browser": true
    }
  },
}

Bookmarklet

For generating a bookmarklet and accompanying documentation. The assumed project structure:

πŸ“‚project
 ∟ πŸ“„ package.json
 ∟ πŸ“„ tsconfig.json
 ∟ πŸ“„ webpack.config.js
 ∟ πŸ“‚src
   ∟ πŸ“„ index.ts

This will generate the following files:

πŸ“‚project
 ∟ πŸ“„ README.md
 ∟ πŸ“‚build
   ∟ πŸ“„ bookmarklet.js
   ∟ πŸ“„ embed.html
   ∟ πŸ“„ install.html

bookmarklet.js is the actual bookmarklet executable. install.html is a preformatted page that guides the user through a drag-and-drop install of the bookmarklet. embed.html is the <iframe/> embed code for the install page. README.md displays basic information about the package, including the embedded install page.

GitHub Pages

This assumes that GitHub Pages has been enabled for the repo, deployed from the main branch root.

webpack.config.mjs

import bundle from '@battis/webpack';

export default bundle.fromTS.toBookmarklet({
  root: import.meta.dirname,
  title: 'Click me!'
});

Additional configuration options

ParameterDescription
title (required)Display name for bookmarklet
1.1.3

8 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

0.4.8

12 months ago

0.4.5

1 year ago

0.4.4

1 year ago

0.4.7

1 year ago

0.4.6

1 year ago

0.4.3

1 year ago

0.4.1

1 year ago

0.4.2

1 year ago

0.3.2

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.1.1

2 years ago