config.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF 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,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. const nodeResolvePlugin = require('@rollup/plugin-node-resolve').default;
  20. const nodePath = require('path');
  21. const ecDir = nodePath.resolve(__dirname, '..');
  22. const {terser} = require('rollup-plugin-terser');
  23. const replace = require('@rollup/plugin-replace');
  24. const MagicString = require('magic-string');
  25. const preamble = require('./preamble');
  26. function createAddLicensePlugin(sourcemap) {
  27. return {
  28. renderChunk(code, chunk) {
  29. const s = new MagicString(code);
  30. s.prepend(preamble.js);
  31. return {
  32. code: s.toString(),
  33. map: sourcemap ? s.generateMap({ hires: true }).toString() : null
  34. };
  35. }
  36. }
  37. }
  38. function createOutputs(basename, { min, fileExtension }, commonOutputOpts) {
  39. commonOutputOpts = {
  40. format: 'umd',
  41. ...commonOutputOpts
  42. }
  43. function createReplacePlugin(replacement) {
  44. const plugin = replace({
  45. 'process.env.NODE_ENV': JSON.stringify(replacement)
  46. });
  47. // Remove transform hook. It will have warning when using in output
  48. delete plugin.transform;
  49. return plugin;
  50. }
  51. const output = [{
  52. ...commonOutputOpts,
  53. // Disable sourcemap in
  54. sourcemap: true,
  55. plugins: [
  56. createReplacePlugin('development'),
  57. createAddLicensePlugin(true)
  58. ],
  59. file: basename + (fileExtension || '.js')
  60. }];
  61. if (min) {
  62. output.push({
  63. ...commonOutputOpts,
  64. // Disable sourcemap in min file.
  65. sourcemap: false,
  66. // TODO preamble
  67. plugins: [
  68. createReplacePlugin('production'),
  69. terser(),
  70. createAddLicensePlugin(false)
  71. ],
  72. file: basename + '.min' + (fileExtension || '.js')
  73. })
  74. }
  75. return output;
  76. }
  77. /**
  78. * @param {Object} [opt]
  79. * @param {string} [opt.type=''] 'all' or 'simple' or 'common', default is 'all'
  80. * @param {boolean} [opt.sourcemap] If set, `opt.input` is required too, and `opt.type` is ignored.
  81. * @param {string} [opt.format='umd'] If set, `opt.input` is required too, and `opt.type` is ignored.
  82. * @param {string} [opt.min=false] If build minified output
  83. * @param {boolean} [opt.addBundleVersion=false] Only for debug in watch, prompt that the two build is different.
  84. * @param {string} [opt.fileExtension=undefined] output file extension, default is '.js'. Should start with '.'.
  85. */
  86. exports.createECharts = function (opt = {}) {
  87. const srcType = opt.type !== 'all' ? '.' + opt.type : '';
  88. const postfixType = srcType;
  89. const format = opt.format || 'umd';
  90. const postfixFormat = (format !== 'umd') ? '.' + format.toLowerCase() : '';
  91. const input = nodePath.resolve(ecDir, `index${srcType}.js`);
  92. return {
  93. plugins: [nodeResolvePlugin()],
  94. treeshake: {
  95. moduleSideEffects: false
  96. },
  97. input: input,
  98. output: createOutputs(
  99. nodePath.resolve(ecDir, `dist/echarts${postfixFormat}${postfixType}`),
  100. opt,
  101. {
  102. name: 'echarts',
  103. // Ignore default exports, which is only for compatible code like:
  104. // import echarts from 'echarts/lib/echarts';
  105. exports: 'named',
  106. format: format
  107. }
  108. )
  109. };
  110. };
  111. exports.createBMap = function (opt) {
  112. const input = nodePath.resolve(ecDir, `extension/bmap/bmap.js`);
  113. return {
  114. plugins: [nodeResolvePlugin()],
  115. input: input,
  116. external: ['echarts'],
  117. output: createOutputs(
  118. nodePath.resolve(ecDir, `dist/extension/bmap`),
  119. opt,
  120. {
  121. name: 'bmap',
  122. globals: {
  123. // For UMD `global.echarts`
  124. echarts: 'echarts'
  125. }
  126. }
  127. )
  128. };
  129. };
  130. exports.createDataTool = function (opt) {
  131. let input = nodePath.resolve(ecDir, `extension/dataTool/index.js`);
  132. return {
  133. plugins: [nodeResolvePlugin()],
  134. input: input,
  135. external: ['echarts'],
  136. output: createOutputs(
  137. nodePath.resolve(ecDir, `dist/extension/dataTool`),
  138. opt,
  139. {
  140. name: 'dataTool',
  141. globals: {
  142. // For UMD `global.echarts`
  143. echarts: 'echarts'
  144. }
  145. }
  146. )
  147. };
  148. };
  149. exports.createMyTransform = function (opt) {
  150. let input = nodePath.resolve(ecDir, `test/lib/myTransform/src/index.ts`);
  151. return {
  152. plugins: [nodeResolvePlugin()],
  153. input: input,
  154. output: createOutputs(
  155. nodePath.resolve(ecDir, `test/lib/myTransform/dist/myTransform`),
  156. opt,
  157. {
  158. name: 'myTransform'
  159. }
  160. )
  161. };
  162. };
  163. exports.createSSRClient = function (opt) {
  164. const input = nodePath.resolve(ecDir, `ssr/client/lib/index.js`);
  165. return {
  166. plugins: [nodeResolvePlugin()],
  167. input: input,
  168. output: createOutputs(
  169. nodePath.resolve(ecDir, `ssr/client/dist/index`),
  170. opt,
  171. {
  172. name: 'echarts-ssr-client'
  173. }
  174. )
  175. };
  176. };