UNPKG

3.92 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4 Modified by Evan You @yyx990803
5*/
6var loaderUtils = require('loader-utils')
7var path = require('path')
8var hash = require('hash-sum')
9var qs = require('querystring')
10
11module.exports = function () {}
12
13module.exports.pitch = function (remainingRequest) {
14 var isServer = this.target === 'node'
15 var isProduction = this.minimize || process.env.NODE_ENV === 'production'
16 var addStylesClientPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesClient.js'))
17 var addStylesServerPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesServer.js'))
18 var addStylesShadowPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesShadow.js'))
19
20 var request = loaderUtils.stringifyRequest(this, '!!' + remainingRequest)
21 var relPath = path.relative(__dirname, this.resourcePath).replace(/\\/g, '/')
22 var id = JSON.stringify(hash(request + relPath))
23 var options = loaderUtils.getOptions(this) || {}
24
25 // direct css import from js --> direct, or manually call `styles.__inject__(ssrContext)` with `manualInject` option
26 // css import from vue file --> component lifecycle linked
27 // style embedded in vue file --> component lifecycle linked
28 var isVue = (
29 /"vue":true/.test(remainingRequest) ||
30 options.manualInject ||
31 qs.parse(this.resourceQuery.slice(1)).vue != null
32 )
33
34 var shared = [
35 '// style-loader: Adds some css to the DOM by adding a <style> tag',
36 '',
37 '// load the styles',
38 'var content = require(' + request + ');',
39 // content list format is [id, css, media, sourceMap]
40 "if(typeof content === 'string') content = [[module.id, content, '']];",
41 'if(content.locals) module.exports = content.locals;'
42 ]
43
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 ])
77 }
78 return shared.concat(code).join('\n')
79 } else {
80 // on the server: attach to Vue SSR context
81 if (isVue) {
82 // inside *.vue file: expose a function so it can be called in
83 // component's lifecycle hooks
84 return shared.concat([
85 '// add CSS to SSR context',
86 'var add = require(' + addStylesServerPath + ').default',
87 'module.exports.__inject__ = function (context) {',
88 ' add(' + id + ', content, ' + isProduction + ', context)',
89 '};'
90 ]).join('\n')
91 } else {
92 // normal import
93 return shared.concat([
94 'require(' + addStylesServerPath + ').default(' + id + ', content, ' + isProduction + ')'
95 ]).join('\n')
96 }
97 }
98}