Skip to content

Commit

Permalink
only insert one entrypoint at once
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona authored and ef4 committed May 27, 2024
1 parent 47cc3ea commit a3ded95
Showing 1 changed file with 83 additions and 20 deletions.
103 changes: 83 additions & 20 deletions packages/ember-auto-import/ts/inserter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ interface ScriptTarget {
// inserted after particular files in the HTML (vendor.js and
// test-support.js). Other custom bundles won't have this.
afterFile: string | undefined;
inserted: boolean;
}

interface StyleTarget {
Expand All @@ -33,7 +32,6 @@ interface StyleTarget {
// inserted after particular files in the HTML (vendor.css and
// test-support.css). Other custom bundles won't have this.
afterFile: string | undefined;
inserted: boolean;
}

interface Targets {
Expand Down Expand Up @@ -134,6 +132,56 @@ export class Inserter extends Plugin {
);
}

const useTestTargets = function (targets: Targets): Targets {
const tests = targets.scripts.find((t) => t.bundleName === 'tests');

// TODO make this a bit cleaner
return {
scripts: targets.scripts.map((target) => {
if (target.bundleName === 'app') {
return { ...target, scriptChunks: tests!.scriptChunks };
} else if (target.bundleName === 'tests') {
return { ...target, scriptChunks: [] };
} else {
return target;
}
}),
styles: targets.styles,
};
};

const testScript = targets.scripts.find(
(script) => script.bundleName === 'tests'
);
if (testScript) {
let foundTests = false;

traverse(ast, (element) => {
if (this.options.insertScriptsAt) {
if (element.tagName === this.options.insertScriptsAt) {
let entrypoint = element.attrs.find((a) => a.name === 'entrypoint');
if (entrypoint?.value === 'tests') {
foundTests = true;
}
} else {
let src = element.attrs.find((a) => a.name === 'src')?.value;
if (
src &&
testScript.afterFile &&
src.endsWith(testScript.afterFile)
) {
foundTests = true;
}
}
}
});
if (foundTests) {
targets = useTestTargets(targets);
}
}

let inserted: { kind: 'script' | 'styles'; bundleName: string }[] = [];

traverse(ast, (element) => {
if (this.options.insertScriptsAt) {
if (element.tagName === this.options.insertScriptsAt) {
Expand All @@ -148,7 +196,8 @@ export class Inserter extends Plugin {
fastbootInfo,
stringInserter,
element,
entrypoint.value
entrypoint.value,
inserted
);
}
} else if (element.tagName === 'script') {
Expand All @@ -160,7 +209,8 @@ export class Inserter extends Plugin {
fastbootInfo,
stringInserter,
element,
src
src,
inserted
);
}
}
Expand All @@ -177,7 +227,8 @@ export class Inserter extends Plugin {
targets,
stringInserter,
element,
entrypoint.value
entrypoint.value,
inserted
);
}
} else if (element.tagName === 'link') {
Expand All @@ -189,7 +240,7 @@ export class Inserter extends Plugin {
let href = element.attrs.find((a) => a.name === 'href')?.value;
if (href) {
debug(`found stylesheet with href=%s`, href);
this.insertStyles(targets, stringInserter, element, href);
this.insertStyles(targets, stringInserter, element, href, inserted);
}
}
}
Expand All @@ -198,10 +249,14 @@ export class Inserter extends Plugin {
let appScripts = [...targets.scripts].find(
(entry) => entry.bundleName === 'app'
);
if (appScripts && !appScripts.inserted) {

if (
appScripts &&
!inserted.find((i) => i.bundleName === 'app' && i.kind === 'script')
) {
if (this.options.insertScriptsAt) {
throw new Error(
`ember-auto-import cannot find <${this.options.insertScriptsAt} entrypoint="${appScripts.bundleName}"> in ${filename}.`
`ember-auto-import cannot find <${this.options.insertScriptsAt} entrypoint="app"> in ${filename}.`
);
} else {
throw new Error(
Expand All @@ -213,7 +268,10 @@ export class Inserter extends Plugin {
let appStyles = [...targets.styles.values()].find(
(entry) => entry.bundleName === 'app'
);
if (appStyles && !appStyles.inserted) {
if (
appStyles &&
!inserted.find((i) => i.bundleName === 'app' && i.kind === 'styles')
) {
if (this.options.insertStylesAt) {
throw new Error(
`ember-auto-import cannot find <${this.options.insertStylesAt} entrypoint="${appStyles.bundleName}"> in ${filename}.`
Expand All @@ -233,12 +291,13 @@ export class Inserter extends Plugin {
fastbootInfo: ReturnType<typeof Inserter.prototype.fastbootManifestInfo>,
stringInserter: StringInserter,
element: parse5.Element,
src: string
src: string,
inserted: { kind: 'script' | 'styles'; bundleName: string }[]
) {
for (let entry of targets.scripts) {
if (entry.afterFile && src.endsWith(entry.afterFile)) {
let { scriptChunks, bundleName } = entry;
entry.inserted = true;
inserted.push({ bundleName, kind: 'script' });
debug(`inserting %s`, scriptChunks);
let insertedSrc = scriptChunks
.map((chunk) => `\n<script src="${this.chunkURL(chunk)}"></script>`)
Expand Down Expand Up @@ -272,7 +331,8 @@ export class Inserter extends Plugin {
fastbootInfo: ReturnType<typeof Inserter.prototype.fastbootManifestInfo>,
stringInserter: StringInserter,
element: parse5.Element,
bundleName: string
bundleName: string,
insertedEntrypoints: { kind: 'script' | 'styles'; bundleName: string }[]
) {
let loc = element.sourceCodeLocation!;
stringInserter.remove(loc.startOffset, loc.endOffset - loc.startOffset);
Expand All @@ -281,7 +341,8 @@ export class Inserter extends Plugin {
continue;
}
let { scriptChunks } = entry;
entry.inserted = true;
insertedEntrypoints.push({ bundleName, kind: 'script' });

debug(`inserting %s`, scriptChunks);
let tags = scriptChunks.map((chunk) =>
this.scriptFromCustomElement(element, chunk)
Expand All @@ -307,7 +368,8 @@ export class Inserter extends Plugin {
targets: Targets,
stringInserter: StringInserter,
element: parse5.Element,
bundleName: string
bundleName: string,
inserted: { kind: 'script' | 'styles'; bundleName: string }[]
) {
let loc = element.sourceCodeLocation!;
stringInserter.remove(loc.startOffset, loc.endOffset - loc.startOffset);
Expand All @@ -316,7 +378,8 @@ export class Inserter extends Plugin {
continue;
}
let { styleChunks } = entry;
entry.inserted = true;
inserted.push({ kind: 'styles', bundleName });

debug(`inserting %s`, styleChunks);
let tags = styleChunks.map((chunk) =>
this.styleFromCustomElement(element, chunk)
Expand Down Expand Up @@ -361,12 +424,14 @@ export class Inserter extends Plugin {
targets: Targets,
stringInserter: StringInserter,
element: parse5.Element,
href: string
href: string,
inserted: { kind: 'script' | 'styles'; bundleName: string }[]
) {
for (let entry of targets.styles) {
if (entry.afterFile && href.endsWith(entry.afterFile)) {
let { styleChunks } = entry;
entry.inserted = true;
let { styleChunks, bundleName } = entry;

inserted.push({ kind: 'styles', bundleName });
debug(`inserting %s`, styleChunks);
stringInserter.insert(
element.sourceCodeLocation!.endOffset,
Expand Down Expand Up @@ -430,7 +495,6 @@ export class Inserter extends Plugin {
scriptChunks,
bundleName,
afterFile,
inserted: false,
});
}
let styleChunks = assets.filter((a) => a.endsWith('.css'));
Expand All @@ -443,7 +507,6 @@ export class Inserter extends Plugin {
styleChunks,
bundleName,
afterFile,
inserted: false,
});
}
}
Expand Down

0 comments on commit a3ded95

Please sign in to comment.