Skip to content

Commit

Permalink
chore: convert analyze to dynamic plugin in local.analyze env
Browse files Browse the repository at this point in the history
  • Loading branch information
jpina1-godaddy committed Jan 14, 2025
1 parent 0ee3568 commit 6710a41
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/create-gasket-app/lib/scaffold/config-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class ConfigBuilder {
* },
*/
addEnvironment(key, value) {
this.add(`environments`, {
this.add('environments', {
[key]: value
});
}
Expand Down
8 changes: 6 additions & 2 deletions packages/gasket-plugin-analyze/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ const { name, version } = require('../package.json');
* @type {import('@gasket/core').HookHandler<'create'>}
*/
module.exports = function create(gasket, { pkg, gasketConfig }) {
gasketConfig.addPlugin('pluginAnalyze', name);
gasketConfig.addEnvironment('local.analyze', {
dynamicPlugins: [
'@gasket/plugin-analyze'
]
});

pkg.add('devDependencies', {
[name]: `^${version}`
});

pkg.add('scripts', {
analyze: 'ANALYZE=true next build'
analyze: 'GASKET_ENV=local.analyze ANALYZE=1 next build'
});
};
6 changes: 5 additions & 1 deletion packages/gasket-plugin-analyze/lib/webpack-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ module.exports = function webpackConfigHook(gasket, webpackConfig, context) {
config: { bundleAnalyzerConfig: userConfig = {} }
} = gasket;

// Only add the analyzer plugin if ANALYZE flag is true
if (process.env.ANALYZE === 'true') {
console.warn("Deprecation Warning: Using 'true' for the ANALYZE environment variable is deprecated. Please use '1' instead.");
}

// Only add the analyzer plugin if ANALYZE flag is true
if (process.env.ANALYZE === 'true' || process.env.ANALYZE === '1') {
const merge = require('deepmerge');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const defaultConfig = require('./default-config');
Expand Down
12 changes: 8 additions & 4 deletions packages/gasket-plugin-analyze/test/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ describe('create', () => {

it('adds the analyze script and adds the plugin', async () => {
const add = jest.fn();
const addPlugin = jest.fn();
await create({}, { pkg: { add }, gasketConfig: { addPlugin } });
const addEnvironment = jest.fn();
await create({}, { pkg: { add }, gasketConfig: { addEnvironment } });
expect(add.mock.calls[0]).toEqual(['devDependencies', {
[name]: `^${version}`
}]);
expect(add.mock.calls[1]).toEqual(['scripts', {
analyze: 'ANALYZE=true next build'
analyze: 'GASKET_ENV=local.analyze ANALYZE=1 next build'
}]);
expect(addPlugin.mock.calls[0]).toEqual(['pluginAnalyze', name]);
expect(addEnvironment).toHaveBeenCalledWith('local.analyze', {
dynamicPlugins: [
'@gasket/plugin-analyze'
]
});
});
});

0 comments on commit 6710a41

Please sign in to comment.