@vue-storefront/nuxt 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/helpers/isProduction.js +1 -0
- package/lib/helpers/log.js +9 -0
- package/lib/helpers/merge.js +7 -0
- package/lib/helpers/resolveDependency.js +7 -0
- package/lib/module.js +97 -0
- package/lib/modules/performance.js +31 -0
- package/lib/modules/raw-sources-loader.js +21 -0
- package/lib/modules/storefront-ui.js +18 -0
- package/lib/plugins/composition-api.js +4 -0
- package/lib/plugins/context.js +18 -0
- package/lib/plugins/e2e-testing.js +13 -0
- package/lib/plugins/i18n-cookies.js +34 -0
- package/lib/plugins/logger.js +8 -0
- package/lib/plugins/ssr.js +31 -0
- package/package.json +28 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = (options) => process.env.NODE_ENV === 'production' || options.coreDevelopment;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const consola = require('consola');
|
|
2
|
+
const chalk = require('chalk');
|
|
3
|
+
|
|
4
|
+
module.exports = Object.freeze({
|
|
5
|
+
info: (message) => consola.info(chalk.bold('VSF'), message),
|
|
6
|
+
success: (message) => consola.success(chalk.bold('VSF'), message),
|
|
7
|
+
warning: (message) => consola.warning(chalk.bold('VSF'), message),
|
|
8
|
+
error: (message) => consola.error(chalk.bold('VSF'), message)
|
|
9
|
+
});
|
package/lib/module.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// TODO proper bundling, for now it's just to experiment with nuxt modules api
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const log = require('./helpers/log');
|
|
5
|
+
const merge = require('./helpers/merge');
|
|
6
|
+
const resolveDependency = require('./helpers/resolveDependency');
|
|
7
|
+
const performanceModule = require('./modules/performance');
|
|
8
|
+
const storefrontUiModule = require('./modules/storefront-ui');
|
|
9
|
+
const rawSourcesModule = require('./modules/raw-sources-loader');
|
|
10
|
+
|
|
11
|
+
module.exports = function VueStorefrontNuxtModule (moduleOptions) {
|
|
12
|
+
const defaultOptions = {
|
|
13
|
+
coreDevelopment: false,
|
|
14
|
+
performance : {
|
|
15
|
+
httpPush: true,
|
|
16
|
+
purgeCSS: {
|
|
17
|
+
enabled: false,
|
|
18
|
+
paths: [
|
|
19
|
+
'**/*.vue'
|
|
20
|
+
]
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
useRawSource: {
|
|
24
|
+
dev: [],
|
|
25
|
+
prod: []
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const options = merge(defaultOptions, moduleOptions);
|
|
30
|
+
|
|
31
|
+
// Add meta data
|
|
32
|
+
this.options.head.meta.push({
|
|
33
|
+
name: 'generator',
|
|
34
|
+
content: 'Vue Storefront 2'
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
log.info('Starting Vue Storefront Nuxt Module');
|
|
38
|
+
|
|
39
|
+
// Enable HTTP/2 push for JS files
|
|
40
|
+
if (options.performance.httpPush) {
|
|
41
|
+
this.options.render = merge(this.options.render, {
|
|
42
|
+
http2: {
|
|
43
|
+
push: true,
|
|
44
|
+
pushAssets: (request, response, publicPath, preloadFiles) => {
|
|
45
|
+
return preloadFiles
|
|
46
|
+
.filter(({ asType }) => asType === 'script')
|
|
47
|
+
.map(({ file, asType }) => `<${publicPath}${file}>; rel=preload; as=${asType}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Context plugin
|
|
54
|
+
this.addPlugin(path.resolve(__dirname, 'plugins/context.js'))
|
|
55
|
+
log.success('Installed Vue Storefront Context plugin');
|
|
56
|
+
|
|
57
|
+
// SSR plugin
|
|
58
|
+
this.addPlugin(path.resolve(__dirname, 'plugins/ssr.js'));
|
|
59
|
+
log.success('Installed Vue Storefront SSR plugin');
|
|
60
|
+
|
|
61
|
+
// Logger plugin
|
|
62
|
+
this.addPlugin({
|
|
63
|
+
src: path.resolve(__dirname, 'plugins/logger.js'),
|
|
64
|
+
options: moduleOptions.logger || {}
|
|
65
|
+
});
|
|
66
|
+
log.success('Installed VSF Logger plugin');
|
|
67
|
+
|
|
68
|
+
// Context plugin
|
|
69
|
+
this.addPlugin(path.resolve(__dirname, 'plugins/e2e-testing.js'))
|
|
70
|
+
log.success('Installed Vue Storefront E2E testing plugin');
|
|
71
|
+
|
|
72
|
+
// i18n-cookies plugin
|
|
73
|
+
this.addPlugin({
|
|
74
|
+
src: path.resolve(__dirname, 'plugins/i18n-cookies.js'),
|
|
75
|
+
options: this.options.i18n
|
|
76
|
+
});
|
|
77
|
+
log.success('Installed Internationalization Cookies plugin');
|
|
78
|
+
|
|
79
|
+
// Composition API plugin
|
|
80
|
+
this.addModule('@nuxtjs/composition-api');
|
|
81
|
+
log.success('Installed nuxt Composition API Module');
|
|
82
|
+
|
|
83
|
+
// StorefrontUI module
|
|
84
|
+
if (fs.existsSync(resolveDependency('@storefront-ui/vue'))) {
|
|
85
|
+
storefrontUiModule.call(this, options);
|
|
86
|
+
log.success('Installed StorefrontUI Module');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Performance module
|
|
90
|
+
performanceModule.call(this, options);
|
|
91
|
+
log.success('Installed Performance Module');
|
|
92
|
+
|
|
93
|
+
// Raw sources loader
|
|
94
|
+
rawSourcesModule.call(this, options);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
module.exports.meta = require('../package.json')
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const { merge } = require('lodash');
|
|
2
|
+
|
|
3
|
+
function pushScripts() {
|
|
4
|
+
this.options.render = merge(this.options.render, {
|
|
5
|
+
http2: {
|
|
6
|
+
push: true,
|
|
7
|
+
pushAssets: (request, response, publicPath, preloadFiles) => {
|
|
8
|
+
return preloadFiles
|
|
9
|
+
.filter(({ asType }) => asType === 'script')
|
|
10
|
+
.map(({ file, asType }) => `<${publicPath}${file}>; rel=preload; as=${asType}`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function loadPurgeCss(options) {
|
|
17
|
+
// PurgeCSS module should be installed after all other modules
|
|
18
|
+
this.nuxt.hook('modules:done', moduleContainer => moduleContainer.addModule([ 'nuxt-purgecss', options ]));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = function VueStorefrontPerformanceModule (options) {
|
|
22
|
+
const { httpPush, purgeCSS } = options.performance;
|
|
23
|
+
|
|
24
|
+
if (httpPush) {
|
|
25
|
+
pushScripts.call(this);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (purgeCSS && purgeCSS.enabled) {
|
|
29
|
+
loadPurgeCss.call(this, purgeCSS);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const log = require('../helpers/log');
|
|
2
|
+
const isProduction = require('../helpers/isProduction');
|
|
3
|
+
const resolveDependency = require('../helpers/resolveDependency');
|
|
4
|
+
|
|
5
|
+
module.exports = function VueStorefrontPerformanceModule (options) {
|
|
6
|
+
const useRawSource = (package) => {
|
|
7
|
+
const pkgPath = resolveDependency(`${package}/package.json`);
|
|
8
|
+
const pkg = require(pkgPath);
|
|
9
|
+
|
|
10
|
+
if (pkg.module) {
|
|
11
|
+
this.extendBuild(config => {
|
|
12
|
+
config.resolve.alias[pkg.name + '$'] = resolveDependency(`${package}/${pkg.module}`);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
this.options.build.transpile.push(package);
|
|
17
|
+
log.info(`Using raw source/ESM for ${pkg.name}`);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
options.useRawSource[isProduction(options) ? 'prod' : 'dev'].map(package => useRawSource(package));
|
|
21
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const merge = require('../helpers/merge');
|
|
2
|
+
|
|
3
|
+
// TODO: Create a separate nuxt module for storefront ui
|
|
4
|
+
function loadStorefrontRawSources (options) {
|
|
5
|
+
const rawSources = [
|
|
6
|
+
'@storefront-ui/vue',
|
|
7
|
+
'@storefront-ui/shared'
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
options.useRawSource = merge(options.useRawSource, {
|
|
11
|
+
dev: rawSources,
|
|
12
|
+
prod: rawSources
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = function VueStorefrontPerformanceModule (options) {
|
|
17
|
+
loadStorefrontRawSources.call(this, options);
|
|
18
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
import { configureContext } from '@vue-storefront/core'
|
|
3
|
+
import { useContext as useBaseContext } from '@nuxtjs/composition-api';
|
|
4
|
+
|
|
5
|
+
const contextPlugin = (ctx, inject) => {
|
|
6
|
+
const sharedMap = new Map();
|
|
7
|
+
|
|
8
|
+
const useVSFContext = () => {
|
|
9
|
+
const { $vsf, ...context } = useBaseContext();
|
|
10
|
+
|
|
11
|
+
return { $vsf, ...context, ...$vsf }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
configureContext({ useVSFContext });
|
|
15
|
+
inject('sharedRefsMap', sharedMap)
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default contextPlugin;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import Vue from 'vue';
|
|
2
|
+
|
|
3
|
+
const e2eTestingPlugin = (ctx) => {
|
|
4
|
+
Vue.directive('e2e', {
|
|
5
|
+
bind: (element, binding) => {
|
|
6
|
+
const enabled = ctx.isDev || ctx.env.NUXT_ENV_E2E === true.toString();
|
|
7
|
+
|
|
8
|
+
return enabled && element.setAttribute(`data-e2e`, binding.value);
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default e2eTestingPlugin;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const { VSF_CURRENCY_COOKIE, VSF_COUNTRY_COOKIE } = require('@vue-storefront/core');
|
|
2
|
+
|
|
3
|
+
const i18nCookiesPlugin = ({ $cookies }) => {
|
|
4
|
+
const i18n = <%= serialize(options) %>;
|
|
5
|
+
|
|
6
|
+
const settings = {
|
|
7
|
+
defaultLocale: i18n.defaultLocale || (i18n.locales.length && i18n.locales[0].code),
|
|
8
|
+
currency: i18n.currency || (i18n.currencies.length && i18n.currencies[0].name),
|
|
9
|
+
country: i18n.country || (i18n.countries.length && i18n.countries[0].name)
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const missingFields = Object
|
|
13
|
+
.entries(settings)
|
|
14
|
+
.reduce((carry, [name, value]) => {
|
|
15
|
+
!value && carry.push(name);
|
|
16
|
+
|
|
17
|
+
return carry;
|
|
18
|
+
}, []);
|
|
19
|
+
|
|
20
|
+
if (missingFields.length) {
|
|
21
|
+
throw new Error(`Following fields are missing in the i18n configuration: ${ missingFields.join(', ') }`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const cookieOptions = {
|
|
25
|
+
path: '/',
|
|
26
|
+
sameSite: 'lax',
|
|
27
|
+
expires: new Date(new Date().setFullYear(new Date().getFullYear() + 1)) // Year from now
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
!$cookies.get(VSF_CURRENCY_COOKIE) && $cookies.set(VSF_CURRENCY_COOKIE, settings.currency, cookieOptions);
|
|
31
|
+
!$cookies.get(VSF_COUNTRY_COOKIE) && $cookies.set(VSF_COUNTRY_COOKIE, settings.country, cookieOptions);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export default i18nCookiesPlugin;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { configureSSR } from '@vue-storefront/core'
|
|
2
|
+
import { ssrRef, getCurrentInstance, onServerPrefetch } from '@nuxtjs/composition-api';
|
|
3
|
+
|
|
4
|
+
const hasRouteChanged = (ctx) => {
|
|
5
|
+
const { from } = ctx.$router.app.context;
|
|
6
|
+
const { current } = ctx.$router.history
|
|
7
|
+
|
|
8
|
+
if (!from) {
|
|
9
|
+
return false
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return from.fullPath !== current.fullPath
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const ssrPlugin = () => {
|
|
16
|
+
configureSSR({
|
|
17
|
+
vsfRef: ssrRef,
|
|
18
|
+
onSSR: (fn) => {
|
|
19
|
+
onServerPrefetch(fn);
|
|
20
|
+
if (typeof window !== 'undefined') {
|
|
21
|
+
const vm = getCurrentInstance();
|
|
22
|
+
|
|
23
|
+
if (hasRouteChanged(vm)) {
|
|
24
|
+
fn();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default ssrPlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vue-storefront/nuxt",
|
|
3
|
+
"version": "2.4.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "lib/module.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "Vue Storefront",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@nuxt/typescript-build": "^2.0.0",
|
|
13
|
+
"@vue/composition-api": "1.0.0-beta.21",
|
|
14
|
+
"@nuxtjs/composition-api": "0.17.0",
|
|
15
|
+
"@nuxtjs/style-resources": "^1.0.0",
|
|
16
|
+
"chalk": "^2.4.2",
|
|
17
|
+
"chokidar": "^3.3.1",
|
|
18
|
+
"consola": "^2.10.1",
|
|
19
|
+
"lodash": "^4.17.15",
|
|
20
|
+
"nuxt-purgecss": "^1.0.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@nuxt/types": "^0.7.9"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
}
|
|
28
|
+
}
|