Skip to content
New issue

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

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

Already on GitHub? # to your account

Refactor to use source-map-explorer, enabling usage with Ember CLI, ember-auto-import and Embroider #26

Merged
merged 19 commits into from
Feb 8, 2023
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
31 changes: 10 additions & 21 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 2018,
ecmaVersion: 2020,
},
plugins: [
'node'
],
extends: [
'eslint:recommended',
'plugin:node/recommended'
],
plugins: ['node'],
extends: ['eslint:recommended', 'plugin:node/recommended'],
env: {
node: true
},
rules: {
node: true,
},
rules: {},
overrides: [
// mocha tests
{
files: [
'node-tests/**/*.js'
],
plugins: [
'chai-expect',
'mocha',
],
files: ['node-tests/**/*.js'],
plugins: ['chai-expect', 'mocha'],
env: {
mocha: true
mocha: true,
},
}
]
},
],
};
27 changes: 24 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ concurrency:

jobs:
lint:
name: "Lint"
name: 'Lint'
runs-on: ubuntu-latest
timeout-minutes: 10

Expand All @@ -29,8 +29,8 @@ jobs:
- name: Lint
run: yarn lint

test:
name: "Test"
test_classic:
name: 'Test Ember CLI + ember-auto-import'
runs-on: ubuntu-latest
timeout-minutes: 10

Expand All @@ -45,3 +45,24 @@ jobs:
run: yarn install --frozen-lockfile
- name: Run Tests
run: yarn test

test_embroider:
name: 'Test Embroider'
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- uses: actions/checkout@v3
- name: Install Node
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: yarn
- name: Install Dependencies
run: yarn install --frozen-lockfile
- name: Install Embroider
run: yarn add -D @embroider/core @embroider/compat @embroider/webpack
- name: Run Tests
run: yarn test
env:
EMBROIDER_TEST_SETUP_OPTIONS: optimized
197 changes: 164 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,199 @@
[![Ember Observer Score](https://emberobserver.com/badges/ember-cli-bundle-analyzer.svg)](https://emberobserver.com/addons/ember-cli-bundle-analyzer)
[![npm version](https://badge.fury.io/js/ember-cli-bundle-analyzer.svg)](https://badge.fury.io/js/ember-cli-bundle-analyzer)

An Ember CLI addon to analyze the size and contents of your app's bundled output,
using an interactive zoomable treemap.
An Ember CLI addon to analyze the size and contents of your app's bundled output, using an interactive zoomable treemap.

View the [interactive Demo](https://cdn.rawgit.com/simonihmig/ember-cli-bundle-analyzer/bceb55a7/docs/demo.html)
View the [interactive Demo](https://raw.githack.com/simonihmig/ember-cli-bundle-analyzer/master/docs/demo.html)

![Screenshot of analyzer output](docs/screen.png)

This helps you to

* analyze which individual modules make it into your final bundle
* find out how big each contained module is, including the raw source, minified and gzipped sizes
* find modules that got there by mistake
* optimize your bundle size
- analyze which individual modules make it into your final bundle
- find out how big each contained module is
- find modules that got there by mistake
- optimize your bundle size

It uses [source-map-explorer](https://github.com/danvk/source-map-explorer) under the hood,
and wraps it in an Ember CLI addon to make it easy to use.

It uses [broccoli-concat-analyser](https://github.com/stefanpenner/broccoli-concat-analyser) under the hood,
which in turn was inspired by
[webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer),
and wraps it in Ember CLI addon to make it easy to use.

It uses [broccoli-concat-analyser](https://github.com/stefanpenner/broccoli-concat-analyser) under the hood,
which in turn was inspired by
[webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer),
and wraps it in Ember CLI addon to make it easy to use.
Given that it works based on source maps, it is agnostic to the actual bundling tool used, be it pure Ember CLI (which is based on `broccoli-concat`), Ember CLI with ember-auto-import (which makes it a mix of `broccoli-concat` and `webpack`) or even Embroider.
The only condition is that _proper_ source maps are generated.

## Compatibility

* Ember CLI v3.28 or above
* Node.js v16 or above
- Ember CLI v3.28 or above
- Node.js v16 or above
- works with ember-auto-import and Embroider, as long as source maps are properly enabled

## Quick Start

To get you going _quickly_, follow these steps:

1. Install the addon:

```
ember install ember-cli-bundle-analyzer
```

2. Setup your `ember-cli-build.js`:

```diff
const EmberApp = require('ember-cli/lib/broccoli/ember-app');
+const {
+ createEmberCLIConfig,
+} = require('ember-cli-bundle-analyzer/create-config');

module.exports = function (defaults) {
const app = new EmberApp(defaults, {
// your other options are here
// ...
+ ...createEmberCLIConfig(),
});

return app.toTree();
};
```

3. Launch your local server in analyze-mode:

```sh
ENABLE_BUNDLE_ANALYZER=true ember serve --prod
```

4. Open [http://localhost:4200/\_analyze](http://localhost:4200/_analyze)

For more detailed instructions, read the following chapters...

## Setup

Two things need to be in place for this addon to work correctly: it needs to have a [config](#Config) that at least enables it, and _proper_ [source maps](#source-maps) need to be enabled for the EmberCLI build pipeline.

While it is possible to provide these settings manually, the addon provides some recommended presets that you can apply using `createEmberCLIConfig`:

```js
// ember-cli-build.js
const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const {
createEmberCLIConfig,
} = require('ember-cli-bundle-analyzer/create-config');

module.exports = function (defaults) {
const app = new EmberApp(defaults, {
// your other options are here
// ...
...createEmberCLIConfig(),
});

## Installation
return app.toTree();
};
```

If you are using Embroider, then this should cover you:

```js
// ember-cli-build.js
const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const {
createEmberCLIConfig,
createWebpackConfig,
} = require('ember-cli-bundle-analyzer/create-config');

module.exports = function (defaults) {
const app = new EmberApp(defaults, {
// your other options are here
// ...
...createEmberCLIConfig(),
});

return require('@embroider/compat').compatBuild(app, Webpack, {
// your Embroider options...
packagerOptions: {
webpackConfig: {
// any custom webpack options you might have
...createWebpackConfig(),
},
},
});
};
```
ember install ember-cli-bundle-analyzer

With this config, when the environment variable `ENABLE_BUNDLE_ANALYZER` is set, it will enable the addon and apply required options to both Ember CLI and ember-auto-import to fully enable source maps with the required fidelity level.

> The reason the addon is not enabled by default is that it would be pointless if not having proper source maps fully enabled as well. And this might not be what you want by default, as generating full source maps comes at a cost of increased build times. On top of that you might also want to analyze the bundles only in production mode.

Note that this might override some of your existing custom settings for source maps or ember-auto-import if you have those. So if that does not work for you, you can either try to deep merge the configs as in the following example, or provide your own explicit configs based of what [`createEmberCLIConfig` provides](./create-config.js).

```js
// ember-cli-build.js
const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const {
createEmberCLIConfig,
} = require('ember-cli-bundle-analyzer/create-config');
const { defaultsDeep } = require('ember-cli-lodash-subset');

module.exports = function (defaults) {
const app = new EmberApp(
defaults,
defaultsDeep(
{
// your other options are here
// ...
},
createEmberCLIConfig()
)
);

return app.toTree();
};
```

## Usage
### Config

After you have started your development server using `ember serve`, this addon adds a custom middleware listening to
`/_analyze`. So just open `http://localhost:4200/_analyze` in your web browser to access the analyzer output.
You can customize the precessing by setting any of the following configs into the `'bundleAnalyzer'` key of your
`ember-cli-build.js`:

While it processes the data, which can take a while due to live minification and compression of all involved modules,
a loading screen is displayed. After processing has finished you should see the final output.
- `enabled` (boolean): set to `true` to enable the addon. `false` by default.

Live reloading is supported, so whenever you change a project file the output will be re-computed and updated.
- `ignoreTestFiles` (boolean): by default it will exclude all test files from the output. Set this to `false` to include
them.

### Options
- `ignore` (string | string[]): add files to ignore. Glob patterns are supported, e.g. `*-fastboot.js`.

You can customize the precessing by setting any of the following options into the `'bundle-analyzer'` key of your
`ember-cli-build.js`:
### Source maps

* `ignoreTestFiles` (boolean): by default it will exclude all test files from the output. Set this to `false` to include
them.
The bundle size output that you see coming from this addon can only be as good as the underlying source maps are. For [source-map-explorer](https://github.com/danvk/source-map-explorer) to correctly process those, they should have the full source code included inline (which is the `sourcesContent` field in a `.map` file).

* `ignore` (string | string[]): add files to ignore. Glob patterns are supported, e.g. `*-fastboot.js`.
For the `broccoli-concat` based part of Ember CLI (processing the app itself as well as all v1 addons), enabling source maps using `sourcemaps: { enabled: true },` in `ember-cli-config.js` will be enough.
For any `webpack` based build (be it ember-auto-import or Embroider), the default settings will _not_ be enough. The only setting that works reliably is enabling high-fidelity source maps using `devtool: 'source-map'`.

Again, for enabling source maps with the recommended options, using the `createEmberCLIConfig()` helper as mentioned above is recommended.

For further information consider these resources:

- [Enabling source maps in Ember CLI](https://cli.emberjs.com/release/advanced-use/asset-compilation/#sourcemaps)
- [Adding custom webpack config to ember-auto-import](https://github.com/ef4/ember-auto-import#customizing-build-behavior)
- [Adding custom webpack config to Embroider](https://github.com/embroider-build/embroider#options)
- [webpack's devtool](https://webpack.js.org/configuration/devtool/)

## Usage

You need to have the addon and source maps enabled as described under [Setup](#setup). When following the recommended approach of using `createEmberCLIConfig` to apply the addon provided presets, then opting into the bundle-analyzer enabled mode is done by enabling the `ENABLE_BUNDLE_ANALYZER` environment flag when starting your local dev server. Most likely you will also want to analyze the production mode build, to exclude any debugging code. So the final invocation would be:

```sh
ENABLE_BUNDLE_ANALYZER=true ember serve --prod
```

After startup this addon adds a custom middleware listening to the `/_analyze` URL path. So just open [http://localhost:4200/\_analyze](http://localhost:4200/_analyze) in your web browser to access the analyzer output.

While it processes the data a loading screen might appear, after which the final output should display.

Live reloading is supported, so whenever you change a project file the output will be re-computed and updated automatically.

## Contributing

See the [Contributing](CONTRIBUTING.md) guide for details.


## License

This project is licensed under the [MIT License](LICENSE.md).
44 changes: 44 additions & 0 deletions create-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function createEmberCLIConfig(forceEnabled = undefined, ownConfig = {}) {
const isEnabled = forceEnabled ?? !!process.env.ENABLE_BUNDLE_ANALYZER;

return isEnabled
? {
bundleAnalyzer: {
enabled: true,
...ownConfig,
},
fingerprint: {
enabled: false,
},
sourcemaps: { enabled: true, extensions: ['js', 'css'] },
autoImport: {
webpack: createWebpackConfig(isEnabled),
},
}
: {};
}

function createWebpackConfig(forceEnabled) {
const isEnabled = forceEnabled ?? !!process.env.ENABLE_BUNDLE_ANALYZER;

return isEnabled
? {
devtool: 'source-map',
optimization: {
chunkIds: 'named',
},
output: {
clean: true,
// Ideally we would tweak the webpack chunk names here like below, to give the user some more understandable names that just random IDs.
// But the problem is that ember-auto-import and Embroider don't allow for the same config to work for both.
// filename: 'assets/chunk.[id].js',
// chunkFilename: 'assets/chunk.[id].js',
},
}
: {};
}

module.exports = {
createEmberCLIConfig,
createWebpackConfig,
};
Loading