webpack.config.npm.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. {
  61. from: './package.json',
  62. to: `${DIST_DIR}/package.json`,
  63. transform: (content) => {
  64. // Remove local dependencies (currently only cuix) since it is not needed
  65. // and cannot be accessed by the npm registry.
  66. const contentStr = content.toString();
  67. const contentObj = JSON.parse(contentStr);
  68. // Iterate over the dependencies and remove local references
  69. Object.keys(contentObj.dependencies || {}).forEach((key) => {
  70. const value = contentObj.dependencies[key];
  71. if (value.startsWith("file:")) {
  72. delete contentObj.dependencies[key];
  73. }
  74. });
  75. return JSON.stringify(contentObj, null, 2);
  76. }
  77. },
  78. {
  79. from: JS_ROOT,
  80. to: `${DIST_DIR}`,
  81. transform: (content, absoluteFrom) => {
  82. // This turns all absolute imports to relative (except for npm dependencies) and solves module
  83. // resolution issues when the Hue source files are used directly from another project.
  84. if (/\.(jsx?|tsx?|vue)$/.test(absoluteFrom)) {
  85. const depth = absoluteFrom.slice(JS_ROOT.length + 1).split('/').length - 1;
  86. return content
  87. .toString()
  88. .replace(
  89. /(import\s(?:[\S\s]+?\sfrom\s[^'"]*)?['"])([^.][^'"/]*)([/])/gi,
  90. (all, pre, module, post) =>
  91. `${pre}${rootFiles.has(module) ? relatives[depth] : ''}${module}${post}`
  92. );
  93. }
  94. return content;
  95. }
  96. }
  97. ]
  98. })
  99. ]
  100. };
  101. const webComponentsConfig = Object.assign({}, defaultConfig, {
  102. entry: {
  103. ErDiagram: [`${JS_ROOT}/components/er-diagram/webcomp.ts`],
  104. QueryEditorWebComponents: [`${JS_ROOT}/webComponents/QueryEditorComponents.ts`],
  105. SqlScratchpadWebComponent: [`${JS_ROOT}/webComponents/SqlScratchpad.ts`]
  106. },
  107. output: {
  108. path: `${DIST_DIR}/lib/components`,
  109. library: '[name]',
  110. libraryExport: 'default',
  111. libraryTarget: 'umd'
  112. },
  113. plugins: [
  114. new VueLoaderPlugin(),
  115. new CopyWebpackPlugin({
  116. patterns: [
  117. {
  118. from: `${JS_ROOT}/webComponents/QueryEditorComponents.d.ts`,
  119. to: `${DIST_DIR}/lib/components`
  120. },
  121. {
  122. from: `${JS_ROOT}/webComponents/SqlScratchpad.d.ts`,
  123. to: `${DIST_DIR}/lib/components`
  124. }
  125. ]
  126. })
  127. ]
  128. });
  129. const parserConfig = Object.assign({}, defaultConfig, {
  130. entry: {
  131. calciteAutocompleteParser: [`${JS_ROOT}/parse/sql/calcite/calciteAutocompleteParser.js`],
  132. calciteSyntaxParser: [`${JS_ROOT}/parse/sql/calcite/calciteSyntaxParser.js`],
  133. flinkAutocompleteParser: [`${JS_ROOT}/parse/sql/flink/flinkAutocompleteParser.js`],
  134. flinkSyntaxParser: [`${JS_ROOT}/parse/sql/flink/flinkSyntaxParser.js`],
  135. genericAutocompleteParser: [`${JS_ROOT}/parse/sql/generic/genericAutocompleteParser.js`],
  136. genericSyntaxParser: [`${JS_ROOT}/parse/sql/generic/genericSyntaxParser.js`],
  137. hiveAutocompleteParser: [`${JS_ROOT}/parse/sql/hive/hiveAutocompleteParser.js`],
  138. hiveSyntaxParser: [`${JS_ROOT}/parse/sql/hive/hiveSyntaxParser.js`],
  139. impalaAutocompleteParser: [`${JS_ROOT}/parse/sql/impala/impalaAutocompleteParser.js`],
  140. impalaSyntaxParser: [`${JS_ROOT}/parse/sql/impala/impalaSyntaxParser.js`],
  141. ksqlAutocompleteParser: [`${JS_ROOT}/parse/sql/ksql/ksqlAutocompleteParser.js`],
  142. ksqlSyntaxParser: [`${JS_ROOT}/parse/sql/ksql/ksqlSyntaxParser.js`],
  143. phoenixAutocompleteParser: [`${JS_ROOT}/parse/sql/phoenix/phoenixAutocompleteParser.js`],
  144. phoenixSyntaxParser: [`${JS_ROOT}/parse/sql/phoenix/phoenixSyntaxParser.js`],
  145. prestoAutocompleteParser: [`${JS_ROOT}/parse/sql/presto/prestoAutocompleteParser.js`],
  146. prestoSyntaxParser: [`${JS_ROOT}/parse/sql/presto/prestoSyntaxParser.js`],
  147. dasksqlAutocompleteParser: [`${JS_ROOT}/parse/sql/dasksql/dasksqlAutocompleteParser.js`],
  148. dasksqlSyntaxParser: [`${JS_ROOT}/parse/sql/dasksql/dasksqlSyntaxParser.js`]
  149. },
  150. performance: {
  151. hints: false,
  152. maxEntrypointSize: 1500000,
  153. maxAssetSize: 1500000
  154. },
  155. output: {
  156. path: `${DIST_DIR}/lib/parsers`,
  157. library: '[name]',
  158. libraryExport: 'default',
  159. libraryTarget: 'umd',
  160. umdNamedDefine: true,
  161. globalObject: `(typeof self !== 'undefined' ? self : this)`
  162. }
  163. });
  164. const vue3WebCompWrapperConfig = Object.assign({}, defaultConfig, {
  165. entry: {
  166. index: [`${JS_ROOT}/vue/wrapper/index.ts`]
  167. },
  168. output: {
  169. path: `${WRAPPER_DIR}/dist`,
  170. library: '[name]',
  171. libraryExport: 'default',
  172. libraryTarget: 'umd',
  173. umdNamedDefine: true,
  174. globalObject: `(typeof self !== 'undefined' ? self : this)`
  175. },
  176. plugins: [
  177. new CleanWebpackPlugin([`${WRAPPER_DIR}/src`, `${WRAPPER_DIR}/dist`]),
  178. new CopyWebpackPlugin({
  179. patterns: [{ from: `${JS_ROOT}/vue/wrapper/`, to: `${WRAPPER_DIR}/src` }]
  180. })
  181. ]
  182. });
  183. module.exports = [copySourceConfig, webComponentsConfig, parserConfig, vue3WebCompWrapperConfig];