-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
464 lines (397 loc) · 11.7 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import { transformSync } from '@babel/core'
import babelPluginTransformImports from './babel-plugin-transform-imports/index.js'
import { minify } from "terser"
import dedent from 'dedent-js'
import glob from 'glob'
import fs from 'mz/fs.js'
import prettier from 'prettier'
import beautify from 'js-beautify'
import { dirname, parse } from 'path'
import { fileURLToPath } from 'url'
import chalk from 'chalk'
import commandLineUsage from 'command-line-usage'
import commandLineArgs from 'command-line-args'
import {
isObjectExpression,
objectProperty,
identifier,
templateLiteral,
templateElement,
isArrowFunctionExpression,
isCallExpression,
isImport
} from '@babel/types'
import {
parse as parseSFC,
compileScript,
compileStyle
} from 'vue/compiler-sfc'
import { exit } from 'process'
const sections = [
{
header: 'babel-transform-vue-ftn',
content: 'Transpiles a vue-cli project into a non-bundled, non node-dependent project.'
},
{
header: 'Options',
optionList: [
{
name: 'input',
alias: 'i',
typeLabel: '{underline string}',
description: 'The root directory of the project to transpile.'
},
{
name: 'output',
alias: 'o',
typeLabel: '{underline string}',
description: `The output directory where the built files will be placed.
The default is /dist under the project root.`
},
{
name: 'help',
alias: 'h',
description: 'Print this usage guide.',
type: Boolean
}
]
}
]
const usage = commandLineUsage(sections)
const optionDefinitions = [
{ name: 'input', alias: 'i', type: String },
{ name: 'output', alias: 'o', type: String },
{ name: 'help', alias: 'h', type: Boolean }
]
const options = commandLineArgs(optionDefinitions)
if (Object.keys(options).length == 0 || options.help){
console.log(usage)
exit(1)
} else if (!options.input) {
console.error('Must specify input directory.')
exit(1)
}
const cwd = options.input
let importsConfigName = 'imports.config.js'
try {
fs.statSync(cwd).isDirectory()
} catch {
console.error(`Directory ${cwd} does not exist`)
exit(1)
}
let importsConfigPath = `${cwd}/${importsConfigName}`
if (!fs.existsSync(importsConfigPath)) {
console.error(`No ${chalk.yellow(importsConfigName)} found at project root ${chalk.cyan(cwd)}`)
exit(1)
}
// Copy the config from the project root because node is retarded and cannot resolve remote
// esm without the project package.json conatining type:module
// const localImportsConfigPath = dirname(fileURLToPath(import.meta.url)) + '/' + importsConfigName
// fs.copyFileSync(importsConfigPath, localImportsConfigPath)
let { importOptions, excludedLibraries } = await import('./' + importsConfigName)
// fs.rmSync(localImportsConfigPath)
const distPath = 'dist'
let outDirectory = `${cwd}/${distPath}`
if (options.output) {
outDirectory = options.output
}
const typesToLoad = [
'vue',
'js',
'jpeg',
'jpg',
'png',
'svg',
'json',
'ico',
'woff',
'woff2',
'ttf',
'otf'
].join('|')
const aliases = [
{
alias: '@/',
replacement: '/src/'
}
]
console.time('[ Build finished ]')
console.log()
console.log('[ Building... ]')
const indent = (content, amount) => {
return content
.split('\n')
.map(l => ' '.repeat(amount) + l)
.join('\n')
}
const hasAliases = importPath => {
if(importPath) {
for (const alias of aliases) {
if (importPath.startsWith(alias.alias)) return true
}
}
return false
}
const replaceAliases = importPath => {
if(importPath) {
aliases.forEach(a => importPath = importPath.replace(a.alias, a.replacement))
}
return importPath
}
const renameImports = importPath => {
if(importPath) {
const importName = importPath.split('/').pop()
let replacement = importName.replace('.vue', '.js')
replaceAliases(replacement)
return importPath.replace(importName, replacement)
}
return importPath
}
const createDistSubdir = (subdir = '') => {
if (subdir.startsWith('public')) return
fs.mkdirSync(`${outDirectory}/${subdir}`, {
recursive: true
})
}
const writeToDist = (subdir, data) => {
fs.writeFileSync(`${outDirectory}/${subdir}`, data)
}
const readFile = (filePath, fileName, fileType) => {
return fs.readFileSync(`${cwd}/${filePath}/${fileName}${fileType}`)
}
const babelPluginRemoveCssImports = {
visitor: {
ImportDeclaration: path => {
if (path.node.source.value.endsWith('.css')) {
path.remove()
}
}
}
}
const babelPluginTransformVueTemplate = template => ({
visitor: {
ExportDefaultDeclaration: path => {
const templateProperty = objectProperty(
identifier('template'),
templateLiteral(
[templateElement({ raw: template })],
[]
)
)
if (isObjectExpression(path.node.declaration)) {
path.node.declaration.properties.unshift(templateProperty)
}
}
}
})
const babelPluginRenameImports = {
visitor: {
StringLiteral: path => {
if (path.node.value.endsWith('.vue')) {
path.node.value = replaceAliases(renameImports(path.node.value))
}
},
ImportDeclaration: path => {
if (path.node.source.value === 'axios') {
path.node.source.value = importOptions.axios.transform
}
path.node.source.value = replaceAliases(renameImports(path.node.source.value))
},
Function: (path, state) => {
if (
isArrowFunctionExpression(path.node) &&
isCallExpression(path.node.body) &&
isImport(path.node.body.callee)
) {
path.node.body.arguments[0].value = replaceAliases(renameImports(path.node.body.arguments[0].value))
}
}
}
}
const babelPluginTransformDirectoryImport = (workDir, subDir, fileTypes) => ({
visitor: {
ImportDeclaration: path => {
let source = path.node.source.value
const isAliased = hasAliases(source)
if (source[0] !== '.' && source[0] !== '/' && !isAliased) return
if (source.startsWith('./')) {
source = `${workDir}/${subDir}${source.slice(1)}`
} else {
source = `${workDir}/${replaceAliases(source)}`
}
if (fs.existsSync(source) && fs.lstatSync(source).isDirectory()) {
const files = fs.readdirSync(source)
for (const filePath of files) {
const { name, ext } = parse(filePath)
if (fileTypes.includes(ext.slice(1))) {
path.node.source.value = `${path.node.source.value}/${name}${ext}`
}
}
} else {
const fileName = source.split('/').pop()
const fileType = fileName.split('.').pop()
if (fileType !== fileName) return // fileType is missing
source = source.replace(fileName, '')
const files = fs.readdirSync(source)
for (const filePath of files) {
const { name, ext } = parse(filePath)
if (name === fileName && fileTypes.includes(ext.slice(1))) {
const finalPath = path.node.source.value.replace(fileName, '')
path.node.source.value = renameImports(`${finalPath}${fileName}${ext}`)
}
}
}
}
}
})
let babelPlugins = [
babelPluginRenameImports,
babelPluginRemoveCssImports,
[babelPluginTransformImports, importOptions]
]
const prettierOptions = {
parser: 'babel',
semi: false,
vueIndentScriptAndStyle: true,
singleQuote: true,
arrowParens: 'avoid',
// printWidth: 200
}
const cssFiles = []
const vueLoader = (filePath, fileName, fileType) => {
let script = readFile(filePath, fileName, fileType).toString()
const parsedSfc = parseSFC(script, { filename: fileName })
const hasScript = !!parsedSfc.descriptor.script
if (hasScript) {
script = compileScript(parsedSfc.descriptor, { id: fileName }).content
} else {
script = 'export default {}'
}
const styles = parsedSfc.descriptor.styles[0]
const hasStyles = !!styles
const compiledStyle = compileStyle({
id: fileName,
filename: fileName,
source: hasStyles && styles.content,
})
let template = parsedSfc.descriptor.template.content.replace(/`/g, '\`') // escape all backticks
aliases.forEach(a => template = template.replaceAll(`src="${a.alias}`, `src="${a.replacement}`))
template = indent(template, 2)
const css = indent(compiledStyle.code, 2)
const babelScript = transformSync(script, {
retainLines: false,
plugins: [
...babelPlugins,
babelPluginTransformDirectoryImport(cwd, filePath, typesToLoad),
babelPluginTransformVueTemplate(template)
]
})
const prettierCode = prettier.format(babelScript.code, prettierOptions)
writeToDist(`${filePath}/${fileName}.js`, prettierCode)
if (hasStyles) {
const finalName = `${filePath}/${fileName}.css`
cssFiles.push(finalName)
writeToDist(finalName, dedent(css))
}
}
const jsLoader = async (filePath, fileName, fileType) => {
const isSource = filePath.includes('src')
if (excludedLibraries.includes(fileName + fileType)) {
const fullPath = `${filePath}/${fileName}${fileType}`
return fs.copyFileSync(`${cwd}/${fullPath}`, `${outDirectory}/${fullPath}`)
}
const script = readFile(filePath, fileName, fileType)
let babelScript = transformSync(script, {
...(isSource && {
retainLines: false
}),
plugins: [
...babelPlugins,
babelPluginTransformDirectoryImport(cwd, filePath, typesToLoad)
]
})
babelScript = babelScript.code
if (isSource) {
babelScript = prettier.format(babelScript, prettierOptions)
} else {
babelScript = (await minify(babelScript)).code
}
writeToDist(`${filePath}/${fileName}${fileType}`, babelScript)
}
const assetLoader = (filePath, fileName, fileType) => {
const fullPath = `${filePath}/${fileName}${fileType}`
fs.copyFileSync(`${cwd}/${fullPath}`, `${outDirectory}/${fullPath}`)
}
const iconLoader = (filePath, fileName, fileType) => {
fs.copyFileSync(`${cwd}/${filePath}/${fileName}${fileType}`, `${outDirectory}/${fileName}${fileType}`)
}
const loaders = [
{
test: '.vue',
use: vueLoader,
},
{
test: '.js',
use: jsLoader
},
{
test: /\.jpeg|\.jpg|\.png|\.svg|\.json|\.woff2|\.ttf|\.otf/,
use: assetLoader
},
{
test: '.ico',
use: iconLoader
}
]
const buildEntrypoint = () => {
const stylesheets = cssFiles.map(href => `<link rel="stylesheet" href="/${href}">`).join('\n')
const html = `
<!DOCTYPE html>
<html lang="">
<head>
<title></title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="/favicon.ico">
<!-- Styles -->
<link rel="stylesheet" href="/index.css">
${stylesheets}
<!-- Entrypoint -->
<script type="module" src="/src/main.js"></script>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
`
writeToDist('index.html', beautify.html(html))
}
const filenames = glob.sync(`**/*.?(${typesToLoad})`, {
cwd,
ignore: [
`*.json`,
`*.js`,
`.*`,
`node_modules/**/*`,
`dist/**/*`,
`${cwd}/node_modules/**/*`,
`${cwd}/dist/**/*`
]
})
createDistSubdir()
for (const filePath of filenames) {
const { dir: fileDirectory, name: fileName, ext: fileType } = parse(filePath)
createDistSubdir(fileDirectory)
for (const loader of loaders) {
if (fileType.match(loader.test)) {
loader.use(fileDirectory, fileName, fileType)
}
}
}
buildEntrypoint()
console.timeEnd('[ Build finished ]')
console.log()
console.log(`${chalk.bgHex('#90fa28').black(' DONE ')} Build complete. The dist directory ${chalk.yellowBright(outDirectory.replaceAll('\\', '/'))} is ready to be deployed.`)
console.log()