Skip to content

Commit

Permalink
Move package from modernweb-dev/web monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Jan 11, 2024
1 parent b2f8a0b commit e0a03e7
Show file tree
Hide file tree
Showing 15 changed files with 6,060 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
118 changes: 118 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# rollup-plugin-workbox

## 8.1.0

### Minor Changes

- 8218a0a5: Update ESBuild to latest version

## 8.0.2

### Patch Changes

- 640ba85f: added types for main entry point

## 8.0.1

### Patch Changes

- 9ae9c2e7: fix: generate sw arg

## 8.0.0

### Major Changes

- f7927b81: feat: update workbox v7

- Update workbox to v7
- Removed the mode option in favour of just always bundling/removing process.env for the service worker when using injectManifest

### Patch Changes

- 85647c10: Update `lit-html`
- 5acaf838: Update `@typescript-eslint/parser`
- d56f6bb0: fix: esbuild options shouldnt override swsrc and swdest
- ab4720fa: fix: terser import

## 7.0.1

### Patch Changes

- 1109ec37: Replace `rollup-plugin-terser` with `@rollup/plugin-terser`

## 7.0.0

### Major Changes

- febd9d9d: Set node 16 as the minimum version.
- 72c63bc5: Require [email protected] and update all Rollup related dependencies to latest.

## 6.2.2

### Patch Changes

- f7ddc726: Revert back to non-ESM version of `pretty-bytes`

## 6.2.1

### Patch Changes

- 7b35551b: Update `pretty-bytes`
- 1113fa09: Update `@rollup/pluginutils`
- 817d674b: Update `browserslist-useragent`
- bd12ff9b: Update `rollup/plugin-replace`
- 8128ca53: Update @rollup/plugin-replace

## 6.2.0

### Minor Changes

- 641e0a9e: Update workbox-build dependency to 6.2.4

## 6.1.4

### Patch Changes

- eb2733de: Update dependency @rollup/plugin-replace to v3

## 6.1.3

### Patch Changes

- 687d4750: Downgrade @rollup/plugin-node-resolve to v11

## 6.1.2

### Patch Changes

- 9c97ea53: update dependency @rollup/plugin-node-resolve to v13

## 6.1.1

### Patch Changes

- 8cf22153: Remove "mode" from passed workbox config.

## 6.1.0

### Minor Changes

- 36f6ab39: update to node-resolve v11

## 6.0.0

### Major Changes

- 5da1505: upgrade workbox to v6

## 5.2.1

### Patch Changes

- 93a4243: Export cjs and esm correctly

## 5.2.0

### Minor Changes

- 1b045b9: Convert code base to TypeScript
149 changes: 147 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,147 @@
# package-template
A template for Modern Web packages
# rollup-plugin-workbox

[![Published on npm](https://img.shields.io/npm/v/rollup-plugin-workbox.svg)](https://www.npmjs.com/package/rollup-plugin-workbox)

Rollup plugin that builds a service worker with workbox as part of your rollup build

## Usage

This package provides two rollup plugins: one that generates a complete service worker for you and one that generates a list of assets to precache that is injected into a service worker file.

The plugins are implemented as two function in the rollup-plugin-workbox module, named `generateSW` and `injectManifest`.

### `generateSW`

Import the `generateSW` plugin from `rollup-plugin-workbox`, and add it to your `plugins` array in your `rollup.config.js`. The plugin takes a workbox config object, and an optional render function as the second argument.

You can find a detailed list of supported properties for the workbox config object [here](https://developers.google.com/web/tools/workbox/modules/workbox-build#generatesw_mode).

```js
const { generateSW } = require('rollup-plugin-workbox');

module.exports = {
input: 'main.js',
output: {
file: 'dist/bundle.js',
format: 'esm',
},
plugins: [
generateSW({
swDest: '/dist/sw.js',
globDirectory: 'demo/dist/',
}),
],
};
```

You can also `require` your `workbox-config.js` file and pass it to the plugin.

```js
const { generateSW } = require('rollup-plugin-workbox');

const workboxConfig = require('./workbox-config.js');

module.exports = {
// ...
plugins: [generateSW(workboxConfig)],
};
```

You can also customize the console output after workbox has generated your service worker by passing an optional render function as the second argument to the plugin:

```js
const { generateSW } = require('rollup-plugin-workbox');

const workboxConfig = require('./workbox-config.js');

module.exports = {
// ...
plugins: [
generateSW(workboxConfig, {
render: ({ swDest, count, size }) => {
console.log('📦', swDest, '#️⃣', count, '🐘', size);
},
}),
],
};
```

### `injectManifest`

Import the `injectManifest` plugin from `rollup-plugin-workbox`, and add it to your `plugins` array in your `rollup.config.js`. The plugin takes a workbox config object, and an optional render function as the second argument.

You can find a detailed list of supported properties for the workbox config object [here](https://developers.google.com/web/tools/workbox/modules/workbox-build#injectmanifest_mode).

```js
const { injectManifest } = require('rollup-plugin-workbox');

module.exports = {
input: 'main.js',
output: {
file: 'dist/bundle.js',
format: 'esm',
},
plugins: [
injectManifest({
swSrc: 'sw.js',
swDest: '/dist/sw.js',
globDirectory: 'demo/dist/',
}),
],
};
```

You can also `require` your `workbox-config.js` file and pass it to the plugin.

```js
const { injectManifest } = require('rollup-plugin-workbox');

const workboxConfig = require('./workbox-config.js');

module.exports = {
// ...
plugins: [injectManifest(workboxConfig)],
};
```

You can also customize the console output after workbox has created your service worker by passing an optional render function as the second argument to the plugin:

```js
const { injectManifest } = require('rollup-plugin-workbox');

const workboxConfig = require('./workbox-config.js');

module.exports = {
// ...
plugins: [
injectManifest(workboxConfig, {
render: ({ swDest, count, size }) => {
console.log('📦', swDest, '#️⃣', count, '🐘', size);
},
}),
],
};
```

### Bundling

When using `injectManifest`, your service worker will also automatically get bundled via `esbuild`. During bundling, any mentions of Workbox's internal usage of `process.env` variables will also be replaced, so you'll end up with a service worker that will have browser-compatible syntax only.

You can override the `esbuild` options used like so:

```js
const { injectManifest } = require('rollup-plugin-workbox');

const workboxConfig = require('./workbox-config.js');

module.exports = {
// ...
plugins: [
injectManifest(workboxConfig, {
esbuild: {
minify: false,
},
}),
],
};
```
10 changes: 10 additions & 0 deletions demo/injectManifestSwSrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** Handles ESM imports */
import { a } from './some-module.js';
console.log(a);

/** Handles process.env */
if (process.env.NODE_ENV === 'production') {
console.log('foo');
}

window.workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);
1 change: 1 addition & 0 deletions demo/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello workbox');
35 changes: 35 additions & 0 deletions demo/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { generateSW, injectManifest } from '../index.mjs';

export default {
input: 'demo/main.js',
output: {
file: 'demo/dist/bundle.js',
format: 'esm',
},
plugins: [
generateSW(
{
swDest: 'demo/dist/generateSW_sw.js',
globDirectory: 'demo/dist/',
globIgnores: ['injectManifest_sw.js'],
},
{
render: ({ swDest, count, size }) => {
console.log(`\nCustom render! ${swDest}`);
console.log(
`Custom render! The service worker will precache ${count} URLs, totaling ${size}.\n`,
);
},
},
),
injectManifest(
{
swSrc: 'demo/injectManifestSwSrc.js',
swDest: 'demo/dist/injectManifest_sw.js',
globDirectory: 'demo/dist/',
globIgnores: ['generateSW_sw.js'],
},
{ esbuild: { minify: false } },
),
],
};
1 change: 1 addition & 0 deletions demo/some-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1;
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this file is autogenerated with the generate-mjs-dts-entrypoints script
export * from './dist/index.js';
6 changes: 6 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// this file is autogenerated with the generate-mjs-dts-entrypoints script
import cjsEntrypoint from './dist/index.js';

const { generateSW, injectManifest } = cjsEntrypoint;

export { generateSW, injectManifest };
Loading

0 comments on commit e0a03e7

Please sign in to comment.