webpack.config.npm.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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}/apps/editor/components/QueryEditorWebComponents.ts`],
  88. SqlScratchpadWebComponent: [
  89. `${JS_ROOT}/apps/editor/components/sqlScratchpad/SqlScratchpadWebComponent.ts`
  90. ]
  91. },
  92. output: {
  93. path: `${DIST_DIR}/lib/components`,
  94. library: '[name]',
  95. libraryExport: 'default',
  96. libraryTarget: 'umd'
  97. },
  98. plugins: [
  99. new VueLoaderPlugin(),
  100. new CopyWebpackPlugin({
  101. patterns: [
  102. {
  103. from: `${JS_ROOT}/apps/editor/components/QueryEditorWebComponents.d.ts`,
  104. to: `${DIST_DIR}/lib/components`
  105. },
  106. {
  107. from: `${JS_ROOT}/apps/editor/components/sqlScratchpad/SqlScratchpadWebComponent.d.ts`,
  108. to: `${DIST_DIR}/lib/components`
  109. }
  110. ]
  111. })
  112. ]
  113. });
  114. const parserConfig = Object.assign({}, defaultConfig, {
  115. entry: {
  116. calciteAutocompleteParser: [`${JS_ROOT}/parse/sql/calcite/calciteAutocompleteParser.js`],
  117. calciteSyntaxParser: [`${JS_ROOT}/parse/sql/calcite/calciteSyntaxParser.js`],
  118. druidAutocompleteParser: [`${JS_ROOT}/parse/sql/druid/druidAutocompleteParser.js`],
  119. druidSyntaxParser: [`${JS_ROOT}/parse/sql/druid/druidSyntaxParser.js`],
  120. elasticsearchAutocompleteParser: [
  121. `${JS_ROOT}/parse/sql/elasticsearch/elasticsearchAutocompleteParser.js`
  122. ],
  123. elasticsearchSyntaxParser: [`${JS_ROOT}/parse/sql/elasticsearch/elasticsearchSyntaxParser.js`],
  124. flinkAutocompleteParser: [`${JS_ROOT}/parse/sql/flink/flinkAutocompleteParser.js`],
  125. flinkSyntaxParser: [`${JS_ROOT}/parse/sql/flink/flinkSyntaxParser.js`],
  126. genericAutocompleteParser: [`${JS_ROOT}/parse/sql/generic/genericAutocompleteParser.js`],
  127. genericSyntaxParser: [`${JS_ROOT}/parse/sql/generic/genericSyntaxParser.js`],
  128. hiveAutocompleteParser: [`${JS_ROOT}/parse/sql/hive/hiveAutocompleteParser.js`],
  129. hiveSyntaxParser: [`${JS_ROOT}/parse/sql/hive/hiveSyntaxParser.js`],
  130. impalaAutocompleteParser: [`${JS_ROOT}/parse/sql/impala/impalaAutocompleteParser.js`],
  131. impalaSyntaxParser: [`${JS_ROOT}/parse/sql/impala/impalaSyntaxParser.js`],
  132. ksqlAutocompleteParser: [`${JS_ROOT}/parse/sql/ksql/ksqlAutocompleteParser.js`],
  133. ksqlSyntaxParser: [`${JS_ROOT}/parse/sql/ksql/ksqlSyntaxParser.js`],
  134. phoenixAutocompleteParser: [`${JS_ROOT}/parse/sql/phoenix/phoenixAutocompleteParser.js`],
  135. phoenixSyntaxParser: [`${JS_ROOT}/parse/sql/phoenix/phoenixSyntaxParser.js`],
  136. prestoAutocompleteParser: [`${JS_ROOT}/parse/sql/presto/prestoAutocompleteParser.js`],
  137. prestoSyntaxParser: [`${JS_ROOT}/parse/sql/presto/prestoSyntaxParser.js`],
  138. dasksqlAutocompleteParser: [`${JS_ROOT}/parse/sql/dasksql/dasksqlAutocompleteParser.js`],
  139. dasksqlSyntaxParser: [`${JS_ROOT}/parse/sql/dasksql/dasksqlSyntaxParser.js`]
  140. },
  141. performance: {
  142. hints: false,
  143. maxEntrypointSize: 1500000,
  144. maxAssetSize: 1500000
  145. },
  146. output: {
  147. path: `${DIST_DIR}/lib/parsers`,
  148. library: '[name]',
  149. libraryExport: 'default',
  150. libraryTarget: 'umd',
  151. umdNamedDefine: true,
  152. globalObject: `(typeof self !== 'undefined' ? self : this)`
  153. }
  154. });
  155. const vue3WebCompWrapperConfig = Object.assign({}, defaultConfig, {
  156. entry: {
  157. index: [`${JS_ROOT}/vue/wrapper/index.ts`]
  158. },
  159. output: {
  160. path: `${WRAPPER_DIR}/dist`,
  161. library: '[name]',
  162. libraryExport: 'default',
  163. libraryTarget: 'umd',
  164. umdNamedDefine: true,
  165. globalObject: `(typeof self !== 'undefined' ? self : this)`
  166. },
  167. plugins: [
  168. new CleanWebpackPlugin([`${WRAPPER_DIR}/src`, `${WRAPPER_DIR}/dist`]),
  169. new CopyWebpackPlugin({
  170. patterns: [{ from: `${JS_ROOT}/vue/wrapper/`, to: `${WRAPPER_DIR}/src` }]
  171. })
  172. ]
  173. });
  174. module.exports = [copySourceConfig, webComponentsConfig, parserConfig, vue3WebCompWrapperConfig];