Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Entry.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. class Entry {
  2. /**
  3. * Create a new Entry instance.
  4. */
  5. constructor() {
  6. this.structure = {};
  7. this.base = '';
  8. }
  9. /**
  10. * Fetch the underlying entry structure.
  11. */
  12. get() {
  13. return this.structure;
  14. }
  15. /**
  16. * Get the object keys for the structure.
  17. */
  18. keys() {
  19. return Object.keys(this.structure);
  20. }
  21. /**
  22. * Add a key key-val pair to the structure.
  23. *
  24. * @param {string} key
  25. * @param {mixed} val
  26. */
  27. add(key, val) {
  28. this.structure[key] = (this.structure[key] || []).concat(val);
  29. return this;
  30. }
  31. /**
  32. * Add a new key-val pair, based on a given output path.
  33. *
  34. * @param {mixed} val
  35. * @param {Object} output
  36. * @param {Object} fallback
  37. */
  38. addFromOutput(val, output, fallback) {
  39. output = this.normalizePath(output, fallback);
  40. return this.add(this.createName(output), val);
  41. }
  42. /**
  43. * Add a vendor extraction.
  44. *
  45. * @param {Object} extraction
  46. */
  47. addExtraction(extraction) {
  48. if (!Mix.bundlingJavaScript && !extraction.output) {
  49. throw new Error(
  50. 'Please provide an output path as the second argument to mix.extract().'
  51. );
  52. }
  53. let outputPath = extraction.output;
  54. // If the extraction output path begins with a slash (forward or backward)
  55. // then the path from the public directory determined below will be wrong
  56. // on Windows, as a drive letter will be incorrectly prepended to
  57. // (e.g. '/dist/vendor' -> 'C:\dist\vendor').
  58. let startsWithSlash = ['\\', '/'].indexOf(outputPath[0]) >= 0;
  59. outputPath = startsWithSlash ? outputPath.substr(1) : outputPath;
  60. let vendorPath = outputPath
  61. ? new File(outputPath)
  62. .pathFromPublic(Config.publicPath)
  63. .replace(/\.js$/, '')
  64. .replace(/\\/g, '/')
  65. : path.join(this.base, 'vendor').replace(/\\/g, '/');
  66. this.add(vendorPath, extraction.libs);
  67. return vendorPath;
  68. }
  69. /**
  70. * Add a default entry script to the structure.
  71. */
  72. addDefault() {
  73. this.add(
  74. 'mix',
  75. new File(path.resolve(__dirname, 'mock-entry.js')).path()
  76. );
  77. }
  78. /**
  79. * Build the proper entry name, based on a given output.
  80. *
  81. * @param {Object} output
  82. */
  83. createName(output) {
  84. let name = output
  85. .pathFromPublic(Config.publicPath)
  86. .replace(/\.js$/, '')
  87. .replace(/\\/g, '/');
  88. this.base = path.parse(name).dir;
  89. return name;
  90. }
  91. /**
  92. * Normalize the given output path.
  93. *
  94. * @param {Object} output
  95. * @param {Object} fallback
  96. */
  97. normalizePath(output, fallback) {
  98. // All output paths need to start at the project's public dir.
  99. let pathFromPublicDir = output.pathFromPublic();
  100. if (
  101. !pathFromPublicDir.startsWith('/' + Config.publicPath) &&
  102. !pathFromPublicDir.startsWith('\\' + Config.publicPath)
  103. ) {
  104. output = new File(
  105. path.join(Config.publicPath, output.pathFromPublic())
  106. );
  107. }
  108. // If the output points to a directory, we'll grab a file name from the fallback src.
  109. if (output.isDirectory()) {
  110. output = new File(
  111. path.join(
  112. output.filePath,
  113. fallback.nameWithoutExtension() + '.js'
  114. )
  115. );
  116. }
  117. return output;
  118. }
  119. }
  120. module.exports = Entry;