Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const COMPILER_NAME = 'serviceworker-plugin'
export default class ServiceWorkerPlugin {
options = []
warnings = []
assets = []
assetMap = {}
dependencyMap = {}

constructor(options) {
this.options = Object.assign(
Expand Down Expand Up @@ -175,6 +178,52 @@ export default class ServiceWorkerPlugin {

assets = validatePaths(assets, this.options)

let shouldUpdate = true

if (compiler.watchMode === true) {
const oldAssetsLength = this.assets.length

for (let asset of assets) {
if (!this.assetMap[asset]) {
this.assetMap[asset] = true
this.assets.push(asset)
}
}

assets = this.assets

shouldUpdate = oldAssetsLength !== assets.length

const childCompilations = compilation.children.filter(child => child.name === COMPILER_NAME)
const childCompilation = childCompilations.length ? childCompilations[0] : { fileDependencies: [] }
const childDependencies = [...childCompilation.fileDependencies]

const dependencyMap = {}
for (let dependency of childDependencies) {
dependencyMap[dependency] = compiler.inputFileSystem.readFileSync(dependency, { encoding: 'utf-8' })
}

if (!shouldUpdate) {
if (Object.keys(dependencyMap).length === Object.keys(this.dependencyMap).length) {
for (let dependency of childDependencies) {
if (shouldUpdate || dependencyMap[dependency] !== this.dependencyMap[dependency]) {
shouldUpdate = true
}
}
}
else {
shouldUpdate = true
}
}

this.dependencyMap = dependencyMap
}

if (!shouldUpdate) {
callback()
return
}

const serviceWorkerOption = this.options.transformOptions({
assets,
jsonStats,
Expand Down