Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function getWorkboxSWImport(compilation, config) {
switch (config.importWorkboxFrom) {
case 'cdn': {
return [getModuleUrl('workbox-sw')];
}
case 'local': {
// This will create a local copy of the Workbox runtime libraries in
// the output directory, independent of the webpack build pipeline.
// In general, this should work, but one thing to keep in mind is that
// when using the webpack-dev-server, the output will be created on
// disk, rather than in the in-memory filesystem. (webpack-dev-server will
// still be able to serve the runtime libraries from disk.)
const wbDir = await copyWorkboxLibraries(path.join(
compilation.options.output.path, config.importsDirectory));
// We need to set this extra option in the config to ensure that the
// workbox library loader knows where to get the local libraries from.
config.modulePathPrefix = (compilation.options.output.publicPath || '') +
path.join(config.importsDirectory, wbDir).split(path.sep).join('/');
const workboxSWImport = config.modulePathPrefix + '/workbox-sw.js';
return [workboxSWImport];
}
case 'disabled': {
return null;
}
default: {
assert(Array.isArray(params.input), errors['missing-input']);
// Default to showing the help message if there's no command provided.
const [command = 'help', option] = params.input;
switch (command) {
case 'wizard': {
await runWizard(params.flags);
break;
}
case 'copyLibraries': {
assert(option, errors['missing-dest-dir-param']);
const parentDirectory = upath.resolve(process.cwd(), option);
const dirName = await workboxBuild.copyWorkboxLibraries(parentDirectory);
const fullPath = upath.join(parentDirectory, dirName);
logger.log(`The Workbox libraries were copied to ${fullPath}`);
logger.log(ol`Add a call to workbox.setConfig({modulePathPrefix: '...'})
to your service worker to use these local libraries.`);
logger.log(`See https://goo.gl/Fo9gPX for further documentation.`);
break;
}
case 'generateSW':
case 'injectManifest': {
const configPath = upath.resolve(process.cwd(),
option || constants.defaultConfigFile);
let config;
try {
join(globals.rootDir, src),
join(build.path, dest),
options
);
}
));
}
}
// SPA build process
else {
await webpackCompile(await this.createSPAConfig(), build.stats);
}
if (serviceWorker.enable !== false) {
// Copy workbox files to dist/static/workbox-v3.*.*/
await copyWorkboxLibraries(join(build.path, ASSETS_DIRNAME_IN_DIST));
}
}
}
async function getWorkboxSWImport(compilation, config) {
switch (config.importWorkboxFrom) {
case 'cdn': {
return [getModuleUrl('workbox-sw')];
}
case 'local': {
// This will create a local copy of the Workbox runtime libraries in
// the output directory, independent of the webpack build pipeline.
// In general, this should work, but one thing to keep in mind is that
// when using the webpack-dev-server, the output will be created on
// disk, rather than in the in-memory filesystem. (webpack-dev-server will
// still be able to serve the runtime libraries from disk.)
const wbDir = await copyWorkboxLibraries(compilation.options.output.path);
const workboxSWImport = (compilation.options.output.publicPath || '')
+ wbDir + '/workbox-sw.js';
// We need to set this extra option in the config to ensure that the
// workbox library loader knows where to get the local libraries from.
config.modulePathPrefix = wbDir;
return [workboxSWImport];
}
case 'disabled': {
return null;
}
default: {
// If importWorkboxFrom is anything else, then treat it as the name of
// a webpack chunk that corresponds to the custom compilation of the
// Workbox code.
async function buildWorker() {
let app = {
buildId: fs.readFileSync(`${dotNext}/BUILD_ID`, 'utf8'),
precaches: []
}
const stats = await loadJsonFile(`${dotNext}/build-stats.json`);
Object.keys(stats).map(src => {
// /_next/9265fa769281943ee96625770433e573/app.js
app.precaches.push(`/_next/${stats[src].hash}/${src}`)
})
app = await addChunks(app);
app = await addBundles(app);
const workboxSrc = await workbox.copyWorkboxLibraries(dotNext);
fs.writeFileSync(
target,
swSnippet(
JSON.stringify(app.precaches, null, 2),
workboxSrc,
),
'utf8'
);
}
async copyLibraries() {
try {
const libraryPath = ".workbox";
const destination = path.resolve(this.options.outDir, libraryPath);
await workbox.copyWorkboxLibraries(destination);
const vendorImports = glob.sync(path.join(destination, "**/workbox-sw.js"), {
dot: true
}).map(p => path.normalize(p).replace(path.normalize(this.options.outDir), ""));
this.importScripts = this.importScripts.concat(vendorImports);
this.hasCopiedLibraries = true;
} catch (error) {
throw error;
}
}
async importWorkboxLibraries({ importWorkboxFrom, swURLRoot, swDestRoot }) {
if (this.options.importWorkboxFrom === 'local') {
try {
const workboxPkg = findUp.sync(
'node_modules/workbox-sw/package.json',
__dirname
);
const workboxName = path.basename(require(workboxPkg).main);
return `${swURLRoot}/${await copyWorkboxLibraries(
swDestRoot
)}/${workboxName}`;
} catch (e) {
throw e;
}
} else {
return getModuleURL('workbox-sw');
}
}