Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve build to reduce bundle size #2430

Merged
merged 4 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
};
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
"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",
"clean": "rm -Rf components && rm -Rf libs && rm -Rf styles",
"clean": "rm -Rf components && rm -Rf libs && rm -Rf styles && rm -Rf esm",
"lint:js": "eslint src",
"lint:styles": "stylelint './src/**/*.js'",
"lint": "yarn lint:js && yarn lint:styles",
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
57 changes: 57 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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,
{
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