bf01e2f054e5ae92244577f66232f9417840cfbde6304314d9fc26971ef32027f9cd7a7dc60e6da487bf044d27e62a6e78884980fa2406b3630b3fdf5bde6e 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @license
  3. * Copyright 2018 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. const assert = require('assert')
  18. const path = require('path')
  19. const { URL } = require('url')
  20. function determineAsValue ({ optionsAs, href, file }) {
  21. assert(href, `The 'href' parameter was not provided.`)
  22. switch (typeof optionsAs) {
  23. case 'string': {
  24. return optionsAs
  25. }
  26. case 'function': {
  27. return optionsAs(href)
  28. }
  29. case 'undefined': {
  30. // If `as` value is not provided in option, dynamically determine the correct
  31. // value based on the suffix of filename.
  32. // We only care about the pathname, so just use any domain when constructing the URL.
  33. // Use file instead of href option because the publicPath part may be malformed.
  34. // See https://github.com/vuejs/vue-cli/issues/5672
  35. const url = new URL(file || href, 'https://example.com')
  36. const extension = path.extname(url.pathname)
  37. if (extension === '.css') {
  38. return 'style'
  39. }
  40. if (extension === '.woff2') {
  41. return 'font'
  42. }
  43. return 'script'
  44. }
  45. default:
  46. throw new Error(`The 'as' option isn't set to a recognized value: ${optionsAs}`)
  47. }
  48. }
  49. module.exports = determineAsValue