From beaa0f42f6c4b30d05018909401568a13f349482 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Thu, 13 Jun 2024 14:52:24 -0400 Subject: [PATCH 1/3] Fix #176, apps could have different names configured in environment.js --- ember-scoped-css/src/lib/path/utils.js | 56 +++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/ember-scoped-css/src/lib/path/utils.js b/ember-scoped-css/src/lib/path/utils.js index b8b0af38..1568358b 100644 --- a/ember-scoped-css/src/lib/path/utils.js +++ b/ember-scoped-css/src/lib/path/utils.js @@ -1,5 +1,6 @@ import assert from 'node:assert'; import fsSync from 'node:fs'; +import { createRequire } from 'node:module'; import path from 'node:path'; import findUp from 'find-up'; @@ -10,6 +11,8 @@ import { hashFromModulePath } from './hash-from-module-path.js'; export { hashFromAbsolutePath } from './hash-from-absolute-path.js'; export { hashFromModulePath } from './hash-from-module-path.js'; +const require = createRequire(import.meta.url); + const EMBROIDER_DIR = 'node_modules/.embroider'; const EMBROIDER_3_REWRITTEN_APP_PATH = `${EMBROIDER_DIR}/rewritten-app`; const EMBROIDER_3_REWRITTEN_APP_ASSETS = `${EMBROIDER_3_REWRITTEN_APP_PATH}/assets`; @@ -236,7 +239,7 @@ export function packageScopedPathToModulePath(packageScopedPath) { */ export function appPath(sourcePath) { let workspacePath = findWorkspacePath(sourcePath); - let name = workspacePackageName(sourcePath); + let name = moduleName(sourcePath); /** * Under embroider builds, the spec-compliant version of the app @@ -293,15 +296,58 @@ export function findWorkspacePath(sourcePath) { const MANIFEST_CACHE = new Map(); /** - * returns the package.json#name for a given sourcePath + * Will return the package.json#name, or config/environment#moudlePrefix (if v1 app) + * + * @param {string} sourcePath */ -function workspacePackageName(sourcePath) { +export function moduleName(sourcePath) { const workspace = findWorkspacePath(sourcePath); + const manifest = getManifest(workspace); + + if (manifest['ember-addon']) { + /** + * Libraries are not allowed to customize their module name + * to be different from their package name. + * + * (It's also bonkers that this is allowed for v1 apps) + */ + return manifest.name; + } + /** + * For v1 apps, we need to reference the config/environment.js file + * to see if the modulePrefix setting differs from manifest.name; + */ + let environmentJS = path.join(workspace, 'config/environment.js'); + + /** + * NOTE: the environment.js is a commonJS file, so we can synchronously require it + * + * TODO: if we have issues with loading this file later we should probably just run jscodeshift on it + * but that could allow us to be brittle in different ways, like if folks stray from the way the + * file is laid out from the blueprint defaults. + */ + if (fsSync.existsSync(environmentJS)) { + const envFn = require(environmentJS); + const env = envFn('ember-scoped-css'); + + return env.modulePrefix || manifest.name; + } + + /** + * Fallback to the true™ module™ name. + */ + return manifest.name; +} + +/** + * @param {string} workspace + */ +function getManifest(workspace) { let existing = MANIFEST_CACHE.get(workspace); if (existing) { - return existing.name; + return existing; } let buffer = fsSync.readFileSync(path.join(workspace, 'package.json')); @@ -310,5 +356,5 @@ function workspacePackageName(sourcePath) { MANIFEST_CACHE.set(workspace, json); - return json.name; + return json; } From fe3623f43fe5269628416584eb07b7445df2ec1f Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:05:44 -0400 Subject: [PATCH 2/3] fix --- ember-scoped-css/src/lib/path/utils.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/ember-scoped-css/src/lib/path/utils.js b/ember-scoped-css/src/lib/path/utils.js index 1568358b..151b5e2a 100644 --- a/ember-scoped-css/src/lib/path/utils.js +++ b/ember-scoped-css/src/lib/path/utils.js @@ -11,8 +11,6 @@ import { hashFromModulePath } from './hash-from-module-path.js'; export { hashFromAbsolutePath } from './hash-from-absolute-path.js'; export { hashFromModulePath } from './hash-from-module-path.js'; -const require = createRequire(import.meta.url); - const EMBROIDER_DIR = 'node_modules/.embroider'; const EMBROIDER_3_REWRITTEN_APP_PATH = `${EMBROIDER_DIR}/rewritten-app`; const EMBROIDER_3_REWRITTEN_APP_ASSETS = `${EMBROIDER_3_REWRITTEN_APP_PATH}/assets`; From a4bd3a879af235639a5d1e137a2256c89bf7719a Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:08:49 -0400 Subject: [PATCH 3/3] lints --- ember-scoped-css/src/lib/path/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ember-scoped-css/src/lib/path/utils.js b/ember-scoped-css/src/lib/path/utils.js index 151b5e2a..6d9265c9 100644 --- a/ember-scoped-css/src/lib/path/utils.js +++ b/ember-scoped-css/src/lib/path/utils.js @@ -1,6 +1,5 @@ import assert from 'node:assert'; import fsSync from 'node:fs'; -import { createRequire } from 'node:module'; import path from 'node:path'; import findUp from 'find-up'; @@ -326,6 +325,7 @@ export function moduleName(sourcePath) { * file is laid out from the blueprint defaults. */ if (fsSync.existsSync(environmentJS)) { + // eslint-disable-next-line no-undef -- this exists const envFn = require(environmentJS); const env = envFn('ember-scoped-css');