Skip to content

Commit

Permalink
Use same code for HMR with shadowMode
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Colon committed Aug 16, 2019
1 parent 477c25e commit c722f1c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 107 deletions.
69 changes: 34 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module.exports.pitch = function (remainingRequest) {
var isProduction = this.minimize || process.env.NODE_ENV === 'production'
var addStylesClientPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesClient.js'))
var addStylesServerPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesServer.js'))
var addStylesShadowPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesShadow.js'))

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

// shadowMode is enabled in vue-cli with vue build --target web-component.
// exposes the same __inject__ method like SSR
if (options.shadowMode) {
return shared.concat([
'// add CSS to Shadow Root',
'var add = require(' + addStylesShadowPath + ').default',
'module.exports.__inject__ = function (shadowRoot) {',
' add(' + id + ', content, shadowRoot)',
'};'
]).join('\n')
} else if (!isServer) {
// on the client: dynamic inject + hot-reload
var code = [
'// add the styles to the DOM',
'var add = require(' + addStylesClientPath + ').default',
'var update = add(' + id + ', content, ' + isProduction + ', ' + JSON.stringify(options) + ');'
]
if (!isProduction) {
code = code.concat([
'// Hot Module Replacement',
'if(module.hot) {',
' // When the styles change, update the <style> tags',
' if(!content.locals) {',
' module.hot.accept(' + request + ', function() {',
' var newContent = require(' + request + ');',
" if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];",
' update(newContent);',
' });',
' }',
' // When the module is disposed, remove the <style> tags',
' module.hot.dispose(function() { update(); });',
'}'
])
if (!isServer) {
var code = function(id, isProduction, options) {
// style-loader: Adds some css to the DOM by adding a <style> tag
var content = require(REQUEST)
var addStylesClient = require(ADD_STYLES_CLIENT_PATH).default
var install = function(target) {
update = addStylesClient(id, content, isProduction, options, target)
}
var update = null
if (typeof content === 'string') content = [[module.id, content, '']]
if (content.locals) {
module.exports = content.locals
return
} else if (module.hot) {
module.hot.accept(REQUEST, function() {
var newContent = require(REQUEST)
if (typeof newContent === 'string') newContent = [[module.id, newContent, '']]
update(newContent)
})
module.hot.dispose(function() {
update();
})
}
if (options.shadowMode) {
module.exports.__inject__ = function(shadowRoot) {
install(shadowRoot)
}
} else {
install(typeof document !== 'undefined' && document.head)
}
}
return shared.concat(code).join('\n')
return `(${code})(${id},${isProduction},${JSON.stringify(options)})`
.split('REQUEST').join(request)
.split('ADD_STYLES_CLIENT_PATH').join(addStylesClientPath)

} else {
// on the server: attach to Vue SSR context
if (isVue) {
Expand Down
4 changes: 3 additions & 1 deletion lib/addStylesClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ var ssrIdKey = 'data-vue-ssr-id'
// tags it will allow on a page
var isOldIE = typeof navigator !== 'undefined' && /msie [6-9]\b/.test(navigator.userAgent.toLowerCase())

export default function addStylesClient (parentId, list, _isProduction, _options) {
export default function addStylesClient (parentId, list, _isProduction, _options, _styleParent) {
isProduction = _isProduction

if (_styleParent) head = _styleParent

options = _options || {}

var styles = listToStyles(parentId, list)
Expand Down
70 changes: 0 additions & 70 deletions lib/addStylesShadow.js

This file was deleted.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-style-loader",
"version": "4.1.2",
"version": "4.1.4",
"author": "Evan You",
"description": "Vue.js style loader module for webpack",
"repository": {
Expand All @@ -22,5 +22,8 @@
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"conventional-changelog-cli": "^2.0.1",
"jest": "^22.1.4"
},
"jest": {
"testURL": "http://localhost/"
}
}

0 comments on commit c722f1c

Please sign in to comment.