Skip to content

Commit 1a96864

Browse files
committed
fix: babel transformations & more
1 parent 1f1d4ae commit 1a96864

File tree

4 files changed

+104
-78
lines changed

4 files changed

+104
-78
lines changed

generator/index.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const fs = require('fs')
2-
const extendPluginOptions = require('../lib/extendPluginOptions')
1+
const
2+
fs = require('fs'),
3+
extendPluginOptions = require('../lib/extendPluginOptions'),
4+
extendBabel = require('../lib/extendBabel')
5+
36
const message = `
4-
Documentation can be found at: http://quasar-framework.org
7+
Documentation can be found at: https://quasar-framework.org
58
69
Quasar is relying on donations to evolve. We'd be very grateful if you can
710
take a look at: https://www.patreon.com/quasarframework
@@ -16,7 +19,7 @@ Enjoy! - Quasar Team
1619

1720
module.exports = (api, opts, rootOpts) => {
1821
const
19-
defaultComponents = [
22+
components = [
2023
'QBtn',
2124
'QLayout',
2225
'QLayoutHeader',
@@ -32,8 +35,8 @@ module.exports = (api, opts, rootOpts) => {
3235
'QItemSide',
3336
'QItemMain',
3437
],
35-
defaultDirectives = [],
36-
defaultPlugins = []
38+
directives = [],
39+
plugins = []
3740

3841
const
3942
tsPath = api.resolve('./src/main.ts'),
@@ -46,7 +49,7 @@ module.exports = (api, opts, rootOpts) => {
4649
'quasar-extras': '^2.0.4'
4750
},
4851
devDependencies: {
49-
"babel-plugin-transform-imports": "1.5.0",
52+
'babel-plugin-transform-imports': '1.5.0',
5053
'stylus': '^0.54.5',
5154
'stylus-loader': '^3.0.2'
5255
}
@@ -59,23 +62,20 @@ module.exports = (api, opts, rootOpts) => {
5962
api.extendPackage(deps)
6063

6164
// modify plugin options
62-
extendPluginOptions(api, pluginOptions => {
65+
extendPluginOptions(api, (pluginOptions, transpileDependencies) => {
6366
pluginOptions.quasar = {
6467
theme: opts.quasar.theme
6568
}
6669
if (opts.quasar.rtlSupport) {
6770
pluginOptions.quasar.rtlSupport = true
6871
}
69-
7072
if (opts.quasar.all) {
71-
pluginOptions.quasar.framework = 'all'
72-
} else {
73-
pluginOptions.quasar.framework = {}
74-
pluginOptions.quasar.framework.components = defaultComponents
75-
pluginOptions.quasar.framework.directives = defaultDirectives
76-
pluginOptions.quasar.framework.plugins = defaultPlugins
77-
}
78-
return pluginOptions
73+
pluginOptions.quasar.importAll = true
74+
}
75+
76+
transpileDependencies.push(/[\\/]node_modules[\\/]quasar-framework[\\/]/)
77+
78+
return { pluginOptions, transpileDependencies }
7979
})
8080

8181
api.render('./templates/common')
@@ -95,12 +95,11 @@ module.exports = (api, opts, rootOpts) => {
9595
}
9696

9797
api.onCreateComplete(() => {
98-
let lines = '\n'
98+
if (!opts.quasar.all) {
99+
extendBabel(api, opts.quasar.theme)
100+
}
99101

100-
const
101-
components = defaultComponents,
102-
directives = defaultDirectives,
103-
plugins = defaultPlugins
102+
let lines = '\n'
104103

105104
const
106105
hasLang = opts.quasar.i18n !== 'en-us',
@@ -143,7 +142,7 @@ module.exports = (api, opts, rootOpts) => {
143142

144143
// build Vue.use()
145144
lines += `\n\nVue.use(Quasar, {`
146-
lines += opts.quasar.all ? ` config: {}` : `\n config: {}`
145+
lines += `\n config: {}`
147146

148147
// if not 'all' we want to include specific defaults
149148
if (!opts.quasar.all) {
@@ -170,7 +169,7 @@ module.exports = (api, opts, rootOpts) => {
170169
lines += `, iconSet: iconSet`
171170
}
172171

173-
lines += ` })`
172+
lines += `\n })`
174173

175174
// Now inject additions to main.[js|ts]
176175
{

index.js

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,41 @@
11
module.exports = (api, options) => {
2-
const
3-
theme = options.pluginOptions.quasar.theme,
4-
rtl = options.pluginOptions.quasar.rtlSupport,
5-
all = options.pluginOptions.quasar.framework === 'all'
6-
7-
if (rtl) {
2+
if (options.pluginOptions.quasar.rtlSupport) {
83
process.env.QUASAR_RTL = true
94
}
105

116
api.chainWebpack(chain => {
12-
chain.module.rule('babel')
13-
.test(/\.jsx?$/)
14-
.exclude
15-
.add(filepath => {
16-
// always transpile js(x) in Vue files
17-
if (/\.vue\.jsx?$/.test(filepath)) {
18-
return false
19-
}
7+
const
8+
theme = options.pluginOptions.quasar.theme,
9+
importAll = options.pluginOptions.quasar.importAll
2010

21-
if ([/[\\/]node_modules[\\/]quasar-framework[\\/]/].some(dep => filepath.match(dep))) {
22-
return false
23-
}
11+
if (!importAll) {
12+
chain.resolve.extensions
13+
.merge([ `.${theme}.js` ])
2414

25-
// don't transpile anything else in node_modules
26-
return /[\\/]node_modules[\\/]/.test(filepath)
27-
})
28-
.end()
29-
.use('babel-loader')
30-
.loader('babel-loader')
31-
.options({
32-
extends: api.resolve('babel.config.js'),
33-
plugins: !all ? [
34-
[
35-
'transform-imports', {
36-
quasar: {
37-
transform: `quasar-framework/dist/babel-transforms/imports.${theme}.js`,
38-
preventFullImport: true
39-
}
40-
}
41-
]
42-
] : []
15+
chain.plugin('define')
16+
.tap(args => {
17+
const { 'process.env': env, ...rest } = args[0]
18+
return [{
19+
'process.env': Object.assign(
20+
{},
21+
env,
22+
{ THEME: JSON.stringify(theme) }
23+
),
24+
...rest
25+
}]
4326
})
27+
}
4428

45-
chain
46-
.resolve
47-
.alias
48-
.set('quasar', api.resolve(`node_modules/quasar-framework/dist/quasar.${theme}.esm.js`))
49-
.set('variables', api.resolve(`src/styles/quasar.variables.styl`))
50-
.set('quasar-variables', api.resolve(`node_modules/quasar-framework/src/css/core.variables.styl`))
51-
.set('quasar-styl', api.resolve(`node_modules/quasar-framework/dist/quasar.${theme}.styl`))
52-
.set('quasar-addon-styl', api.resolve(`node_modules/quasar-framework/src/css/flex-addon.styl`))
29+
chain.resolve.alias
30+
.set(
31+
'quasar',
32+
importAll
33+
? api.resolve(`node_modules/quasar-framework/dist/quasar.${theme}.esm.js`)
34+
: 'quasar-framework'
35+
)
36+
.set('variables', api.resolve(`src/styles/quasar.variables.styl`))
37+
.set('quasar-variables', api.resolve(`node_modules/quasar-framework/src/css/core.variables.styl`))
38+
.set('quasar-styl', api.resolve(`node_modules/quasar-framework/dist/quasar.${theme}.styl`))
39+
.set('quasar-addon-styl', api.resolve(`node_modules/quasar-framework/src/css/flex-addon.styl`))
5340
})
5441
}

lib/extendBabel.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const fs = require('fs')
2+
3+
function updateBabel (babel, theme) {
4+
babel.plugins = babel.plugins || []
5+
babel.plugins.push([
6+
'transform-imports', {
7+
quasar: {
8+
transform: `quasar-framework/dist/babel-transforms/imports.${theme}.js`,
9+
preventFullImport: true
10+
}
11+
}
12+
])
13+
return babel
14+
}
15+
16+
module.exports = function (api, theme) {
17+
console.log('extending babel')
18+
const
19+
babelPath = api.resolve('babel.config.js'),
20+
packageJsonPath = api.resolve('package.json')
21+
22+
let content, filePath
23+
24+
if (fs.existsSync(babelPath)) {
25+
filePath = babelPath
26+
const config = updateBabel(require(filePath), theme)
27+
content = `module.exports = ${JSON.stringify(config, null, 2)}`
28+
}
29+
else if (fs.existsSync(packageJsonPath)) {
30+
filePath = packageJsonPath
31+
const config = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
32+
config.babel = updateBabel(config.babel || {}, theme)
33+
content = JSON.stringify(config, null, 2)
34+
}
35+
36+
fs.writeFileSync(filePath, content, 'utf-8')
37+
}

lib/extendPluginOptions.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,36 @@
33
const fs = require('fs')
44
const isObject = val => val && Object(val) === val
55

6-
let pluginOptions = null
7-
86
module.exports = (api, cb) => {
9-
// Look up pluginOptions from the vue.config.js file
10-
// only do this once when its not yet defined
7+
let pluginOptions, transpileDependencies
118
const vueConfig = api.resolve('vue.config.js')
129

13-
if (!pluginOptions && fs.existsSync(vueConfig)) {
10+
if (fs.existsSync(vueConfig)) {
1411
let projectOptions = require(vueConfig)
1512

1613
if (isObject(projectOptions.pluginOptions)) {
1714
pluginOptions = projectOptions.pluginOptions
1815
}
16+
if (Array.isArray(projectOptions.transpileDependencies)) {
17+
transpileDependencies = projectOptions.transpileDependencies
18+
}
1919
}
2020

2121
api.extendPackage(pkg => {
22+
transpileDependencies = transpileDependencies || []
2223
pluginOptions = pluginOptions || {}
2324

2425
// extend pluginOptions
2526
// with those already defined in the pkg
26-
if (isObject(pkg.vue) && isObject(pkg.vue.pluginOptions)) {
27-
Object.assign(pluginOptions, pkg.vue.pluginOptions)
27+
if (isObject(pkg.vue)) {
28+
if (isObject(pkg.vue.pluginOptions)) {
29+
Object.assign(pluginOptions, pkg.vue.pluginOptions)
30+
}
31+
if (Array.isArray(pkg.vue.transpileDependencies)) {
32+
transpileDependencies = pkg.vue.transpileDependencies
33+
}
2834
}
2935

30-
pluginOptions = cb(pluginOptions)
31-
return {
32-
vue: { pluginOptions }
33-
}
36+
return { vue: cb(pluginOptions, transpileDependencies) }
3437
})
3538
}

0 commit comments

Comments
 (0)