Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const manifest = originalManifest.filter(
(entry) => entry.url !== 'ignored.html');
// Optionally, set warning messages.
const warnings = ['warning'];
return {manifest, warnings: [] };
}
]
});
}
// InjectManifest
{
let plugin: InjectManifest;
// No options object
plugin = new InjectManifest();
// Minimal options object (swSrc is required)
plugin = new InjectManifest({
swSrc: 'service-worker.js'
});
// With all of the examples
plugin = new InjectManifest({
swDest: 'custom-sw-name.js',
importWorkboxFrom: 'local',
// *Only* include assets that belong to these chunks:
chunks: ['chunk-name-1', 'chunk-name-2'],
// Exclude assets that belong to these chunks:
excludeChunks: ['chunk-name-1', 'chunk-name-2'],
// Only include HTML and JS assets when precaching:
include: [/\.html$/, /\.js$/],
'window.React': 'React',
ReactDom: 'ReactDOM',
ReactDOM: 'ReactDOM',
'window.ReactDOM': 'ReactDOM',
'window.ReactDom': 'ReactDOM',
}),
] : []),
new webpack.EnvironmentPlugin({ pawConfig: JSON.stringify(pawConfig), ...process.env }),
...(isHot ? [] : [new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: 'css/[hash].css',
chunkFilename: 'css/[chunkhash].css',
})]),
...(pawConfig.serviceWorker ? [
new WorkboxPlugin.InjectManifest({
swSrc: pawExistsSync(path.join(process.env.LIB_ROOT || '', 'src', 'service-worker')),
swDest: 'sw.js',
}),
new SwVariables({
fileName: 'sw.js',
variables: { workboxDebug: true, ...pawConfig },
text: projectSW,
}),
] : []),
...devPlugins,
],
};
});
} else {
// env.beta and env.release
config.plugins.push(
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ['.awcache', 'node_modules/.cache']
}),
// Tell React we're in Production mode
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
'process.env': JSON.stringify({ NODE_ENV: 'production' })
}),
// Generate a service worker
new InjectManifest({
include: [/\.(html|js|css|woff2|json|wasm)$/, /static\/.*\.(png|gif|jpg|svg)$/],
exclude: [
/version\.json/,
/extension-dist/,
/\.map$/,
// Ignore both the webapp manifest and the d1-manifest files
/data\/d1\/manifests/,
/manifest-webapp/
],
swSrc: './src/service-worker.js',
swDest: 'service-worker.js',
importWorkboxFrom: 'local'
})
);
if (process.env.PT_PROJECT_TOKEN) {
rendererConfig.entry = './src/renderer/renderer.tsx';
rendererConfig.target = 'electron-renderer';
rendererConfig.output.filename = 'renderer.bundle.js';
rendererConfig.plugins = [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './public/index.html'),
}),
new CopyPlugin([
{ from: srcPaths('static'), to: srcPaths('docs') },
])
];
if (isEnvProduction) {
rendererConfig.plugins.push(
// @see https://github.com/xiaoiver/sw-tools/issues/1
new InjectManifest({
swSrc: srcPaths('src/service-worker.js'),
swDest: 'service-worker.js',
exclude: [/\.png|\.DS_Store$/],
importWorkboxFrom: 'local'
})
);
}
module.exports = [mainConfig, rendererConfig];
new webpack.NoEmitOnErrorsPlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new HtmlWebpackPlugin({
filename: 'index.html',
template,
inject: true
})
]
})
if (process.env.SW_ENV === 'development') {
try {
fs.accessSync(swPath, fs.constants.F_OK)
devConf.plugins.push(
new WorkboxPlugin.InjectManifest({
swSrc: swPath,
importWorkboxFrom: 'disabled'
})
)
} catch (error) {
console.log(`\n在应用的 dist/sw 未找到 sw.js 文件,需要先运行 npm run sw:prod 生成对应文件。\n`)
}
}
return devConf
}
applyInjectManifest(compiler) {
let injectManifest;
if (this.config.injectManifestConfig) {
injectManifest = new WorkboxPlugin.InjectManifest(
this.config.injectManifestConfig
);
} else {
injectManifest = new WorkboxPlugin.InjectManifest({
swSrc: this.config.paths.src + '/sw.js',
swDest: this.config.paths.dest + '/sw.js'
});
}
injectManifest.apply(compiler);
}
applyInjectManifest(compiler) {
let injectManifest;
if (this.config.injectManifestConfig) {
injectManifest = new WorkboxPlugin.InjectManifest(
this.config.injectManifestConfig
);
} else {
injectManifest = new WorkboxPlugin.InjectManifest({
swSrc: this.config.paths.src + '/sw.js',
swDest: this.config.paths.dest + '/sw.js'
});
}
injectManifest.apply(compiler);
}
module.exports = function(config, helpers, workboxConfig) {
const defaultConfig = getDefaultConfig(config);
delete defaultConfig['navigateFallback'];
delete defaultConfig['navigateFallbackWhitelist'];
const esmConfig = getESMDefaultConfig(config);
delete esmConfig['navigateFallback'];
delete esmConfig['navigateFallbackWhitelist'];
esmConfig.swDest = defaultConfig.swDest.substring(0, defaultConfig.swDest.lastIndexOf('.js')) + '-esm.js';
esmConfig.include.push(/(\.[\w]{5}\.esm\.js)/);
const swGenerator = new InjectManifest(Object.assign({}, defaultConfig, workboxConfig));
return replaceDefaultPugin(config, helpers, swGenerator);
}
export default (plugins = []) => {
manifest.icons.map(icon => {
const inCwdPath = path.resolve(process.cwd(), icon.src)
if (fs.existsSync(inCwdPath)) {
icon.src = inCwdPath
}
return icon
})
plugins = plugins.concat(new GenerateSW())
if (fs.existsSync(swSrc)) {
plugins.push(new InjectManifest({ swSrc }))
}
return plugins.concat([
new WebpackPwaManifestPlugin(manifest),
new WebpackManifestPlugin({
fileName: './manifest.webpack.json'
}),
new webpack.DefinePlugin({
mhy: JSON.stringify(mhyConfig)
})
])
}