This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
197 lines (155 loc) · 4.29 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
const recursiveRead = require("recursive-readdir");
const path = require("path");
const VirtualModulePlugin = require("virtual-module-webpack-plugin");
let directory = __dirname;
let folder = false;
let allScoped = false;
const createdFiles = [];
function VueBuilderPlugin(options) {
if (path.isAbsolute(options.path)) {
directory = options.path;
} else {
directory = path.resolve(
path.join(__dirname, "..", "..", options.path || "")
);
}
if (options.folder) {
folder = true;
}
if (options.allScoped) {
allScoped = true;
}
}
const buildVues = (callback, compiler) => {
// eslint-disable-next-line no-console
console.log("Building vue files");
recursiveRead(directory, (err, files) => {
if (err) {
return callback(err);
}
const vues = {};
const sources = {
script: {},
template: {},
style: {},
};
const langCheck = (file, extension, type) => {
const length = -5 - extension.length;
let scoped = false;
if (file.slice(length) === `.vue.${extension}`) {
let name = file.slice(0, length);
if (type === "style" && name.slice(-7) === ".scoped") {
scoped = true;
name = name.slice(0, -7);
}
if (type === "style" && allScoped) {
scoped = true;
}
vues[name] = true;
sources[type][name] = {
file,
lang: extension,
};
if (scoped) {
sources.style[name].scoped = true;
}
return true;
}
return false;
};
const singleVue = (name, dirname) => {
let data = "";
const script = sources.script[name];
const style = sources.style[name];
const template = sources.template[name];
const relate = (file) => `.${path.sep}${path.relative(dirname, file)}`;
if (script) {
data += `<script src="${relate(script.file)}" lang="${
script.lang
}"></script>\n`;
}
if (style) {
data += `<style src="${relate(style.file)}" lang="${style.lang}"${
style.scoped ? " scoped" : ""
}></style>\n`;
}
if (template) {
data += `<template src="${relate(template.file)}" lang="${
template.lang
}"></template>\n`;
}
return data;
};
files.forEach((file) => {
if (langCheck(file, "html", "template")) {
return;
}
if (langCheck(file, "js", "script")) {
return;
}
if (langCheck(file, "css", "style")) {
return;
}
// HTML alternatives
if (langCheck(file, "jade", "template")) {
return;
}
if (langCheck(file, "pug", "template")) {
return;
}
// JS alternatives
if (langCheck(file, "coffee", "script")) {
return;
}
if (langCheck(file, "ts", "script")) {
return;
}
// CSS alternatives
if (langCheck(file, "sass", "style")) {
return;
}
if (langCheck(file, "scss", "style")) {
return;
}
if (langCheck(file, "less", "style")) {
return;
}
langCheck(file, "styl", "style");
});
Object.keys(vues).forEach((vue) => {
let dest = vue;
if (folder && path.basename(vue) === path.basename(path.dirname(vue))) {
dest = path.dirname(vue);
}
if (sources.script[vue] || sources.style[vue] || sources.template[vue]) {
const modulePath = `${dest}.vue`;
const ctime = VirtualModulePlugin.statsDate();
const contents = singleVue(vue, path.dirname(dest));
const fs = (this && this.fileSystem) || compiler.inputFileSystem;
createdFiles.push(modulePath);
VirtualModulePlugin.populateFilesystem({
fs,
modulePath,
contents,
ctime,
});
}
});
return callback();
});
};
VueBuilderPlugin.prototype.apply = (compiler) => {
compiler.plugin("run", (compilation, callback) =>
buildVues(callback, compiler)
);
compiler.plugin("watch-run", (compilation, callback) =>
buildVues(callback, compiler)
);
compiler.plugin("after-compile", (compilation, callback) => {
createdFiles.forEach((file) => {
compilation.fileDependencies.delete(file);
});
callback();
});
};
module.exports = VueBuilderPlugin;