mirror of
https://github.com/discourse/discourse.git
synced 2025-05-25 00:32:52 +08:00
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:
@ -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 = {
|
module.exports = {
|
||||||
name: require("./package").name,
|
name: require("./package").name,
|
||||||
|
|
||||||
@ -76,19 +92,31 @@ module.exports = {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return pluginDirectories.map((directory) => {
|
return pluginDirectories.map((directory) => {
|
||||||
const name = directory.name;
|
const directoryName = directory.name;
|
||||||
const jsDirectory = path.resolve(root, name, "assets/javascripts");
|
const pluginName = parsePluginName(
|
||||||
|
path.resolve(root, directoryName, "plugin.rb")
|
||||||
|
);
|
||||||
|
const jsDirectory = path.resolve(
|
||||||
|
root,
|
||||||
|
directoryName,
|
||||||
|
"assets/javascripts"
|
||||||
|
);
|
||||||
const adminJsDirectory = path.resolve(
|
const adminJsDirectory = path.resolve(
|
||||||
root,
|
root,
|
||||||
name,
|
directoryName,
|
||||||
"admin/assets/javascripts"
|
"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 hasJs = fs.existsSync(jsDirectory);
|
||||||
const hasAdminJs = fs.existsSync(adminJsDirectory);
|
const hasAdminJs = fs.existsSync(adminJsDirectory);
|
||||||
const hasTests = fs.existsSync(testDirectory);
|
const hasTests = fs.existsSync(testDirectory);
|
||||||
return {
|
return {
|
||||||
name,
|
pluginName,
|
||||||
|
directoryName,
|
||||||
jsDirectory,
|
jsDirectory,
|
||||||
adminJsDirectory,
|
adminJsDirectory,
|
||||||
testDirectory,
|
testDirectory,
|
||||||
@ -109,11 +137,11 @@ module.exports = {
|
|||||||
_generatePluginAppTree() {
|
_generatePluginAppTree() {
|
||||||
const trees = this.pluginInfos()
|
const trees = this.pluginInfos()
|
||||||
.filter((p) => p.hasJs)
|
.filter((p) => p.hasJs)
|
||||||
.map(({ name, jsDirectory }) =>
|
.map(({ pluginName, directoryName, jsDirectory }) =>
|
||||||
this._buildAppTree({
|
this._buildAppTree({
|
||||||
directory: jsDirectory,
|
directory: jsDirectory,
|
||||||
pluginName: name,
|
pluginName,
|
||||||
outputFile: `assets/plugins/${name}.js`,
|
outputFile: `assets/plugins/${directoryName}.js`,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return mergeTrees(trees);
|
return mergeTrees(trees);
|
||||||
@ -122,11 +150,11 @@ module.exports = {
|
|||||||
_generatePluginAdminTree() {
|
_generatePluginAdminTree() {
|
||||||
const trees = this.pluginInfos()
|
const trees = this.pluginInfos()
|
||||||
.filter((p) => p.hasAdminJs)
|
.filter((p) => p.hasAdminJs)
|
||||||
.map(({ name, adminJsDirectory }) =>
|
.map(({ pluginName, directoryName, adminJsDirectory }) =>
|
||||||
this._buildAppTree({
|
this._buildAppTree({
|
||||||
directory: adminJsDirectory,
|
directory: adminJsDirectory,
|
||||||
pluginName: name,
|
pluginName,
|
||||||
outputFile: `assets/plugins/${name}_admin.js`,
|
outputFile: `assets/plugins/${directoryName}_admin.js`,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return mergeTrees(trees);
|
return mergeTrees(trees);
|
||||||
@ -154,16 +182,16 @@ module.exports = {
|
|||||||
_generatePluginTestTree() {
|
_generatePluginTestTree() {
|
||||||
const trees = this.pluginInfos()
|
const trees = this.pluginInfos()
|
||||||
.filter((p) => p.hasTests)
|
.filter((p) => p.hasTests)
|
||||||
.map(({ name, testDirectory }) => {
|
.map(({ pluginName, directoryName, testDirectory }) => {
|
||||||
let tree = new WatchedDir(testDirectory);
|
let tree = new WatchedDir(testDirectory);
|
||||||
|
|
||||||
tree = fixLegacyExtensions(tree);
|
tree = fixLegacyExtensions(tree);
|
||||||
tree = namespaceModules(tree, name);
|
tree = namespaceModules(tree, pluginName);
|
||||||
tree = this.processedAddonJsFiles(tree);
|
tree = this.processedAddonJsFiles(tree);
|
||||||
|
|
||||||
return concat(mergeTrees([tree]), {
|
return concat(mergeTrees([tree]), {
|
||||||
inputFiles: ["**/*.js"],
|
inputFiles: ["**/*.js"],
|
||||||
outputFile: `assets/plugins/test/${name}_tests.js`,
|
outputFile: `assets/plugins/test/${directoryName}_tests.js`,
|
||||||
allowNone: true,
|
allowNone: true,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -386,17 +386,31 @@ module.exports = {
|
|||||||
.findAddonByName("discourse-plugins")
|
.findAddonByName("discourse-plugins")
|
||||||
.pluginInfos();
|
.pluginInfos();
|
||||||
|
|
||||||
for (const { name, hasJs, hasAdminJs } of pluginInfos) {
|
for (const {
|
||||||
|
pluginName,
|
||||||
|
directoryName,
|
||||||
|
hasJs,
|
||||||
|
hasAdminJs,
|
||||||
|
} of pluginInfos) {
|
||||||
if (hasJs) {
|
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`)) {
|
if (fs.existsSync(`../plugins/${directoryName}_extras.js.erb`)) {
|
||||||
scripts.push({ src: `plugins/${name}_extras.js`, name });
|
scripts.push({
|
||||||
|
src: `plugins/${directoryName}_extras.js`,
|
||||||
|
name: pluginName,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasAdminJs) {
|
if (hasAdminJs) {
|
||||||
scripts.push({ src: `plugins/${name}_admin.js`, name });
|
scripts.push({
|
||||||
|
src: `plugins/${directoryName}_admin.js`,
|
||||||
|
name: pluginName,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -420,8 +434,8 @@ module.exports = {
|
|||||||
.pluginInfos()
|
.pluginInfos()
|
||||||
.filter(({ hasTests }) => hasTests)
|
.filter(({ hasTests }) => hasTests)
|
||||||
.map(
|
.map(
|
||||||
({ name }) =>
|
({ directoryName, pluginName }) =>
|
||||||
`<script src="${config.rootURL}assets/plugins/test/${name}_tests.js" data-discourse-plugin="${name}"></script>`
|
`<script src="${config.rootURL}assets/plugins/test/${directoryName}_tests.js" data-discourse-plugin="${pluginName}"></script>`
|
||||||
)
|
)
|
||||||
.join("\n");
|
.join("\n");
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user