config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 }, 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 + '.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.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. */
  85. exports.createECharts = function (opt = {}) {
  86. const srcType = opt.type !== 'all' ? '.' + opt.type : '';
  87. const postfixType = srcType;
  88. const format = opt.format || 'umd';
  89. const postfixFormat = (format !== 'umd') ? '.' + format.toLowerCase() : '';
  90. const input = nodePath.resolve(ecDir, `index${srcType}.js`);
  91. return {
  92. plugins: [nodeResolvePlugin()],
  93. treeshake: {
  94. moduleSideEffects: false
  95. },
  96. input: input,
  97. output: createOutputs(
  98. nodePath.resolve(ecDir, `dist/echarts${postfixFormat}${postfixType}`),
  99. opt,
  100. {
  101. name: 'echarts',
  102. // Ignore default exports, which is only for compitable code like:
  103. // import echarts from 'echarts/lib/echarts';
  104. exports: 'named',
  105. format: format
  106. }
  107. )
  108. };
  109. };
  110. exports.createBMap = function (opt) {
  111. const input = nodePath.resolve(ecDir, `extension/bmap/bmap.js`);
  112. return {
  113. plugins: [nodeResolvePlugin()],
  114. input: input,
  115. external: ['echarts'],
  116. output: createOutputs(
  117. nodePath.resolve(ecDir, `dist/extension/bmap`),
  118. opt,
  119. {
  120. name: 'bmap',
  121. globals: {
  122. // For UMD `global.echarts`
  123. echarts: 'echarts'
  124. }
  125. }
  126. )
  127. };
  128. };
  129. exports.createDataTool = function (opt) {
  130. let input = nodePath.resolve(ecDir, `extension/dataTool/index.js`);
  131. return {
  132. plugins: [nodeResolvePlugin()],
  133. input: input,
  134. external: ['echarts'],
  135. output: createOutputs(
  136. nodePath.resolve(ecDir, `dist/extension/dataTool`),
  137. opt,
  138. {
  139. name: 'dataTool',
  140. globals: {
  141. // For UMD `global.echarts`
  142. echarts: 'echarts'
  143. }
  144. }
  145. )
  146. };
  147. };
  148. exports.createMyTransform = function (opt) {
  149. let input = nodePath.resolve(ecDir, `test/lib/myTransform/src/index.ts`);
  150. return {
  151. plugins: [nodeResolvePlugin()],
  152. input: input,
  153. output: createOutputs(
  154. nodePath.resolve(ecDir, `test/lib/myTransform/dist/myTransform`),
  155. opt,
  156. {
  157. name: 'myTransform'
  158. }
  159. )
  160. };
  161. };