FIX: Use plugin's defined name for es6 module path (#18159)

We were using the directory name rather than the plugin's defined `name`. This was an unintended change in behaviour from the old sprockets implementation. This commit makes the ember-cli naming logic match the old sprockets logic.
This commit is contained in:
David Taylor 2022-09-01 22:15:32 +01:00 committed by GitHub
parent a335492d20
commit 1279966f59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 21 deletions

View File

@ -62,6 +62,22 @@ function namespaceModules(tree, pluginDirectoryName) {
});
}
function parsePluginName(pluginRbPath) {
const pluginRb = fs.readFileSync(pluginRbPath, "utf8");
// Match parsing logic in `lib/plugin/metadata.rb`
for (const line of pluginRb.split("\n")) {
if (line.startsWith("#")) {
const [attribute, value] = line.slice(1).split(":", 2);
if (attribute.trim() === "name") {
return value.trim();
}
}
}
throw new Error(
`Unable to parse plugin name from metadata in ${pluginRbPath}`
);
}
module.exports = {
name: require("./package").name,
@ -76,19 +92,31 @@ module.exports = {
);
return pluginDirectories.map((directory) => {
const name = directory.name;
const jsDirectory = path.resolve(root, name, "assets/javascripts");
const directoryName = directory.name;
const pluginName = parsePluginName(
path.resolve(root, directoryName, "plugin.rb")
);
const jsDirectory = path.resolve(
root,
directoryName,
"assets/javascripts"
);
const adminJsDirectory = path.resolve(
root,
name,
directoryName,
"admin/assets/javascripts"
);
const testDirectory = path.resolve(root, name, "test/javascripts");
const testDirectory = path.resolve(
root,
directoryName,
"test/javascripts"
);
const hasJs = fs.existsSync(jsDirectory);
const hasAdminJs = fs.existsSync(adminJsDirectory);
const hasTests = fs.existsSync(testDirectory);
return {
name,
pluginName,
directoryName,
jsDirectory,
adminJsDirectory,
testDirectory,
@ -109,11 +137,11 @@ module.exports = {
_generatePluginAppTree() {
const trees = this.pluginInfos()
.filter((p) => p.hasJs)
.map(({ name, jsDirectory }) =>
.map(({ pluginName, directoryName, jsDirectory }) =>
this._buildAppTree({
directory: jsDirectory,
pluginName: name,
outputFile: `assets/plugins/${name}.js`,
pluginName,
outputFile: `assets/plugins/${directoryName}.js`,
})
);
return mergeTrees(trees);
@ -122,11 +150,11 @@ module.exports = {
_generatePluginAdminTree() {
const trees = this.pluginInfos()
.filter((p) => p.hasAdminJs)
.map(({ name, adminJsDirectory }) =>
.map(({ pluginName, directoryName, adminJsDirectory }) =>
this._buildAppTree({
directory: adminJsDirectory,
pluginName: name,
outputFile: `assets/plugins/${name}_admin.js`,
pluginName,
outputFile: `assets/plugins/${directoryName}_admin.js`,
})
);
return mergeTrees(trees);
@ -154,16 +182,16 @@ module.exports = {
_generatePluginTestTree() {
const trees = this.pluginInfos()
.filter((p) => p.hasTests)
.map(({ name, testDirectory }) => {
.map(({ pluginName, directoryName, testDirectory }) => {
let tree = new WatchedDir(testDirectory);
tree = fixLegacyExtensions(tree);
tree = namespaceModules(tree, name);
tree = namespaceModules(tree, pluginName);
tree = this.processedAddonJsFiles(tree);
return concat(mergeTrees([tree]), {
inputFiles: ["**/*.js"],
outputFile: `assets/plugins/test/${name}_tests.js`,
outputFile: `assets/plugins/test/${directoryName}_tests.js`,
allowNone: true,
});
});

View File

@ -386,17 +386,31 @@ module.exports = {
.findAddonByName("discourse-plugins")
.pluginInfos();
for (const { name, hasJs, hasAdminJs } of pluginInfos) {
for (const {
pluginName,
directoryName,
hasJs,
hasAdminJs,
} of pluginInfos) {
if (hasJs) {
scripts.push({ src: `plugins/${name}.js`, name });
scripts.push({
src: `plugins/${directoryName}.js`,
name: pluginName,
});
}
if (fs.existsSync(`../plugins/${name}_extras.js.erb`)) {
scripts.push({ src: `plugins/${name}_extras.js`, name });
if (fs.existsSync(`../plugins/${directoryName}_extras.js.erb`)) {
scripts.push({
src: `plugins/${directoryName}_extras.js`,
name: pluginName,
});
}
if (hasAdminJs) {
scripts.push({ src: `plugins/${name}_admin.js`, name });
scripts.push({
src: `plugins/${directoryName}_admin.js`,
name: pluginName,
});
}
}
} else {
@ -420,8 +434,8 @@ module.exports = {
.pluginInfos()
.filter(({ hasTests }) => hasTests)
.map(
({ name }) =>
`<script src="${config.rootURL}assets/plugins/test/${name}_tests.js" data-discourse-plugin="${name}"></script>`
({ directoryName, pluginName }) =>
`<script src="${config.rootURL}assets/plugins/test/${directoryName}_tests.js" data-discourse-plugin="${pluginName}"></script>`
)
.join("\n");
} else {