Skip to content

Commit

Permalink
feat(new config): align the error message with what was done for app-…
Browse files Browse the repository at this point in the history
…boot in #1957
  • Loading branch information
BlueCutOfficial committed Jul 5, 2024
1 parent efa9f21 commit c42d4ce
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/compat/src/compat-app-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,20 +605,25 @@ export class CompatAppBuilder {

// This is the default script provided by https://github.com/ember-cli/ember-cli/blob/master/lib/utilities/ember-app-utils.js#L84
// When storeConfigInMeta is true, this content is always present in the config-module key of content-for.json
const defaultConfigModule = `var prefix = '${modulePrefix}';\ntry {\n var metaName = prefix + '/config/environment';\n var rawConfig = document.querySelector('meta[name=\"' + metaName + '\"]').getAttribute('content');\n var config = JSON.parse(decodeURIComponent(rawConfig));\n\n var exports = { 'default': config };\n\n Object.defineProperty(exports, '__esModule', { value: true });\n\n return exports;\n}\ncatch(err) {\n throw new Error('Could not read config from meta tag with name \"' + metaName + '\".');\n}\n`;
const defaultConfigModule =
`var prefix = '${modulePrefix}';\ntry {\n var metaName = prefix + '/config/environment';\n var rawConfig = document.querySelector('meta[name=\"' + metaName + '\"]').getAttribute('content');\n var config = JSON.parse(decodeURIComponent(rawConfig));\n\n var exports = { 'default': config };\n\n Object.defineProperty(exports, '__esModule', { value: true });\n\n return exports;\n}\ncatch(err) {\n throw new Error('Could not read config from meta tag with name \"' + metaName + '\".');\n}\n`.replace(
/\s/g,
''
);

const diff = contentForConfig['/index.html']['config-module'].replace(defaultConfigModule, '');
const configModule = contentForConfig['/index.html']['config-module'];
const diff = configModule.replace(/\s/g, '').replace(defaultConfigModule, '');

if (diff.length) {
console.warn(`
throw new Error(`
Your app uses at least one classic addon that provides content-for 'config-module'. This is no longer supported.
In classic builds, the following code was included in the config via content-for 'config-module':
${diff}
With Embroider, you have full control over the config module, so classic addons no longer need to modify it under the hood.
If the code above is still required, you should add it to your ${this.compatApp.name}/app/environment.js.
Once the required code is moved, you can remove the present warning by setting "useAddonConfigModule" to false in the build options.
The following code is included via content-for 'config-module':
${configModule}
1. If you want to keep the same behavior, add it to the app/environment.js.
2. Once app/environment.js has the content you need, remove the present error by setting "useAddonConfigModule" to false in the build options.
`);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/macro-sample-addon/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function(defaults) {

return maybeEmbroider(app, {
useAddonAppBoot: false,
useAddonConfigModule: false,
skipBabel: [
{
package: 'qunit',
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/macro-test/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ module.exports = function (defaults) {

return maybeEmbroider(app, {
useAddonAppBoot: false,
useAddonConfigModule: false,
});
};
38 changes: 38 additions & 0 deletions tests/scenarios/macro-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ dummyAppScenarios
module.exports = function(defaults) {
let app = new EmberAddon(defaults, {});
return maybeEmbroider(app, {
useAddonConfigModule: false,
useAddonAppBoot: true,
});
};
Expand All @@ -321,3 +322,40 @@ dummyAppScenarios
});
});
});

dummyAppScenarios
.map('macro-sample-addon-useAddonConfigModule', project => {
dummyAppScenarioSetup(project);
project.mergeFiles({
'ember-cli-build.js': `
'use strict';
const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');
const { maybeEmbroider } = require('@embroider/test-setup');
module.exports = function(defaults) {
let app = new EmberAddon(defaults, {});
return maybeEmbroider(app, {
useAddonConfigModule: true,
useAddonAppBoot: false,
});
};
`,
});
})
.forEachScenario(scenario => {
Qmodule(scenario.name, function (hooks) {
let addon: PreparedApp;

hooks.before(async () => {
addon = await scenario.prepare();
});

test(`pnpm test`, async function (assert) {
let result = await addon.execute('pnpm test');
assert.equal(result.exitCode, 1, 'tests exit with errors');
assert.true(
result.output.includes(`Your app uses at least one classic addon that provides content-for 'config-module'.`),
'the output contains the error message about migrating custom config-module code'
);
});
});
});

0 comments on commit c42d4ce

Please sign in to comment.