@pedrolamas/plugin-vue2 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +129 -0
- package/dist/index.cjs +1260 -0
- package/dist/index.d.cts +41 -0
- package/dist/index.d.mts +39 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.mjs +1248 -0
- package/package.json +65 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1248 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { transformWithOxc, formatPostcssSourceMap, createFilter } from 'vite';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
|
|
7
|
+
function resolveCompiler(root) {
|
|
8
|
+
const compiler = tryRequire("vue/compiler-sfc", root) || tryRequire("vue/compiler-sfc");
|
|
9
|
+
if (!compiler) {
|
|
10
|
+
throw new Error(
|
|
11
|
+
`Failed to resolve vue/compiler-sfc.
|
|
12
|
+
@pedrolamas/plugin-vue2 requires vue (>=2.7.0) to be present in the dependency tree.`
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
return compiler;
|
|
16
|
+
}
|
|
17
|
+
const _require = createRequire(import.meta.url);
|
|
18
|
+
function tryRequire(id, from) {
|
|
19
|
+
try {
|
|
20
|
+
return from ? _require(_require.resolve(id, { paths: [from] })) : _require(id);
|
|
21
|
+
} catch (e) {
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function parseVueRequest(id) {
|
|
26
|
+
const [filename, rawQuery] = id.split(`?`, 2);
|
|
27
|
+
const query = Object.fromEntries(new URLSearchParams(rawQuery));
|
|
28
|
+
if (query.vue != null) {
|
|
29
|
+
query.vue = true;
|
|
30
|
+
}
|
|
31
|
+
if (query.index != null) {
|
|
32
|
+
query.index = Number(query.index);
|
|
33
|
+
}
|
|
34
|
+
if (query.raw != null) {
|
|
35
|
+
query.raw = true;
|
|
36
|
+
}
|
|
37
|
+
if (query.scoped != null) {
|
|
38
|
+
query.scoped = true;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
filename,
|
|
42
|
+
query
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function slash(path) {
|
|
47
|
+
const isExtendedLengthPath = path.startsWith('\\\\?\\');
|
|
48
|
+
|
|
49
|
+
if (isExtendedLengthPath) {
|
|
50
|
+
return path;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return path.replace(/\\/g, '/');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const cache = /* @__PURE__ */ new Map();
|
|
57
|
+
const prevCache = /* @__PURE__ */ new Map();
|
|
58
|
+
function createDescriptor(filename, source, { root, isProduction, sourceMap, compiler }) {
|
|
59
|
+
let descriptor;
|
|
60
|
+
let errors = [];
|
|
61
|
+
try {
|
|
62
|
+
descriptor = compiler.parse({
|
|
63
|
+
source,
|
|
64
|
+
filename,
|
|
65
|
+
sourceMap
|
|
66
|
+
});
|
|
67
|
+
} catch (e) {
|
|
68
|
+
errors = [e];
|
|
69
|
+
descriptor = compiler.parse({ source: ``, filename });
|
|
70
|
+
}
|
|
71
|
+
const normalizedPath = slash(path.normalize(path.relative(root, filename)));
|
|
72
|
+
descriptor.id = getHash(normalizedPath + (isProduction ? source : ""));
|
|
73
|
+
cache.set(filename, descriptor);
|
|
74
|
+
return { descriptor, errors };
|
|
75
|
+
}
|
|
76
|
+
function getPrevDescriptor(filename) {
|
|
77
|
+
return prevCache.get(filename);
|
|
78
|
+
}
|
|
79
|
+
function setPrevDescriptor(filename, entry) {
|
|
80
|
+
prevCache.set(filename, entry);
|
|
81
|
+
}
|
|
82
|
+
function getDescriptor(filename, options, createIfNotFound = true) {
|
|
83
|
+
if (cache.has(filename)) {
|
|
84
|
+
return cache.get(filename);
|
|
85
|
+
}
|
|
86
|
+
if (createIfNotFound) {
|
|
87
|
+
const { descriptor, errors } = createDescriptor(
|
|
88
|
+
filename,
|
|
89
|
+
fs.readFileSync(filename, "utf-8"),
|
|
90
|
+
options
|
|
91
|
+
);
|
|
92
|
+
if (errors.length) {
|
|
93
|
+
throw errors[0];
|
|
94
|
+
}
|
|
95
|
+
return descriptor;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function getSrcDescriptor(filename, query) {
|
|
99
|
+
if (query.scoped) {
|
|
100
|
+
return cache.get(`${filename}?src=${query.src}`);
|
|
101
|
+
}
|
|
102
|
+
return cache.get(filename);
|
|
103
|
+
}
|
|
104
|
+
function setSrcDescriptor(filename, entry, scoped) {
|
|
105
|
+
if (scoped) {
|
|
106
|
+
cache.set(`${filename}?src=${entry.id}`, entry);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
cache.set(filename, entry);
|
|
110
|
+
}
|
|
111
|
+
function getHash(text) {
|
|
112
|
+
return createHash("sha256").update(text).digest("hex").substring(0, 8);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const clientCache = /* @__PURE__ */ new WeakMap();
|
|
116
|
+
const ssrCache = /* @__PURE__ */ new WeakMap();
|
|
117
|
+
function getResolvedScript(descriptor, ssr) {
|
|
118
|
+
return (ssr ? ssrCache : clientCache).get(descriptor);
|
|
119
|
+
}
|
|
120
|
+
function setResolvedScript(descriptor, script, ssr) {
|
|
121
|
+
(clientCache).set(descriptor, script);
|
|
122
|
+
}
|
|
123
|
+
function resolveScript(descriptor, options, ssr) {
|
|
124
|
+
if (!descriptor.script && !descriptor.scriptSetup) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const cacheToUse = ssr ? ssrCache : clientCache;
|
|
128
|
+
const cached = cacheToUse.get(descriptor);
|
|
129
|
+
if (cached) {
|
|
130
|
+
return cached;
|
|
131
|
+
}
|
|
132
|
+
const resolved = options.compiler.compileScript(descriptor, {
|
|
133
|
+
...options.script,
|
|
134
|
+
id: descriptor.id,
|
|
135
|
+
isProd: options.isProduction,
|
|
136
|
+
sourceMap: options.sourceMap
|
|
137
|
+
});
|
|
138
|
+
cacheToUse.set(descriptor, resolved);
|
|
139
|
+
return resolved;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function getDefaultExportFromCjs (x) {
|
|
143
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
var hashSum;
|
|
147
|
+
var hasRequiredHashSum;
|
|
148
|
+
|
|
149
|
+
function requireHashSum () {
|
|
150
|
+
if (hasRequiredHashSum) return hashSum;
|
|
151
|
+
hasRequiredHashSum = 1;
|
|
152
|
+
|
|
153
|
+
function pad (hash, len) {
|
|
154
|
+
while (hash.length < len) {
|
|
155
|
+
hash = '0' + hash;
|
|
156
|
+
}
|
|
157
|
+
return hash;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function fold (hash, text) {
|
|
161
|
+
var i;
|
|
162
|
+
var chr;
|
|
163
|
+
var len;
|
|
164
|
+
if (text.length === 0) {
|
|
165
|
+
return hash;
|
|
166
|
+
}
|
|
167
|
+
for (i = 0, len = text.length; i < len; i++) {
|
|
168
|
+
chr = text.charCodeAt(i);
|
|
169
|
+
hash = ((hash << 5) - hash) + chr;
|
|
170
|
+
hash |= 0;
|
|
171
|
+
}
|
|
172
|
+
return hash < 0 ? hash * -2 : hash;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function foldObject (hash, o, seen) {
|
|
176
|
+
return Object.keys(o).sort().reduce(foldKey, hash);
|
|
177
|
+
function foldKey (hash, key) {
|
|
178
|
+
return foldValue(hash, o[key], key, seen);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function foldValue (input, value, key, seen) {
|
|
183
|
+
var hash = fold(fold(fold(input, key), toString(value)), typeof value);
|
|
184
|
+
if (value === null) {
|
|
185
|
+
return fold(hash, 'null');
|
|
186
|
+
}
|
|
187
|
+
if (value === undefined) {
|
|
188
|
+
return fold(hash, 'undefined');
|
|
189
|
+
}
|
|
190
|
+
if (typeof value === 'object' || typeof value === 'function') {
|
|
191
|
+
if (seen.indexOf(value) !== -1) {
|
|
192
|
+
return fold(hash, '[Circular]' + key);
|
|
193
|
+
}
|
|
194
|
+
seen.push(value);
|
|
195
|
+
|
|
196
|
+
var objHash = foldObject(hash, value, seen);
|
|
197
|
+
|
|
198
|
+
if (!('valueOf' in value) || typeof value.valueOf !== 'function') {
|
|
199
|
+
return objHash;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
return fold(objHash, String(value.valueOf()))
|
|
204
|
+
} catch (err) {
|
|
205
|
+
return fold(objHash, '[valueOf exception]' + (err.stack || err.message))
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return fold(hash, value.toString());
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function toString (o) {
|
|
212
|
+
return Object.prototype.toString.call(o);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function sum (o) {
|
|
216
|
+
return pad(foldValue(0, o, '', []).toString(16), 8);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
hashSum = sum;
|
|
220
|
+
return hashSum;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
var hashSumExports = requireHashSum();
|
|
224
|
+
const hash = /*@__PURE__*/getDefaultExportFromCjs(hashSumExports);
|
|
225
|
+
|
|
226
|
+
function createRollupError(id, error) {
|
|
227
|
+
if ("msg" in error) {
|
|
228
|
+
return {
|
|
229
|
+
id,
|
|
230
|
+
plugin: "vue",
|
|
231
|
+
message: error.msg,
|
|
232
|
+
name: "vue-compiler-error"
|
|
233
|
+
};
|
|
234
|
+
} else {
|
|
235
|
+
return {
|
|
236
|
+
id,
|
|
237
|
+
plugin: "vue",
|
|
238
|
+
message: error.message,
|
|
239
|
+
name: error.name,
|
|
240
|
+
stack: error.stack
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const HMR_RUNTIME_ID = "\0plugin-vue2:hmr-runtime";
|
|
246
|
+
const hmrRuntimeCode = `
|
|
247
|
+
var __VUE_HMR_RUNTIME__ = Object.create(null)
|
|
248
|
+
var map = Object.create(null)
|
|
249
|
+
|
|
250
|
+
__VUE_HMR_RUNTIME__.createRecord = function (id, options) {
|
|
251
|
+
if(map[id]) { return }
|
|
252
|
+
|
|
253
|
+
var Ctor = null
|
|
254
|
+
if (typeof options === 'function') {
|
|
255
|
+
Ctor = options
|
|
256
|
+
options = Ctor.options
|
|
257
|
+
}
|
|
258
|
+
makeOptionsHot(id, options)
|
|
259
|
+
map[id] = {
|
|
260
|
+
Ctor: Ctor,
|
|
261
|
+
options: options,
|
|
262
|
+
instances: []
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
__VUE_HMR_RUNTIME__.isRecorded = function (id) {
|
|
267
|
+
return typeof map[id] !== 'undefined'
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function makeOptionsHot(id, options) {
|
|
271
|
+
if (options.functional) {
|
|
272
|
+
var render = options.render
|
|
273
|
+
options.render = function (h, ctx) {
|
|
274
|
+
var instances = map[id].instances
|
|
275
|
+
if (ctx && instances.indexOf(ctx.parent) < 0) {
|
|
276
|
+
instances.push(ctx.parent)
|
|
277
|
+
}
|
|
278
|
+
return render(h, ctx)
|
|
279
|
+
}
|
|
280
|
+
} else {
|
|
281
|
+
injectHook(options, 'beforeCreate', function() {
|
|
282
|
+
var record = map[id]
|
|
283
|
+
if (!record.Ctor) {
|
|
284
|
+
record.Ctor = this.constructor
|
|
285
|
+
}
|
|
286
|
+
record.instances.push(this)
|
|
287
|
+
})
|
|
288
|
+
injectHook(options, 'beforeDestroy', function() {
|
|
289
|
+
var instances = map[id].instances
|
|
290
|
+
instances.splice(instances.indexOf(this), 1)
|
|
291
|
+
})
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function injectHook(options, name, hook) {
|
|
296
|
+
var existing = options[name]
|
|
297
|
+
options[name] = existing
|
|
298
|
+
? Array.isArray(existing) ? existing.concat(hook) : [existing, hook]
|
|
299
|
+
: [hook]
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function tryWrap(fn) {
|
|
303
|
+
return function (id, arg) {
|
|
304
|
+
try {
|
|
305
|
+
fn(id, arg)
|
|
306
|
+
} catch (e) {
|
|
307
|
+
console.error(e)
|
|
308
|
+
console.warn(
|
|
309
|
+
'Something went wrong during Vue component hot-reload. Full reload required.'
|
|
310
|
+
)
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function updateOptions (oldOptions, newOptions) {
|
|
316
|
+
for (var key in oldOptions) {
|
|
317
|
+
if (!(key in newOptions)) {
|
|
318
|
+
delete oldOptions[key]
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
for (var key$1 in newOptions) {
|
|
322
|
+
oldOptions[key$1] = newOptions[key$1]
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
__VUE_HMR_RUNTIME__.rerender = tryWrap(function (id, options) {
|
|
327
|
+
var record = map[id]
|
|
328
|
+
if (!options) {
|
|
329
|
+
record.instances.slice().forEach(function (instance) {
|
|
330
|
+
instance.$forceUpdate()
|
|
331
|
+
})
|
|
332
|
+
return
|
|
333
|
+
}
|
|
334
|
+
if (typeof options === 'function') {
|
|
335
|
+
options = options.options
|
|
336
|
+
}
|
|
337
|
+
if(record.functional){
|
|
338
|
+
record.render = options.render
|
|
339
|
+
record.staticRenderFns = options.staticRenderFns
|
|
340
|
+
__VUE_HMR_RUNTIME__.reload(id, record)
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
if (record.Ctor) {
|
|
344
|
+
record.Ctor.options.render = options.render
|
|
345
|
+
record.Ctor.options.staticRenderFns = options.staticRenderFns
|
|
346
|
+
record.instances.slice().forEach(function (instance) {
|
|
347
|
+
instance.$options.render = options.render
|
|
348
|
+
instance.$options.staticRenderFns = options.staticRenderFns
|
|
349
|
+
// reset static trees
|
|
350
|
+
// pre 2.5, all static trees are cached together on the instance
|
|
351
|
+
if (instance._staticTrees) {
|
|
352
|
+
instance._staticTrees = []
|
|
353
|
+
}
|
|
354
|
+
// 2.5.0
|
|
355
|
+
if (Array.isArray(record.Ctor.options.cached)) {
|
|
356
|
+
record.Ctor.options.cached = []
|
|
357
|
+
}
|
|
358
|
+
// 2.5.3
|
|
359
|
+
if (Array.isArray(instance.$options.cached)) {
|
|
360
|
+
instance.$options.cached = []
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// post 2.5.4: v-once trees are cached on instance._staticTrees.
|
|
364
|
+
// Pure static trees are cached on the staticRenderFns array
|
|
365
|
+
// (both already reset above)
|
|
366
|
+
|
|
367
|
+
// 2.6: temporarily mark rendered scoped slots as unstable so that
|
|
368
|
+
// child components can be forced to update
|
|
369
|
+
var restore = patchScopedSlots(instance)
|
|
370
|
+
instance.$forceUpdate()
|
|
371
|
+
instance.$nextTick(restore)
|
|
372
|
+
})
|
|
373
|
+
} else {
|
|
374
|
+
// functional or no instance created yet
|
|
375
|
+
record.options.render = options.render
|
|
376
|
+
record.options.staticRenderFns = options.staticRenderFns
|
|
377
|
+
|
|
378
|
+
// handle functional component re-render
|
|
379
|
+
if (record.options.functional) {
|
|
380
|
+
// rerender with full options
|
|
381
|
+
if (Object.keys(options).length > 2) {
|
|
382
|
+
updateOptions(record.options, options)
|
|
383
|
+
} else {
|
|
384
|
+
// template-only rerender.
|
|
385
|
+
// need to inject the style injection code for CSS modules
|
|
386
|
+
// to work properly.
|
|
387
|
+
var injectStyles = record.options._injectStyles
|
|
388
|
+
if (injectStyles) {
|
|
389
|
+
var render = options.render
|
|
390
|
+
record.options.render = function (h, ctx) {
|
|
391
|
+
injectStyles.call(ctx)
|
|
392
|
+
return render(h, ctx)
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
record.options._Ctor = null
|
|
397
|
+
// 2.5.3
|
|
398
|
+
if (Array.isArray(record.options.cached)) {
|
|
399
|
+
record.options.cached = []
|
|
400
|
+
}
|
|
401
|
+
record.instances.slice().forEach(function (instance) {
|
|
402
|
+
instance.$forceUpdate()
|
|
403
|
+
})
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
})
|
|
407
|
+
|
|
408
|
+
__VUE_HMR_RUNTIME__.reload = tryWrap(function (id, options) {
|
|
409
|
+
var record = map[id]
|
|
410
|
+
if (options) {
|
|
411
|
+
if (typeof options === 'function') {
|
|
412
|
+
options = options.options
|
|
413
|
+
}
|
|
414
|
+
makeOptionsHot(id, options)
|
|
415
|
+
if (record.Ctor) {
|
|
416
|
+
var newCtor = record.Ctor.super.extend(options)
|
|
417
|
+
// prevent record.options._Ctor from being overwritten accidentally
|
|
418
|
+
newCtor.options._Ctor = record.options._Ctor
|
|
419
|
+
record.Ctor.options = newCtor.options
|
|
420
|
+
record.Ctor.cid = newCtor.cid
|
|
421
|
+
record.Ctor.prototype = newCtor.prototype
|
|
422
|
+
if (newCtor.release) {
|
|
423
|
+
// temporary global mixin strategy used in < 2.0.0-alpha.6
|
|
424
|
+
newCtor.release()
|
|
425
|
+
}
|
|
426
|
+
} else {
|
|
427
|
+
updateOptions(record.options, options)
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
record.instances.slice().forEach(function (instance) {
|
|
431
|
+
if (instance.$vnode && instance.$vnode.context) {
|
|
432
|
+
instance.$vnode.context.$forceUpdate()
|
|
433
|
+
} else {
|
|
434
|
+
console.warn(
|
|
435
|
+
'Root or manually mounted instance modified. Full reload required.'
|
|
436
|
+
)
|
|
437
|
+
}
|
|
438
|
+
})
|
|
439
|
+
})
|
|
440
|
+
|
|
441
|
+
// 2.6 optimizes template-compiled scoped slots and skips updates if child
|
|
442
|
+
// only uses scoped slots. We need to patch the scoped slots resolving helper
|
|
443
|
+
// to temporarily mark all scoped slots as unstable in order to force child
|
|
444
|
+
// updates.
|
|
445
|
+
function patchScopedSlots (instance) {
|
|
446
|
+
if (!instance._u) { return }
|
|
447
|
+
// https://github.com/vuejs/vue/blob/dev/src/core/instance/render-helpers/resolve-scoped-slots.js
|
|
448
|
+
var original = instance._u
|
|
449
|
+
instance._u = function (slots) {
|
|
450
|
+
try {
|
|
451
|
+
// 2.6.4 ~ 2.6.6
|
|
452
|
+
return original(slots, true)
|
|
453
|
+
} catch (e) {
|
|
454
|
+
// 2.5 / >= 2.6.7
|
|
455
|
+
return original(slots, null, true)
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return function () {
|
|
459
|
+
instance._u = original
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
export default __VUE_HMR_RUNTIME__
|
|
463
|
+
`;
|
|
464
|
+
|
|
465
|
+
async function transformTemplateAsModule(code, descriptor, options, pluginContext, ssr) {
|
|
466
|
+
let returnCode = compile(code, descriptor, options, pluginContext, ssr);
|
|
467
|
+
if (options.devServer && options.devServer.config.server.hmr !== false && !ssr && !options.isProduction) {
|
|
468
|
+
returnCode += `
|
|
469
|
+
import __VUE_HMR_RUNTIME__ from "${HMR_RUNTIME_ID}"`;
|
|
470
|
+
returnCode += `
|
|
471
|
+
import.meta.hot.accept((updated) => {
|
|
472
|
+
__VUE_HMR_RUNTIME__.rerender(${JSON.stringify(descriptor.id)}, updated)
|
|
473
|
+
})`;
|
|
474
|
+
}
|
|
475
|
+
return returnCode + `
|
|
476
|
+
export { render, staticRenderFns }`;
|
|
477
|
+
}
|
|
478
|
+
function transformTemplateInMain(code, descriptor, options, pluginContext, ssr) {
|
|
479
|
+
return compile(code, descriptor, options, pluginContext, ssr).replace(/var (render|staticRenderFns) =/g, "var _sfc_$1 =").replace(/(render._withStripped)/, "_sfc_$1");
|
|
480
|
+
}
|
|
481
|
+
function compile(code, descriptor, options, pluginContext, ssr) {
|
|
482
|
+
const filename = descriptor.filename;
|
|
483
|
+
const result = options.compiler.compileTemplate({
|
|
484
|
+
...resolveTemplateCompilerOptions(descriptor, options, ssr),
|
|
485
|
+
source: code
|
|
486
|
+
});
|
|
487
|
+
if (result.errors.length) {
|
|
488
|
+
result.errors.forEach(
|
|
489
|
+
(error) => pluginContext.error(
|
|
490
|
+
typeof error === "string" ? { id: filename, message: error } : createRollupError(filename, error)
|
|
491
|
+
)
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
if (result.tips.length) {
|
|
495
|
+
result.tips.forEach(
|
|
496
|
+
(tip) => pluginContext.warn({
|
|
497
|
+
id: filename,
|
|
498
|
+
message: typeof tip === "string" ? tip : tip.msg
|
|
499
|
+
})
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
return transformRequireToImport(result.code);
|
|
503
|
+
}
|
|
504
|
+
function resolveTemplateCompilerOptions(descriptor, options, ssr) {
|
|
505
|
+
const block = descriptor.template;
|
|
506
|
+
if (!block) {
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
const resolvedScript = getResolvedScript(descriptor, ssr);
|
|
510
|
+
const hasScoped = descriptor.styles.some((s) => s.scoped);
|
|
511
|
+
const { id, filename } = descriptor;
|
|
512
|
+
let preprocessOptions = block.lang && options.template?.preprocessOptions;
|
|
513
|
+
if (block.lang === "pug") {
|
|
514
|
+
preprocessOptions = {
|
|
515
|
+
doctype: "html",
|
|
516
|
+
...preprocessOptions
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
const transformAssetUrls = options.template?.transformAssetUrls ?? true;
|
|
520
|
+
let assetUrlOptions;
|
|
521
|
+
if (options.devServer) {
|
|
522
|
+
if (filename.startsWith(options.root)) {
|
|
523
|
+
assetUrlOptions = {
|
|
524
|
+
base: (options.devServer.config.server?.origin ?? "") + options.devServer.config.base + slash(path.relative(options.root, path.dirname(filename)))
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
} else if (transformAssetUrls !== false) {
|
|
528
|
+
assetUrlOptions = {
|
|
529
|
+
includeAbsolute: true
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
return {
|
|
533
|
+
transformAssetUrls,
|
|
534
|
+
...options.template,
|
|
535
|
+
filename,
|
|
536
|
+
isProduction: options.isProduction,
|
|
537
|
+
isFunctional: !!block.attrs.functional,
|
|
538
|
+
optimizeSSR: ssr,
|
|
539
|
+
transformAssetUrlsOptions: {
|
|
540
|
+
...assetUrlOptions,
|
|
541
|
+
...options.template?.transformAssetUrlsOptions
|
|
542
|
+
},
|
|
543
|
+
preprocessLang: block.lang,
|
|
544
|
+
preprocessOptions,
|
|
545
|
+
bindings: resolvedScript ? resolvedScript.bindings : void 0,
|
|
546
|
+
prettify: false,
|
|
547
|
+
compilerOptions: {
|
|
548
|
+
whitespace: "condense",
|
|
549
|
+
outputSourceRange: true,
|
|
550
|
+
...options.template?.compilerOptions,
|
|
551
|
+
scopeId: hasScoped ? `data-v-${id}` : void 0
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
function transformRequireToImport(code) {
|
|
556
|
+
const imports = {};
|
|
557
|
+
let strImports = "";
|
|
558
|
+
code = code.replace(
|
|
559
|
+
/require\(("(?:[^"\\]|\\.)+"|'(?:[^'\\]|\\.)+')\)/g,
|
|
560
|
+
(_, name) => {
|
|
561
|
+
if (!(name in imports)) {
|
|
562
|
+
imports[name] = `__$_require_${hash(name)}__`;
|
|
563
|
+
strImports += `import ${imports[name]} from ${name}
|
|
564
|
+
`;
|
|
565
|
+
}
|
|
566
|
+
return imports[name];
|
|
567
|
+
}
|
|
568
|
+
);
|
|
569
|
+
return strImports + code;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const directRequestRE = /(\?|&)direct\b/;
|
|
573
|
+
async function handleHotUpdate({ file, modules, read, server }, options) {
|
|
574
|
+
const prevDescriptor = getDescriptor(file, options, false);
|
|
575
|
+
if (!prevDescriptor) {
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
setPrevDescriptor(file, prevDescriptor);
|
|
579
|
+
const content = await read();
|
|
580
|
+
const { descriptor } = createDescriptor(file, content, options);
|
|
581
|
+
let needRerender = false;
|
|
582
|
+
const affectedModules = /* @__PURE__ */ new Set();
|
|
583
|
+
const mainModule = modules.find(
|
|
584
|
+
(m) => !/type=/.test(m.url) || /type=script/.test(m.url)
|
|
585
|
+
);
|
|
586
|
+
const templateModule = modules.find((m) => /type=template/.test(m.url));
|
|
587
|
+
const scriptChanged = hasScriptChanged(prevDescriptor, descriptor);
|
|
588
|
+
if (scriptChanged) {
|
|
589
|
+
let scriptModule;
|
|
590
|
+
if (descriptor.scriptSetup?.lang && !descriptor.scriptSetup.src || descriptor.script?.lang && !descriptor.script.src) {
|
|
591
|
+
const scriptModuleRE = new RegExp(
|
|
592
|
+
`type=script.*&lang.${descriptor.scriptSetup?.lang || descriptor.script?.lang}$`
|
|
593
|
+
);
|
|
594
|
+
scriptModule = modules.find((m) => scriptModuleRE.test(m.url));
|
|
595
|
+
}
|
|
596
|
+
affectedModules.add(scriptModule || mainModule);
|
|
597
|
+
}
|
|
598
|
+
if (!isEqualBlock(descriptor.template, prevDescriptor.template)) {
|
|
599
|
+
if (!scriptChanged) {
|
|
600
|
+
setResolvedScript(
|
|
601
|
+
descriptor,
|
|
602
|
+
getResolvedScript(prevDescriptor, false));
|
|
603
|
+
}
|
|
604
|
+
affectedModules.add(templateModule);
|
|
605
|
+
needRerender = true;
|
|
606
|
+
}
|
|
607
|
+
const prevStyles = prevDescriptor.styles || [];
|
|
608
|
+
const nextStyles = descriptor.styles || [];
|
|
609
|
+
if (prevStyles.some((s) => s.scoped) !== nextStyles.some((s) => s.scoped)) {
|
|
610
|
+
affectedModules.add(templateModule);
|
|
611
|
+
affectedModules.add(mainModule);
|
|
612
|
+
}
|
|
613
|
+
for (let i = 0; i < nextStyles.length; i++) {
|
|
614
|
+
const prev = prevStyles[i];
|
|
615
|
+
const next = nextStyles[i];
|
|
616
|
+
if (!prev || !isEqualBlock(prev, next)) {
|
|
617
|
+
const mod = modules.find(
|
|
618
|
+
(m) => m.url.includes(`type=style&index=${i}`) && m.url.endsWith(`.${next.lang || "css"}`) && !directRequestRE.test(m.url)
|
|
619
|
+
);
|
|
620
|
+
if (mod) {
|
|
621
|
+
affectedModules.add(mod);
|
|
622
|
+
if (mod.url.includes("&inline")) {
|
|
623
|
+
affectedModules.add(mainModule);
|
|
624
|
+
}
|
|
625
|
+
} else {
|
|
626
|
+
affectedModules.add(mainModule);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
if (prevStyles.length > nextStyles.length) {
|
|
631
|
+
affectedModules.add(mainModule);
|
|
632
|
+
}
|
|
633
|
+
const prevCustoms = prevDescriptor.customBlocks || [];
|
|
634
|
+
const nextCustoms = descriptor.customBlocks || [];
|
|
635
|
+
if (prevCustoms.length !== nextCustoms.length) {
|
|
636
|
+
affectedModules.add(mainModule);
|
|
637
|
+
} else {
|
|
638
|
+
for (let i = 0; i < nextCustoms.length; i++) {
|
|
639
|
+
const prev = prevCustoms[i];
|
|
640
|
+
const next = nextCustoms[i];
|
|
641
|
+
if (!prev || !isEqualBlock(prev, next)) {
|
|
642
|
+
const mod = modules.find(
|
|
643
|
+
(m) => m.url.includes(`type=${prev.type}&index=${i}`)
|
|
644
|
+
);
|
|
645
|
+
if (mod) {
|
|
646
|
+
affectedModules.add(mod);
|
|
647
|
+
} else {
|
|
648
|
+
affectedModules.add(mainModule);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
if (needRerender) {
|
|
654
|
+
if (!templateModule) {
|
|
655
|
+
affectedModules.add(mainModule);
|
|
656
|
+
} else if (mainModule && !affectedModules.has(mainModule)) {
|
|
657
|
+
const styleImporters = [...mainModule.importers].filter(
|
|
658
|
+
(m) => /\.css($|\?)/.test(m.url)
|
|
659
|
+
);
|
|
660
|
+
styleImporters.forEach((m) => affectedModules.add(m));
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
return [...affectedModules].filter(Boolean);
|
|
664
|
+
}
|
|
665
|
+
function isEqualBlock(a, b) {
|
|
666
|
+
if (!a && !b) return true;
|
|
667
|
+
if (!a || !b) return false;
|
|
668
|
+
if (a.src && b.src && a.src === b.src) return true;
|
|
669
|
+
if (a.content !== b.content) return false;
|
|
670
|
+
const keysA = Object.keys(a.attrs);
|
|
671
|
+
const keysB = Object.keys(b.attrs);
|
|
672
|
+
if (keysA.length !== keysB.length) {
|
|
673
|
+
return false;
|
|
674
|
+
}
|
|
675
|
+
return keysA.every((key) => a.attrs[key] === b.attrs[key]);
|
|
676
|
+
}
|
|
677
|
+
function isOnlyTemplateChanged(prev, next) {
|
|
678
|
+
return !hasScriptChanged(prev, next) && prev.styles.length === next.styles.length && prev.styles.every((s, i) => isEqualBlock(s, next.styles[i])) && prev.customBlocks.length === next.customBlocks.length && prev.customBlocks.every((s, i) => isEqualBlock(s, next.customBlocks[i]));
|
|
679
|
+
}
|
|
680
|
+
function hasScriptChanged(prev, next) {
|
|
681
|
+
if (!isEqualBlock(prev.script, next.script)) {
|
|
682
|
+
return true;
|
|
683
|
+
}
|
|
684
|
+
if (!isEqualBlock(prev.scriptSetup, next.scriptSetup)) {
|
|
685
|
+
return true;
|
|
686
|
+
}
|
|
687
|
+
const prevResolvedScript = getResolvedScript(prev, false);
|
|
688
|
+
const prevImports = prevResolvedScript?.imports;
|
|
689
|
+
if (prevImports) {
|
|
690
|
+
return next.shouldForceReload(prevImports);
|
|
691
|
+
}
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
const NORMALIZER_ID = "\0plugin-vue2:normalizer";
|
|
696
|
+
const normalizerCode = `
|
|
697
|
+
export default function normalizeComponent (
|
|
698
|
+
scriptExports,
|
|
699
|
+
render,
|
|
700
|
+
staticRenderFns,
|
|
701
|
+
functionalTemplate,
|
|
702
|
+
injectStyles,
|
|
703
|
+
scopeId,
|
|
704
|
+
moduleIdentifier, /* server only */
|
|
705
|
+
shadowMode /* vue-cli only */
|
|
706
|
+
) {
|
|
707
|
+
// Vue.extend constructor export interop
|
|
708
|
+
var options = typeof scriptExports === 'function'
|
|
709
|
+
? scriptExports.options
|
|
710
|
+
: scriptExports
|
|
711
|
+
|
|
712
|
+
// render functions
|
|
713
|
+
if (render) {
|
|
714
|
+
options.render = render
|
|
715
|
+
options.staticRenderFns = staticRenderFns
|
|
716
|
+
options._compiled = true
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// functional template
|
|
720
|
+
if (functionalTemplate) {
|
|
721
|
+
options.functional = true
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// scopedId
|
|
725
|
+
if (scopeId) {
|
|
726
|
+
options._scopeId = 'data-v-' + scopeId
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
var hook
|
|
730
|
+
if (moduleIdentifier) { // server build
|
|
731
|
+
hook = function (context) {
|
|
732
|
+
// 2.3 injection
|
|
733
|
+
context =
|
|
734
|
+
context || // cached call
|
|
735
|
+
(this.$vnode && this.$vnode.ssrContext) || // stateful
|
|
736
|
+
(this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext) // functional
|
|
737
|
+
// 2.2 with runInNewContext: true
|
|
738
|
+
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
|
|
739
|
+
context = __VUE_SSR_CONTEXT__
|
|
740
|
+
}
|
|
741
|
+
// inject component styles
|
|
742
|
+
if (injectStyles) {
|
|
743
|
+
injectStyles.call(this, context)
|
|
744
|
+
}
|
|
745
|
+
// register component module identifier for async chunk inference
|
|
746
|
+
if (context && context._registeredComponents) {
|
|
747
|
+
context._registeredComponents.add(moduleIdentifier)
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
// used by ssr in case component is cached and beforeCreate
|
|
751
|
+
// never gets called
|
|
752
|
+
options._ssrRegister = hook
|
|
753
|
+
} else if (injectStyles) {
|
|
754
|
+
hook = shadowMode
|
|
755
|
+
? function () {
|
|
756
|
+
injectStyles.call(
|
|
757
|
+
this,
|
|
758
|
+
(options.functional ? this.parent : this).$root.$options.shadowRoot
|
|
759
|
+
)
|
|
760
|
+
}
|
|
761
|
+
: injectStyles
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
if (hook) {
|
|
765
|
+
if (options.functional) {
|
|
766
|
+
// for template-only hot-reload because in that case the render fn doesn't
|
|
767
|
+
// go through the normalizer
|
|
768
|
+
options._injectStyles = hook
|
|
769
|
+
// register for functional component in vue file
|
|
770
|
+
var originalRender = options.render
|
|
771
|
+
options.render = function renderWithStyleInjection (h, context) {
|
|
772
|
+
hook.call(context)
|
|
773
|
+
return originalRender(h, context)
|
|
774
|
+
}
|
|
775
|
+
} else {
|
|
776
|
+
// inject component registration as beforeCreate hook
|
|
777
|
+
var existing = options.beforeCreate
|
|
778
|
+
options.beforeCreate = existing
|
|
779
|
+
? [].concat(existing, hook)
|
|
780
|
+
: [hook]
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
return {
|
|
785
|
+
exports: scriptExports,
|
|
786
|
+
options: options
|
|
787
|
+
}
|
|
788
|
+
}`;
|
|
789
|
+
|
|
790
|
+
async function transformMain(code, filename, options, pluginContext, ssr) {
|
|
791
|
+
const { devServer, isProduction, devToolsEnabled } = options;
|
|
792
|
+
const prevDescriptor = getPrevDescriptor(filename);
|
|
793
|
+
const { descriptor, errors } = createDescriptor(filename, code, options);
|
|
794
|
+
if (errors.length) {
|
|
795
|
+
errors.forEach(
|
|
796
|
+
(error) => pluginContext.error(createRollupError(filename, error))
|
|
797
|
+
);
|
|
798
|
+
return null;
|
|
799
|
+
}
|
|
800
|
+
const hasScoped = descriptor.styles.some((s) => s.scoped);
|
|
801
|
+
const hasCssModules = descriptor.styles.some((s) => s.module);
|
|
802
|
+
const hasFunctional = descriptor.template && descriptor.template.attrs.functional;
|
|
803
|
+
const { code: scriptCode, map: scriptMap } = await genScriptCode(
|
|
804
|
+
descriptor,
|
|
805
|
+
options,
|
|
806
|
+
pluginContext,
|
|
807
|
+
ssr
|
|
808
|
+
);
|
|
809
|
+
const templateCode = await genTemplateCode(
|
|
810
|
+
descriptor,
|
|
811
|
+
options,
|
|
812
|
+
pluginContext,
|
|
813
|
+
ssr
|
|
814
|
+
);
|
|
815
|
+
const stylesCode = await genStyleCode(descriptor, pluginContext);
|
|
816
|
+
const customBlocksCode = await genCustomBlockCode(descriptor, pluginContext);
|
|
817
|
+
const output = [
|
|
818
|
+
scriptCode,
|
|
819
|
+
templateCode,
|
|
820
|
+
stylesCode,
|
|
821
|
+
customBlocksCode
|
|
822
|
+
];
|
|
823
|
+
output.push(
|
|
824
|
+
`/* normalize component */
|
|
825
|
+
import __normalizer from "${NORMALIZER_ID}"
|
|
826
|
+
var __component__ = /*#__PURE__*/__normalizer(
|
|
827
|
+
_sfc_main,
|
|
828
|
+
_sfc_render,
|
|
829
|
+
_sfc_staticRenderFns,
|
|
830
|
+
${hasFunctional ? "true" : "false"},
|
|
831
|
+
${hasCssModules ? `_sfc_injectStyles` : `null`},
|
|
832
|
+
${hasScoped ? JSON.stringify(descriptor.id) : "null"},
|
|
833
|
+
null,
|
|
834
|
+
null
|
|
835
|
+
)`
|
|
836
|
+
);
|
|
837
|
+
if (devToolsEnabled || devServer && !isProduction) {
|
|
838
|
+
output.push(
|
|
839
|
+
`__component__.options.__file = ${JSON.stringify(
|
|
840
|
+
isProduction ? path.basename(filename) : filename
|
|
841
|
+
)}`
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
if (devServer && devServer.config.server.hmr !== false && !ssr && !isProduction) {
|
|
845
|
+
const id = JSON.stringify(descriptor.id);
|
|
846
|
+
output.push(
|
|
847
|
+
`import __VUE_HMR_RUNTIME__ from "${HMR_RUNTIME_ID}"`,
|
|
848
|
+
`if (!__VUE_HMR_RUNTIME__.isRecorded(${id})) {`,
|
|
849
|
+
` __VUE_HMR_RUNTIME__.createRecord(${id}, __component__.options)`,
|
|
850
|
+
`}`
|
|
851
|
+
);
|
|
852
|
+
if (hasFunctional || prevDescriptor && isOnlyTemplateChanged(prevDescriptor, descriptor)) {
|
|
853
|
+
output.push(`export const _rerender_only = true`);
|
|
854
|
+
}
|
|
855
|
+
output.push(
|
|
856
|
+
`import.meta.hot.accept(mod => {`,
|
|
857
|
+
` if (!mod) return`,
|
|
858
|
+
` const { default: updated, _rerender_only } = mod`,
|
|
859
|
+
` if (_rerender_only) {`,
|
|
860
|
+
` __VUE_HMR_RUNTIME__.rerender(${id}, updated)`,
|
|
861
|
+
` } else {`,
|
|
862
|
+
` __VUE_HMR_RUNTIME__.reload(${id}, updated)`,
|
|
863
|
+
` }`,
|
|
864
|
+
`})`
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
let resolvedMap = scriptMap;
|
|
868
|
+
output.push(`export default __component__.exports`);
|
|
869
|
+
let resolvedCode = output.join("\n");
|
|
870
|
+
if ((descriptor.script?.lang === "ts" || descriptor.scriptSetup?.lang === "ts") && !descriptor.script?.src) {
|
|
871
|
+
resolvedMap = resolvedMap ? { ...resolvedMap, file: resolvedMap.file ?? filename } : void 0;
|
|
872
|
+
const { code: code2, map } = await transformWithOxc(
|
|
873
|
+
resolvedCode,
|
|
874
|
+
filename,
|
|
875
|
+
{
|
|
876
|
+
lang: "ts",
|
|
877
|
+
sourcemap: options.sourceMap
|
|
878
|
+
},
|
|
879
|
+
resolvedMap
|
|
880
|
+
);
|
|
881
|
+
resolvedCode = code2;
|
|
882
|
+
resolvedMap = resolvedMap ? map : resolvedMap;
|
|
883
|
+
}
|
|
884
|
+
return {
|
|
885
|
+
code: resolvedCode,
|
|
886
|
+
map: resolvedMap || {
|
|
887
|
+
mappings: ""
|
|
888
|
+
},
|
|
889
|
+
meta: {
|
|
890
|
+
vite: {
|
|
891
|
+
lang: descriptor.script?.lang || descriptor.scriptSetup?.lang || "js"
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
async function genTemplateCode(descriptor, options, pluginContext, ssr) {
|
|
897
|
+
const template = descriptor.template;
|
|
898
|
+
if (!template) {
|
|
899
|
+
return "const _sfc_render = null; const _sfc_staticRenderFns = null";
|
|
900
|
+
}
|
|
901
|
+
const hasScoped = descriptor.styles.some((style) => style.scoped);
|
|
902
|
+
if (!template.lang && !template.src) {
|
|
903
|
+
return transformTemplateInMain(
|
|
904
|
+
template.content,
|
|
905
|
+
descriptor,
|
|
906
|
+
options,
|
|
907
|
+
pluginContext,
|
|
908
|
+
ssr
|
|
909
|
+
);
|
|
910
|
+
} else {
|
|
911
|
+
if (template.src) {
|
|
912
|
+
await linkSrcToDescriptor(
|
|
913
|
+
template.src,
|
|
914
|
+
descriptor,
|
|
915
|
+
pluginContext,
|
|
916
|
+
hasScoped
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
const src = template.src || descriptor.filename;
|
|
920
|
+
const srcQuery = template.src ? hasScoped ? `&src=${descriptor.id}` : "&src=true" : "";
|
|
921
|
+
const scopedQuery = hasScoped ? `&scoped=${descriptor.id}` : ``;
|
|
922
|
+
const attrsQuery = attrsToQuery(template.attrs, "js", true);
|
|
923
|
+
const query = `?vue&type=template${srcQuery}${scopedQuery}${attrsQuery}`;
|
|
924
|
+
const request = JSON.stringify(src + query);
|
|
925
|
+
return `import { render as _sfc_render, staticRenderFns as _sfc_staticRenderFns } from ${request}`;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
async function genScriptCode(descriptor, options, pluginContext, ssr) {
|
|
929
|
+
let scriptCode = `const _sfc_main = {}`;
|
|
930
|
+
let map;
|
|
931
|
+
const script = resolveScript(descriptor, options, ssr);
|
|
932
|
+
if (script) {
|
|
933
|
+
if ((!script.lang || script.lang === "ts" && options.devServer) && !script.src) {
|
|
934
|
+
const userPlugins = options.script?.babelParserPlugins || [];
|
|
935
|
+
const defaultPlugins = script.lang === "ts" ? userPlugins.includes("decorators") ? ["typescript"] : ["typescript", "decorators-legacy"] : [];
|
|
936
|
+
scriptCode = options.compiler.rewriteDefault(
|
|
937
|
+
script.content,
|
|
938
|
+
"_sfc_main",
|
|
939
|
+
[...defaultPlugins, ...userPlugins]
|
|
940
|
+
);
|
|
941
|
+
map = script.map;
|
|
942
|
+
} else {
|
|
943
|
+
if (script.src) {
|
|
944
|
+
await linkSrcToDescriptor(script.src, descriptor, pluginContext, false);
|
|
945
|
+
}
|
|
946
|
+
const src = script.src || descriptor.filename;
|
|
947
|
+
const langFallback = script.src && path.extname(src).slice(1) || "js";
|
|
948
|
+
const attrsQuery = attrsToQuery(script.attrs, langFallback);
|
|
949
|
+
const srcQuery = script.src ? `&src=true` : ``;
|
|
950
|
+
const query = `?vue&type=script${srcQuery}${attrsQuery}`;
|
|
951
|
+
const request = JSON.stringify(src + query);
|
|
952
|
+
scriptCode = `import _sfc_main from ${request}
|
|
953
|
+
export * from ${request}`;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return {
|
|
957
|
+
code: scriptCode,
|
|
958
|
+
map
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
async function genStyleCode(descriptor, pluginContext) {
|
|
962
|
+
let stylesCode = ``;
|
|
963
|
+
let cssModulesMap;
|
|
964
|
+
if (descriptor.styles.length) {
|
|
965
|
+
for (let i = 0; i < descriptor.styles.length; i++) {
|
|
966
|
+
const style = descriptor.styles[i];
|
|
967
|
+
if (style.src) {
|
|
968
|
+
await linkSrcToDescriptor(
|
|
969
|
+
style.src,
|
|
970
|
+
descriptor,
|
|
971
|
+
pluginContext,
|
|
972
|
+
style.scoped
|
|
973
|
+
);
|
|
974
|
+
}
|
|
975
|
+
const src = style.src || descriptor.filename;
|
|
976
|
+
const attrsQuery = attrsToQuery(style.attrs, "css");
|
|
977
|
+
const srcQuery = style.src ? style.scoped ? `&src=${descriptor.id}` : "&src=true" : "";
|
|
978
|
+
const directQuery = ``;
|
|
979
|
+
const scopedQuery = style.scoped ? `&scoped=${descriptor.id}` : ``;
|
|
980
|
+
const query = `?vue&type=style&index=${i}${srcQuery}${directQuery}${scopedQuery}`;
|
|
981
|
+
const styleRequest = src + query + attrsQuery;
|
|
982
|
+
if (style.module) {
|
|
983
|
+
const [importCode, nameMap] = genCSSModulesCode(
|
|
984
|
+
i,
|
|
985
|
+
styleRequest,
|
|
986
|
+
style.module
|
|
987
|
+
);
|
|
988
|
+
stylesCode += importCode;
|
|
989
|
+
Object.assign(cssModulesMap ||= {}, nameMap);
|
|
990
|
+
} else {
|
|
991
|
+
stylesCode += `
|
|
992
|
+
import ${JSON.stringify(styleRequest)}`;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
if (cssModulesMap) {
|
|
997
|
+
const mappingCode = Object.entries(cssModulesMap).reduce(
|
|
998
|
+
(code, [key, value]) => code + `"${key}":${value},
|
|
999
|
+
`,
|
|
1000
|
+
"{\n"
|
|
1001
|
+
) + "}";
|
|
1002
|
+
stylesCode += `
|
|
1003
|
+
const __cssModules = ${mappingCode}`;
|
|
1004
|
+
stylesCode += `
|
|
1005
|
+
function _sfc_injectStyles(ctx) {
|
|
1006
|
+
for (var key in __cssModules) {
|
|
1007
|
+
this[key] = __cssModules[key]
|
|
1008
|
+
}
|
|
1009
|
+
}`;
|
|
1010
|
+
}
|
|
1011
|
+
return stylesCode;
|
|
1012
|
+
}
|
|
1013
|
+
function genCSSModulesCode(index, request, moduleName) {
|
|
1014
|
+
const styleVar = `style${index}`;
|
|
1015
|
+
const exposedName = typeof moduleName === "string" ? moduleName : "$style";
|
|
1016
|
+
const moduleRequest = request.replace(/\.(\w+)$/, ".module.$1");
|
|
1017
|
+
return [
|
|
1018
|
+
`
|
|
1019
|
+
import ${styleVar} from ${JSON.stringify(moduleRequest)}`,
|
|
1020
|
+
{ [exposedName]: styleVar }
|
|
1021
|
+
];
|
|
1022
|
+
}
|
|
1023
|
+
async function genCustomBlockCode(descriptor, pluginContext) {
|
|
1024
|
+
let code = "";
|
|
1025
|
+
for (let index = 0; index < descriptor.customBlocks.length; index++) {
|
|
1026
|
+
const block = descriptor.customBlocks[index];
|
|
1027
|
+
if (block.src) {
|
|
1028
|
+
await linkSrcToDescriptor(block.src, descriptor, pluginContext, false);
|
|
1029
|
+
}
|
|
1030
|
+
const src = block.src || descriptor.filename;
|
|
1031
|
+
const attrsQuery = attrsToQuery(block.attrs, block.type);
|
|
1032
|
+
const srcQuery = block.src ? `&src=true` : ``;
|
|
1033
|
+
const query = `?vue&type=${block.type}&index=${index}${srcQuery}${attrsQuery}`;
|
|
1034
|
+
const request = JSON.stringify(src + query);
|
|
1035
|
+
code += `import block${index} from ${request}
|
|
1036
|
+
`;
|
|
1037
|
+
code += `if (typeof block${index} === 'function') block${index}(_sfc_main)
|
|
1038
|
+
`;
|
|
1039
|
+
}
|
|
1040
|
+
return code;
|
|
1041
|
+
}
|
|
1042
|
+
async function linkSrcToDescriptor(src, descriptor, pluginContext, scoped) {
|
|
1043
|
+
const srcFile = (await pluginContext.resolve(src, descriptor.filename))?.id || src;
|
|
1044
|
+
setSrcDescriptor(srcFile.replace(/\?.*$/, ""), descriptor, scoped);
|
|
1045
|
+
}
|
|
1046
|
+
const ignoreList = ["id", "index", "src", "type", "lang", "module", "scoped"];
|
|
1047
|
+
function attrsToQuery(attrs, langFallback, forceLangFallback = false) {
|
|
1048
|
+
let query = ``;
|
|
1049
|
+
for (const name in attrs) {
|
|
1050
|
+
const value = attrs[name];
|
|
1051
|
+
if (!ignoreList.includes(name)) {
|
|
1052
|
+
query += `&${encodeURIComponent(name)}${value ? `=${encodeURIComponent(value)}` : ``}`;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
if (langFallback || attrs.lang) {
|
|
1056
|
+
query += `lang` in attrs ? forceLangFallback ? `&lang.${langFallback}` : `&lang.${attrs.lang}` : `&lang.${langFallback}`;
|
|
1057
|
+
}
|
|
1058
|
+
return query;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
async function transformStyle(code, descriptor, index, options, pluginContext, filename) {
|
|
1062
|
+
const block = descriptor.styles[index];
|
|
1063
|
+
const result = await options.compiler.compileStyleAsync({
|
|
1064
|
+
...options.style,
|
|
1065
|
+
filename: descriptor.filename,
|
|
1066
|
+
id: `data-v-${descriptor.id}`,
|
|
1067
|
+
isProd: options.isProduction,
|
|
1068
|
+
source: code,
|
|
1069
|
+
scoped: !!block.scoped,
|
|
1070
|
+
...options.cssDevSourcemap ? {
|
|
1071
|
+
postcssOptions: {
|
|
1072
|
+
map: {
|
|
1073
|
+
from: filename,
|
|
1074
|
+
inline: false,
|
|
1075
|
+
annotation: false
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
} : {}
|
|
1079
|
+
});
|
|
1080
|
+
if (result.errors.length) {
|
|
1081
|
+
result.errors.forEach((error) => {
|
|
1082
|
+
if (error.line && error.column) {
|
|
1083
|
+
error.loc = {
|
|
1084
|
+
file: descriptor.filename,
|
|
1085
|
+
line: error.line + getLine(descriptor.source, block.start),
|
|
1086
|
+
column: error.column
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
1089
|
+
pluginContext.error(error);
|
|
1090
|
+
});
|
|
1091
|
+
return null;
|
|
1092
|
+
}
|
|
1093
|
+
const map = result.map ? await formatPostcssSourceMap(
|
|
1094
|
+
// version property of result.map is declared as string
|
|
1095
|
+
// but actually it is a number
|
|
1096
|
+
result.map,
|
|
1097
|
+
filename
|
|
1098
|
+
) : { mappings: "" };
|
|
1099
|
+
return {
|
|
1100
|
+
code: result.code,
|
|
1101
|
+
map
|
|
1102
|
+
};
|
|
1103
|
+
}
|
|
1104
|
+
function getLine(source, start) {
|
|
1105
|
+
const lines = source.split(/\r?\n/g);
|
|
1106
|
+
let cur = 0;
|
|
1107
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1108
|
+
cur += lines[i].length;
|
|
1109
|
+
if (cur >= start) {
|
|
1110
|
+
return i;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
function vuePlugin(rawOptions = {}) {
|
|
1116
|
+
const {
|
|
1117
|
+
include = /\.vue$/,
|
|
1118
|
+
exclude
|
|
1119
|
+
// customElement = /\.ce\.vue$/,
|
|
1120
|
+
// reactivityTransform = false
|
|
1121
|
+
} = rawOptions;
|
|
1122
|
+
const filter = createFilter(include, exclude);
|
|
1123
|
+
let options = {
|
|
1124
|
+
isProduction: process.env.NODE_ENV === "production",
|
|
1125
|
+
compiler: null,
|
|
1126
|
+
// to be set in buildStart
|
|
1127
|
+
...rawOptions,
|
|
1128
|
+
include,
|
|
1129
|
+
exclude,
|
|
1130
|
+
// customElement,
|
|
1131
|
+
// reactivityTransform,
|
|
1132
|
+
root: process.cwd(),
|
|
1133
|
+
sourceMap: true,
|
|
1134
|
+
cssDevSourcemap: false,
|
|
1135
|
+
devToolsEnabled: process.env.NODE_ENV !== "production"
|
|
1136
|
+
};
|
|
1137
|
+
return {
|
|
1138
|
+
name: "vite:vue2",
|
|
1139
|
+
handleHotUpdate(ctx) {
|
|
1140
|
+
if (!filter(ctx.file)) {
|
|
1141
|
+
return;
|
|
1142
|
+
}
|
|
1143
|
+
return handleHotUpdate(ctx, options);
|
|
1144
|
+
},
|
|
1145
|
+
configResolved(config) {
|
|
1146
|
+
options = {
|
|
1147
|
+
...options,
|
|
1148
|
+
root: config.root,
|
|
1149
|
+
isProduction: config.isProduction,
|
|
1150
|
+
sourceMap: config.command === "build" ? !!config.build.sourcemap : true,
|
|
1151
|
+
cssDevSourcemap: config.css?.devSourcemap ?? false,
|
|
1152
|
+
devToolsEnabled: !config.isProduction
|
|
1153
|
+
};
|
|
1154
|
+
if (!config.resolve.alias.some(({ find }) => find === "vue")) {
|
|
1155
|
+
config.resolve.alias.push({
|
|
1156
|
+
find: "vue",
|
|
1157
|
+
replacement: "vue/dist/vue.runtime.esm.js"
|
|
1158
|
+
});
|
|
1159
|
+
}
|
|
1160
|
+
},
|
|
1161
|
+
configureServer(server) {
|
|
1162
|
+
options.devServer = server;
|
|
1163
|
+
},
|
|
1164
|
+
buildStart() {
|
|
1165
|
+
options.compiler = options.compiler || resolveCompiler(options.root);
|
|
1166
|
+
},
|
|
1167
|
+
async resolveId(id) {
|
|
1168
|
+
if (id === NORMALIZER_ID || id === HMR_RUNTIME_ID) {
|
|
1169
|
+
return id;
|
|
1170
|
+
}
|
|
1171
|
+
if (parseVueRequest(id).query.vue) {
|
|
1172
|
+
return id;
|
|
1173
|
+
}
|
|
1174
|
+
},
|
|
1175
|
+
load(id, opt) {
|
|
1176
|
+
const ssr = opt?.ssr === true;
|
|
1177
|
+
if (id === NORMALIZER_ID) {
|
|
1178
|
+
return normalizerCode;
|
|
1179
|
+
}
|
|
1180
|
+
if (id === HMR_RUNTIME_ID) {
|
|
1181
|
+
return hmrRuntimeCode;
|
|
1182
|
+
}
|
|
1183
|
+
const { filename, query } = parseVueRequest(id);
|
|
1184
|
+
if (query.vue) {
|
|
1185
|
+
if (query.src) {
|
|
1186
|
+
return fs.readFileSync(filename, "utf-8");
|
|
1187
|
+
}
|
|
1188
|
+
const descriptor = getDescriptor(filename, options);
|
|
1189
|
+
let block;
|
|
1190
|
+
if (query.type === "script") {
|
|
1191
|
+
block = getResolvedScript(descriptor, ssr);
|
|
1192
|
+
} else if (query.type === "template") {
|
|
1193
|
+
block = descriptor.template;
|
|
1194
|
+
} else if (query.type === "style") {
|
|
1195
|
+
block = descriptor.styles[query.index];
|
|
1196
|
+
} else if (query.index != null) {
|
|
1197
|
+
block = descriptor.customBlocks[query.index];
|
|
1198
|
+
}
|
|
1199
|
+
if (block) {
|
|
1200
|
+
return {
|
|
1201
|
+
code: block.content,
|
|
1202
|
+
map: block.map
|
|
1203
|
+
};
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
},
|
|
1207
|
+
async transform(code, id, opt) {
|
|
1208
|
+
const ssr = opt?.ssr === true;
|
|
1209
|
+
const { filename, query } = parseVueRequest(id);
|
|
1210
|
+
if (query.raw) {
|
|
1211
|
+
return;
|
|
1212
|
+
}
|
|
1213
|
+
if (!filter(filename) && !query.vue) {
|
|
1214
|
+
return;
|
|
1215
|
+
}
|
|
1216
|
+
if (!query.vue) {
|
|
1217
|
+
return transformMain(code, filename, options, this, ssr);
|
|
1218
|
+
} else {
|
|
1219
|
+
const descriptor = query.src ? getSrcDescriptor(filename, query) : getDescriptor(filename, options);
|
|
1220
|
+
if (query.type === "template") {
|
|
1221
|
+
return {
|
|
1222
|
+
code: await transformTemplateAsModule(
|
|
1223
|
+
code,
|
|
1224
|
+
descriptor,
|
|
1225
|
+
options,
|
|
1226
|
+
this,
|
|
1227
|
+
ssr
|
|
1228
|
+
),
|
|
1229
|
+
map: {
|
|
1230
|
+
mappings: ""
|
|
1231
|
+
}
|
|
1232
|
+
};
|
|
1233
|
+
} else if (query.type === "style") {
|
|
1234
|
+
return transformStyle(
|
|
1235
|
+
code,
|
|
1236
|
+
descriptor,
|
|
1237
|
+
Number(query.index),
|
|
1238
|
+
options,
|
|
1239
|
+
this,
|
|
1240
|
+
filename
|
|
1241
|
+
);
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
export { vuePlugin as default, parseVueRequest };
|