Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
The issue is particularly prone to happen while refactoring files
into .gjs which I am about to do a lot of :)
  • Loading branch information
chancancode committed Nov 7, 2023
1 parent f20b6a0 commit 90116c5
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions app/assets/javascripts/patches/@embroider+compat+3.2.3.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
diff --git a/node_modules/@embroider/compat/src/synthesize-template-only-components.js b/node_modules/@embroider/compat/src/synthesize-template-only-components.js
index 0b11e4a..311a342 100644
--- a/node_modules/@embroider/compat/src/synthesize-template-only-components.js
+++ b/node_modules/@embroider/compat/src/synthesize-template-only-components.js
@@ -10,6 +10,9 @@ const fs_extra_1 = require("fs-extra");
const source = `import templateOnlyComponent from '@ember/component/template-only';
export default templateOnlyComponent();`;
const jsExtensions = ['.js', '.ts', '.mjs', '.mts'];
+function importTemplate(files) {
+ return `/* import __COLOCATED_TEMPLATE__ from './${(0, path_1.basename)(files.template.relativePath)}'; */\n`;
+}
class SynthesizeTemplateOnlyComponents extends broccoli_plugin_1.default {
constructor(tree, params) {
super([tree], {
@@ -17,60 +20,89 @@ class SynthesizeTemplateOnlyComponents extends broccoli_plugin_1.default {
persistentOutput: true,
needsCache: false,
});
- this.emitted = new Set();
+ this.emitted = new Map();
this.allowedPaths = params.allowedPaths;
this.templateExtensions = params.templateExtensions;
}
async build() {
+ let unneeded = new Set(this.emitted.keys());
for (let dir of this.allowedPaths) {
- let { needed, seen } = this.crawl((0, path_1.join)(this.inputPaths[0], dir));
- for (let file of needed) {
- let fullName = (0, path_1.join)(this.outputPath, dir, file);
- if (seen.has(file)) {
- this.remove(fullName);
+ let entries = this.crawl((0, path_1.join)(this.inputPaths[0], dir));
+ for (let [name, files] of entries) {
+ let fullName = (0, path_1.join)(this.outputPath, dir, name);
+ unneeded.delete(fullName);
+ if (files.javascript && files.template) {
+ this.addTemplateImport(fullName, files);
+ }
+ else if (files.template) {
+ this.addTemplateOnlyComponent(fullName, files);
}
else {
- this.add(fullName);
+ this.remove(fullName);
}
}
}
+ for (let fullName of unneeded) {
+ this.remove(fullName);
+ }
}
- add(filename) {
- if (!this.emitted.has(filename)) {
+ addTemplateOnlyComponent(filename, files) {
+ const emitted = this.emitted.get(filename);
+ if ((emitted === null || emitted === void 0 ? void 0 : emitted.type) !== 'template-only-component') {
// special case: ember-cli doesn't allow template-only components named
// "template.hbs" because there are too many people doing a "pods-like"
// layout that happens to match that pattern.🤮
if ((0, path_1.basename)(filename) !== 'template') {
- (0, fs_extra_1.outputFileSync)(filename + '.js', source, 'utf8');
+ const outputPath = filename + '.js';
+ (0, fs_extra_1.outputFileSync)(outputPath, importTemplate(files) + source, 'utf8');
+ this.emitted.set(filename, { type: 'template-only-component', outputPath });
+ if (emitted && emitted.outputPath !== outputPath) {
+ (0, fs_extra_1.removeSync)(emitted.outputPath);
+ }
+ }
+ }
+ }
+ addTemplateImport(filename, files) {
+ const emitted = this.emitted.get(filename);
+ const mtime = files.javascript.mtime;
+ if (!((emitted === null || emitted === void 0 ? void 0 : emitted.type) === 'template-import' && emitted.mtime === mtime)) {
+ const inputSource = (0, fs_extra_1.readFileSync)(files.javascript.fullPath, { encoding: 'utf8' });
+ const outputPath = filename + (0, path_1.extname)(files.javascript.relativePath);
+ // If we are ok with appending instead, copy + append maybe more efficient?
+ (0, fs_extra_1.outputFileSync)(outputPath, importTemplate(files) + inputSource, 'utf8');
+ this.emitted.set(filename, { type: 'template-import', outputPath, mtime });
+ if (emitted && emitted.outputPath !== outputPath) {
+ (0, fs_extra_1.removeSync)(emitted.outputPath);
}
- this.emitted.add(filename);
}
}
remove(filename) {
- if (this.emitted.has(filename)) {
- (0, fs_extra_1.remove)(filename + '.js');
+ const emitted = this.emitted.get(filename);
+ if (emitted) {
+ (0, fs_extra_1.removeSync)(emitted.outputPath);
this.emitted.delete(filename);
}
}
crawl(dir) {
- const needed = new Set();
- const seen = new Set();
+ var _a, _b;
+ const entries = new Map();
if ((0, fs_extra_1.pathExistsSync)(dir)) {
- for (let file of (0, walk_sync_1.default)(dir, { directories: false })) {
- for (const templateExtension of this.templateExtensions) {
- if (file.endsWith(templateExtension)) {
- needed.add(file.slice(0, -1 * templateExtension.length));
- }
- else {
- const jsExtension = jsExtensions.find(ext => file.endsWith(ext));
- if (jsExtension) {
- seen.add(file.slice(0, -1 * jsExtension.length));
- }
- }
+ for (let entry of walk_sync_1.default.entries(dir, { directories: false })) {
+ const templateExtension = this.templateExtensions.find(ext => entry.relativePath.endsWith(ext));
+ if (templateExtension) {
+ const key = entry.relativePath.slice(0, -1 * templateExtension.length);
+ entries.set(key, { template: entry, javascript: (_a = entries.get(key)) === null || _a === void 0 ? void 0 : _a.javascript });
+ continue;
+ }
+ const jsExtension = jsExtensions.find(ext => entry.relativePath.endsWith(ext));
+ if (jsExtension) {
+ const key = entry.relativePath.slice(0, -1 * jsExtension.length);
+ entries.set(key, { template: (_b = entries.get(key)) === null || _b === void 0 ? void 0 : _b.template, javascript: entry });
+ continue;
}
}
}
- return { needed, seen };
+ return entries;
}
}
exports.default = SynthesizeTemplateOnlyComponents;

0 comments on commit 90116c5

Please sign in to comment.