diff --git a/.gitignore b/.gitignore index 8bc7941666..baac52a82b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ examples/**/dist coverage *.lcov .nyc_output +.vscode diff --git a/examples/offline-nuxt/nuxt.config.js b/examples/offline-nuxt/nuxt.config.js index 168ad0499c..915a70f372 100644 --- a/examples/offline-nuxt/nuxt.config.js +++ b/examples/offline-nuxt/nuxt.config.js @@ -1,6 +1,6 @@ module.exports = { offline: true, // true or https://github.com/NekR/offline-plugin/blob/master/docs/options.md plugins: [ - { src: '~plugins/init-offline.js', ssr: false } + { src: '~plugins/offline.js', ssr: false } ] } diff --git a/examples/offline-nuxt/package.json b/examples/offline-nuxt/package.json index 20c76d1b6b..e202ad873a 100644 --- a/examples/offline-nuxt/package.json +++ b/examples/offline-nuxt/package.json @@ -1,8 +1,11 @@ { "name": "offline-config-nuxt", "scripts": { - "dev": "../../bin/nuxt", - "build": "../../bin/nuxt build", - "start": "../../bin/nuxt start" + "dev": "nuxt", + "build": "nuxt build", + "start": "nuxt start" + }, + "dependencies": { + "nuxt": "latest" } } diff --git a/examples/offline-nuxt/pages/index.vue b/examples/offline-nuxt/pages/index.vue index bb1b2bacae..5846d84ea3 100644 --- a/examples/offline-nuxt/pages/index.vue +++ b/examples/offline-nuxt/pages/index.vue @@ -1,5 +1,3 @@ diff --git a/examples/offline-nuxt/plugins/init-offline.js b/examples/offline-nuxt/plugins/init-offline.js deleted file mode 100644 index a0fd8f5edb..0000000000 --- a/examples/offline-nuxt/plugins/init-offline.js +++ /dev/null @@ -1,20 +0,0 @@ -if (process.env.NODE_ENV === 'production') { - require('offline-plugin/runtime').install() - window.onNuxtReady((app) => { - if ('serviceWorker' in navigator) { - if (navigator.serviceWorker.controller) {} else { - navigator.serviceWorker.register('/sw.js').then(function(res) { - console.log('sw loaded...') - }).catch(function(err) { - console.log(err); - }); - } - } else if (window.applicationCache) { - // register appcache code - var iframe = document.createElement('iframe'); - iframe.style.display = "none"; - iframe.src = '/appcache/manifest.html'; - document.body.appendChild(iframe); - } - }) -} diff --git a/examples/offline-nuxt/plugins/offline.js b/examples/offline-nuxt/plugins/offline.js new file mode 100644 index 0000000000..7224e02c00 --- /dev/null +++ b/examples/offline-nuxt/plugins/offline.js @@ -0,0 +1,19 @@ +if (process.env.NODE_ENV === 'production') { + var OfflinePlugin = require('offline-plugin/runtime') + window.onNuxtReady(() => { + OfflinePlugin.install({ + onInstalled: function () { + console.log('Offline plugin installed.') // eslint-disable-line no-console + }, + onUpdating: function () { + + }, + onUpdateReady: function () { + OfflinePlugin.applyUpdate() + }, + onUpdated: function () { + window.location.reload() + } + }) + }) +} diff --git a/lib/webpack/base.config.js b/lib/webpack/base.config.js index cd45498db6..5d560d306d 100644 --- a/lib/webpack/base.config.js +++ b/lib/webpack/base.config.js @@ -67,6 +67,7 @@ export default function ({ isClient, isServer }) { exclude: /node_modules/, query: defaults(this.options.build.babel, { presets: ['vue-app'], + babelrc: false, cacheDirectory: !!this.dev }) }, diff --git a/lib/webpack/client.config.js b/lib/webpack/client.config.js index b9a3616708..c2e53b90a5 100644 --- a/lib/webpack/client.config.js +++ b/lib/webpack/client.config.js @@ -1,6 +1,6 @@ 'use strict' -import { each } from 'lodash' +import { each, defaults } from 'lodash' import webpack from 'webpack' import HTMLPlugin from 'html-webpack-plugin' import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin' @@ -11,38 +11,6 @@ import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer' import OfflinePlugin from 'offline-plugin' import base from './base.config.js' import { resolve } from 'path' -import fs from 'fs-extra' -import pify from 'pify' -const rename = pify(fs.rename) - -// offline plugin copy generated assets to static directory -function OfflinePluginCopyAssetsPlugin (assets, toDir) { - this.assets = assets - this.toDir = toDir -} -OfflinePluginCopyAssetsPlugin.prototype.apply = function (compiler) { - compiler.plugin('after-emit', function (compilation, callback) { - const assets = this.assets.length > 0 ? this.assets : [] - - if (!fs.existsSync(this.toDir)) { - fs.mkdirSync(this.toDir) - fs.mkdirSync(`${this.toDir}/appcache`) - } - - let renamePromises = [] - assets.forEach((asset) => { - renamePromises.push(rename(`.nuxt/dist/${asset}`, `${this.toDir}/${asset}`)) - }) - - Promise.all(renamePromises) - .then(() => { - console.log('\nOffline content to static directory...') // eslint-disable-line no-console - }) - .catch((error) => { - console.error('\nOffline-plugin copy error', error) // eslint-disable-line no-console - }) - }.bind(this)) -} /* |-------------------------------------------------------------------------- @@ -148,11 +116,7 @@ export default function () { if (!this.dev && this.options.offline) { const offlineOpts = typeof this.options.offline === 'object' ? this.options.offline : {} config.plugins.push( - new OfflinePlugin(offlineOpts), - new OfflinePluginCopyAssetsPlugin( - ['sw.js', 'appcache/manifest.appcache', 'appcache/manifest.html'], - 'static' - ) + new OfflinePlugin(defaults(offlineOpts, {})) ) } // Webpack Bundle Analyzer diff --git a/lib/webpack/vue-loader.config.js b/lib/webpack/vue-loader.config.js index 7b431505ac..d419725ddf 100644 --- a/lib/webpack/vue-loader.config.js +++ b/lib/webpack/vue-loader.config.js @@ -5,6 +5,7 @@ import { defaults } from 'lodash' export default function ({ isClient }) { let babelOptions = JSON.stringify(defaults(this.options.build.babel, { presets: ['vue-app'], + babelrc: false, cacheDirectory: !!this.dev })) let config = {