Skip to content

Commit 36f19bb

Browse files
author
Tim Lindvall
committed
Allow for looking up modules using old name paths.
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.
1 parent 5833a5b commit 36f19bb

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

packages/ember-css-modules/lib/modules-preprocessor.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ module.exports = class ModulesPreprocessor {
1515
this._modulesTree = null;
1616
this._modulesBasePath = null;
1717
this._modulesBridge = new Bridge();
18+
19+
/*
20+
* The addon name we should use to look up modules.
21+
* Uses moduleName if defined, else uses the parent/package name.
22+
*/
23+
this._ownerName = null;
24+
25+
/*
26+
* The parent/package name. This is used as a fallback to look up
27+
* paths that may reference the package name instead of the
28+
* module name. (See resolve-path.js)
29+
*/
1830
this._parentName = null;
1931
}
2032

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

4760
let modulesSources = new ModuleSourceFunnel(inputRoot, modulesInput, {
4861
include: ['**/*.' + this.owner.getFileExtension()],
4962
outputRoot,
50-
parentName,
63+
parentName: ownerName,
5164
});
5265

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

192205
return this._resolvePath(importPath, fromFile, {
193206
defaultExtension: this.owner.getFileExtension(),
194-
ownerName: this._parentName,
207+
ownerName: this._ownerName,
208+
parentName: this._parentName,
195209
addonModulesRoot: this.owner.getAddonModulesRoot(),
196210
root: ensurePosixPath(this._modulesTree.inputPaths[0]),
197-
parent: this.owner.getParent()
211+
parent: this.owner.getParent(),
212+
ui: this.owner.ui
198213
});
199214
}
200215
};

packages/ember-css-modules/lib/resolve-path.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ module.exports = function resolvePath(importPath, fromFile, options) {
1010
return resolveRelativePath(pathWithExtension, fromFile, options);
1111
} else if (isLocalPath(pathWithExtension, options)) {
1212
return resolveLocalPath(pathWithExtension, fromFile, options);
13+
} else if (isLocalPathWithOldPackageNameRef(pathWithExtension, options)) {
14+
const amendedPathWithExtension = pathWithExtension.replace(options.parentName, options.ownerName);
15+
options.ui.writeWarnLine(
16+
'For addons that define a moduleName, you should reference any CSS Modules provided by that addon ' +
17+
'using its moduleName instead of the package name.\n' +
18+
'Current path: ' + importPath + '\n' +
19+
'Replace with: ' + importPath.replace(options.parentName, options.ownerName) + '\n' +
20+
'File: ' + fromFile
21+
);
22+
return resolveLocalPath(amendedPathWithExtension, fromFile, options);
1323
} else {
1424
return resolveExternalPath(pathWithExtension, importPath, fromFile, options);
1525
}
@@ -23,6 +33,10 @@ function isLocalPath(importPath, options) {
2333
return importPath.indexOf(options.ownerName + '/') === 0;
2434
}
2535

36+
function isLocalPathWithOldPackageNameRef(importPath, options) {
37+
return (options.ownerName !== options.parentName) && importPath.indexOf(options.parentName + '/') === 0;
38+
}
39+
2640
function resolveRelativePath(importPath, fromFile, options) {
2741
let absolutePath = ensurePosixPath(path.resolve(path.dirname(fromFile), importPath));
2842
return internalDep(absolutePath, options);

0 commit comments

Comments
 (0)