forked from vuejs/vue-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplateLoader.ts
65 lines (57 loc) · 2 KB
/
templateLoader.ts
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
import webpack from 'webpack'
import qs from 'querystring'
import loaderUtils from 'loader-utils'
import { VueLoaderOptions } from './'
import { formatError } from './formatError'
import { compileTemplate } from '@vue/compiler-sfc'
// Loader that compiles raw template into JavaScript functions.
// This is injected by the global pitcher (../pitch) for template
// selection requests initiated from vue files.
const TemplateLoader: webpack.loader.Loader = function(source, inMap) {
source = String(source)
const loaderContext = this
// although this is not the main vue-loader, we can get access to the same
// vue-loader options because we've set an ident in the plugin and used that
// ident to create the request for this loader in the pitcher.
const options = (loaderUtils.getOptions(loaderContext) ||
{}) as VueLoaderOptions
// const isServer = loaderContext.target === 'node'
// const isProduction = options.productionMode || loaderContext.minimize || process.env.NODE_ENV === 'production'
const query = qs.parse(loaderContext.resourceQuery.slice(1))
const scopeId = query.scoped ? `data-v-${query.id}` : null
const compiled = compileTemplate({
source,
inMap,
filename: loaderContext.resourcePath,
compiler: options.compiler,
compilerOptions: {
...options.compilerOptions,
scopeId
},
transformAssetUrls: options.transformAssetUrls || true
})
// tips
if (compiled.tips.length) {
compiled.tips.forEach(tip => {
loaderContext.emitWarning(tip)
})
}
// errors
if (compiled.errors && compiled.errors.length) {
compiled.errors.forEach(err => {
if (typeof err === 'string') {
loaderContext.emitError(err)
} else {
formatError(
err,
inMap ? inMap.sourcesContent![0] : (source as string),
loaderContext.resourcePath
)
loaderContext.emitError(err)
}
})
}
const { code, map } = compiled
loaderContext.callback(null, code, map)
}
export default TemplateLoader