Skip to content

Commit

Permalink
Allow for looking up modules using old name paths.
Browse files Browse the repository at this point in the history
If an app is referencing a CSS Module using its package name instead of
the moduleName, show a warning and remap to the updated path.
  • Loading branch information
timlindvall committed Jan 28, 2021
1 parent 5833a5b commit 36f19bb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/ember-css-modules/lib/modules-preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ module.exports = class ModulesPreprocessor {
this._modulesTree = null;
this._modulesBasePath = null;
this._modulesBridge = new Bridge();

/*
* The addon name we should use to look up modules.
* Uses moduleName if defined, else uses the parent/package name.
*/
this._ownerName = null;

/*
* The parent/package name. This is used as a fallback to look up
* paths that may reference the package name instead of the
* module name. (See resolve-path.js)
*/
this._parentName = null;
}

Expand All @@ -41,13 +53,14 @@ module.exports = class ModulesPreprocessor {
// If moduleName is defined, that should override the parent's name.
// Otherwise, the template and generated module will disagree as to what the path should be.
let ownerParent = this.owner.getParent();
let parentName = ownerParent.moduleName ? ownerParent.moduleName() : this.owner.getParentName();
this._parentName = parentName;
this._parentName = this.owner.getParentName();
let ownerName = ownerParent.moduleName ? ownerParent.moduleName() : this._parentName;
this._ownerName = ownerName;

let modulesSources = new ModuleSourceFunnel(inputRoot, modulesInput, {
include: ['**/*.' + this.owner.getFileExtension()],
outputRoot,
parentName,
parentName: ownerName,
});

let modulesTree = new (require('broccoli-css-modules'))(modulesSources, {
Expand Down Expand Up @@ -191,10 +204,12 @@ module.exports = class ModulesPreprocessor {

return this._resolvePath(importPath, fromFile, {
defaultExtension: this.owner.getFileExtension(),
ownerName: this._parentName,
ownerName: this._ownerName,
parentName: this._parentName,
addonModulesRoot: this.owner.getAddonModulesRoot(),
root: ensurePosixPath(this._modulesTree.inputPaths[0]),
parent: this.owner.getParent()
parent: this.owner.getParent(),
ui: this.owner.ui
});
}
};
Expand Down
14 changes: 14 additions & 0 deletions packages/ember-css-modules/lib/resolve-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ module.exports = function resolvePath(importPath, fromFile, options) {
return resolveRelativePath(pathWithExtension, fromFile, options);
} else if (isLocalPath(pathWithExtension, options)) {
return resolveLocalPath(pathWithExtension, fromFile, options);
} else if (isLocalPathWithOldPackageNameRef(pathWithExtension, options)) {
const amendedPathWithExtension = pathWithExtension.replace(options.parentName, options.ownerName);
options.ui.writeWarnLine(
'For addons that define a moduleName, you should reference any CSS Modules provided by that addon ' +
'using its moduleName instead of the package name.\n' +
'Current path: ' + importPath + '\n' +
'Replace with: ' + importPath.replace(options.parentName, options.ownerName) + '\n' +
'File: ' + fromFile
);
return resolveLocalPath(amendedPathWithExtension, fromFile, options);
} else {
return resolveExternalPath(pathWithExtension, importPath, fromFile, options);
}
Expand All @@ -23,6 +33,10 @@ function isLocalPath(importPath, options) {
return importPath.indexOf(options.ownerName + '/') === 0;
}

function isLocalPathWithOldPackageNameRef(importPath, options) {
return (options.ownerName !== options.parentName) && importPath.indexOf(options.parentName + '/') === 0;
}

function resolveRelativePath(importPath, fromFile, options) {
let absolutePath = ensurePosixPath(path.resolve(path.dirname(fromFile), importPath));
return internalDep(absolutePath, options);
Expand Down

0 comments on commit 36f19bb

Please sign in to comment.