Skip to content
This repository was archived by the owner on Jul 16, 2022. It is now read-only.

Commit 456060c

Browse files
committed
feat(vue): Add VUE_APP_CURRENT_WEBSITE env on client side
1 parent 49c44fb commit 456060c

File tree

7 files changed

+667
-47
lines changed

7 files changed

+667
-47
lines changed

docs/packages/vue-cli.md

Lines changed: 198 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,37 @@ Then install `@node-sitecore/cli-plugin-vue` with npm or yarn:
2828
$ npm install @node-sitecore/cli-plugin-vue
2929
```
3030

31+
## Cli
32+
33+
This package add a new command:
34+
35+
```bash
36+
Usage: nsc vue <build|check> [options]
37+
38+
Options:
39+
40+
-V, --version output the version number
41+
--configPath <path> Path to .nscrc file
42+
--currentWebsite <code> Default current website to compile source
43+
-p, --pattern <cmd> Glob pattern to list project
44+
-l, --list <cmd> Website code list (EU,FR,etc)
45+
-e, --exclude <cmd> Exclude Website code list (Common,etc)
46+
-h, --help output usage information
47+
48+
Example:
49+
nsc vue build // build only the default currentWebsite
50+
nsc vue build --list EU,FR
51+
nsc vue build --pattern "scr/Project/*" --exclude Hotfix,Common
52+
nsc check // Check if entries exists for a given currentWebsite
53+
```
54+
55+
Build command is a shortcut to compile multiple Sitecore Projects. It's an equivalent of:
56+
57+
```bash
58+
vue-cli-service build --currentWebsite EU --production
59+
vue-cli-service build --currentWebsite FR --production
60+
```
61+
3162
## Configuration
3263
3364
Now, edit the `vue.config.js` and copy this configuration:
@@ -79,7 +110,7 @@ const vueConfig = {
79110
/manifest\.json$/
80111
],
81112
workboxOptions: {
82-
swDest: '../../service-worker.js',
113+
swDest: `../../service-worker.${config.currentWebsite}.js`,
83114
runtimeCaching: []
84115
}
85116
},
@@ -208,11 +239,177 @@ Finally, edit your `.nscrc` file and add Vue-cli configuration:
208239
209240
> A project example is available on this repository [https://github.com/NodeSitecore/sitecore-vue-boilerplate](https://github.com/NodeSitecore/sitecore-vue-boilerplate).
210241
242+
### Options
243+
244+
- `scssMixinsPath`: Add the scss file which include all of your mixins/variables should be shared with Vue Component.
245+
- `outputDir`: Output directory where the source will be compiled.
246+
- `baseUrl`: Set the base url for the different profile.
247+
- `entries`: Configure the bundle compilation strategies (See Define entries section).
248+
- `alias`: Set alias list for webpack (See Define alias section).
249+
211250
## Getters
212251
213252
## Configuration fields
214253
215254
Key | Default value | Tags | Description
216255
--- | --- | --- | ---
217256
vueCli | `{...}` | Vue-cli, Vue | Return the configuration dedicated to Vue-cli.
257+
entries | `[...]` | Vue-cli, Vue | Entries list which will be compilied by webpack
258+
259+
260+
## Define entries
261+
262+
VueCli configuration accept multiple entries files. Webpack will create different bundles according to your configuration.
263+
Also, each entry can be configured by environment profile (production or development).
264+
265+
Here three possible scenario (production, development and all profile):
266+
```json
267+
{
268+
"vueCli": {
269+
"entries": [
270+
{
271+
"mode": "production",
272+
"name": "bundle",
273+
"paths": [
274+
"<projectDir>/<currentWebsite>/code/Scripts/polyfills.js",
275+
"<projectDir>/<currentWebsite>/code/Scripts/entry.production.js"
276+
]
277+
},
278+
{
279+
"mode": "development",
280+
"name": "bundle",
281+
"paths": [
282+
"<projectDir>/<currentWebsite>/code/Scripts/polyfills.js",
283+
"<projectDir>/<currentWebsite>/code/Scripts/entry.development.js"
284+
]
285+
},
286+
{
287+
"name": "admin",
288+
"extractVendors": false,
289+
"paths": [
290+
"<projectDir>/<currentWebsite>/code/Scripts/admin.entry.js"
291+
]
292+
}
293+
]
294+
}
295+
}
296+
```
297+
298+
> This example generate two bundles `bundle.js` and `admin.js` but `bundle.js`.
299+
300+
Entry options:
301+
302+
- `mode`: NODE_ENV profile. Indicates for which profile the bundle should be generated
303+
- `name`: Bundle name generated by webpack
304+
- `extractVendors`: Compile NPM module in a separated file (`vendors.bundle.js`). By default `true`.
305+
- `paths`: List of files you want to compile for the bundle.
306+
307+
308+
## Define new alias
309+
310+
Webpack provide a alias mechanism to facilitate javascript module import.
311+
By default, this package provide these following alias:
312+
313+
```
314+
{
315+
"vueCli": {
316+
"alias": {
317+
"@Foundation": "<foundationScriptsDir>",
318+
"@Feature": "<featureDir>",
319+
"@Project": "<projectDir>",
320+
"@Themes": "<foundationDir>/Core/code/Styles/"
321+
}
322+
}
323+
}
324+
```
325+
326+
Alias can be used in you Vue and Javascript file like that:
327+
328+
```javascript
329+
import {something} from "@Foundation";
330+
import CarouselModule from "@Feature/Carousel";
218331
332+
// ...
333+
```
334+
335+
## PWA
336+
337+
Vue-cli provide an extension to manage Progressive web application with webpack.
338+
To generate multiple serviceWorker per project/site, you have to configure PWA options in your `vue.config.js`
339+
and edit the `registerServiveWorker.js` generated by Vue-cli.
340+
341+
In `vue.config.js` add this configuration:
342+
343+
```javascript
344+
const config = require('@node-sitecore/config');
345+
346+
const vueConfig = {
347+
pwa: { // add this
348+
exclude: [
349+
/\.map$/,
350+
/img\/icons\//,
351+
/manifest\.json$/
352+
],
353+
workboxOptions: {
354+
swDest: `../../service-worker.${config.currentWebsite}.js`,
355+
runtimeCaching: []
356+
}
357+
}
358+
}
359+
360+
module.exports = config.buildVueConfig(vueConfig);
361+
```
362+
363+
Then in the `registerServiceWorker.js:`
364+
365+
```javascript
366+
/* eslint-disable no-console */
367+
import { register } from 'register-service-worker';
368+
369+
if (process.env.NODE_ENV === 'production') {
370+
const currentWebsite = process.env.VUE_APP_CURRENT_WEBSITE || 'Common';
371+
372+
register(`/service-worker.${currentWebsite}.js`, {
373+
ready() {
374+
console.log('App is being served from cache by a service worker.\n' +
375+
'For more details, visit https://goo.gl/AFskqB');
376+
},
377+
cached() {
378+
console.log('Content has been cached for offline use.');
379+
},
380+
updated() {
381+
console.log('New content is available; please refresh.');
382+
},
383+
offline() {
384+
console.log('No internet connection found. App is running in offline mode.');
385+
},
386+
error(error) {
387+
console.error('Error during service worker registration:', error);
388+
}
389+
});
390+
}
391+
```
392+
393+
And finally, edit your nuspec file and add this entry:
394+
395+
```xml
396+
<?xml version="1.0"?>
397+
<package>
398+
<files>
399+
<file src="build\Website\service-worker.**.js"/>
400+
</files>
401+
</package>
402+
```
403+
404+
### Switch Project/Site
405+
406+
Sitecore allow to have multiple site workspace like EU, GR, etc... the `npm run develop` compile only one localization
407+
to preview your code in Fractal for a given project. To change the default currentWebsite without change the `.nscrc` default configuration,
408+
create a new file `.dev.nscrc` and copy this code:
409+
410+
```json
411+
{
412+
"currentWebsite": "EU"
413+
}
414+
```
415+
> Change the code according to the project code you want to preview in fractal then run `npm run develop`.

packages/cli-plugin-fractal/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = (api, config) => {
2323
case 'serve':
2424
let port;
2525
if (commander.execute) {
26-
port = await fractal.runDevBefore(commander.execute);
26+
port = await fractal.runDevBefore(commander.execute, config);
2727
}
2828

2929
await fractal.dev(config, port);

packages/cli-plugin-fractal/src/fractal/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ module.exports = {
9797
await execa(task, args, {
9898
shell: true,
9999
env: {
100+
VUE_APP_CURRENT_WEBSITE: config.currentWebsite,
100101
FORCE_COLOR: true
101102
},
102103
stdio: 'inherit'
@@ -106,10 +107,16 @@ module.exports = {
106107
await fs.copy(config.vueCli.outputDir, path.join(config.fractal.outputDir, config.get('vueCli').baseUrl.production));
107108
},
108109

109-
async runDevBefore(cmd) {
110+
async runDevBefore(cmd, config) {
110111
return new Promise(resolve => {
111112
cmd = cmd.split(' ');
112-
const stream = execa(cmd[0], cmd.splice(1), { shell: true, env: { FORCE_COLOR: true } });
113+
const stream = execa(cmd[0], cmd.splice(1), {
114+
shell: true,
115+
env: {
116+
VUE_APP_CURRENT_WEBSITE: config.currentWebsite,
117+
FORCE_COLOR: true
118+
}
119+
});
113120
stream.stderr.pipe(process.stderr);
114121
// stream.stdout.pipe(process.stdout);
115122
let hasError = false;

packages/cli-plugin-vue/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,34 @@ module.exports = (api, config) => {
88
api.registerCommand(
99
'vue',
1010
{
11-
usage: '<build|check> <pattern> [options]',
11+
usage: '<build|check> [options]',
1212
description: 'Build multiple vue app',
1313
options: {
14-
'-e, --execute <cmd>': {
14+
'-p, --pattern <cmd>': {
1515
type: String,
16-
description: 'Run command to build a vue application'
16+
description: 'Glob pattern to list project'
17+
},
18+
'-l, --list <cmd>': {
19+
type: Array,
20+
description: 'Website code list (EU,FR,etc)'
21+
},
22+
'-e, --exclude <cmd>': {
23+
type: Array,
24+
description: 'Exclude Website code list (Common,etc)'
1725
}
1826
}
1927
},
2028
async (commander, args) => {
21-
const [mode = 'build', pattern = path.join(config.projectDir, '**')] = args;
29+
const [mode = 'build'] = args;
2230

2331
switch (mode) {
2432
case 'build':
2533
info(`Starting build vue app...`);
2634

2735
await multipleBuild(config, {
28-
cmd: commander.execute || 'vue-cli-service build --mode production',
29-
pattern
36+
pattern: commander.pattern,
37+
exclude: commander.exclude || [],
38+
list: commander.list || [config.currentWebsite]
3039
});
3140
break;
3241

0 commit comments

Comments
 (0)