Skip to content

Commit c722f1c

Browse files
author
Alan Colon
committed
Use same code for HMR with shadowMode
1 parent 477c25e commit c722f1c

File tree

4 files changed

+41
-107
lines changed

4 files changed

+41
-107
lines changed

Diff for: index.js

+34-35
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ module.exports.pitch = function (remainingRequest) {
1515
var isProduction = this.minimize || process.env.NODE_ENV === 'production'
1616
var addStylesClientPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesClient.js'))
1717
var addStylesServerPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesServer.js'))
18-
var addStylesShadowPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesShadow.js'))
1918

2019
var request = loaderUtils.stringifyRequest(this, '!!' + remainingRequest)
2120
var relPath = path.relative(__dirname, this.resourcePath).replace(/\\/g, '/')
@@ -41,41 +40,41 @@ module.exports.pitch = function (remainingRequest) {
4140
'if(content.locals) module.exports = content.locals;'
4241
]
4342

44-
// shadowMode is enabled in vue-cli with vue build --target web-component.
45-
// exposes the same __inject__ method like SSR
46-
if (options.shadowMode) {
47-
return shared.concat([
48-
'// add CSS to Shadow Root',
49-
'var add = require(' + addStylesShadowPath + ').default',
50-
'module.exports.__inject__ = function (shadowRoot) {',
51-
' add(' + id + ', content, shadowRoot)',
52-
'};'
53-
]).join('\n')
54-
} else if (!isServer) {
55-
// on the client: dynamic inject + hot-reload
56-
var code = [
57-
'// add the styles to the DOM',
58-
'var add = require(' + addStylesClientPath + ').default',
59-
'var update = add(' + id + ', content, ' + isProduction + ', ' + JSON.stringify(options) + ');'
60-
]
61-
if (!isProduction) {
62-
code = code.concat([
63-
'// Hot Module Replacement',
64-
'if(module.hot) {',
65-
' // When the styles change, update the <style> tags',
66-
' if(!content.locals) {',
67-
' module.hot.accept(' + request + ', function() {',
68-
' var newContent = require(' + request + ');',
69-
" if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];",
70-
' update(newContent);',
71-
' });',
72-
' }',
73-
' // When the module is disposed, remove the <style> tags',
74-
' module.hot.dispose(function() { update(); });',
75-
'}'
76-
])
43+
if (!isServer) {
44+
var code = function(id, isProduction, options) {
45+
// style-loader: Adds some css to the DOM by adding a <style> tag
46+
var content = require(REQUEST)
47+
var addStylesClient = require(ADD_STYLES_CLIENT_PATH).default
48+
var install = function(target) {
49+
update = addStylesClient(id, content, isProduction, options, target)
50+
}
51+
var update = null
52+
if (typeof content === 'string') content = [[module.id, content, '']]
53+
if (content.locals) {
54+
module.exports = content.locals
55+
return
56+
} else if (module.hot) {
57+
module.hot.accept(REQUEST, function() {
58+
var newContent = require(REQUEST)
59+
if (typeof newContent === 'string') newContent = [[module.id, newContent, '']]
60+
update(newContent)
61+
})
62+
module.hot.dispose(function() {
63+
update();
64+
})
65+
}
66+
if (options.shadowMode) {
67+
module.exports.__inject__ = function(shadowRoot) {
68+
install(shadowRoot)
69+
}
70+
} else {
71+
install(typeof document !== 'undefined' && document.head)
72+
}
7773
}
78-
return shared.concat(code).join('\n')
74+
return `(${code})(${id},${isProduction},${JSON.stringify(options)})`
75+
.split('REQUEST').join(request)
76+
.split('ADD_STYLES_CLIENT_PATH').join(addStylesClientPath)
77+
7978
} else {
8079
// on the server: attach to Vue SSR context
8180
if (isVue) {

Diff for: lib/addStylesClient.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ var ssrIdKey = 'data-vue-ssr-id'
4949
// tags it will allow on a page
5050
var isOldIE = typeof navigator !== 'undefined' && /msie [6-9]\b/.test(navigator.userAgent.toLowerCase())
5151

52-
export default function addStylesClient (parentId, list, _isProduction, _options) {
52+
export default function addStylesClient (parentId, list, _isProduction, _options, _styleParent) {
5353
isProduction = _isProduction
5454

55+
if (_styleParent) head = _styleParent
56+
5557
options = _options || {}
5658

5759
var styles = listToStyles(parentId, list)

Diff for: lib/addStylesShadow.js

-70
This file was deleted.

Diff for: package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-style-loader",
3-
"version": "4.1.2",
3+
"version": "4.1.4",
44
"author": "Evan You",
55
"description": "Vue.js style loader module for webpack",
66
"repository": {
@@ -22,5 +22,8 @@
2222
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
2323
"conventional-changelog-cli": "^2.0.1",
2424
"jest": "^22.1.4"
25+
},
26+
"jest": {
27+
"testURL": "http://localhost/"
2528
}
2629
}

0 commit comments

Comments
 (0)