Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
gulp.task('service-worker', () => {
return workboxBuild.injectManifest({
swSrc: path.join(__dirname, './public/src/js/sw.js'),
swDest: path.join(__dirname, './sw.js'),
globDirectory: path.join(__dirname, './'),
// 注意:这里有个坑!后缀{js,css,xxx} 逗号之间不能加空格,否则识别有问题!!!
globPatterns: [
'public/**/*.{js,css,html,png,jpg,svg,ico,swf}',
// 'node_modules/bootstrap/**/*.min.{js,css}',
// 'node_modules/@fortawesome/**/*.{min.css,eot,ttf,woff,woff2}',
// 'node_modules/blueimp-file-upload/**/*.{js,css}',
// 'node_modules/sweetalert/dist/**/*.{js,css}',
// 'node_modules/jquery/dist/**/*.{js,css}',
// 'node_modules/metismenu/dist/**/*.{js,css}',
// 'node_modules/lodash/**/*.min.js',
// 'node_modules/fuelux/dist/**/*.{js,css}',
// 'node_modules/simplemde/dist/**/*.{js,css}',
function buildSw() {
return workboxBuild.injectManifest({
swSrc: 'app/sw.js',
swDest: 'dist/sw.js',
globDirectory: 'dist',
globPatterns: [
'index.html',
'scripts/main.min.js',
'styles/main.css',
'images/*',
'images/touch/*'
]
}).catch(err => {
console.log('Uh oh 😬', err);
});
}
const { injectManifest } = require('workbox-build');
const swSrc = './sw-template.js';
const swDest = 'dist/sw.js';
injectManifest({
swSrc,
swDest,
globDirectory: 'dist'
}).then(({count, size}) => {
console.log(`Generated ${swDest}, which will precache ${count} files, totaling ${size} bytes.`);
});
const workboxSWDestMapPath = `${workboxSWDestPath}.map`
fs.createReadStream(workboxSWSrcPath).pipe(fs.createWriteStream(workboxSWDestPath))
fs.createReadStream(workboxSWSrcMapPath).pipe(fs.createWriteStream(workboxSWDestMapPath))
const updateUrl = (manifestEntries) => manifestEntries.map((entry) => {
if (entry.url.startsWith(buildPrefix)) {
const regex = new RegExp(buildPrefix, 'g')
entry.url = entry.url.replace(regex, '')
}
return entry
})
config.manifestTransforms = [updateUrl]
swBuild.injectManifest(config).then(() => {
const wbSwRegex = /{fileName}/g
fs.readFile(config.swDest, 'utf8', (err, data) => {
if (err) {
throw err
}
const swFileContents = data.replace(wbSwRegex, wbFileName)
fs.writeFile(config.swDest, swFileContents, () => {
console.log('Pre-cache Manifest generated.')
})
})
})
const swBuild = require('workbox-build');
const patterns = ['assets/templates/*.html',
'assets/templates/*.json',
'scripts/client.js',
'styles/main.css'];
const config = {
globDirectory: './dist/server/public/',
globPatterns: patterns,
swSrc: './dist/server/public/sw.src.js',
swDest: './dist/server/public/sw.js',
injectionPointRegexp: /(\[)insertfileshere(\])/,
modifyUrlPrefix: {'': '/'}
};
swBuild.injectManifest(config).then((data) => {
console.log(data);
console.log('Build Manifest generated.');
});
builder.plugin('built', () => {
if (workboxOptions.swSrc) {
return swBuild.injectManifest(workboxOptions)
} else {
return swBuild.generateSW(workboxOptions)
}
})
})
function buildSW(matchers) {
return workbox.injectManifest({
globDirectory: './src',
globPatterns: ['**\/*.{html,js,css,png,jpg,json}'],
globIgnores: ['sw.main.js','404.html', 'images/icons/**/*', 'index.html'],
swSrc: './build/sw.main.js',
swDest: './public/sw.main.js',
});
}
try {
const defaultConfig = {
globDirectory: this.options.outDir,
globPatterns: [
'**/*.{js,css,html,png,jpg,jpeg,gif,tiff}'
],
swSrc: destination + ".tmp",
swDest: destination
}
const cfg = Object.assign(defaultConfig, config);
logger.progress('Injecting service worker manifest...');
fs.copySync(cfg.swDest, cfg.swDest + ".tmp");
let { count, size } = await workbox.injectManifest(cfg);
fs.unlinkSync(cfg.swDest + ".tmp");
logger.log('Service worker manifest injected at: ' + cfg.swDest);
logger.log(`Will precache ${count} files, totaling ${size} bytes.`);
} catch (error) {
throw error;
}
}
const hook = () => {
const opts = pick(options, [
'swDest', 'swSrc', 'globDirectory', 'globFollow', 'globIgnores', 'globPatterns', 'dontCacheBustUrlsMatching',
'globStrict', 'templatedUrls', 'maximumFileSizeToCacheInBytes', 'modifyUrlPrefix', 'manifestTransforms'
])
return swBuild.injectManifest(opts)
}