From 72ff0bb00f3db3cbeb764d61cf219194d7509579 Mon Sep 17 00:00:00 2001 From: Rahul Kadyan Date: Sat, 13 Jan 2018 18:28:59 +0000 Subject: [PATCH] fix: update gen-id implementation (#43) * fix: update gen-id implementation fixes #42 * fix: add data-v prefix to generated scope id --- src/gen-id.js | 10 ++-------- test/setup/utils.js | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/gen-id.js b/src/gen-id.js index 2c78a7f..3ad847c 100644 --- a/src/gen-id.js +++ b/src/gen-id.js @@ -2,13 +2,7 @@ // used in scoped CSS rewriting const path = require('path') const hash = require('hash-sum') -const cache = Object.create(null) -const sepRE = new RegExp(path.sep.replace('\\', '\\\\'), 'g') -module.exports = function genId (file, context, key) { - const contextPath = context.split(path.sep) - const rootId = contextPath[contextPath.length - 1] - file = rootId + '/' + path.relative(context, file).replace(sepRE, '/') + (key || '') - - return cache[file] || (cache[file] = 'data-v-' + hash(file)) +module.exports = function genId (filename, content, isProduction = true) { + return 'data-v' + (isProduction ? hash(path.basename(filename) + '\n' + content) : hash(filename)) } diff --git a/test/setup/utils.js b/test/setup/utils.js index c8d8e7a..bb971b0 100644 --- a/test/setup/utils.js +++ b/test/setup/utils.js @@ -33,7 +33,7 @@ function load (ext, handle) { function compile (filename, source) { source = source || readFileSync(filename).toString() const descriptor = compiler.parse(source, filename, { needMap: true }) - const scopeId = compiler.generateScopeId(filename, join(__dirname, 'fixtures')) + const scopeId = compiler.generateScopeId(filename, source) const render = descriptor.template ? compiler.compileTemplate( { code: descriptor.template.content, descriptor: descriptor.template }, filename, { scopeId } ) : null