Skip to content

Commit 1963de1

Browse files
author
Pooya Parsa
committed
feat: optimize builds
Also new build options to customize less files
1 parent edb9c86 commit 1963de1

File tree

11 files changed

+302
-131
lines changed

11 files changed

+302
-131
lines changed

Diff for: README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@ Add options in `framework7` section inside `nuxt.config.js` file.
6060

6161
Option | Type | Default | Description
6262
----------------|--------------|-----------|--------------------------------------------------------------
63-
`css` | Boolean | `true` | Include Framework7 css (disable to provide a custom build)
63+
`css` | Boolean | `true` | Include Framework7 styles (Useful to disable and provide your own)
6464
`rtl` | Boolean | `false` | Enable RTL layout
6565
`f7Icons` | Boolean | `true` | Include Framework7 Icons (IOS)
6666
`mdIcons` | Boolean | `true` | Include MD Icons
67+
`pwa` | Boolean | `true` | Enable [@nuxtjs/pwa](https://github.com/nuxt-community/pwa-module) module
6768
`routes` | Object | `true` | Route overrides (see below)
6869
`mode` | String | `hash` | Router mode. Can be `hash` or `history`
69-
`view` | Object | `{ ... }` | Options passed to root view of framework7
70+
`view` | Object | defaults | Options passed to root view of framework7
71+
`build` | Object | [framework7 defaults](https://unpkg.com/framework7/scripts/build-config.js) | Framework7 build config used for customizing less variables
72+
`themeColor` | String | `undefined` | If specified, automatically sets all global theme colors to this value
7073

7174
### routes override
7275
Routes are auto generated using pages directory structure.

Diff for: examples/kitchen-sink/components/tab.vue

-18
This file was deleted.

Diff for: examples/kitchen-sink/nuxt.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ module.exports = {
1212
},
1313
manifest: {
1414
name: 'Nuxt7',
15-
description: 'Nuxt7 PWA Demo',
16-
theme_color: '#2196f3'
15+
description: 'Nuxt7 PWA Demo'
1716
},
1817
framework7: {
18+
themeColor: '#2196f3',
1919
mode: 'history',
2020
routes: {
2121
'tabs-routable': {

Diff for: lib/module.js

+114-78
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
const { resolve } = require('path')
2-
const { defaultsDeep, cloneDeep } = require('lodash')
3-
4-
const defaults = {
5-
css: true,
6-
rtl: false,
7-
8-
f7Icons: true,
9-
mdIcons: true,
10-
11-
routes: {},
12-
13-
mode: 'hash',
14-
15-
view: {
16-
main: true,
17-
pushState: true,
18-
pushStateSeparator: '!#',
19-
pushStateRoot: null
20-
}
21-
}
2+
const { defaultsDeep } = require('lodash')
3+
const f7BuildConfig = require('framework7/scripts/build-config')
224

235
const resolvePath = (...args) => resolve(__dirname, ...args)
24-
const resolveDep = p => require.resolve(p)
256

26-
module.exports = function nuxt7 (moduleOptions) {
27-
const options = defaultsDeep({}, moduleOptions, this.options.framework7, defaults)
28-
const cloneOptions = () => cloneDeep(options)
7+
module.exports = function nuxt7 (_options) {
8+
const options = defaultsDeep({}, _options, this.options.framework7, defaults)
299

3010
// Router Mode
3111
if (options.mode === 'history') {
3212
options.view.pushStateSeparator = '__window.location.origin ? \'\' : \'#\'__'
3313
options.view.pushStateRoot = '__window.location.origin__'
3414
}
35-
delete options.mode
3615

3716
// Force mode to SPA
3817
this.options.mode = 'spa'
@@ -42,35 +21,104 @@ module.exports = function nuxt7 (moduleOptions) {
4221
// Customize build
4322
this.extendBuild(config => extendConfig(config, options))
4423

45-
// Force add F7 dependencies to common chunk
46-
this.addVendor(`framework7`)
24+
// Global theme color
25+
if (options.themeColor) {
26+
options.build.ios.themeColor = options.themeColor
27+
options.build.md.themeColor = options.themeColor
28+
29+
if (!this.options.manifest) {
30+
this.options.manifest = {}
31+
}
32+
this.options.manifest.theme_color = options.themeColor
33+
}
34+
35+
// Move F7 dependencies to common chunk
4736
this.addVendor(`framework7-vue`)
4837
this.addVendor('dom7')
4938
this.addVendor('template7')
5039

51-
// Framework7 css files
52-
if (options.css) {
53-
this.options.css.push(resolveDep(`framework7/dist/css/framework7${options.rtl ? '.rtl' : ''}.css`))
54-
}
55-
delete options.css
40+
// Add templates
41+
addTemplates.call(this, options)
5642

5743
// Icons
5844
if (options.f7Icons) {
59-
this.options.css.push('framework7-icons/css/framework7-icons.css')
45+
options.f7IconsSrc = 'framework7-icons/css/framework7-icons.css'
6046
}
61-
delete options.f7Icons
6247

6348
if (options.mdIcons) {
64-
this.options.css.push(resolvePath('assets/material-icons/material-icons.css'))
49+
options.mdIconsSrc = resolvePath('assets/material-icons/material-icons.css')
6550
}
66-
delete options.mdIcons
51+
52+
// Enable IOS specific meta for web app
53+
if (!this.options.meta) {
54+
this.options.meta = {}
55+
}
56+
this.options.meta.mobileAppIOS = true
57+
58+
// Prevent zooming page for a better Native experience
59+
this.options.meta.viewport = 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0, minimal-ui'
60+
61+
// Require PWA module
62+
if (options.pwa !== false) {
63+
return this.requireModule('@nuxtjs/pwa')
64+
}
65+
66+
return Promise.resolve()
67+
}
68+
69+
// ----------------------------------------------------------
70+
// Add templates
71+
// ----------------------------------------------------------
72+
function addTemplates (options) {
73+
// --- Framework7 templates ---
74+
75+
// Build framework7 from source
76+
// options.require = require
77+
// this.addTemplate({
78+
// src: resolvePath('templates/framework7.js'),
79+
// fileName: 'framework7.js',
80+
// options
81+
// })
82+
83+
// Framework7 plugin
84+
this.addPlugin({
85+
src: resolvePath('templates/f7-plugin.js'),
86+
fileName: 'f7-plugin.js',
87+
options
88+
})
89+
90+
// Framework7 routes
91+
this.addTemplate({
92+
src: resolvePath('templates/f7-router.js'),
93+
fileName: 'f7-router.js',
94+
options
95+
})
96+
97+
// Framework7 styles
98+
if (options.css) {
99+
this.addTemplate({
100+
src: resolvePath('templates/framework7.less'),
101+
fileName: 'framework7.less',
102+
options
103+
})
104+
}
105+
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 ---
67114

68115
// App.js
69116
this.addTemplate({
70117
src: resolvePath('templates/App.js'),
71118
fileName: 'App.js',
72119
options
73120
})
121+
74122
// ... Support for nuxt <= 1.0.0-rc11
75123
this.addTemplate({
76124
src: resolvePath('templates/App.vue'),
@@ -84,6 +132,7 @@ module.exports = function nuxt7 (moduleOptions) {
84132
fileName: 'components/nuxt.js',
85133
options
86134
})
135+
87136
// ... Support for nuxt <= 1.0.0-rc11
88137
this.addTemplate({
89138
src: resolvePath('templates/components/nuxt.vue'),
@@ -104,60 +153,47 @@ module.exports = function nuxt7 (moduleOptions) {
104153
fileName: 'router.js',
105154
options
106155
})
107-
108-
// Framework7 routes
109-
this.addTemplate({
110-
src: resolvePath('templates/f7-router.js'),
111-
fileName: 'f7-router.js',
112-
options: cloneOptions()
113-
})
114-
delete options.routes
115-
116-
// Framework7 plugin
117-
this.addPlugin({
118-
src: resolvePath('templates/f7.js'),
119-
fileName: 'f7.js',
120-
options
121-
})
122-
123-
// Framework 7 components
124-
this.addPlugin({
125-
src: resolvePath('templates/f7-components.js'),
126-
fileName: 'f7-components.js',
127-
options
128-
})
129-
130-
// Enable IOS specific meta for web app
131-
if (!this.options.meta) this.options.meta = {}
132-
this.options.meta.mobileAppIOS = true
133-
134-
// Prevent zooming page for a better Native experience
135-
this.options.meta.viewport = 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0, minimal-ui'
136-
137-
// Require PWA module
138-
return Promise.all([
139-
this.requireModule('@nuxtjs/pwa')
140-
])
141156
}
142157

158+
// ----------------------------------------------------------
159+
// Extend webpack config
160+
// ----------------------------------------------------------
143161
function extendConfig (config, options) {
162+
// Don't exclude babel for F7 stuff
163+
const babelLoader = config.module.rules.find(r => r.loader === 'babel-loader')
164+
babelLoader.exclude = /node_modules\/(?!(framework7|framework7-vue|template7|dom7)\/).*/
165+
144166
// Increase performance check limits to 2M (non-gzipped)
145167
const MAX_SIZE = 2 * 1024 * 1024
146168
Object.assign(config.performance, {
147169
maxEntrypointSize: MAX_SIZE,
148170
maxAssetSize: MAX_SIZE
149171
})
150172

151-
// Don't exclude babel for F7 stuff
152-
const babelLoader = config.module.rules.find(r => r.loader === 'babel-loader')
153-
babelLoader.exclude = /node_modules\/(?!(framework7|framework7-vue|template7|dom7)\/).*/
154-
155173
// Disable warning for babel on framework7 dist files
156174
babelLoader.options.compact = false
175+
}
176+
177+
// ----------------------------------------------------------
178+
// Default options
179+
// ----------------------------------------------------------
180+
const defaults = {
181+
css: true,
182+
rtl: false,
183+
184+
f7Icons: true,
185+
mdIcons: true,
186+
187+
routes: {},
157188

158-
// framework7
159-
config.resolve.alias['framework7'] = resolveDep(`framework7/dist/js/framework7.esm.bundle.js`)
189+
mode: 'hash',
190+
191+
build: Object.assign({}, f7BuildConfig),
160192

161-
// framework7-vue
162-
config.resolve.alias['framework7-vue'] = resolveDep(`framework7-vue/dist/framework7-vue.esm.js`)
193+
view: {
194+
main: true,
195+
pushState: true,
196+
pushStateSeparator: '#',
197+
pushStateRoot: null
198+
}
163199
}

Diff for: lib/templates/f7-plugin.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Vue from 'vue'
2+
import { Framework7Vue } from 'framework7-vue'
3+
4+
import { routes } from './f7-router'
5+
import Framework7VueComponents from './f7-components'
6+
7+
// Imports styles
8+
<% if (options.css) { %>import './framework7.less'<% } %>
9+
<% if (options.f7IconsSrc) { %>import '<%= options.f7IconsSrc %>'<% } %>
10+
<% if (options.mdIconsSrc) { %>import '<%= options.mdIconsSrc %>'<% } %>
11+
12+
// Async import F7 core
13+
const _Framework7 = import('framework7/dist/js/framework7.esm.bundle'/* webpackChunkName: f7 */).then(m => m.default)
14+
15+
// Register F7 Vue Components
16+
Vue.use(Framework7VueComponents)
17+
18+
// Register plugin
19+
export default async function framework7({ app }, inject) {
20+
// Register F7 Vue Plugin
21+
const Framework7 = await _Framework7
22+
Vue.use(Framework7Vue, Framework7)
23+
24+
// Framework7 options
25+
app.framework7 = <%= serialize({mode: options.mode,view: options.view}).replace(/"__([^_]+)__"/g,"$1") %>
26+
27+
// Routes
28+
app.framework7.routes = routes
29+
}
30+

Diff for: lib/templates/f7.js

-28
This file was deleted.

0 commit comments

Comments
 (0)