Skip to content

Commit cebbc59

Browse files
committed
fix: Look for broccoli tree paths in additional scopes.
1 parent 9aafacd commit cebbc59

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

packages/@css-blocks/ember-app/src/brocolli-plugin.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Analyzer, Block, BlockFactory, Options as CSSBlocksOptions, SerializedSourceAnalysis, resolveConfiguration } from "@css-blocks/core";
2-
import { BroccoliTreeImporter, EmberAnalysis, IDENTIFIER_PREFIX as BROCCOLI_IMPORTER_IDENTIFIER_PREFIX, TEMPLATE_TYPE } from "@css-blocks/ember-support";
2+
import { BroccoliTreeImporter, EmberAnalysis, TEMPLATE_TYPE, pathToIdent } from "@css-blocks/ember-support";
33
import { TemplateIntegrationOptions } from "@opticss/template-api";
44
import mergeTrees = require("broccoli-merge-trees");
55
import type { InputNode } from "broccoli-node-api";
@@ -52,7 +52,7 @@ export class CSSBlocksApplicationPlugin extends Filter {
5252
this.previousSourceTree = currentFSTree;
5353
}
5454
let config = resolveConfiguration(this.cssBlocksOptions);
55-
let importer = new BroccoliTreeImporter(this.input, config.importer);
55+
let importer = new BroccoliTreeImporter(this.input, null, config.importer);
5656
config = resolveConfiguration({importer}, config);
5757
let factory = new BlockFactory(config, postcss);
5858
let analyzer = new EmberAnalyzer(factory);
@@ -72,10 +72,11 @@ export class CSSBlocksApplicationPlugin extends Filter {
7272
};
7373
let optimizer = new Optimizer(optimizerOptions, analyzer.optimizationOptions);
7474
for (let entry of entries) {
75+
let ident = pathToIdent(entry.relativePath);
7576
if (entry.relativePath.endsWith(".compiledblock.css")) {
7677
debug(`Parsing precompiled block: ${entry.relativePath}`);
7778
let block: Block;
78-
block = await factory.getBlock(`${BROCCOLI_IMPORTER_IDENTIFIER_PREFIX}${entry.relativePath}`);
79+
block = await factory.getBlock(ident);
7980
debug(`Got block: ${block.identifier}`);
8081
optimizer.addSource({
8182
filename: entry.relativePath,
@@ -88,7 +89,7 @@ export class CSSBlocksApplicationPlugin extends Filter {
8889
let serializedAnalysis: SerializedSourceAnalysis<TEMPLATE_TYPE> = JSON.parse(this.input.readFileSync(entry.relativePath, "utf8"));
8990
debug("blocks", serializedAnalysis.stylesFound);
9091
for (let blockId of Object.keys(serializedAnalysis.blocks)) {
91-
serializedAnalysis.blocks[blockId] = `${BROCCOLI_IMPORTER_IDENTIFIER_PREFIX}${serializedAnalysis.blocks[blockId]}`;
92+
serializedAnalysis.blocks[blockId] = pathToIdent(serializedAnalysis.blocks[blockId]);
9293
}
9394
let analysis = await EmberAnalysis.deserializeSource(serializedAnalysis, factory, analyzer);
9495
optimizer.addAnalysis(analysis.forOptimizer(config));

packages/@css-blocks/ember-support/src/BroccoliTreeImporter.ts

+33-9
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,39 @@ const IDENTIFIER_PREFIX_LENGTH = IDENTIFIER_PREFIX.length;
1010
const IDENTIFIER_PREFIX_RE = new RegExp(`^${IDENTIFIER_PREFIX}`);
1111
export const EMBEDDED_DEFINITION_TAG = "#blockDefinitionURL";
1212

13-
export function isBroccoliTreeIdentifier(identifier: string | null): identifier is string {
13+
export function isBroccoliTreeIdentifier(identifier: string | null): boolean {
1414
return !!(identifier && IDENTIFIER_PREFIX_RE.test(identifier));
1515
}
1616

17-
export function identToPath(identifier: string): string {
18-
return identifier.substring(IDENTIFIER_PREFIX_LENGTH);
17+
export function identToPath(input: MergedFileSystem, identifier: string): string {
18+
if (!isBroccoliTreeIdentifier(identifier)) {
19+
return identifier;
20+
}
21+
let relativePath = identifier.substring(IDENTIFIER_PREFIX_LENGTH);
22+
if (!input.existsSync(relativePath)) {
23+
debug(`Couldn't find ${relativePath}. Looking in addon-tree-output.`);
24+
let addonRelativePath = `addon-tree-output/${relativePath}`;
25+
if (input.existsSync(addonRelativePath)) {
26+
relativePath = addonRelativePath;
27+
} else {
28+
let addonModulesRelativePath = `addon-tree-output/modules/${relativePath}`;
29+
if (input.existsSync(addonModulesRelativePath)) {
30+
relativePath = addonModulesRelativePath;
31+
}
32+
}
33+
}
34+
return relativePath;
1935
}
2036

2137
export function pathToIdent(relativePath: string): string {
38+
if (isBroccoliTreeIdentifier(relativePath)) {
39+
return relativePath;
40+
}
41+
if (relativePath.startsWith("addon-tree-output/modules/")) {
42+
relativePath = relativePath.substring(26);
43+
} else if (relativePath.startsWith("addon-tree-output/")) {
44+
relativePath = relativePath.substring(18);
45+
}
2246
return IDENTIFIER_PREFIX + relativePath;
2347
}
2448

@@ -38,7 +62,7 @@ export class BroccoliTreeImporter extends BaseImporter {
3862
identifier(fromIdentifier: string | null, importPath: string, config: Readonly<Configuration>): string {
3963
if (isBroccoliTreeIdentifier(fromIdentifier)) {
4064
if (importPath.startsWith("./") || importPath.startsWith("../")) {
41-
let parsedPath = path.parse(identToPath(fromIdentifier));
65+
let parsedPath = path.parse(identToPath(this.input, fromIdentifier!));
4266
// We have to make resolve think the path is absolute or else it will
4367
// prepend the current working directory.
4468
let dir = "/" + parsedPath.dir;
@@ -55,7 +79,7 @@ export class BroccoliTreeImporter extends BaseImporter {
5579

5680
async import(identifier: string, config: Readonly<Configuration>): Promise<ImportedFile | ImportedCompiledCssFile> {
5781
if (isBroccoliTreeIdentifier(identifier)) {
58-
let relativePath = identToPath(identifier);
82+
let relativePath = identToPath(this.input, identifier);
5983
let contents = this.input.readFileSync(relativePath, "utf8");
6084
let syntax = syntaxFromExtension(path.extname(relativePath));
6185
let defaultName = path.parse(relativePath).name;
@@ -111,7 +135,7 @@ export class BroccoliTreeImporter extends BaseImporter {
111135

112136
defaultName(identifier: string, configuration: Readonly<Configuration>): string {
113137
if (isBroccoliTreeIdentifier(identifier)) {
114-
let relativePath = identToPath(identifier);
138+
let relativePath = identToPath(this.input, identifier);
115139
let defaultName = path.basename(relativePath);
116140
defaultName = defaultName.replace(/.block$/, "");
117141
return defaultName;
@@ -122,7 +146,7 @@ export class BroccoliTreeImporter extends BaseImporter {
122146

123147
filesystemPath(identifier: string, config: Readonly<Configuration>): string | null {
124148
if (isBroccoliTreeIdentifier(identifier)) {
125-
let relativePath = identToPath(identifier);
149+
let relativePath = identToPath(this.input, identifier);
126150
return relativePath;
127151
} else {
128152
return this.fallbackImporter.filesystemPath(identifier, config);
@@ -131,7 +155,7 @@ export class BroccoliTreeImporter extends BaseImporter {
131155

132156
debugIdentifier(identifier: string, config: Readonly<Configuration>): string {
133157
if (isBroccoliTreeIdentifier(identifier)) {
134-
let relativePath = identToPath(identifier);
158+
let relativePath = identToPath(this.input, identifier);
135159
return relativePath;
136160
} else {
137161
return this.fallbackImporter.debugIdentifier(identifier, config);
@@ -140,7 +164,7 @@ export class BroccoliTreeImporter extends BaseImporter {
140164

141165
syntax(identifier: string, config: Readonly<Configuration>): Syntax {
142166
if (isBroccoliTreeIdentifier(identifier)) {
143-
let relativePath = identToPath(identifier);
167+
let relativePath = identToPath(this.input, identifier);
144168
let syntax = syntaxFromExtension(path.extname(relativePath));
145169
return syntax;
146170
} else {

packages/@css-blocks/ember/src/CSSBlocksTemplateCompilerPlugin.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import persistentStrategy = require("broccoli-persistent-filter/lib/strategies/p
99
import debugGenerator from "debug";
1010
import TemplateCompilerPlugin = require("ember-cli-htmlbars/lib/template-compiler-plugin");
1111
import FSMerger = require("fs-merger");
12+
import type { FS as MergedFileSystem } from "fs-merger";
1213
import * as FSTree from "fs-tree-diff";
1314
import { OptiCSSOptions, postcss } from "opticss";
1415
import * as path from "path";
@@ -159,7 +160,7 @@ export class CSSBlocksTemplateCompilerPlugin extends TemplateCompilerPlugin {
159160
// we have to pre-compute the paths of all the local blocks so that we can
160161
// rewrite the path in the compiled output of the definition file.
161162
for (let block of blocks) {
162-
let outputPath = getOutputPath(block);
163+
let outputPath = getOutputPath(this.input, block);
163164
if (outputPath) blockOutputPaths.set(block, outputPath);
164165
}
165166

@@ -328,9 +329,9 @@ function analysisPath(templatePath: string): string {
328329
return path.format(analysisPath);
329330
}
330331

331-
function getOutputPath(block: Block): string | null {
332+
function getOutputPath(input: MergedFileSystem, block: Block): string | null {
332333
if (isBroccoliTreeIdentifier(block.identifier)) {
333-
return identToPath(block.identifier).replace(".block", ".compiledblock");
334+
return identToPath(input, block.identifier).replace(".block", ".compiledblock");
334335
} else {
335336
return null;
336337
}

0 commit comments

Comments
 (0)