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

feat: added addEnvironment to ConfigBuilder #1010

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
19 changes: 19 additions & 0 deletions packages/create-gasket-app/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ export interface ConfigBuilder<Config> {
*/
addPlugin(pluginImport: string, pluginName: string): void;

/**
* addEnvironment - Add environments to the gasket file and use the value in the plugins array
* @param {string} key - name of the environment - `local.analyze`
* @param {object} value - configuration for the environment - `{
* dynamicPlugins: [
* '@gasket/plugin-analyze',
* ]
* }`
* @example
* environments: {
* 'local.analyze': {
* dynamicPlugins: [
* '@gasket/plugin-analyze',
* ]
* }
* },
*/
addEnvironment(key:string, value: object): void;

/**
* addImport - Add a non-plugin import to the gasket file
* @param {string} importName - name of the import used as a value - `import fs...`
Expand Down
31 changes: 31 additions & 0 deletions packages/create-gasket-app/lib/scaffold/config-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,37 @@ export class ConfigBuilder {
this.add('pluginImports', { [pluginImport]: pluginName });
}

/**
* addEnvironment - Add environments to the gasket file
* @param {string} key - name of the environment - `local.analyze`
* @param {object} value - configuration for the environment - `{
* dynamicPlugins: [
* '@gasket/plugin-analyze',
* ]
* }`
* @example
* environments: {
* 'local.analyze': {
* dynamicPlugins: [
* '@gasket/plugin-analyze',
* ]
* }
* },
*/
addEnvironment(key, value) {
this.add('environments', {
[key]: value
});
}

/**
* addDynamicPlugin - Add plugin to the the dynamicPlugins array
* @param {string} pluginName - Name of the plugin to add to the dynamicPlugins array - `@gasket/plugin-example`
*/
addDynamicPlugin(pluginName) {
this.add('dynamicPlugins', [`${pluginName}`]);
}

/**
* addImport - Add a non-plugin import to the gasket file
* @param {string} importName - name of the import used as a value - `import fs...`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function makeCreateContext(argv = [], options = {}) {
/**
* Input context which will be appended by prompts and passed to create hooks
* @type {import('../internal').PartialCreateContext}
*/
*/
const context = new CreateContext({
destOverride: true,
cwd,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,46 @@ describe('ConfigBuilder', () => {
expect(warnings).toHaveLength(1);
});
});

describe('gasketConfig', () => {
let gasketConfig;
beforeEach(() => {
gasketConfig = new ConfigBuilder();
});

describe('.addDynamicPlugin', () => {
it('adds a dynamic plugin to gasketConfig', () => {
gasketConfig.addDynamicPlugin('pluginOne');
expect(gasketConfig.fields.dynamicPlugins).toEqual(['pluginOne']);
});

it('adds multiple dynamic plugins to gasketConfig', () => {
gasketConfig.addDynamicPlugin('pluginOne');
gasketConfig.addDynamicPlugin('pluginTwo');
expect(gasketConfig.fields.dynamicPlugins).toEqual(['pluginOne', 'pluginTwo']);
});
});

describe('.addEnvironment', () => {
it('adds an environment to gasketConfig', () => {
gasketConfig.addEnvironment('test.env', { test: 'config ' });
expect(gasketConfig.fields.environments).toEqual({ 'test.env': { test: 'config ' } });
});

it('adds values to an environment in gasketConfig', () => {
gasketConfig.addEnvironment('test.env', { test: 'config ' });
gasketConfig.addEnvironment('test.env', { test2: 'config 2' });
expect(gasketConfig.fields.environments).toEqual({ 'test.env': { test: 'config ', test2: 'config 2' } });
});

it('adds multiple environments to gasketConfig', () => {
gasketConfig.addEnvironment('test.env', { test: 'config ' });
gasketConfig.addEnvironment('prod.env', { prod: 'config ' });
expect(gasketConfig.fields.environments).toEqual({
'test.env': { test: 'config ' },
'prod.env': { prod: 'config ' }
});
});
});
});
});
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.");
}
// Run the analyzer plugin if the ANALYZE flag is true or 1
// @deprecated: 'true' will be removed in a future release
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'
]
});
});
});
Loading