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

New config module - storeConfigInMeta #1953

Merged
merged 6 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
/packages/shared-internals/**/*.d.ts
/packages/compat/**/*.js
/packages/compat/**/*.d.ts
/packages/config-meta-loader/**/*.js
/packages/config-meta-loader/**/*.d.ts
/packages/macros/**/*.js
/packages/macros/**/*.d.ts
/packages/util/src/**/*.js
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
{
files: ['**/*.ts'],
parserOptions: {
project: './tsconfig.json',
project: true,
},
rules: {
'@typescript-eslint/consistent-type-imports': 'error',
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/packages/shared-internals/**/*.d.ts
/packages/compat/**/*.js
/packages/compat/**/*.d.ts
/packages/config-meta-loader/**/*.js
/packages/config-meta-loader/**/*.d.ts
/packages/macros/**/*.js
/packages/macros/**/*.d.ts
/packages/util/src/**/*.js
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
},
"scripts": {
"clean": "git clean -x -f",
"compile": "tsc",
"dev": "tsc --watch",
"compile": "tsc -b",
"dev": "tsc -b --watch",
"lint": "concurrently 'npm:lint:*(!fix)' --names 'lint:'",
"lint:fix": "concurrently 'npm:lint:*:fix' --names 'fix:'",
"lint:eslint": "eslint . --cache --report-unused-disable-directives",
"lint:eslint:fix": "eslint . --fix --report-unused-disable-directives",
"lint:prettier": "prettier . -c",
"lint:prettier:fix": "prettier . -w",
"lint:router-types": "cd packages/router && pnpm lint:types",
"prepare": "tsc && pnpm build-v2-addons",
"prepare": "tsc -b && pnpm build-v2-addons",
"build-v2-addons": "concurrently 'pnpm:build-v2-addons:*'",
"build-v2-addons:router": "cd packages/router && pnpm build",
"test": "cd tests/scenarios && pnpm test"
Expand Down Expand Up @@ -71,4 +71,4 @@
},
"wildcardLabel": "unlabeled"
}
}
}
10 changes: 1 addition & 9 deletions packages/compat/src/compat-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import mergeTrees from 'broccoli-merge-trees';
import { WatchedDir } from 'broccoli-source';
import resolve from 'resolve';
import ContentForConfig from './content-for-config';
import { V1Config, WriteV1Config } from './v1-config';
import { V1Config } from './v1-config';
import { WriteV1AppBoot, ReadV1AppBoot } from './v1-appboot';
import type { AddonMeta, EmberAppInstance, OutputFileToInputFileMap, PackageInfo } from '@embroider/core';
import { writeJSONSync, ensureDirSync, copySync, pathExistsSync, existsSync, writeFileSync } from 'fs-extra';
Expand Down Expand Up @@ -690,21 +690,13 @@ export default class CompatApp {
let appTree = this.appTree;
let testsTree = this.testsTree;
let lintTree = this.lintTree;
let config = new WriteV1Config(this.config, this.storeConfigInMeta, this.testConfig);
let patterns = this.configReplacePatterns;
let configReplaced = new this.configReplace(config, this.configTree, {
configPath: join('environments', `${this.legacyEmberAppInstance.env}.json`),
files: ['config/environment.js'],
patterns,
});

let trees: BroccoliNode[] = [];
trees.push(appTree);
trees.push(
new SynthesizeTemplateOnlyComponents(appTree, { allowedPaths: ['components'], templateExtensions: ['.hbs'] })
);

trees.push(configReplaced);
if (testsTree) {
trees.push(testsTree);
}
Expand Down
49 changes: 1 addition & 48 deletions packages/compat/src/v1-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Plugin from 'broccoli-plugin';
import type { Node } from 'broccoli-node-api';
import { join } from 'path';
import { readFileSync, outputFileSync } from 'fs-extra';
import { readFileSync } from 'fs-extra';

export interface ConfigContents {
modulePrefix: string;
Expand All @@ -26,50 +26,3 @@ export class V1Config extends Plugin {
return this.lastConfig;
}
}

export class WriteV1Config extends Plugin {
private lastContents: string | undefined;
constructor(private inputTree: V1Config, private storeConfigInMeta: boolean, private testInputTree?: V1Config) {
super([inputTree, testInputTree as V1Config].filter(Boolean), {
persistentOutput: true,
needsCache: false,
});
}
build() {
let filename = join(this.outputPath, 'config/environment.js');
let contents;
if (this.storeConfigInMeta) {
contents = metaLoader();
} else {
if (this.testInputTree) {
contents = `
import { isTesting } from '@embroider/macros';
let env;
if (isTesting()) {
env = ${JSON.stringify(this.testInputTree.readConfig())};
} else {
env = ${JSON.stringify(this.inputTree.readConfig())};
}
export default env;
`;
} else {
contents = `export default ${JSON.stringify(this.inputTree.readConfig())};`;
}
}
if (!this.lastContents || this.lastContents !== contents) {
outputFileSync(filename, contents);
}
this.lastContents = contents;
}
}

function metaLoader() {
// Supporting config content as JS Module.
// Wrapping the content with immediate invoked function as
// replaced content for config-module was meant to support AMD module.
return `
export default (function() {
{{content-for 'config-module'}}
})().default;
`;
}
8 changes: 8 additions & 0 deletions packages/config-meta-loader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/node_modules
/src/**/*.js
/src/**/*.d.ts
/src/**/*.map
/*/tests/**/*.js
/*/tests/**/*.d.ts
/*/tests/**/*.map
*.tsbuildinfo
28 changes: 28 additions & 0 deletions packages/config-meta-loader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@embroider/config-meta-loader",
"version": "1.0.0",
"private": false,
"type": "module",
"description": "Read the config meta in the document. Inspired by ember-cli/lib/broccoli/app-config-from-meta.js",
"repository": {
"type": "git",
"url": "https://github.com/embroider-build/embroider.git",
"directory": "packages/config-meta-loader"
},
"license": "MIT",
"author": "Marine Dunstetter",
"main": "src/index.js",
"files": [
"src/**/*.js",
"src/**/*.d.ts",
"src/**/*.js.map"
],
"scripts": {},
"dependencies": {},
"devDependencies": {
"typescript": "^5.4.5"
},
"engines": {
"node": "12.* || 14.* || >= 16"
}
}
11 changes: 11 additions & 0 deletions packages/config-meta-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// from https://github.com/ember-cli/ember-cli/blob/master/lib/broccoli/app-config-from-meta.js
export default function loadConfigFromMeta(prefix: string): any {
let metaName = `${prefix}/config/environment`;
try {
let rawConfig = document.querySelector(`meta[name="${metaName}"]`)!.getAttribute('content') ?? '{}';
let config = JSON.parse(decodeURIComponent(rawConfig));
return config;
} catch (err) {
return `Could not read config from meta tag with name "${metaName}".`;
}
}
22 changes: 22 additions & 0 deletions packages/config-meta-loader/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"include": [
"src/**/*.ts",
],
"compilerOptions": {
"composite": true,
"target": "es2019",
"module": "ESNext",
"declaration": true,
"typeRoots": ["types", "node_modules/@types"],
"esModuleInterop": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"allowUnreachableCode": false,
"strict": true,
"skipLibCheck": true,
"useUnknownInCatchVariables": false,
},
}
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/addon-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"test:ember-compatibility": "ember try:each"
},
"dependencies": {
"@embroider/config-meta-loader": "workspace:*",
"ember-cli-babel": "^7.26.11",
"ember-cli-htmlbars": "^6.1.0"
},
Expand Down
3 changes: 3 additions & 0 deletions tests/addon-template/tests/dummy/app/config/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import loadConfigFromMeta from '@embroider/config-meta-loader';

export default loadConfigFromMeta('dummy');
3 changes: 3 additions & 0 deletions tests/app-template/app/config/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import loadConfigFromMeta from '@embroider/config-meta-loader';

export default loadConfigFromMeta('app-template');
1 change: 1 addition & 0 deletions tests/app-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.0.3",
"@embroider/compat": "workspace:*",
"@embroider/config-meta-loader": "workspace:*",
"@embroider/core": "workspace:*",
"@embroider/router": "workspace:*",
"@embroider/test-setup": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import loadConfigFromMeta from '@embroider/config-meta-loader';

let config = loadConfigFromMeta('dummy');
config.APP.fromConfigModule = 'hello new world';

export default config;
3 changes: 3 additions & 0 deletions tests/scenarios/macro-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ function dummyAppScenarioSetup(project: Project) {
this._super.included.apply(this, arguments);
},
contentFor(type, config, contents) {
// This if block is used only by macro-sample-addon-classic, which does not build with Embroider.
// When building with Embroider, the responsibility for the config is moved to the app.
// Therefore, macro-sample-addon scenario defines 'fromConfigModule' differently using fixtures.
if (type === 'config-module') {
const originalContents = contents.join('');
contents.splice(0, contents.length);
Expand Down
3 changes: 3 additions & 0 deletions tests/ts-app-template-classic/app/config/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import loadConfigFromMeta from '@embroider/config-meta-loader';

export default loadConfigFromMeta('ts-app-template');
1 change: 1 addition & 0 deletions tests/ts-app-template-classic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0",
"@embroider/compat": "workspace:*",
"@embroider/config-meta-loader": "workspace:*",
"@embroider/core": "workspace:*",
"@embroider/router": "workspace:*",
"@embroider/test-setup": "workspace:*",
Expand Down
3 changes: 3 additions & 0 deletions tests/ts-app-template/app/config/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import loadConfigFromMeta from '@embroider/config-meta-loader';

export default loadConfigFromMeta('ts-app-template');
1 change: 1 addition & 0 deletions tests/ts-app-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0",
"@embroider/compat": "workspace:*",
"@embroider/config-meta-loader": "workspace:*",
"@embroider/core": "workspace:*",
"@embroider/router": "workspace:*",
"@embroider/test-setup": "workspace:*",
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"test-packages/*/node_modules",
"tmp",
"tests/scenarios/output",
"packages/config-meta-loader/src",
"packages/router/src"
],
"references": [
{ "path": "packages/config-meta-loader" }
]
}
Loading