Skip to content

Commit 1123b12

Browse files
committed
feat: cacheBusting opiton (close #987)
1 parent 94d374b commit 1123b12

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

lib/loader.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module.exports = function (content) {
8181
(this._compiler && this._compiler.context) ||
8282
this.options.context ||
8383
process.cwd()
84+
const sourceRoot = path.dirname(path.relative(context, filePath))
8485
const moduleId = 'data-v-' + genId(filePath, context, options.hashKey)
8586
const shortFilePath = path
8687
.relative(context, filePath)
@@ -98,7 +99,8 @@ module.exports = function (content) {
9899
hasBuble && options.buble ? '?' + JSON.stringify(options.buble) : ''
99100

100101
let output = ''
101-
const parts = parse(content, fileName, this.sourceMap)
102+
const bustCache = !isProduction && options.cacheBusting !== false
103+
const parts = parse(content, fileName, this.sourceMap, sourceRoot, bustCache)
102104
const hasScoped = parts.styles.some(({ scoped }) => scoped)
103105
const templateAttrs =
104106
parts.template && parts.template.attrs && parts.template.attrs
@@ -652,8 +654,8 @@ module.exports = function (content) {
652654
(type === 'script' || type === 'template' || type === 'styles'
653655
? type
654656
: 'customBlocks') +
655-
'&index=' +
656-
index +
657+
'&index=' + index +
658+
(bustCache ? '&bustCache' : '') +
657659
'!'
658660
)
659661
}

lib/parser.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ const SourceMapGenerator = require('source-map').SourceMapGenerator
66
const splitRE = /\r?\n/g
77
const emptyRE = /^(?:\/\/)?\s*$/
88

9-
module.exports = (content, filename, needMap, sourceRoot) => {
9+
module.exports = (content, filename, needMap, sourceRoot, bustCache) => {
1010
const cacheKey = hash(filename + content)
1111
// source-map cache busting for hot-reloadded modules
12-
const filenameWithHash = filename + '?' + cacheKey
12+
const filenameWithHash = bustCache
13+
? filename + '?' + cacheKey
14+
: filename
1315
let output = cache.get(cacheKey)
1416
if (output) return output
1517
output = compiler.parseComponent(content, { pad: 'line' })

lib/selector.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (content) {
1313
let filename = path.basename(this.resourcePath)
1414
filename = filename.substring(0, filename.lastIndexOf(path.extname(filename))) + '.vue'
1515
const sourceRoot = path.dirname(path.relative(context, this.resourcePath))
16-
const parts = parse(content, filename, this.sourceMap, sourceRoot)
16+
const parts = parse(content, filename, this.sourceMap, sourceRoot, query.bustCache)
1717
let part = parts[query.type]
1818
if (Array.isArray(part)) {
1919
part = part[query.index]

test/test.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,14 @@ describe('vue-loader', function () {
435435
expect(includeDataURL(vnode.children[2].children[0].data.attrs['xlink:href'])).to.equal(true)
436436
var style = window.document.querySelector('style').textContent
437437

438-
let dataURL = vnode.children[0].data.attrs.src
438+
const dataURL = vnode.children[0].data.attrs.src
439439

440440
// image tag with srcset
441-
expect(vnode.children[4].data.attrs.srcset).to.equal(dataURL + " 2x")
441+
expect(vnode.children[4].data.attrs.srcset).to.equal(dataURL + ' 2x')
442442
// image tag with srcset with two candidates
443-
expect(vnode.children[6].data.attrs.srcset).to.equal(dataURL + " 2x, " + dataURL + " 3x")
443+
expect(vnode.children[6].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL + ' 3x')
444444
// image tag with multiline srcset
445-
expect(vnode.children[8].data.attrs.srcset).to.equal(dataURL + " 2x, " + dataURL + " 3x")
445+
expect(vnode.children[8].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL + ' 3x')
446446

447447
// style
448448
expect(includeDataURL(style)).to.equal(true)
@@ -960,6 +960,30 @@ describe('vue-loader', function () {
960960
})
961961
})
962962

963+
it('cacheBusting: false', done => {
964+
test({
965+
entry: './test/fixtures/basic.vue',
966+
vue: {
967+
cacheBusting: false
968+
}
969+
}, (window, module, rawModule) => {
970+
var vnode = mockRender(module, {
971+
msg: 'hi'
972+
})
973+
974+
// <h2 class="red">{{msg}}</h2>
975+
expect(vnode.tag).to.equal('h2')
976+
expect(vnode.data.staticClass).to.equal('red')
977+
expect(vnode.children[0].text).to.equal('hi')
978+
979+
expect(module.data().msg).to.contain('Hello from Component A!')
980+
var style = window.document.querySelector('style').textContent
981+
style = normalizeNewline(style)
982+
expect(style).to.contain('comp-a h2 {\n color: #f00;\n}')
983+
done()
984+
})
985+
})
986+
963987
// Vue required tests for more complete test cases
964988
// this test case requires Vue >= 2.5
965989
if (Number(Vue.version.split('.')[1]) >= 5) {

0 commit comments

Comments
 (0)