Skip to content

Commit

Permalink
fix: improve build to reduce bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
HellWolf93 committed Jul 20, 2022
1 parent 27b4da6 commit 849df84
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 18 deletions.
13 changes: 0 additions & 13 deletions .babelrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ coverage
.env
package-lock.json
components
esm
propTypes
libs
styles
Expand Down
27 changes: 27 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = function getBabelConfig(api) {
const useESModules = api.env(['esm']);

const presets = [
[
'@babel/preset-env',
{
modules: useESModules ? false : 'commonjs',
},
],
'@babel/preset-react',
[
'@babel/preset-typescript',
{
isTSX: true,
allExtensions: true,
},
],
];
const plugins = ['@babel/plugin-transform-runtime'];

return {
presets,
plugins,
comments: false,
};
};
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"scripts": {
"start": "./node_modules/.bin/styleguidist server",
"build:library": "./node_modules/.bin/styleguidist build",
"build:components": "npx babel ./src --out-dir ./",
"build:components": "yarn build:components:node && yarn build:components:esm",
"build:components:node": "node ./scripts/build node --out-dir ./",
"build:components:esm": "node ./scripts/build esm --out-dir ./",
"build:copy-ts-files": "node ./scripts/copy-files.js",
"build": "npm-run-all --parallel build:components --serial build:copy-ts-files",
"prepublishOnly": "yarn clean && yarn build",
Expand Down Expand Up @@ -52,10 +54,17 @@
"react-dom": ">=16.8.0",
"styled-components": ">=4.3.2 <6"
},
"main": "index.js",
"types": "index.d.ts",
"main": "./index.js",
"module": "./esm/index.js",
"exports": {
"import": "./esm/index.js",
"require": "./index.js"
},
"types": "./index.d.ts",
"sideEffects": false,
"files": [
"components",
"esm",
"styles",
"libs",
"index.d.ts"
Expand Down Expand Up @@ -192,7 +201,8 @@
"stylelint-scss": "^3.3.0",
"wait-on": "^6.0.1",
"webdriverio": "^7",
"webpack": "^4.41.2"
"webpack": "^4.41.2",
"yargs": "^17.5.1"
},
"pre-push": [
"lint",
Expand Down
64 changes: 64 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const childProcess = require('child_process');
const path = require('path');
const yargs = require('yargs');
const { promisify } = require('util');

const exec = promisify(childProcess.exec);

const validBundles = [
// modern build with a rolling target using ES6 modules
'esm',
// build for node using commonJS modules
'node',
];

async function run(argv) {
const { bundle, outDir: relativeOutDir } = argv;
const env = {
NODE_ENV: 'production',
BABEL_ENV: bundle,
};
const babelConfigPath = path.resolve(__dirname, '../babel.config.js');
const srcDir = path.resolve('./src');
const outDir = path.resolve(
relativeOutDir,
// We generally support top level path imports e.g.
// 1. `import ArrowDownIcon from '@mui/icons-material/ArrowDown'`.
// 2. `import Typography from '@mui/material/Typography'`.
// The first case resolves to a file while the second case resolves to a package first i.e. a package.json
// This means that only in the second case the bundler can decide whether it uses ES modules or CommonJS modules.
// Different extensions are not viable yet since they require additional bundler config for users and additional transpilation steps in our repo.
// Switch to `exports` field in v6.
{
node: './',
esm: './esm',
}[bundle],
);
const babelArgs = ['--config-file', babelConfigPath, srcDir, '--out-dir', outDir];
const command = ['yarn babel', ...babelArgs].join(' ');
const { stderr, stdout } = await exec(command, { env: { ...process.env, ...env } });

if (stderr) {
throw new Error(`'${command}' failed with \n${stderr}`);
}
console.log(stdout);
}

yargs
.command({
command: '$0 <bundle>',
description: 'build package',
builder: command => {
return command
.positional('bundle', {
description: `Valid bundles: "${validBundles.join('" | "')}"`,
type: 'string',
})
.option('out-dir', { default: './', type: 'string' });
},
handler: run,
})
.help()
.strict(true)
.version(false)
.parse();
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17992,7 +17992,7 @@ yargs@^13.3.0, yargs@^13.3.2:
y18n "^4.0.0"
yargs-parser "^13.1.2"

yargs@^17.0.0:
yargs@^17.0.0, yargs@^17.5.1:
version "17.5.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.5.1.tgz#e109900cab6fcb7fd44b1d8249166feb0b36e58e"
integrity sha512-t6YAJcxDkNX7NFYiVtKvWUz8l+PaKTLiL63mJYWR2GnHq2gjEWISzsLp9wg3aY36dY1j+gfIEL3pIF+XlJJfbA==
Expand Down

0 comments on commit 849df84

Please sign in to comment.