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

Fix #176, apps could have different names configured in environment.js #235

Merged
merged 3 commits into from
Jun 13, 2024
Merged
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
54 changes: 49 additions & 5 deletions ember-scoped-css/src/lib/path/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,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
Expand Down Expand Up @@ -293,15 +293,59 @@ 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)) {
// eslint-disable-next-line no-undef -- this exists
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'));
Expand All @@ -310,5 +354,5 @@ function workspacePackageName(sourcePath) {

MANIFEST_CACHE.set(workspace, json);

return json.name;
return json;
}
Loading