Skip to content

Commit

Permalink
Cleaned esbuild working
Browse files Browse the repository at this point in the history
  • Loading branch information
arhtudormorar committed May 13, 2024
1 parent 6aa4d09 commit 35c63da
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 82 deletions.
108 changes: 41 additions & 67 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,50 @@
const esbuild = require('esbuild');
const glob = require('glob');

const buildTypes = {
cjs: {
splitting: false,
format: 'cjs',
tsconfig: './tsconfig.json',
destination: '/cjs'
},
esm: {
splitting: true,
format: 'esm',
tsconfig: './tsconfig.esm.json',
destination: ''
}
};
/*
The reason why we use esbuild instead of tsc is because esbuild can output .mjs files
*/

function esbuildWrapper(
buildType = 'esm',
options = {
outDir: 'out'
}
) {
const { format, splitting, tsconfig, destination } = buildTypes[buildType];
const filesToInclude = ['./src/**/*.ts'].join(',');

const filesToInclude = [
// './src/**/*.tsx',
'./src/**/*.ts'
// './src/**/*.scss',
].join(',');
const allFiles = glob.sync(filesToInclude);

return function executeBuildCommand() {
const allFiles = glob.sync(filesToInclude);
const files = allFiles.filter((file) => {
const hasTestFiles = file.includes('/tests/') || file.includes('/stories/');
return !hasTestFiles;
});

const files = allFiles.filter((file) => {
const hasTestFiles =
file.includes('/tests/') || file.includes('/stories/');
return !hasTestFiles;
const executeBuild = () =>
esbuild
.build({
entryPoints: files,
splitting: true,
format: 'esm',
outdir: 'out',
treeShaking: true,
minify: true,
bundle: true,
sourcemap: true,
chunkNames: '__chunks__/[name]-[hash]',
target: ['es2021'],
outExtension: { '.js': '.mjs' },
tsconfig: './tsconfig.json',
platform: 'node',
define: {
global: 'global',
process: 'process',
Buffer: 'Buffer'
}
})
.then(() => {
console.log(
'\x1b[36m%s\x1b[0m',
`[${new Date().toLocaleTimeString()}] sdk-dapp-core build succeeded for esm types`
);
})
.catch((err) => {
console.log(11, err);
process.exit(1);
});

esbuild
.build({
entryPoints: files,
splitting,
format,
outdir: `${options.outDir}${destination}`,
treeShaking: true,
minify: true,
bundle: true,
sourcemap: true,
chunkNames: '__chunks__/[name]-[hash]',
target: ['es2021'],
outExtension: { '.js': '.mjs' },
tsconfig,
platform: 'node',
define: {
global: 'global',
process: 'process',
Buffer: 'Buffer',
'process.env.NODE_ENV': `"production"`
}
})
.then(() => {
console.log(
'\x1b[36m%s\x1b[0m',
`[${new Date().toLocaleTimeString()}] sdk-dapp build succeeded for ${format} types`
);
})
.catch(() => process.exit(1));
};
}

esbuildWrapper('esm')();
// cjs will be performed by tsc
// esbuildWrapper('cjs')();
executeBuild();
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
"url": "git+https://github.com/multiversx/mx-sdk-dapp-core.git"
},
"scripts": {
"compile": "tsc",
"build:esm-types": "tsc --project tsconfig.esm.json && tsc-alias --project tsconfig.esm.json",
"build:cjs": "tsc && tsc-alias",
"compile": "tsc && tsc-alias",
"compile-next": "rimraf out && tsc --p tsconfig.next.json && tsc-alias --project tsconfig.next.json",
"build-esbuild": "rimraf out && node esbuild.js",
"build": "rimraf out && node esbuild.js && yarn build:esm-types && yarn build:cjs",
"test": "jest",
"compile-next": "rimraf out && tsc --p tsconfig.next.json && tsc-alias --project tsconfig.next.json"
"build": "yarn build-esbuild && yarn compile",
"test": "jest"
},
"publishConfig": {
"access": "public"
Expand Down
8 changes: 0 additions & 8 deletions tsconfig.esm.json

This file was deleted.

1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"extends": "./tsconfig.base.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "./out",
}
}

0 comments on commit 35c63da

Please sign in to comment.