Skip to content

Commit 6cc0eec

Browse files
author
Pooya Parsa
committed
feat: rewrite to be fully es compatible
BREAKING CHANGE: by default all components are included as before but may introduce breaking changes
1 parent 2821f0f commit 6cc0eec

File tree

8 files changed

+98
-112
lines changed

8 files changed

+98
-112
lines changed

lib/module.js

+50-69
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
const { resolve, relative } = require('path')
2-
const { defaultsDeep, omit } = require('lodash')
2+
const { defaultsDeep, omit, startCase } = require('lodash')
33
const f7BuildConfig = require('./f7-config')
44

55
const resolvePath = (...args) => resolve(__dirname, ...args)
66

77
module.exports = function nuxt7 (_options) {
88
const options = defaultsDeep({}, _options, this.options.framework7, defaults)
99

10+
// CamelCase component names
11+
options.build._components = normalizeComponents(options.build.components)
12+
1013
// Router Mode
1114
if (options.mode === 'history') {
1215
options.view.pushStateSeparator = "__window.location.origin ? '' : '#'__"
@@ -21,9 +24,6 @@ module.exports = function nuxt7 (_options) {
2124
// Disable postcss for less transformations
2225
this.options.build.postcss = false
2326

24-
// Customize build
25-
this.extendBuild(config => extendConfig(config, options))
26-
2727
// Global theme color
2828
if (options.themeColor) {
2929
options.build.ios.themeColor = options.themeColor
@@ -40,6 +40,9 @@ module.exports = function nuxt7 (_options) {
4040
this.addVendor('dom7')
4141
this.addVendor('template7')
4242

43+
// Remove custom keys for framework7 plugin
44+
options.plugin = omit(options, extraKeys)
45+
4346
// Add templates
4447
addTemplates.call(this, options)
4548

@@ -78,86 +81,64 @@ module.exports = function nuxt7 (_options) {
7881
// Add templates
7982
// ----------------------------------------------------------
8083
function addTemplates (options) {
81-
// --- Framework7 templates ---
82-
8384
// Framework7 plugin
8485
this.addPlugin({
85-
src: resolvePath('templates/f7-plugin.js'),
86-
fileName: 'f7-plugin.js',
87-
options: omit(options, extraKeys)
88-
})
89-
90-
// Framework7 routes
91-
this.addTemplate({
92-
src: resolvePath('templates/f7-router.js'),
93-
fileName: 'f7-router.js',
86+
src: resolvePath('templates/framework7/f7.plugin.js'),
87+
fileName: 'framework7/f7.plugin.js',
9488
options
9589
})
9690

97-
// Framework7 styles
98-
if (options.css) {
91+
// Copy all templates
92+
for (const template of templates) {
9993
this.addTemplate({
100-
src: resolvePath('templates/framework7.less'),
101-
fileName: 'framework7.less',
94+
src: resolvePath('templates/' + template),
95+
fileName: template,
10296
options
10397
})
10498
}
99+
}
105100

106-
// Framework 7 components
107-
this.addTemplate({
108-
src: resolvePath('templates/f7-components.js'),
109-
fileName: 'f7-components.js',
110-
options
111-
})
112-
113-
// --- Nuxt internals ---
114-
115-
// App.js
116-
this.addTemplate({
117-
src: resolvePath('templates/App.js'),
118-
fileName: 'App.js',
119-
options
120-
})
121-
122-
// Nuxt component
123-
this.addTemplate({
124-
src: resolvePath('templates/components/nuxt.js'),
125-
fileName: 'components/nuxt.js',
126-
options
127-
})
128-
129-
// Default layout
130-
this.addTemplate({
131-
src: resolvePath('templates/layouts/default.vue'),
132-
fileName: 'layouts/default.vue',
133-
options
134-
})
135-
136-
// Vue router
137-
this.addTemplate({
138-
src: resolvePath('templates/router.js'),
139-
fileName: 'router.js',
140-
options
141-
})
101+
function tryResolve (path) {
102+
try {
103+
return require.resolve(path)
104+
} catch (err) {
105+
return null
106+
}
142107
}
143108

144-
// ----------------------------------------------------------
145-
// Extend webpack config
146-
// ----------------------------------------------------------
147-
function extendConfig (config, options) {
148-
// Increase performance check limits to 2M (non-gzipped)
149-
const MAX_SIZE = 2 * 1024 * 1024
150-
Object.assign(config.performance, {
151-
maxEntrypointSize: MAX_SIZE,
152-
maxAssetSize: MAX_SIZE
153-
})
109+
function normalizeComponents (components) {
110+
return components.map((name) => ({
111+
name,
112+
camelName: startCase(name).replace(/ /g, ''),
113+
js: tryResolve(`framework7/dist/components/${name}/${name}.js`),
114+
less: tryResolve(`framework7/dist/components/${name}/${name}.less`)
115+
}))
154116
}
155117

156-
// ----------------------------------------------------------
157-
// Default options
158-
// ----------------------------------------------------------
118+
const templates = [
119+
// Framework 7
120+
'framework7/f7.router.js',
121+
'framework7/f7.styles.less',
122+
'framework7/f7.components.js',
123+
// Nuxt
124+
'App.js',
125+
'components/nuxt.js',
126+
'layouts/default.vue',
127+
'router.js'
128+
]
129+
159130
const extraKeys = [
160-
'build', 'mode', 'themeColor', 'css', 'f7Icons', 'mdIcons', 'f7IconsSrc', 'mdIconsSrc', 'pwa', 'routes'
131+
'build',
132+
'mode',
133+
'themeColor',
134+
'css',
135+
'f7Icons',
136+
'mdIcons',
137+
'f7IconsSrc',
138+
'mdIconsSrc',
139+
'pwa',
140+
'routes',
141+
'plugin'
161142
]
162143

163144
const defaults = {

lib/templates/App.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import Vue from 'vue'
22

3-
// Framework7 Styles
4-
<% if (options.css) { %>import './framework7.less'<% } %>
5-
<% if (options.f7IconsSrc) { %>import '<%= relativeToBuild(resolvePath(options.f7IconsSrc)) %>'<% } %>
6-
<% if (options.mdIconsSrc) { %>import '<%= relativeToBuild(resolvePath(options.mdIconsSrc)) %>'<% } %>
7-
83
// Styles
94
<% css.forEach(function (c) { %>import '<%= relativeToBuild(resolvePath(c.src || c)) %>'
105
<% }) %>

lib/templates/f7-components.js

-7
This file was deleted.

lib/templates/f7-plugin.js

-25
This file was deleted.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Framework7 from 'framework7'
2+
3+
import * as components from 'framework7-vue'
4+
5+
<%= options.build._components.filter(c => c.js).map(c =>
6+
`import ${c.camelName} from 'framework7/dist/components/${c.name}/${c.name}.js'`
7+
).join('\n')%>
8+
9+
Framework7.use([
10+
<%= options.build._components.filter(c => c.js).map(c => c.camelName).join(', \n') %>
11+
])
12+
13+
export default {
14+
install(Vue) {
15+
Vue.mixin({ components })
16+
}
17+
}

lib/templates/framework7/f7.plugin.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Vue from 'vue'
2+
import Framework7 from 'framework7'
3+
import Framework7Vue from 'framework7-vue'
4+
import { routes } from './f7.router'
5+
import Framework7VueComponents from './f7.components'
6+
7+
// Framework7 Styles
8+
<% if (options.css) { %>import './f7.styles.less'<% } %>
9+
<% if (options.f7IconsSrc) { %>import '../<%= relativeToBuild(resolvePath(options.f7IconsSrc)) %>'<% } %>
10+
<% if (options.mdIconsSrc) { %>import '../<%= relativeToBuild(resolvePath(options.mdIconsSrc)) %>'<% } %>
11+
12+
// Register F7 Vue Components
13+
Vue.use(Framework7VueComponents)
14+
15+
// Register F7 Vue Plugin
16+
Vue.use(Framework7Vue, Framework7)
17+
18+
// Register plugin
19+
export default async function framework7({ app }, inject) {
20+
// Framework7 options
21+
app.framework7 = <%= serialize(options.plugin).replace(/"__([^_]+)__"/g, "$1") %>
22+
23+
// Routes
24+
app.framework7.routes = routes
25+
}
26+

lib/templates/f7-router.js renamed to lib/templates/framework7/f7.router.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ const _routes = recursiveRoutes(router.routes, '\t\t', _components)
2828

2929
%>
3030

31-
<% uniqBy(_components, '_name').forEach(route => { %>import <%= route._name %> from '<%= relativeToBuild(route.component) %>'
32-
<% }) %>
31+
<%= uniqBy(_components, '_name').map(route =>
32+
`import ${route._name} from '../${relativeToBuild(route.component)}'`
33+
).join('\n')
34+
%>
3335

3436
export const routes = [
3537
<%= _routes %>

lib/templates/framework7.less renamed to lib/templates/framework7/f7.styles.less

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,4 @@
3030
@import url('~framework7/dist/components/modal/modal.less');
3131

3232
// Components
33-
<%= options.build.components
34-
.filter(component => component !== 'form')
35-
.map(component => `@import url('~framework7/dist/components/${component}/${component}.less');`).join('\n')
36-
%>
33+
<%= options.build._components.filter(c => c.less).map(c => `@import url('${c.less}');`).join('\n') %>

0 commit comments

Comments
 (0)