This repository has been archived by the owner on Aug 27, 2020. It is now read-only.
forked from vuejs-templates/webpack
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmetalsmith.js
87 lines (69 loc) · 2.72 KB
/
metalsmith.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
const path = require('path')
const copy = require('./lib/metalsmith-copy')
const deIndent = require('./lib/de-indent')
const srcRegexp = /^src[\/|\\]/g
const libRegexp = /^(.*\.)(lib\.)(ts|js)$/g
const componentsRegexp = /^(.*)([\/|\\]components)([\/|\\].*)$/g
module.exports = function (metalsmith, opts, helpers) {
function libraryProjectCopy (files, metalsmith, done) {
if (metalsmith.metadata().projectType === 'lib') {
copy({
move: true,
transform: function (file) {
// Move all files but .lib files and components directory to app directory
if (file.match(srcRegexp)) {
console.log(file)
if (!file.match(libRegexp) && !file.match(componentsRegexp)) {
file = 'app' + file.substring(3)
}
// Rename .lib files
file = file.replace(libRegexp, '$1$3')
}
return file
}
})(files, metalsmith, done)
} else {
done(null, files)
}
}
metalsmith.use(libraryProjectCopy)
function sfcExternals (files, metalsmith, done) {
function buildTagRegex(tag) {
// Sorry for that, but vue-cli doesn't support custom dependencies to parse vue files cleanly
return new RegExp('^(<' + tag + '.*?)(\\slang="(.*)")?(.*?>)([\\s|\\S]*)^(<\\/' + tag + '>)', 'm')
}
for (tag of ['template', 'style', 'script']) {
if (metalsmith.metadata().sfcExternals[tag]) {
const regex = buildTagRegex(tag)
for (const vueFilepath of Object.keys(files).filter(f => f.endsWith('.vue'))) {
const vueParsed = path.parse(vueFilepath)
const vueDirectory = vueParsed.dir.replace('\\', '/')
const vueBaseName = (vueParsed.name + vueParsed.ext).replace('\\', '/')
const vueFile = files[vueFilepath]
const text = vueFile.contents.toString('utf8')
const match = regex.exec(text)
if (!match) continue
let ext = match[3]
if (!ext) {
if (tag === 'template') {
ext = 'html'
} else if (tag === 'style') {
ext = 'css'
} else if (tag === 'script') {
ext = 'js'
}
}
const externalFilename = vueBaseName.replace(/vue$/, ext)
const replacedText = text.replace(regex, '$1$2 src="./' + externalFilename + '"$4\n$6')
const vueContent = Buffer.from(replacedText, 'utf8')
vueFile.contents = vueContent
const contents = Buffer.from(deIndent(match[5].replace(/\r?\n|\r/, '')))
const scriptFilepath = vueDirectory + '/' + externalFilename
files[scriptFilepath] = { contents }
}
}
}
done(null, files)
}
metalsmith.use(sfcExternals)
}