Skip to content

Commit ee26c3a

Browse files
committed
feat: properly map template position
1 parent 81c2b4c commit ee26c3a

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"chalk": "^3.0.0",
2222
"hash-sum": "^2.0.0",
2323
"loader-utils": "^1.2.3",
24+
"merge-source-map": "^1.1.0",
2425
"source-map": "^0.6.1"
2526
},
2627
"devDependencies": {

src/index.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,9 @@ const loader: webpack.loader.Loader = function(source) {
7070
const isProduction =
7171
options.productionMode || minimize || process.env.NODE_ENV === 'production'
7272

73-
const filename = path.basename(resourcePath)
74-
const context = rootContext || process.cwd()
75-
const sourceRoot = path.dirname(path.relative(context, resourcePath))
76-
7773
const descriptor = parse(String(source), {
78-
filename,
79-
sourceMap,
80-
sourceRoot
74+
filename: resourcePath,
75+
sourceMap
8176
})
8277

8378
// if the query has a type field, this is a language block request
@@ -94,7 +89,7 @@ const loader: webpack.loader.Loader = function(source) {
9489

9590
// module id for scoped CSS & hot-reload
9691
const rawShortFilePath = path
97-
.relative(context, resourcePath)
92+
.relative(rootContext || process.cwd(), resourcePath)
9893
.replace(/^(\.\.[\/\\])+/, '')
9994
const shortFilePath = rawShortFilePath.replace(/\\/g, '/') + resourceQuery
10095
const id = hash(isProduction ? shortFilePath + '\n' + source : shortFilePath)
@@ -180,7 +175,7 @@ const loader: webpack.loader.Loader = function(source) {
180175
} else if (options.exposeFilename) {
181176
// Libraies can opt-in to expose their components' filenames in production builds.
182177
// For security reasons, only expose the file's basename in production.
183-
code += `\nscript.__file = ${JSON.stringify(filename)}`
178+
code += `\nscript.__file = ${JSON.stringify(path.basename(resourcePath))}`
184179
}
185180

186181
// finalize

src/shim.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'merge-source-map' {
2+
export default function merge(inMap: any, outMap: any): any
3+
}

src/templateLoader.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import loaderUtils from 'loader-utils'
55
import { VueLoaderOptions } from './'
66
import { SourceMapConsumer, RawSourceMap } from 'source-map'
77
import { compileTemplate, generateCodeFrame } from '@vue/compiler-sfc'
8+
import mergeSourceMap from 'merge-source-map'
89

910
// Loader that compiles raw template into JavaScript functions.
1011
// This is injected by the global pitcher (../pitch) for template
@@ -21,12 +22,13 @@ const TemplateLoader: webpack.loader.Loader = function(source, inMap) {
2122

2223
// const isServer = loaderContext.target === 'node'
2324
// const isProduction = options.productionMode || loaderContext.minimize || process.env.NODE_ENV === 'production'
24-
const query = qs.parse(this.resourceQuery.slice(1))
25+
const query = qs.parse(loaderContext.resourceQuery.slice(1))
2526
const scopeId = query.scoped ? `data-v-${query.id}` : null
2627

2728
const compiled = compileTemplate({
2829
source,
29-
filename: this.resourcePath,
30+
// avoid source content overwriting the original
31+
filename: loaderContext.resourcePath,
3032
compiler: options.compiler,
3133
compilerOptions: {
3234
...options.compilerOptions,
@@ -69,7 +71,12 @@ const TemplateLoader: webpack.loader.Loader = function(source, inMap) {
6971
})
7072
}
7173

72-
const { code, map } = compiled
74+
let { code, map } = compiled
75+
if (map && inMap) {
76+
// avoid overwritting original *.vue source during merge
77+
map.sourcesContent = []
78+
map = mergeSourceMap(inMap, map)
79+
}
7380
loaderContext.callback(null, code, map)
7481
}
7582

yarn.lock

+7
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,13 @@ [email protected]:
23382338
resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61"
23392339
integrity sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=
23402340

2341+
merge-source-map@^1.1.0:
2342+
version "1.1.0"
2343+
resolved "https://registry.yarnpkg.com/merge-source-map/-/merge-source-map-1.1.0.tgz#2fdde7e6020939f70906a68f2d7ae685e4c8c646"
2344+
integrity sha512-Qkcp7P2ygktpMPh2mCQZaf3jhN6D3Z/qVZHSdWvQ+2Ef5HgRAPBO57A77+ENm0CPx2+1Ce/MYKi3ymqdfuqibw==
2345+
dependencies:
2346+
source-map "^0.6.1"
2347+
23412348
methods@~1.1.2:
23422349
version "1.1.2"
23432350
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"

0 commit comments

Comments
 (0)