@nuxtjs/prismic 1.4.2 → 1.5.0-alpha.0

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/README.md CHANGED
@@ -61,9 +61,9 @@ Thanks to [James Pegg](https://github.com/jamespeggsh) for the initial implement
61
61
  [npm-version-href]: https://npmjs.com/package/@nuxtjs/prismic
62
62
  [npm-downloads-src]: https://img.shields.io/npm/dm/@nuxtjs/prismic.svg
63
63
  [npm-downloads-href]: https://npmjs.com/package/@nuxtjs/prismic
64
- [github-actions-ci-src]: https://github.com/nuxt-community/prismic-module/workflows/ci/badge.svg
65
- [github-actions-ci-href]: https://github.com/nuxt-community/prismic-module/actions?query=workflow%3Aci
66
- [codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/prismic-module.svg
67
- [codecov-href]: https://codecov.io/gh/nuxt-community/prismic-module
64
+ [github-actions-ci-src]: https://github.com/nuxt-modules/prismic/workflows/ci/badge.svg
65
+ [github-actions-ci-href]: https://github.com/nuxt-modules/prismic/actions?query=workflow%3Aci
66
+ [codecov-src]: https://img.shields.io/codecov/c/github/nuxt-modules/prismic.svg
67
+ [codecov-href]: https://codecov.io/gh/nuxt-modules/prismic
68
68
  [license-src]: https://img.shields.io/npm/l/@nuxtjs/prismic.svg
69
69
  [license-href]: https://npmjs.com/package/@nuxtjs/prismic
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxtjs/prismic",
3
- "version": "1.4.2",
3
+ "version": "1.5.0-alpha.0",
4
4
  "description": "Easily connect your Nuxt.js application to your content hosted on Prismic",
5
5
  "keywords": [
6
6
  "nuxt",
@@ -9,7 +9,7 @@
9
9
  "prismic",
10
10
  "cms"
11
11
  ],
12
- "repository": "nuxt-community/prismic-module",
12
+ "repository": "nuxt-modules/prismic",
13
13
  "license": "MIT",
14
14
  "main": "src/module.js",
15
15
  "files": [
@@ -27,13 +27,16 @@
27
27
  "test": "npm run lint && npm run unit"
28
28
  },
29
29
  "dependencies": {
30
- "@prismicio/client": "^4.0.0 || ^5.1.0",
31
- "@prismicio/vue": "^2.1.0",
30
+ "@prismicio/client": "^5.1.1 || ^4.0.0",
31
+ "@prismicio/vue": "^2.2.0",
32
+ "@slicemachine/manager": "0.1.1-dev-plugins.3",
32
33
  "consola": "^2.15.3",
33
- "prismic-dom": "^2.2.6"
34
+ "io-ts-types": "^0.5.19",
35
+ "prismic-dom": "^2.2.7"
34
36
  },
35
37
  "devDependencies": {
36
38
  "@nuxtjs/eslint-config": "latest",
39
+ "@prismicio/slice-simulator-vue": "^0.2.3",
37
40
  "@types/jest": "latest",
38
41
  "babel-eslint": "latest",
39
42
  "eslint": "latest",
package/src/module.js CHANGED
@@ -3,10 +3,11 @@ const fs = require('fs')
3
3
  const logger = require('./logger')
4
4
  const generate = require('./generator')
5
5
 
6
- function install (moduleOptions) {
6
+ async function install (moduleOptions) {
7
7
  const options = {
8
8
  preview: true,
9
9
  previewReloadType: 'hot',
10
+ simulator: true,
10
11
  components: true,
11
12
  modern: false,
12
13
  ...moduleOptions,
@@ -24,6 +25,7 @@ function install (moduleOptions) {
24
25
  }
25
26
  const repo = options.endpoint.replace(/^https?:\/\//, '').replace(/(\.cdn)?\.prismic.+/, '')
26
27
  const app = this.options.dir.app || 'app'
28
+ const pages = this.options.dir.pages || 'pages'
27
29
 
28
30
  // Add in Prismic libraries to enable preview
29
31
  if (options.preview) {
@@ -31,6 +33,12 @@ function install (moduleOptions) {
31
33
  const userPreviewPage = path.join(this.options.srcDir, app, 'prismic', 'pages', 'preview.vue')
32
34
  const userPreviewPageExists = fs.existsSync(userPreviewPage)
33
35
 
36
+ if (userPreviewPageExists) {
37
+ logger.info(`Using user-defined preview page, available at \`${options.preview}\``)
38
+ } else {
39
+ logger.info(`Using default preview page, available at \`${options.preview}\``)
40
+ }
41
+
34
42
  this.addTemplate({
35
43
  fileName: 'prismic/pages/preview.vue',
36
44
  src: userPreviewPageExists ? userPreviewPage : path.join(__dirname, '../templates/pages/preview.vue')
@@ -44,6 +52,121 @@ function install (moduleOptions) {
44
52
  })
45
53
  }
46
54
 
55
+ // Add in Prismic Simulator for Slice Machine
56
+ if (options.simulator) {
57
+ // Skip on existing simulator (legacy)
58
+ const slashSliceSimulator = path.join(this.options.srcDir, pages, 'slice-simulator.vue')
59
+ if (fs.existsSync(slashSliceSimulator)) {
60
+ logger.info('Using user-defined simulator page within the `pages` directory, available at `/slice-simulator')
61
+ } else {
62
+ // Detect Simulator provider (flavor)
63
+ let flavor
64
+ try {
65
+ require('@slicemachine/adapter-nuxt/simulator')
66
+ flavor = 'adapter'
67
+ } catch {
68
+ try {
69
+ require('@prismicio/slice-simulator-vue')
70
+ flavor = 'standalone'
71
+ } catch {
72
+ if (options.simulator !== true) {
73
+ logger.warn('Could not injected simulator page, `@slicemachine/adapter-nuxt` is not installed')
74
+ }
75
+ }
76
+ }
77
+
78
+ // Add page if flavor was detected
79
+ if (flavor) {
80
+ const simulatorPath = options.simulator === true ? '/simulator' : options.simulator
81
+
82
+ const userSimulatorPage = path.join(this.options.srcDir, app, 'prismic', 'pages', 'simulator.vue')
83
+ const userSimulatorPageExists = fs.existsSync(userSimulatorPage)
84
+
85
+ let registerPage = false
86
+
87
+ // Only needed for built-in Simulator
88
+ if (!userSimulatorPageExists) {
89
+ try {
90
+ const { createSliceMachineManager } = require('@slicemachine/manager')
91
+
92
+ const manager = createSliceMachineManager({ cwd: this.options.rootDir })
93
+
94
+ let root
95
+
96
+ try {
97
+ root = await manager.project.getRoot()
98
+ } catch {
99
+ root = this.options.rootDir
100
+ }
101
+
102
+ let config
103
+ try {
104
+ config = await manager.project.getSliceMachineConfig()
105
+ } catch {
106
+ let configPath
107
+ try {
108
+ configPath = await manager.project.getSliceMachineConfigPath()
109
+ config = JSON.parse(fs.readFileSync(configPath))
110
+ } catch {
111
+ configPath = path.join(root, 'sm.json')
112
+ config = JSON.parse(fs.readFileSync(configPath))
113
+ }
114
+
115
+ if (!Array.isArray(config.libraries)) {
116
+ throw new TypeError(`\`${configPath}\` is missing \`libraries\` array`)
117
+ }
118
+ }
119
+ const rawLibraries = config.libraries
120
+ const libraries = []
121
+
122
+ for (const rawLibrary of rawLibraries) {
123
+ if (/^[@~.]\//.test(rawLibrary)) {
124
+ const libraryAbsolutePath = path.resolve(root, rawLibrary.replace(/^[@~.]/, '.'))
125
+ const libraryRelativePath = path.relative(this.options.rootDir, libraryAbsolutePath).replaceAll('\\', '/')
126
+ libraries.push(`~~/${libraryRelativePath}`)
127
+ } else {
128
+ libraries.push(rawLibrary)
129
+ }
130
+ }
131
+
132
+ this.addTemplate({
133
+ fileName: 'prismic/all-slices.js',
134
+ src: path.join(__dirname, '../templates/all-slices.js'),
135
+ options: { libraries }
136
+ })
137
+ this.addTemplate({
138
+ fileName: 'prismic/pages/simulator.vue',
139
+ src: path.join(__dirname, `../templates/pages/simulator-${flavor}.vue`)
140
+ })
141
+
142
+ registerPage = true
143
+ logger.info(`Using default simulator page, available at \`${simulatorPath}\``)
144
+ } catch (error) {
145
+ logger.warn("Could not resolve slice libraries, simulator page won't work\n\n", error)
146
+ }
147
+ } else {
148
+ this.addTemplate({
149
+ fileName: 'prismic/pages/simulator.vue',
150
+ src: userSimulatorPage
151
+ })
152
+
153
+ registerPage = true
154
+ logger.info(`Using user-defined simulator page, available at \`${simulatorPath}\``)
155
+ }
156
+
157
+ if (registerPage) {
158
+ this.extendRoutes((routes, resolve) => {
159
+ routes.unshift({
160
+ name: 'prismic-slice-simulator',
161
+ path: simulatorPath,
162
+ component: resolve(this.options.buildDir, 'prismic/pages/simulator.vue')
163
+ })
164
+ })
165
+ }
166
+ }
167
+ }
168
+ }
169
+
47
170
  // Add components
48
171
  if (options.components) {
49
172
  this.addPlugin({
@@ -0,0 +1,7 @@
1
+ <% options.libraries.forEach((library, index) => { %>import { components as library<%= index %> } from '<%= library %>'
2
+ <% }); %>
3
+
4
+ export default {
5
+ <% options.libraries.forEach((library, index) => { %> ...library<%= index %>,
6
+ <% }); %>
7
+ }
@@ -0,0 +1,20 @@
1
+ <template>
2
+ <SliceSimulator #default="{ slices }">
3
+ <SliceZone :slices="slices" :components="components" />
4
+ </SliceSimulator>
5
+ </template>
6
+
7
+ <script>
8
+ import { SliceSimulator } from "@slicemachine/adapter-nuxt/simulator";
9
+
10
+ import components from "../all-slices.js";
11
+
12
+ export default {
13
+ components: {
14
+ SliceSimulator
15
+ },
16
+ data() {
17
+ return { components };
18
+ }
19
+ };
20
+ </script>
@@ -0,0 +1,20 @@
1
+ <template>
2
+ <SliceSimulator #default="{ slices }" :state="{}">
3
+ <SliceZone :slices="slices" :components="components" />
4
+ </SliceSimulator>
5
+ </template>
6
+
7
+ <script>
8
+ import { SliceSimulator } from "@prismicio/slice-simulator-vue";
9
+
10
+ import components from "../all-slices.js";
11
+
12
+ export default {
13
+ components: {
14
+ SliceSimulator
15
+ },
16
+ data() {
17
+ return { components };
18
+ }
19
+ };
20
+ </script>