Skip to content

Commit

Permalink
[dev-tools] bundle command always merges babel config (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress authored Oct 13, 2023
1 parent 7474511 commit 64376b5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
10 changes: 10 additions & 0 deletions modules/dev-tools/docs/api-reference/get-babel-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ module.exports = getBabelConfig({
debug: true
});
```

## Environments

The following environments may be used by various commands:

- `es5` - default commonjs entry point for non-ESM module used by `ocular-build`
- `esm` - default ESM entry point for non-ESM module used by `ocular-build`
- `esm-strict` - default ESM entry point for ESM module used by `ocular-build`
- `bundle` - production bundle settings used by `ocular-bundle`
- `bundle-dev` - developer bundle settings used by `ocular-bundle --env=dev`
31 changes: 14 additions & 17 deletions modules/dev-tools/src/configuration/esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ function getExternalGlobalsIIFE(externalPackages, mapping) {
}

/** Evaluate root babel config */
async function getBabelConfigProd(configPath, target) {
async function getBabelConfig(configPath, env, target) {
let config = await import(configPath);
if (config.default) {
config = config.default;
}
if (typeof config === 'function') {
config = config({
env: () => 'bundle'
env: () => env
});
}
if (target) {
config.presets.find((item) => item[0] === '@babel/env')[1].targets = target;
const envPreset = config.presets.find((item) => item[0] === '@babel/env');
if (target && envPreset) {
envPreset[1] = envPreset[1] || {};
envPreset[1].targets = target;
}
return config;
}
Expand Down Expand Up @@ -107,20 +109,15 @@ export default async function getBundleConfig(opts) {
sourcemap = false
} = opts;

let babelConfig;
if (devMode) {
babelConfig = {
filter: /src|bundle/,
config: {
presets: ['@babel/preset-typescript', '@babel/preset-react']
const babelConfig = devMode
? {
filter: /src|bundle/,
config: await getBabelConfig(ocularConfig.babel.configPath, 'bundle-dev', target)
}
};
} else {
babelConfig = {
filter: /src|bundle|esm/,
config: await getBabelConfigProd(ocularConfig.babel.configPath, target)
};
}
: {
filter: /src|bundle|esm/,
config: await getBabelConfig(ocularConfig.babel.configPath, 'bundle', target)
};

let externalPackages = Object.keys(packageInfo.peerDependencies || {});
if (typeof externals === 'string') {
Expand Down
7 changes: 4 additions & 3 deletions modules/dev-tools/src/configuration/get-babel-config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ const ENV_CONFIG = {
modules: false
}
]
],
plugins: [
'@babel/transform-runtime'
]
},

'bundle-dev': {
presets: [...COMMON_PRESETS]
}
};

// Ensure we have an entry for the default BABEL_ENV
Expand Down

0 comments on commit 64376b5

Please sign in to comment.