webpack.config.npm.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Licensed to Cloudera, Inc. under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. Cloudera, Inc. licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. const CleanWebpackPlugin = require('clean-webpack-plugin');
  19. const CopyWebpackPlugin = require('copy-webpack-plugin');
  20. const path = require('path');
  21. const fs = require('fs');
  22. const { VueLoaderPlugin } = require('vue-loader');
  23. const DIST_DIR = path.join(__dirname, 'npm_dist');
  24. const JS_ROOT = path.join(__dirname, '/desktop/core/src/desktop/js');
  25. const WRAPPER_DIR = `${__dirname}/tools/vue3-webcomponent-wrapper`;
  26. const defaultConfig = Object.assign({}, require('./webpack.config'), {
  27. mode: 'production',
  28. optimization: {
  29. minimize: true
  30. },
  31. plugins: []
  32. });
  33. const rootFiles = new Set();
  34. fs.readdirSync(JS_ROOT).forEach(file => {
  35. rootFiles.add(file.replace(/\..+$/, ''));
  36. });
  37. const relatives = ['./'];
  38. let current = '';
  39. for (let i = 1; i < 50; i++) {
  40. current += '../';
  41. relatives.push(current);
  42. }
  43. const copySourceConfig = {
  44. mode: 'production',
  45. entry: {
  46. pjson: './package.json'
  47. },
  48. output: {
  49. path: `${DIST_DIR}`
  50. },
  51. optimization: {
  52. minimize: false,
  53. usedExports: true
  54. },
  55. plugins: [
  56. new CleanWebpackPlugin([DIST_DIR]),
  57. new CopyWebpackPlugin({
  58. patterns: [
  59. { from: './NPM-README.md', to: `${DIST_DIR}/README.md` },
  60. { from: './package.json', to: `${DIST_DIR}/package.json` },
  61. {
  62. from: JS_ROOT,
  63. to: `${DIST_DIR}`,
  64. transform: (content, absoluteFrom) => {
  65. // This turns all absolute imports to relative (except for npm dependencies) and solves module
  66. // resolution issues when the Hue source files are used directly from another project.
  67. if (/\.(jsx?|tsx?|vue)$/.test(absoluteFrom)) {
  68. const depth = absoluteFrom.slice(JS_ROOT.length + 1).split('/').length - 1;
  69. return content
  70. .toString()
  71. .replace(
  72. /(import\s(?:[\S\s]+?\sfrom\s[^'"]*)?['"])([^.][^'"/]*)([/])/gi,
  73. (all, pre, module, post) =>
  74. `${pre}${rootFiles.has(module) ? relatives[depth] : ''}${module}${post}`
  75. );
  76. }
  77. return content;
  78. }
  79. }
  80. ]
  81. })
  82. ]
  83. };
  84. const webComponentsConfig = Object.assign({}, defaultConfig, {
  85. entry: {
  86. ErDiagram: [`${JS_ROOT}/components/er-diagram/webcomp.ts`],
  87. QueryEditorWebComponents: [`${JS_ROOT}/webComponents/QueryEditorComponents.ts`],
  88. SqlScratchpadWebComponent: [`${JS_ROOT}/webComponents/SqlScratchpad.ts`]
  89. },
  90. output: {
  91. path: `${DIST_DIR}/lib/components`,
  92. library: '[name]',
  93. libraryExport: 'default',
  94. libraryTarget: 'umd'
  95. },
  96. plugins: [
  97. new VueLoaderPlugin(),
  98. new CopyWebpackPlugin({
  99. patterns: [
  100. {
  101. from: `${JS_ROOT}/webComponents/QueryEditorComponents.d.ts`,
  102. to: `${DIST_DIR}/lib/components`
  103. },
  104. {
  105. from: `${JS_ROOT}/webComponents/SqlScratchpad.d.ts`,
  106. to: `${DIST_DIR}/lib/components`
  107. }
  108. ]
  109. })
  110. ]
  111. });
  112. const parserConfig = Object.assign({}, defaultConfig, {
  113. entry: {
  114. calciteAutocompleteParser: [`${JS_ROOT}/parse/sql/calcite/calciteAutocompleteParser.js`],
  115. calciteSyntaxParser: [`${JS_ROOT}/parse/sql/calcite/calciteSyntaxParser.js`],
  116. druidAutocompleteParser: [`${JS_ROOT}/parse/sql/druid/druidAutocompleteParser.js`],
  117. druidSyntaxParser: [`${JS_ROOT}/parse/sql/druid/druidSyntaxParser.js`],
  118. elasticsearchAutocompleteParser: [
  119. `${JS_ROOT}/parse/sql/elasticsearch/elasticsearchAutocompleteParser.js`
  120. ],
  121. elasticsearchSyntaxParser: [`${JS_ROOT}/parse/sql/elasticsearch/elasticsearchSyntaxParser.js`],
  122. flinkAutocompleteParser: [`${JS_ROOT}/parse/sql/flink/flinkAutocompleteParser.js`],
  123. flinkSyntaxParser: [`${JS_ROOT}/parse/sql/flink/flinkSyntaxParser.js`],
  124. genericAutocompleteParser: [`${JS_ROOT}/parse/sql/generic/genericAutocompleteParser.js`],
  125. genericSyntaxParser: [`${JS_ROOT}/parse/sql/generic/genericSyntaxParser.js`],
  126. hiveAutocompleteParser: [`${JS_ROOT}/parse/sql/hive/hiveAutocompleteParser.js`],
  127. hiveSyntaxParser: [`${JS_ROOT}/parse/sql/hive/hiveSyntaxParser.js`],
  128. impalaAutocompleteParser: [`${JS_ROOT}/parse/sql/impala/impalaAutocompleteParser.js`],
  129. impalaSyntaxParser: [`${JS_ROOT}/parse/sql/impala/impalaSyntaxParser.js`],
  130. ksqlAutocompleteParser: [`${JS_ROOT}/parse/sql/ksql/ksqlAutocompleteParser.js`],
  131. ksqlSyntaxParser: [`${JS_ROOT}/parse/sql/ksql/ksqlSyntaxParser.js`],
  132. phoenixAutocompleteParser: [`${JS_ROOT}/parse/sql/phoenix/phoenixAutocompleteParser.js`],
  133. phoenixSyntaxParser: [`${JS_ROOT}/parse/sql/phoenix/phoenixSyntaxParser.js`],
  134. prestoAutocompleteParser: [`${JS_ROOT}/parse/sql/presto/prestoAutocompleteParser.js`],
  135. prestoSyntaxParser: [`${JS_ROOT}/parse/sql/presto/prestoSyntaxParser.js`],
  136. dasksqlAutocompleteParser: [`${JS_ROOT}/parse/sql/dasksql/dasksqlAutocompleteParser.js`],
  137. dasksqlSyntaxParser: [`${JS_ROOT}/parse/sql/dasksql/dasksqlSyntaxParser.js`]
  138. },
  139. performance: {
  140. hints: false,
  141. maxEntrypointSize: 1500000,
  142. maxAssetSize: 1500000
  143. },
  144. output: {
  145. path: `${DIST_DIR}/lib/parsers`,
  146. library: '[name]',
  147. libraryExport: 'default',
  148. libraryTarget: 'umd',
  149. umdNamedDefine: true,
  150. globalObject: `(typeof self !== 'undefined' ? self : this)`
  151. }
  152. });
  153. const vue3WebCompWrapperConfig = Object.assign({}, defaultConfig, {
  154. entry: {
  155. index: [`${JS_ROOT}/vue/wrapper/index.ts`]
  156. },
  157. output: {
  158. path: `${WRAPPER_DIR}/dist`,
  159. library: '[name]',
  160. libraryExport: 'default',
  161. libraryTarget: 'umd',
  162. umdNamedDefine: true,
  163. globalObject: `(typeof self !== 'undefined' ? self : this)`
  164. },
  165. plugins: [
  166. new CleanWebpackPlugin([`${WRAPPER_DIR}/src`, `${WRAPPER_DIR}/dist`]),
  167. new CopyWebpackPlugin({
  168. patterns: [{ from: `${JS_ROOT}/vue/wrapper/`, to: `${WRAPPER_DIR}/src` }]
  169. })
  170. ]
  171. });
  172. module.exports = [copySourceConfig, webComponentsConfig, parserConfig, vue3WebCompWrapperConfig];