Skip to content

Commit

Permalink
Merge pull request apache#13890 from apache/treeshakable-exports
Browse files Browse the repository at this point in the history
New minimal import API, improve exported option types.
  • Loading branch information
pissang authored Jan 3, 2021
2 parents bad5ffc + 469fd04 commit 0fc80c7
Show file tree
Hide file tree
Showing 402 changed files with 11,694 additions and 9,559 deletions.
5 changes: 3 additions & 2 deletions .eslintrc-common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# ```
# Note that it should be "workingDirectories" rather than "WorkingDirectories".

root: true
rules:
# Check the rules in: node_modules/@typescript-eslint/eslint-plugin/README.md
no-console:
Expand Down Expand Up @@ -80,7 +81,6 @@ rules:
no-octal: 2
no-octal-escape: 2
no-proto: 2
no-redeclare: 2
no-self-compare: 2
no-unneeded-ternary: 2
no-with: 2
Expand Down Expand Up @@ -215,4 +215,5 @@ rules:
- 1
-
vars: "local"
args: "none"
args: "none"
"@typescript-eslint/no-redeclare": 2
15 changes: 12 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ todo
*.sublime-workspace
*.sublime-project

# Result of node.js perf
*.asm

# Distribution files
/esm
/echarts.all.js
/echarts.simple.js
Expand All @@ -191,7 +195,12 @@ todo
/index.blank.js
/extension-esm
/extension
/core.js
/core.d.ts
/charts.js
/charts.d.ts
/components.js
/components.d.ts
/renderers.js
/renderers.d.ts
*.tgz

# Result of node.js perf
*.asm
70 changes: 0 additions & 70 deletions .jshintrc

This file was deleted.

2 changes: 1 addition & 1 deletion build/build-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ${preamble.js}
typeof exports.nodeName !== 'string'
) {
// CommonJS
factory(exports, require('echarts'));
factory(exports, require('echarts/lib/echarts'));
} else {
// Browser globals
factory({}, root.echarts);
Expand Down
6 changes: 4 additions & 2 deletions build/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

const assert = require('assert');
const nodeResolvePlugin = require('rollup-plugin-node-resolve');
const nodeResolvePlugin = require('@rollup/plugin-node-resolve').default;
const nodePath = require('path');
const ecDir = nodePath.resolve(__dirname, '..');
const typescriptPlugin = require('rollup-plugin-typescript2');
Expand Down Expand Up @@ -117,7 +117,9 @@ exports.createECharts = function (opt = {}) {
plugins: preparePlugins(opt, {
include
}),

treeshake: {
moduleSideEffects: false
},
// external: ['zrender'],
// external: id => ['zrender'].includes(id),

Expand Down
2 changes: 1 addition & 1 deletion build/dev-fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function wrapUMDCode() {

function rebuild() {
build({
stdio: 'inherit',
// stdio: 'inherit',
entryPoints: [path.resolve(__dirname, '../src/echarts.all.ts')],
outfile: outFilePath,
format: 'cjs',
Expand Down
161 changes: 75 additions & 86 deletions build/pre-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ const ts = require('typescript');
const globby = require('globby');
const transformDEVUtil = require('./transform-dev');
const preamble = require('./preamble');
// NOTE: v1.4.2 is the latest version that supports ts 3.8.3
const dts = require('rollup-plugin-dts').default;
const dts = require('@lang/rollup-plugin-dts').default;
const rollup = require('rollup');

const ecDir = nodePath.resolve(__dirname, '..');
Expand Down Expand Up @@ -67,10 +66,10 @@ const extensionSrcGlobby = {
cwd: ecDir
};
const extensionSrcDir = nodePath.resolve(ecDir, 'extension-src');
const extensionCJSDir = nodePath.resolve(ecDir, 'extension');
const extensionESMDir = nodePath.resolve(ecDir, 'extension-esm');
const extensionESMDir = nodePath.resolve(ecDir, 'extension');

const typesDir = nodePath.resolve(ecDir, 'types');
const esmDir = 'lib';


const compileWorkList = [
Expand All @@ -92,49 +91,8 @@ const compileWorkList = [
},
before: async function () {
fsExtra.removeSync(tmpDir);
fsExtra.removeSync(nodePath.resolve(ecDir, 'esm'));
fsExtra.removeSync(nodePath.resolve(ecDir, 'echarts.all.js'));
fsExtra.removeSync(nodePath.resolve(ecDir, 'echarts.blank.js'));
fsExtra.removeSync(nodePath.resolve(ecDir, 'echarts.common.js'));
fsExtra.removeSync(nodePath.resolve(ecDir, 'echarts.simple.js'));
},
after: async function () {
fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.all.js'), nodePath.resolve(ecDir, 'echarts.all.js'));
fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.blank.js'), nodePath.resolve(ecDir, 'echarts.blank.js'));
fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.common.js'), nodePath.resolve(ecDir, 'echarts.common.js'));
fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.simple.js'), nodePath.resolve(ecDir, 'echarts.simple.js'));
fs.renameSync(nodePath.resolve(tmpDir, 'src'), nodePath.resolve(ecDir, 'esm'));

transformRootFolderInEntry(nodePath.resolve(ecDir, 'echarts.all.js'), 'esm');
transformRootFolderInEntry(nodePath.resolve(ecDir, 'echarts.blank.js'), 'esm');
transformRootFolderInEntry(nodePath.resolve(ecDir, 'echarts.common.js'), 'esm');
transformRootFolderInEntry(nodePath.resolve(ecDir, 'echarts.simple.js'), 'esm');

await transformDistributionFiles(nodePath.resolve(ecDir, 'esm'), 'esm');
await transformDistributionFiles(nodePath.resolve(ecDir, 'types'), 'esm');
fsExtra.removeSync(tmpDir);

await bundleDTS();
}
},
{
logLabel: 'main ts -> js-cjs',
compilerOptionsOverride: {
module: 'CommonJS',
// `rootDir` Only use to control the output
// directory structure with --outDir.
rootDir: ecDir,
outDir: tmpDir
},
srcGlobby: mainSrcGlobby,
transformOptions: {
filesGlobby: {patterns: ['**/*.js'], cwd: tmpDir},
preamble: preamble.js,
transformDEV: true
},
before: async function () {
fsExtra.removeSync(tmpDir);
fsExtra.removeSync(nodePath.resolve(ecDir, 'lib'));
fsExtra.removeSync(nodePath.resolve(ecDir, 'types'));
fsExtra.removeSync(nodePath.resolve(ecDir, esmDir));
fsExtra.removeSync(nodePath.resolve(ecDir, 'index.js'));
fsExtra.removeSync(nodePath.resolve(ecDir, 'index.blank.js'));
fsExtra.removeSync(nodePath.resolve(ecDir, 'index.common.js'));
Expand All @@ -145,39 +103,18 @@ const compileWorkList = [
fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.blank.js'), nodePath.resolve(ecDir, 'index.blank.js'));
fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.common.js'), nodePath.resolve(ecDir, 'index.common.js'));
fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.simple.js'), nodePath.resolve(ecDir, 'index.simple.js'));
fs.renameSync(nodePath.resolve(tmpDir, 'src'), nodePath.resolve(ecDir, 'lib'));

transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.js'), 'lib');
transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.blank.js'), 'lib');
transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.common.js'), 'lib');
transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.simple.js'), 'lib');
fs.renameSync(nodePath.resolve(tmpDir, 'src'), nodePath.resolve(ecDir, esmDir));

await transformDistributionFiles(nodePath.resolve(ecDir, 'lib'), 'lib');
removeESmoduleMark();
transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.js'), esmDir);
transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.blank.js'), esmDir);
transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.common.js'), esmDir);
transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.simple.js'), esmDir);

await transformDistributionFiles(nodePath.resolve(ecDir, esmDir), esmDir);
await transformDistributionFiles(nodePath.resolve(ecDir, 'types'), esmDir);
fsExtra.removeSync(tmpDir);
}
},
{
logLabel: 'extension ts -> js-cjs',
compilerOptionsOverride: {
module: 'CommonJS',
rootDir: extensionSrcDir,
outDir: extensionCJSDir
},
srcGlobby: extensionSrcGlobby,
transformOptions: {
filesGlobby: {patterns: ['**/*.js'], cwd: extensionCJSDir},
preamble: preamble.js,
transformDEV: true
},
before: async function () {
fsExtra.removeSync(extensionCJSDir);
},
after: async function () {
await transformDistributionFiles(extensionCJSDir, 'lib');
}
},
{
logLabel: 'extension ts -> js-esm',
compilerOptionsOverride: {
Expand All @@ -195,7 +132,7 @@ const compileWorkList = [
fsExtra.removeSync(extensionESMDir);
},
after: async function () {
await transformDistributionFiles(extensionESMDir, 'esm');
await transformDistributionFiles(extensionESMDir, 'lib');
}
}
];
Expand Down Expand Up @@ -231,6 +168,11 @@ module.exports = async function () {
process.stdout.write(chalk.green.dim(` done \n`));
}

process.stdout.write(chalk.green.dim(`Generating entries ...`));
generateEntries();
process.stdout.write(chalk.green.dim(`Bundling DTS ...`));
await bundleDTS();

console.log(chalk.green.dim('All done.'));
};

Expand Down Expand Up @@ -395,8 +337,9 @@ async function readFilePaths({patterns, cwd}) {
}

async function bundleDTS() {
const bundle = await rollup.rollup({
input: nodePath.resolve(__dirname, '../index.d.ts'),

const outDir = nodePath.resolve(__dirname, '../types/dist');
const commonConfig = {
onwarn(warning, rollupWarn) {
// Not warn circular dependency
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
Expand All @@ -407,18 +350,50 @@ async function bundleDTS() {
dts({
respectExternal: true
})
// {
// generateBundle(options, bundle) {
// for (let chunk of Object.values(bundle)) {
// chunk.code = `
// type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
// ${chunk.code}`
// }
// }
// }
]
};

// Bundle chunks.
const parts = [
'core', 'charts', 'components', 'renderers', 'option'
];
const inputs = {};
parts.forEach(partName => {
inputs[partName] = nodePath.resolve(__dirname, `../types/src/export/${partName}.d.ts`)
});

const bundle = await rollup.rollup({
input: inputs,
...commonConfig
});
const bundleFile = nodePath.resolve(__dirname, '../types/dist/echarts.d.ts');
let idx = 1;
await bundle.write({
file: bundleFile
dir: outDir,
minifyInternalExports: false,
manualChunks: (id) => {
// Only create one chunk.
return 'shared';
},
chunkFileNames: 'shared.d.ts'
});

// Bundle all in one
const bundleAllInOne = await rollup.rollup({
input: nodePath.resolve(__dirname, `../types/src/export/all.d.ts`),
...commonConfig
});
await bundleAllInOne.write({
file: nodePath.resolve(outDir, 'echarts.d.ts')
});
// To support ts 3.4
const extra = `
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
`
const code = extra + fs.readFileSync(bundleFile, 'utf-8');
fs.writeFileSync(bundleFile, code, 'utf-8');
}

function readTSConfig() {
Expand All @@ -428,4 +403,18 @@ function readTSConfig() {
const tsConfigText = fs.readFileSync(filePath, {encoding: 'utf8'});
return (new Function(`return ( ${tsConfigText} )`))();
}


function generateEntries() {
['charts', 'components', 'renderers', 'core'].forEach(entryName => {
if (entryName !== 'option') {
const jsCode = fs.readFileSync(nodePath.join(__dirname, `template/${entryName}.js`), 'utf-8');
fs.writeFileSync(nodePath.join(__dirname, `../${entryName}.js`), jsCode, 'utf-8');
}

const dtsCode = fs.readFileSync(nodePath.join(__dirname, `/template/${entryName}.d.ts`), 'utf-8');
fs.writeFileSync(nodePath.join(__dirname, `../${entryName}.d.ts`), dtsCode, 'utf-8');
});
}

module.exports.readTSConfig = readTSConfig;
Loading

0 comments on commit 0fc80c7

Please sign in to comment.