Skip to content

Commit

Permalink
Introduce config/addons.js for addon macro-config
Browse files Browse the repository at this point in the history
  • Loading branch information
simonihmig committed Oct 16, 2024
1 parent 81af886 commit 5d4173d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/macros/src/macros-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ export default class MacrosConfig {
// true to distinguish the two.
isTesting: false,
};

this.loadAddonConfigFile();
}

private loadAddonConfigFile() {
let addonConfig: Record<string, object>;

try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
addonConfig = require(join(this.appRoot, 'config', 'addons'));
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
addonConfig = {};
}

for (const [packageName, config] of Object.entries(addonConfig)) {
this.setConfig(this.appRoot, packageName, config);
}
}

private get packageCache() {
Expand Down
75 changes: 75 additions & 0 deletions packages/macros/tests/babel/addon-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { allBabelVersions } from '@embroider/test-support';
import { MacrosConfig } from '../../src/node';
import { Project } from 'scenario-tester';
import { makeBabelConfig, makeRunner } from './helpers';
import { dirname, join } from 'path';

describe(`addon-config`, function () {
let project: Project;
let addon: Project;
let config: MacrosConfig;
let filename: string;
let run: ReturnType<typeof makeRunner>;

allBabelVersions({
babelConfig(version: number) {
let c = makeBabelConfig(version, config);
c.filename = filename;
return c;
},
createTests: function (transform) {
beforeEach(function () {
project = new Project('app', '1.0.0', {
files: {
config: {
'addons.js': `'use strict';
module.exports = {
'v2-addon': {
enabled: true
},
};`,
},
},
});

addon = Project.fromDir(dirname(require.resolve('../../../../tests/v2-addon-template/package.json')), {
linkDeps: true,
});
addon.pkg.name = 'v2-addon';
addon.addDependency('@embroider/macros');

project.addDependency(addon);
project.write();

config = MacrosConfig.for({}, project.baseDir);
filename = join(addon.baseDir, 'sample.js');

run = makeRunner(transform);
});

test('reads config from config/addon.js', () => {
config.finalize();
let code = transform(`
import { getOwnConfig } from '@embroider/macros';
export default function() {
return getOwnConfig();
}
`);
expect(run(code, { filename })).toEqual({ enabled: true });
});

test('overriddes config from addon', () => {
config.setOwnConfig(filename, { enabled: false, other: true });
config.finalize();
let code = transform(`
import { getOwnConfig } from '@embroider/macros';
export default function() {
return getOwnConfig();
}
`);
expect(run(code, { filename })).toEqual({ enabled: true, other: true });
});
},
});
});
7 changes: 7 additions & 0 deletions tests/fixtures/macro-test/config/addons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
'macro-sample-addon': {
configFromConfigFile: 'got it'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module('Integration | cross-package-config', function (hooks) {
shouldBeOverwritten: 'overwritten',
configFromAddonItself: 'this is the addon',
configFromMacrosTests: 'exists',
configFromConfigFile: 'got it',
});
})
);
Expand All @@ -28,6 +29,7 @@ module('Integration | cross-package-config', function (hooks) {
shouldBeOverwritten: 'overwritten',
configFromAddonItself: 'this is the addon',
configFromMacrosTests: 'exists',
configFromConfigFile: 'got it',
});
});

Expand All @@ -40,6 +42,7 @@ module('Integration | cross-package-config', function (hooks) {
shouldBeOverwritten: 'overwritten',
configFromAddonItself: 'this is the addon',
configFromMacrosTests: 'exists',
configFromConfigFile: 'got it',
});
})
);
Expand All @@ -55,6 +58,7 @@ module('Integration | cross-package-config', function (hooks) {
shouldBeOverwritten: 'overwritten',
configFromAddonItself: 'this is the addon',
configFromMacrosTests: 'exists',
configFromConfigFile: 'got it',
});
})
);
Expand Down

0 comments on commit 5d4173d

Please sign in to comment.