loader.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. const path = require('path')
  2. const hash = require('hash-sum')
  3. const parse = require('./parser')
  4. const querystring = require('querystring')
  5. const loaderUtils = require('loader-utils')
  6. const normalize = require('./utils/normalize')
  7. const tryRequire = require('./utils/try-require')
  8. // internal lib loaders
  9. const selectorPath = normalize.lib('selector')
  10. const styleCompilerPath = normalize.lib('style-compiler/index')
  11. const templateCompilerPath = normalize.lib('template-compiler/index')
  12. const templatePreprocessorPath = normalize.lib('template-compiler/preprocessor')
  13. const componentNormalizerPath = normalize.lib('component-normalizer')
  14. // dep loaders
  15. const styleLoaderPath = normalize.dep('vue-style-loader')
  16. const hotReloadAPIPath = normalize.dep('vue-hot-reload-api')
  17. // check whether default js loader exists
  18. const hasBabel = !!tryRequire('babel-loader')
  19. const hasBuble = !!tryRequire('buble-loader')
  20. const rewriterInjectRE = /\b(css(?:-loader)?(?:\?[^!]+)?)(?:!|$)/
  21. const defaultLang = {
  22. template: 'html',
  23. styles: 'css',
  24. script: 'js'
  25. }
  26. const postcssExtensions = [
  27. 'postcss', 'pcss', 'sugarss', 'sss'
  28. ]
  29. // When extracting parts from the source vue file, we want to apply the
  30. // loaders chained before vue-loader, but exclude some loaders that simply
  31. // produces side effects such as linting.
  32. function getRawRequest (
  33. { resource, loaderIndex, loaders },
  34. excludedPreLoaders = /eslint-loader/
  35. ) {
  36. return loaderUtils.getRemainingRequest({
  37. resource: resource,
  38. loaderIndex: loaderIndex,
  39. loaders: loaders.filter(loader => !excludedPreLoaders.test(loader.path))
  40. })
  41. }
  42. module.exports = function (content) {
  43. this.cacheable()
  44. const isServer = this.target === 'node'
  45. const isProduction = this.minimize || process.env.NODE_ENV === 'production'
  46. const loaderContext = this
  47. const query = loaderUtils.getOptions(this) || {}
  48. const options = Object.assign(
  49. {
  50. esModule: true
  51. },
  52. this.options.vue,
  53. this.vue,
  54. query
  55. )
  56. // disable esModule in inject mode
  57. // because import/export must be top-level
  58. if (query.inject) {
  59. options.esModule = false
  60. }
  61. // #824 avoid multiple webpack runs complaining about unknown option
  62. Object.defineProperty(this.options, '__vueOptions__', {
  63. value: options,
  64. enumerable: false,
  65. configurable: true
  66. })
  67. const rawRequest = getRawRequest(this, options.excludedPreLoaders)
  68. const filePath = this.resourcePath
  69. const fileName = path.basename(filePath)
  70. const context =
  71. (this._compiler && this._compiler.context) ||
  72. this.options.context ||
  73. process.cwd()
  74. const sourceRoot = path.dirname(path.relative(context, filePath))
  75. const shortFilePath = path.relative(context, filePath).replace(/^(\.\.[\\\/])+/, '').replace(/\\/g, '/')
  76. const moduleId = 'data-v-' + hash(isProduction ? (shortFilePath + '\n' + content) : shortFilePath)
  77. let cssLoaderOptions = ''
  78. const cssSourceMap =
  79. !isProduction &&
  80. this.sourceMap &&
  81. options.cssSourceMap !== false
  82. if (cssSourceMap) {
  83. cssLoaderOptions += '?sourceMap'
  84. }
  85. if (isProduction) {
  86. cssLoaderOptions += (cssLoaderOptions ? '&' : '?') + 'minimize'
  87. }
  88. const bubleOptions =
  89. hasBuble && options.buble ? '?' + JSON.stringify(options.buble) : ''
  90. let output = ''
  91. const parts = parse(
  92. content,
  93. fileName,
  94. this.sourceMap,
  95. sourceRoot,
  96. cssSourceMap
  97. )
  98. const hasScoped = parts.styles.some(({ scoped }) => scoped)
  99. const templateAttrs =
  100. parts.template && parts.template.attrs && parts.template.attrs
  101. const hasComment = templateAttrs && templateAttrs.comments
  102. const functionalTemplate = templateAttrs && templateAttrs.functional
  103. const bubleTemplateOptions = Object.assign({}, options.buble)
  104. bubleTemplateOptions.transforms = Object.assign(
  105. {},
  106. bubleTemplateOptions.transforms
  107. )
  108. bubleTemplateOptions.transforms.stripWithFunctional = functionalTemplate
  109. const templateCompilerOptions =
  110. '?' +
  111. JSON.stringify({
  112. id: moduleId,
  113. hasScoped,
  114. hasComment,
  115. transformToRequire: options.transformToRequire,
  116. preserveWhitespace: options.preserveWhitespace,
  117. buble: bubleTemplateOptions,
  118. // only pass compilerModules if it's a path string
  119. compilerModules:
  120. typeof options.compilerModules === 'string'
  121. ? options.compilerModules
  122. : undefined
  123. })
  124. const defaultLoaders = {
  125. html: templateCompilerPath + templateCompilerOptions,
  126. css: options.extractCSS
  127. ? getCSSExtractLoader()
  128. : styleLoaderPath + '!' + 'css-loader' + cssLoaderOptions,
  129. js: hasBuble
  130. ? 'buble-loader' + bubleOptions
  131. : hasBabel ? 'babel-loader' : ''
  132. }
  133. // check if there are custom loaders specified via
  134. // webpack config, otherwise use defaults
  135. const loaders = Object.assign({}, defaultLoaders, options.loaders)
  136. const preLoaders = options.preLoaders || {}
  137. const postLoaders = options.postLoaders || {}
  138. const needsHotReload =
  139. !isServer && !isProduction && (parts.script || parts.template) && options.hotReload !== false
  140. if (needsHotReload) {
  141. output += 'var disposed = false\n'
  142. }
  143. // add requires for styles
  144. let cssModules
  145. if (parts.styles.length) {
  146. let styleInjectionCode = 'function injectStyle (ssrContext) {\n'
  147. if (needsHotReload) {
  148. styleInjectionCode += ` if (disposed) return\n`
  149. }
  150. if (isServer) {
  151. styleInjectionCode += `var i\n`
  152. }
  153. parts.styles.forEach((style, i) => {
  154. // require style
  155. let requireString = style.src
  156. ? getRequireForImport('styles', style, style.scoped)
  157. : getRequire('styles', style, i, style.scoped)
  158. const hasStyleLoader = requireString.indexOf('style-loader') > -1
  159. const hasVueStyleLoader = requireString.indexOf('vue-style-loader') > -1
  160. // vue-style-loader exposes inject functions during SSR so they are
  161. // always called
  162. const invokeStyle =
  163. isServer && hasVueStyleLoader
  164. ? code => `;(i=${code},i.__inject__&&i.__inject__(ssrContext),i)\n`
  165. : code => ` ${code}\n`
  166. const moduleName = style.module === true ? '$style' : style.module
  167. // setCssModule
  168. if (moduleName) {
  169. if (!cssModules) {
  170. cssModules = {}
  171. if (needsHotReload) {
  172. output += `var cssModules = {}\n`
  173. }
  174. }
  175. if (moduleName in cssModules) {
  176. loaderContext.emitError(
  177. 'CSS module name "' + moduleName + '" is not unique!'
  178. )
  179. styleInjectionCode += invokeStyle(requireString)
  180. } else {
  181. cssModules[moduleName] = true
  182. // `(vue-)style-loader` exposes the name-to-hash map directly
  183. // `css-loader` exposes it in `.locals`
  184. // add `.locals` if the user configured to not use style-loader.
  185. if (!hasStyleLoader) {
  186. requireString += '.locals'
  187. }
  188. if (!needsHotReload) {
  189. styleInjectionCode += invokeStyle(
  190. 'this["' + moduleName + '"] = ' + requireString
  191. )
  192. } else {
  193. // handle hot reload for CSS modules.
  194. // we store the exported locals in an object and proxy to it by
  195. // defining getters inside component instances' lifecycle hook.
  196. styleInjectionCode +=
  197. invokeStyle(`cssModules["${moduleName}"] = ${requireString}`) +
  198. `Object.defineProperty(this, "${moduleName}", { get: function () { return cssModules["${moduleName}"] }})\n`
  199. const requirePath = style.src
  200. ? getRequireForImportString('styles', style, style.scoped)
  201. : getRequireString('styles', style, i, style.scoped)
  202. output +=
  203. `module.hot && module.hot.accept([${requirePath}], function () {\n` +
  204. // 1. check if style has been injected
  205. ` var oldLocals = cssModules["${moduleName}"]\n` +
  206. ` if (!oldLocals) return\n` +
  207. // 2. re-import (side effect: updates the <style>)
  208. ` var newLocals = ${requireString}\n` +
  209. // 3. compare new and old locals to see if selectors changed
  210. ` if (JSON.stringify(newLocals) === JSON.stringify(oldLocals)) return\n` +
  211. // 4. locals changed. Update and force re-render.
  212. ` cssModules["${moduleName}"] = newLocals\n` +
  213. ` require("${hotReloadAPIPath}").rerender("${moduleId}")\n` +
  214. `})\n`
  215. }
  216. }
  217. } else {
  218. styleInjectionCode += invokeStyle(requireString)
  219. }
  220. })
  221. styleInjectionCode += '}\n'
  222. output += styleInjectionCode
  223. }
  224. // we require the component normalizer function, and call it like so:
  225. // normalizeComponent(
  226. // scriptExports,
  227. // compiledTemplate,
  228. // functionalTemplate,
  229. // injectStyles,
  230. // scopeId,
  231. // moduleIdentifier (server only)
  232. // )
  233. output +=
  234. 'var normalizeComponent = require(' +
  235. loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerPath) +
  236. ')\n'
  237. // <script>
  238. output += '/* script */\n'
  239. const script = parts.script
  240. if (script) {
  241. if (options.esModule) {
  242. output += script.src
  243. ? (
  244. getNamedExportForImport('script', script) + '\n' +
  245. getImportForImport('script', script)
  246. )
  247. : (
  248. getNamedExport('script', script) + '\n' +
  249. getImport('script', script)
  250. ) + '\n'
  251. } else {
  252. output +=
  253. 'var __vue_script__ = ' +
  254. (script.src
  255. ? getRequireForImport('script', script)
  256. : getRequire('script', script)) +
  257. '\n'
  258. }
  259. // inject loader interop
  260. if (query.inject) {
  261. output += '__vue_script__ = __vue_script__(injections)\n'
  262. }
  263. } else {
  264. output += 'var __vue_script__ = null\n'
  265. }
  266. // <template>
  267. output += '/* template */\n'
  268. const template = parts.template
  269. if (template) {
  270. if (options.esModule) {
  271. output +=
  272. (template.src
  273. ? getImportForImport('template', template)
  274. : getImport('template', template)) + '\n'
  275. } else {
  276. output +=
  277. 'var __vue_template__ = ' +
  278. (template.src
  279. ? getRequireForImport('template', template)
  280. : getRequire('template', template)) +
  281. '\n'
  282. }
  283. } else {
  284. output += 'var __vue_template__ = null\n'
  285. }
  286. // template functional
  287. output += '/* template functional */\n'
  288. output +=
  289. 'var __vue_template_functional__ = ' +
  290. (functionalTemplate ? 'true' : 'false') +
  291. '\n'
  292. // style
  293. output += '/* styles */\n'
  294. output +=
  295. 'var __vue_styles__ = ' +
  296. (parts.styles.length ? 'injectStyle' : 'null') +
  297. '\n'
  298. // scopeId
  299. output += '/* scopeId */\n'
  300. output +=
  301. 'var __vue_scopeId__ = ' +
  302. (hasScoped ? JSON.stringify(moduleId) : 'null') +
  303. '\n'
  304. // moduleIdentifier (server only)
  305. output += '/* moduleIdentifier (server only) */\n'
  306. output +=
  307. 'var __vue_module_identifier__ = ' +
  308. (isServer ? JSON.stringify(hash(this.request)) : 'null') +
  309. '\n'
  310. // close normalizeComponent call
  311. output +=
  312. 'var Component = normalizeComponent(\n' +
  313. ' __vue_script__,\n' +
  314. ' __vue_template__,\n' +
  315. ' __vue_template_functional__,\n' +
  316. ' __vue_styles__,\n' +
  317. ' __vue_scopeId__,\n' +
  318. ' __vue_module_identifier__\n' +
  319. ')\n'
  320. // development-only code
  321. if (!isProduction) {
  322. // add filename in dev
  323. output +=
  324. 'Component.options.__file = ' + JSON.stringify(shortFilePath) + '\n'
  325. }
  326. // add requires for customBlocks
  327. if (parts.customBlocks && parts.customBlocks.length) {
  328. let addedPrefix = false
  329. parts.customBlocks.forEach((customBlock, i) => {
  330. if (loaders[customBlock.type]) {
  331. // require customBlock
  332. customBlock.src = customBlock.attrs.src
  333. const requireString = customBlock.src
  334. ? getRequireForImport(customBlock.type, customBlock)
  335. : getRequire(customBlock.type, customBlock, i)
  336. if (!addedPrefix) {
  337. output += '\n/* customBlocks */\n'
  338. addedPrefix = true
  339. }
  340. output +=
  341. 'var customBlock = ' + requireString + '\n' +
  342. 'if (customBlock && customBlock.__esModule) {\n' +
  343. ' customBlock = customBlock.default\n' +
  344. '}\n' +
  345. 'if (typeof customBlock === "function") {\n' +
  346. ' customBlock(Component)\n' +
  347. '}\n'
  348. }
  349. })
  350. output += '\n'
  351. }
  352. if (!query.inject) {
  353. // hot reload
  354. if (needsHotReload) {
  355. output +=
  356. '\n/* hot reload */\n' +
  357. 'if (module.hot) {(function () {\n' +
  358. ' var hotAPI = require("' + hotReloadAPIPath + '")\n' +
  359. ' hotAPI.install(require("vue"), false)\n' +
  360. ' if (!hotAPI.compatible) return\n' +
  361. ' module.hot.accept()\n' +
  362. ' if (!module.hot.data) {\n' +
  363. // initial insert
  364. ' hotAPI.createRecord("' + moduleId + '", Component.options)\n' +
  365. ' } else {\n'
  366. // update
  367. if (cssModules) {
  368. output +=
  369. ' if (module.hot.data.cssModules && Object.keys(module.hot.data.cssModules) !== Object.keys(cssModules)) {\n' +
  370. ' delete Component.options._Ctor\n' +
  371. ' }\n'
  372. }
  373. output +=
  374. ` hotAPI.${
  375. functionalTemplate ? 'rerender' : 'reload'
  376. }("${moduleId}", Component.options)\n }\n`
  377. // dispose
  378. output +=
  379. ' module.hot.dispose(function (data) {\n' +
  380. (cssModules ? ' data.cssModules = cssModules\n' : '') +
  381. ' disposed = true\n' +
  382. ' })\n'
  383. output += '})()}\n'
  384. }
  385. // final export
  386. if (options.esModule) {
  387. output += '\nexport default Component.exports\n'
  388. } else {
  389. output += '\nmodule.exports = Component.exports\n'
  390. }
  391. } else {
  392. // inject-loader support
  393. output =
  394. '\n/* dependency injection */\n' +
  395. 'module.exports = function (injections) {\n' +
  396. output +
  397. '\n' +
  398. '\nreturn Component.exports\n}'
  399. }
  400. // done
  401. return output
  402. // --- helpers ---
  403. function getRequire (type, part, index, scoped) {
  404. return 'require(' + getRequireString(type, part, index, scoped) + ')'
  405. }
  406. function getImport (type, part, index, scoped) {
  407. return (
  408. 'import __vue_' + type + '__ from ' +
  409. getRequireString(type, part, index, scoped)
  410. )
  411. }
  412. function getNamedExport (type, part, index, scoped) {
  413. return (
  414. 'export * from ' +
  415. getRequireString(type, part, index, scoped)
  416. )
  417. }
  418. function getRequireString (type, part, index, scoped) {
  419. return loaderUtils.stringifyRequest(
  420. loaderContext,
  421. // disable all configuration loaders
  422. '!!' +
  423. // get loader string for pre-processors
  424. getLoaderString(type, part, index, scoped) +
  425. // select the corresponding part from the vue file
  426. getSelectorString(type, index || 0) +
  427. // the url to the actual vue file, including remaining requests
  428. rawRequest
  429. )
  430. }
  431. function getRequireForImport (type, impt, scoped) {
  432. return 'require(' + getRequireForImportString(type, impt, scoped) + ')'
  433. }
  434. function getImportForImport (type, impt, scoped) {
  435. return (
  436. 'import __vue_' + type + '__ from ' +
  437. getRequireForImportString(type, impt, scoped)
  438. )
  439. }
  440. function getNamedExportForImport (type, impt, scoped) {
  441. return (
  442. 'export * from ' +
  443. getRequireForImportString(type, impt, scoped)
  444. )
  445. }
  446. function getRequireForImportString (type, impt, scoped) {
  447. return loaderUtils.stringifyRequest(
  448. loaderContext,
  449. '!!' + getLoaderString(type, impt, -1, scoped) + impt.src
  450. )
  451. }
  452. function addCssModulesToLoader (loader, part, index) {
  453. if (!part.module) return loader
  454. const option = options.cssModules || {}
  455. const DEFAULT_OPTIONS = {
  456. modules: true
  457. }
  458. const OPTIONS = {
  459. localIdentName: '[hash:base64]',
  460. importLoaders: true
  461. }
  462. return loader.replace(/((?:^|!)css(?:-loader)?)(\?[^!]*)?/, (m, $1, $2) => {
  463. // $1: !css-loader
  464. // $2: ?a=b
  465. const query = loaderUtils.parseQuery($2 || '?')
  466. Object.assign(query, OPTIONS, option, DEFAULT_OPTIONS)
  467. if (index !== -1) {
  468. // Note:
  469. // Class name is generated according to its filename.
  470. // Different <style> tags in the same .vue file may generate same names.
  471. // Append `_[index]` to class name to avoid this.
  472. query.localIdentName += '_' + index
  473. }
  474. return $1 + '?' + JSON.stringify(query)
  475. })
  476. }
  477. function buildCustomBlockLoaderString (attrs) {
  478. const noSrcAttrs = Object.assign({}, attrs)
  479. delete noSrcAttrs.src
  480. const qs = querystring.stringify(noSrcAttrs)
  481. return qs ? '?' + qs : qs
  482. }
  483. // stringify an Array of loader objects
  484. function stringifyLoaders (loaders) {
  485. return loaders
  486. .map(
  487. obj =>
  488. obj && typeof obj === 'object' && typeof obj.loader === 'string'
  489. ? obj.loader +
  490. (obj.options ? '?' + JSON.stringify(obj.options) : '')
  491. : obj
  492. )
  493. .join('!')
  494. }
  495. function getLoaderString (type, part, index, scoped) {
  496. let loader = getRawLoaderString(type, part, index, scoped)
  497. const lang = getLangString(type, part)
  498. if (preLoaders[lang]) {
  499. loader = loader + ensureBang(preLoaders[lang])
  500. }
  501. if (postLoaders[lang]) {
  502. loader = ensureBang(postLoaders[lang]) + loader
  503. }
  504. return loader
  505. }
  506. function getLangString (type, { lang }) {
  507. if (type === 'script' || type === 'template' || type === 'styles') {
  508. return lang || defaultLang[type]
  509. } else {
  510. return type
  511. }
  512. }
  513. function getRawLoaderString (type, part, index, scoped) {
  514. let lang = part.lang || defaultLang[type]
  515. let styleCompiler = ''
  516. if (type === 'styles') {
  517. // style compiler that needs to be applied for all styles
  518. styleCompiler =
  519. styleCompilerPath +
  520. '?' +
  521. JSON.stringify({
  522. // a marker for vue-style-loader to know that this is an import from a vue file
  523. vue: true,
  524. id: moduleId,
  525. scoped: !!scoped,
  526. hasInlineConfig: !!query.postcss
  527. }) +
  528. '!'
  529. // normalize scss/sass/postcss if no specific loaders have been provided
  530. if (!loaders[lang]) {
  531. if (postcssExtensions.indexOf(lang) !== -1) {
  532. lang = 'css'
  533. } else if (lang === 'sass') {
  534. lang = 'sass?indentedSyntax'
  535. } else if (lang === 'scss') {
  536. lang = 'sass'
  537. }
  538. }
  539. }
  540. let loader =
  541. options.extractCSS && type === 'styles'
  542. ? loaders[lang] || getCSSExtractLoader(lang)
  543. : loaders[lang]
  544. const injectString =
  545. type === 'script' && query.inject ? 'inject-loader!' : ''
  546. if (loader != null) {
  547. if (Array.isArray(loader)) {
  548. loader = stringifyLoaders(loader)
  549. } else if (typeof loader === 'object') {
  550. loader = stringifyLoaders([loader])
  551. }
  552. if (type === 'styles') {
  553. // add css modules
  554. loader = addCssModulesToLoader(loader, part, index)
  555. // inject rewriter before css loader for extractTextPlugin use cases
  556. if (rewriterInjectRE.test(loader)) {
  557. loader = loader.replace(
  558. rewriterInjectRE,
  559. (m, $1) => ensureBang($1) + styleCompiler
  560. )
  561. } else {
  562. loader = ensureBang(loader) + styleCompiler
  563. }
  564. }
  565. // if user defines custom loaders for html, add template compiler to it
  566. if (type === 'template' && loader.indexOf(defaultLoaders.html) < 0) {
  567. loader = defaultLoaders.html + '!' + loader
  568. }
  569. return injectString + ensureBang(loader)
  570. } else {
  571. // unknown lang, infer the loader to be used
  572. switch (type) {
  573. case 'template':
  574. return (
  575. defaultLoaders.html +
  576. '!' +
  577. templatePreprocessorPath +
  578. '?engine=' +
  579. lang +
  580. '!'
  581. )
  582. case 'styles':
  583. loader = addCssModulesToLoader(defaultLoaders.css, part, index)
  584. return loader + '!' + styleCompiler + ensureBang(ensureLoader(lang))
  585. case 'script':
  586. return injectString + ensureBang(ensureLoader(lang))
  587. default:
  588. loader = loaders[type]
  589. if (Array.isArray(loader)) {
  590. loader = stringifyLoaders(loader)
  591. }
  592. return ensureBang(loader + buildCustomBlockLoaderString(part.attrs))
  593. }
  594. }
  595. }
  596. // sass => sass-loader
  597. // sass-loader => sass-loader
  598. // sass?indentedSyntax!css => sass-loader?indentedSyntax!css-loader
  599. function ensureLoader (lang) {
  600. return lang
  601. .split('!')
  602. .map(loader =>
  603. loader.replace(
  604. /^([\w-]+)(\?.*)?/,
  605. (_, name, query) =>
  606. (/-loader$/.test(name) ? name : name + '-loader') + (query || '')
  607. )
  608. )
  609. .join('!')
  610. }
  611. function getSelectorString (type, index) {
  612. return (
  613. selectorPath +
  614. '?type=' +
  615. (type === 'script' || type === 'template' || type === 'styles'
  616. ? type
  617. : 'customBlocks') +
  618. '&index=' + index +
  619. '!'
  620. )
  621. }
  622. function ensureBang (loader) {
  623. if (loader.charAt(loader.length - 1) !== '!') {
  624. return loader + '!'
  625. } else {
  626. return loader
  627. }
  628. }
  629. function getCSSExtractLoader (lang) {
  630. let extractor
  631. const op = options.extractCSS
  632. // extractCSS option is an instance of ExtractTextPlugin
  633. if (typeof op.extract === 'function') {
  634. extractor = op
  635. } else {
  636. extractor = tryRequire('extract-text-webpack-plugin')
  637. if (!extractor) {
  638. throw new Error(
  639. '[vue-loader] extractCSS: true requires extract-text-webpack-plugin ' +
  640. 'as a peer dependency.'
  641. )
  642. }
  643. }
  644. const langLoader = lang ? ensureBang(ensureLoader(lang)) : ''
  645. return extractor.extract({
  646. use: 'css-loader' + cssLoaderOptions + '!' + langLoader,
  647. fallback: 'vue-style-loader'
  648. })
  649. }
  650. }