forked from egoist/vue-compile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (158 loc) · 4.72 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const path = require('path')
const { EventEmitter } = require('events')
const fs = require('fs-extra')
const JoyCon = require('joycon')
const isBinaryPath = require('is-binary-path')
const debug = require('debug')('vue-compile:cli')
const { replaceContants, cssExtensionsRe } = require('./utils')
class VueCompile extends EventEmitter {
constructor(options) {
super()
if (options.config !== false) {
const joycon = new JoyCon({
files: [
typeof options.config === 'string' ?
options.config :
'vue-compile.config.js'
],
stopDir: path.dirname(process.cwd())
})
const { data: config, path: configPath } = joycon.loadSync()
if (configPath) {
debug(`Using config file: ${configPath}`)
}
options = Object.assign({}, options, config)
}
this.options = this.normalizeOptions(options)
this.isInputFile =
this.options.input && fs.statSync(this.options.input).isFile()
}
normalizeOptions(options) {
return Object.assign({}, options, {
input: options.input && path.resolve(options.input),
output: options.output && path.resolve(options.output)
})
}
async normalize() {
if (!this.options.input) {
return
}
if (this.isInputFile) {
if (!this.options.output) {
throw new Error('You must specify the path to output file.')
}
await this.normalizeFile(this.options.input, this.options.output)
} else {
if (!this.options.output) {
throw new Error('You must specify the path to output directory.')
}
await this.normalizeDir(this.options.input, this.options.output)
}
}
async normalizeFile(input, outFile) {
let source = await fs.readFile(input)
if (isBinaryPath(input)) {
return this.writeBinary(source, {
filename: input,
outFile
})
}
source = replaceContants(source.toString(), this.options.constants)
const ctx = {
filename: input,
outFile,
modern: this.options.modern,
babelrc: this.options.babelrc
}
if (!input.endsWith('.vue')) {
return this.writeText(source, ctx)
}
const sfcDescriptor = require('@vue/component-compiler-utils').parse({
compiler: require('vue-template-compiler'),
source,
filename: input,
needMap: false
})
const script = await require('./compileScript')(sfcDescriptor.script, ctx)
const template = await require('./compileTemplate')(
sfcDescriptor.template,
ctx
)
const styles = await require('./compileStyles')(sfcDescriptor.styles, ctx)
await require('./writeSFC')(
{
script,
styles,
template,
customBlocks: sfcDescriptor.customBlocks
},
outFile
)
this.emit('normalized', input, outFile)
}
async normalizeDir(input, outDir) {
const include = [].concat(this.options.include || [])
const files = await require('fast-glob')(
include.length > 0 ? include : ['**/*'],
{
cwd: input,
ignore: ['**/node_modules/**'].concat(this.options.exclude || [])
}
)
await Promise.all(
files.map(file => {
return this.normalizeFile(
path.join(input, file),
path.join(outDir, file)
)
})
)
}
async writeText(source, { filename, babelrc, modern, outFile }) {
let output
if (filename.endsWith('.js')) {
output = await require('./script-compilers/babel')(source, {
filename,
babelrc,
modern
})
} else if (filename.endsWith('.ts')) {
output = await require('./script-compilers/ts')(source, {
filename,
babelrc,
modern
})
outFile = outFile.replace(/\.ts$/, '.js')
} else if (filename.endsWith('.css')) {
output = await require('./style-compilers/postcss')(source, { filename })
} else if (/\.s[ac]ss$/.test(filename)) {
const basename = path.basename(filename)
if (basename.startsWith('_')) {
// Ignore sass partial files
return
}
output = await require('./style-compilers/sass')(source, {
filename,
indentedSyntax: /\.sass$/.test(filename)
})
} else if (/\.styl(us)?/.test(filename)) {
output = await require('./style-compilers/stylus')(source, {
filename
})
} else {
output = source
}
outFile = outFile.replace(cssExtensionsRe, '.css')
await fs.outputFile(
outFile,
output,
'utf8'
)
this.emit('normalized', filename, outFile)
}
async writeBinary(source, { filename, outFile }) {
await fs.outputFile(outFile, source, 'utf8')
this.emit('normalized', filename, outFile)
}
}
module.exports = opts => new VueCompile(opts)