-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
121 lines (113 loc) · 2.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const consola = require('consola')
const babelTransform = require('@babel/standalone').transform
const babelTransformConfigPlugin = require('./plugin')
const nuxt = {
css(value) {
// arr of strings
return ['css', {
action: 'create:merge',
value
}]
},
script(value) {
// arr of objects or strings
return ['head:script', {
action: 'create:merge',
value
}]
},
module(value) {
return ['modules', {
action: 'create:merge',
value: [value]
}]
},
modules(value) {
return ['modules', {
action: 'create:merge',
value
}]
},
transpile(value) {
return ['build:transpile', {
action: 'create:merge',
value
}]
},
libraries(value) {
return ['build:transpile', {
action: 'create:merge',
value
}]
}
}
const table = { nuxt }
function createTransformArgs(framework, args, strict) {
const frameworkTable = table[framework]
const keysNotFound = []
if (!frameworkTable) {
return consola.error(`[transform-configs] ${framework} Framework not supported\nUse babel plugin directly instead`)
}
try {
const transforms = Object.entries(args).reduce((acc, [k, v]) => {
if (frameworkTable[k]) {
const [key, value] = frameworkTable[k](v)
return {
...acc,
[key]: value
}
}
keysNotFound.push(k)
if (!strict) {
return {
...acc,
[k]: {
action: "create:merge",
value: v
}
}
}
return acc
}, {})
return {
transforms,
keysNotFound
}
} catch(e) {
return {
reason: e
}
}
}
function transform(code, transforms) {
return babelTransform(code, {
plugins: [
[
babelTransformConfigPlugin,
transforms
]
]
}) // { code } or Throws error
}
function handleKeysNotFound(keys) {
keys.forEach((key) => {
consola.warn(`[transform-args] Key "${key}" not recognized.\nDefaulting to default transform`)
})
}
function transformConfig(code, framework, args, strict = true) {
const {
transforms,
keysNotFound,
reason,
} = createTransformArgs(framework, args, strict)
if (!transforms) {
return consola.error(reason)
}
if (!strict && keysNotFound.length) {
handleKeysNotFound(keysNotFound);
}
return transform(code, transforms)
}
transformConfig.transform = transform
transformConfig.plugin = babelTransformConfigPlugin
module.exports = transformConfig