Skip to content

Commit 574483d

Browse files
committed
feat: Ember CLI addon Preprocessor support.
- Pass preprocessors down to core from ember config. - Don't deep clone config object to preserve preprocessor functions. - Auto discover Block files with supported preprocessor file endings.
1 parent 612bcf8 commit 574483d

File tree

6 files changed

+52
-145
lines changed

6 files changed

+52
-145
lines changed

packages/@css-blocks/core/src/BlockParser/BlockFactory.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ObjectDictionary } from "@opticss/util";
1+
import { ObjectDictionary, isString } from "@opticss/util";
22
import * as debugGenerator from "debug";
33
import { postcss } from "opticss";
44
import * as path from "path";
@@ -254,8 +254,8 @@ export class BlockFactory {
254254

255255
function sourceMapFromProcessedFile(result: ProcessedFile): RawSourceMap | string | undefined {
256256
let sourceMap: RawSourceMap | string | undefined = result.sourceMap;
257-
if (!sourceMap && (<postcss.Result>result.content).map) {
258-
sourceMap = (<postcss.Result>result.content).map.toJSON();
257+
if (!sourceMap && !isString(result.content) && result.content.map) {
258+
sourceMap = result.content.map.toJSON();
259259
}
260260
return sourceMap;
261261
}

packages/@css-blocks/ember-cli/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ module.exports = {
4949
return { name: 'css-blocks-noop', visitors: {} };
5050
}
5151

52+
// If no specifier data for this template, pass through silently.
53+
if (!env.meta.moduleName && !env.meta.specifier) {
54+
return { name: 'css-blocks-noop', visitors: {} };
55+
}
56+
5257
// TODO: Write a better `getAnalysis` method on `Analyzer`
5358
// TODO: The differences in what Ember and Glimmer provide in env.meta should be resolved.
5459
let analysis;
@@ -208,7 +213,7 @@ module.exports = {
208213

209214
// Get CSS Blocks options provided by the application, if present.
210215
const options = app.options["css-blocks"]
211-
? JSON.parse(JSON.stringify(app.options["css-blocks"]))
216+
? app.options["css-blocks"] // Do not clone! Contains non-json-safe data.
212217
: {
213218
parserOpts: {},
214219
analysisOpts: {},

packages/@css-blocks/glimmer/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@
4444
"url": "https://github.com/linkedin/css-blocks/issues"
4545
},
4646
"engines": {
47-
"node": ">=8"
47+
"node": "6.* || >= 8.*"
4848
},
4949
"homepage": "https://github.com/linkedin/css-blocks/tree/master/packages/@css-blocks/glimmer#readme",
5050
"publishConfig": {
5151
"access": "public"
5252
},
5353
"devDependencies": {
5454
"@css-blocks/code-style": "^0.18.0",
55+
"@types/fs-extra": "^5.0.3",
56+
"@types/glob": "^5.0.30",
5557
"watch": "^1.0.2"
5658
},
5759
"dependencies": {
@@ -64,8 +66,6 @@
6466
"@opticss/element-analysis": "^0.3.0",
6567
"@opticss/template-api": "^0.3.0",
6668
"@opticss/util": "^0.3.0",
67-
"@types/fs-extra": "^5.0.3",
68-
"@types/glob": "^5.0.30",
6969
"debug": "^2.6.8",
7070
"fs-extra": "^6.0.1",
7171
"glob": "^7.1.2",

packages/@css-blocks/glimmer/src/Analyzer.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ export class GlimmerAnalyzer extends Analyzer<TEMPLATE_TYPE> {
3131
moduleConfig: ResolverConfiguration,
3232
) {
3333
super(cssBlocksOpts, analysisOpts);
34-
3534
this.blockFactory = new BlockFactory(this.cssBlocksOptions, postcss);
36-
this.resolver = new Resolver(moduleConfig);
35+
this.resolver = new Resolver(this.cssBlocksOptions, moduleConfig);
3736
this.debug = debugGenerator("css-blocks:glimmer:analyzer");
3837
}
3938

packages/@css-blocks/glimmer/src/Resolver.ts

+38-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from "fs-extra";
22
import * as path from "path";
33

44
import DependencyAnalyzer from "@amiller-gh/glimmer-analyzer";
5+
import { ResolvedConfiguration } from "@css-blocks/core";
56
import { ResolverConfiguration } from "@glimmer/resolver";
67

78
import * as debugGenerator from "debug";
@@ -10,6 +11,22 @@ import { ResolvedFile } from "./Template";
1011

1112
const DEBUG = debugGenerator("css-blocks:glimmer:resolver");
1213

14+
function toClassicPath(base: string, templatePath: string, ext: string): string {
15+
// TODO: There is a more robust way to do all this path munging!
16+
let classic = path.parse(templatePath.replace("templates/", "styles/"));
17+
delete classic.base; // Required for path.format to pick up new extension.
18+
classic.ext = `.block.${ext}`;
19+
return path.join(base, path.format(classic));
20+
}
21+
22+
function toPodsPath(base: string, templatePath: string, ext: string): string {
23+
let pods = path.parse(templatePath);
24+
delete pods.base; // Required for path.format to pick up new extension.
25+
pods.name = "stylesheet";
26+
pods.ext = `.block.${ext}`;
27+
return path.join(base, path.format(pods));
28+
}
29+
1330
/**
1431
* The Glimmer CSS Blocks Resolver deals in three
1532
* kinds of project structure modes:
@@ -21,11 +38,14 @@ export class Resolver {
2138

2239
private depAnalyzers: Map<string, DependencyAnalyzer> = new Map();
2340
private moduleConfig?: ResolverConfiguration;
41+
private fileEndings: Set<string>;
2442

25-
constructor(moduleConfig?: ResolverConfiguration) {
43+
constructor(cssBlocksConfig: ResolvedConfiguration, moduleConfig?: ResolverConfiguration) {
2644
if (moduleConfig) {
2745
this.moduleConfig = moduleConfig;
2846
}
47+
this.fileEndings = new Set(["css", ...Object.keys(cssBlocksConfig.preprocessors)]);
48+
DEBUG(`Discovering all Block files that end with ("${[...this.fileEndings].join(`|`)}")`);
2949
}
3050

3151
private dependencyAnalyzerFor(dir: string): DependencyAnalyzer | undefined {
@@ -45,27 +65,24 @@ export class Resolver {
4565
}
4666

4767
// TODO: We need to automatically discover the file ending here – its not guaranteed to be a css file.
48-
private async tmplPathToStylesheetPath(dir: string, template: string): Promise<string | undefined> {
49-
// First try Classic Ember structure.
50-
// TODO: There is a more robust way to do this path munging!
51-
let classic = template.replace("templates/", "styles/").replace(".hbs", ".block.css");
52-
classic = path.join(dir, classic);
53-
if (fs.pathExistsSync(classic)) {
54-
DEBUG(`Discovered classic Block for template ${template}:`);
55-
DEBUG(` - ${classic}`);
56-
return classic;
57-
}
58-
let pods = path.parse(template);
59-
pods.base = "stylesheet.block.css";
60-
let podsPath = path.join(dir, path.format(pods));
61-
if (fs.pathExistsSync(podsPath)) {
62-
DEBUG(`Discovered pods Block for template ${template}:`);
63-
DEBUG(` - ${podsPath}`);
64-
return podsPath;
68+
private async tmplPathToStylesheetPath(base: string, template: string): Promise<string | undefined> {
69+
let triedPaths = [];
70+
// For every supported block extension:
71+
for (let ext of this.fileEndings) {
72+
// First try Classic Ember structure.
73+
let classic = toClassicPath(base, template, ext);
74+
if (fs.pathExistsSync(classic)) {
75+
DEBUG(`Discovered classic Block for template ${template}: ${classic}`);
76+
return classic;
77+
}
78+
let podsPath = toPodsPath(base, template, ext);
79+
if (fs.pathExistsSync(podsPath)) {
80+
DEBUG(`Discovered pods Block for template ${template}: ${podsPath}`);
81+
return podsPath;
82+
}
83+
triedPaths.push(path.relative(base, classic), path.relative(base, podsPath));
6584
}
66-
DEBUG(`No Block discovered for template ${template}. Attempted at:`);
67-
DEBUG(` - ${classic}`);
68-
DEBUG(` - ${podsPath}`);
85+
DEBUG(`No Block discovered for template ${template}. Attempted at:${triedPaths.join(`\n - `)}`);
6986
return undefined;
7087
}
7188

yarn.lock

+1-115
Original file line numberDiff line numberDiff line change
@@ -134,74 +134,6 @@
134134
dependencies:
135135
find-up "^2.1.0"
136136

137-
"@css-blocks/broccoli@^0.19.0":
138-
version "0.19.0"
139-
resolved "https://registry.npmjs.org/@css-blocks/broccoli/-/broccoli-0.19.0.tgz#4c36cd54542078fe523f106c6a510d21f7caf61c"
140-
dependencies:
141-
"@css-blocks/core" "^0.19.0"
142-
"@glimmer/compiler" "^0.33.0"
143-
"@glimmer/syntax" "^0.33.0"
144-
"@opticss/template-api" "^0.3.0"
145-
"@types/recursive-readdir" "^2.2.0"
146-
broccoli-funnel "^2.0.1"
147-
broccoli-merge-trees "^3.0.0"
148-
broccoli-plugin "^1.3.0"
149-
broccoli-test-helper "^1.2.0"
150-
colors "^1.2.1"
151-
debug "^3.1.0"
152-
fs-extra "^5.0.0"
153-
opticss "^0.3.0"
154-
postcss "^6.0.21"
155-
recursive-readdir "^2.2.2"
156-
walk-sync "^0.3.2"
157-
158-
"@css-blocks/core@^0.19.0":
159-
version "0.19.0"
160-
resolved "https://registry.npmjs.org/@css-blocks/core/-/core-0.19.0.tgz#89cb5584b09ffb2dbbb597570ccc7a6e32eb4839"
161-
dependencies:
162-
"@opticss/element-analysis" "^0.3.0"
163-
"@opticss/template-api" "^0.3.0"
164-
"@opticss/util" "^0.3.0"
165-
"@types/async" "^2.0.40"
166-
"@types/debug" "0.0.29"
167-
async "^2.5.0"
168-
css-property-parser "^1.0.6"
169-
debug "^2.6.8"
170-
inline-source-map-comment "^1.0.5"
171-
json-parse-better-errors "^1.0.1"
172-
object.values "^1.0.4"
173-
opticss "^0.3.0"
174-
regexpu-core "^4.0.11"
175-
source-map "^0.6.1"
176-
watch "^1.0.2"
177-
178-
"@css-blocks/ember-cli@*":
179-
version "0.19.0"
180-
resolved "https://registry.npmjs.org/@css-blocks/ember-cli/-/ember-cli-0.19.0.tgz#86e9798277c65bbb95d725d4e55a99eee038edfd"
181-
dependencies:
182-
"@css-blocks/broccoli" "^0.19.0"
183-
"@css-blocks/glimmer" "^0.19.0"
184-
"@glimmer/application-pipeline" "^0.11.1"
185-
broccoli-funnel "^2.0.1"
186-
187-
"@css-blocks/glimmer@^0.19.0":
188-
version "0.19.0"
189-
resolved "https://registry.npmjs.org/@css-blocks/glimmer/-/glimmer-0.19.0.tgz#bb076d7de613a44114f9dbf8f4f75285a1d64217"
190-
dependencies:
191-
"@css-blocks/core" "^0.19.0"
192-
"@glimmer/compiler" "^0.33.0"
193-
"@glimmer/resolution-map-builder" "0.5.1"
194-
"@glimmer/resolver" "^0.4.3"
195-
"@glimmer/syntax" "^0.33.0"
196-
"@opticss/element-analysis" "^0.3.0"
197-
"@opticss/template-api" "^0.3.0"
198-
"@opticss/util" "^0.3.0"
199-
"@types/glob" "^5.0.30"
200-
debug "^2.6.8"
201-
glimmer-analyzer "^0.2.0"
202-
glob "^7.1.2"
203-
object.values "^1.0.4"
204-
205137
"@ember/test-helpers@^0.7.18":
206138
version "0.7.25"
207139
resolved "https://registry.npmjs.org/@ember/test-helpers/-/test-helpers-0.7.25.tgz#b4014c108b40ffaf74f3c4d5918800917541541d"
@@ -365,12 +297,6 @@
365297
dependencies:
366298
babel-plugin-glimmer-inline-precompile "^1.2.0"
367299

368-
"@glimmer/interfaces@^0.29.10":
369-
version "0.29.10"
370-
resolved "https://registry.npmjs.org/@glimmer/interfaces/-/interfaces-0.29.10.tgz#7744451ca329a42c62b08fa460808bccbddeb2ab"
371-
dependencies:
372-
"@glimmer/wire-format" "^0.29.10"
373-
374300
"@glimmer/interfaces@^0.31.0":
375301
version "0.31.0"
376302
resolved "https://registry.npmjs.org/@glimmer/interfaces/-/interfaces-0.31.0.tgz#642764765b266d5cf921f64354e06a3112551081"
@@ -472,15 +398,6 @@
472398
"@glimmer/vm" "^0.31.0"
473399
"@glimmer/wire-format" "^0.31.0"
474400

475-
"@glimmer/syntax@^0.29.1":
476-
version "0.29.10"
477-
resolved "https://registry.npmjs.org/@glimmer/syntax/-/syntax-0.29.10.tgz#2a074223fc3b42d49c8b9345684a80b5133dc030"
478-
dependencies:
479-
"@glimmer/interfaces" "^0.29.10"
480-
"@glimmer/util" "^0.29.10"
481-
handlebars "^4.0.6"
482-
simple-html-tokenizer "^0.4.1"
483-
484401
"@glimmer/syntax@^0.31.0":
485402
version "0.31.0"
486403
resolved "https://registry.npmjs.org/@glimmer/syntax/-/syntax-0.31.0.tgz#5e4f2732c39b07060a716a95e55e7a67c7d1a213"
@@ -512,10 +429,6 @@
512429
version "0.30.3"
513430
resolved "https://registry.npmjs.org/@glimmer/test-helpers/-/test-helpers-0.30.3.tgz#57bd84fba9aa498de025f8d0b4c3f0ab2e9afa97"
514431

515-
"@glimmer/util@^0.29.10":
516-
version "0.29.10"
517-
resolved "https://registry.npmjs.org/@glimmer/util/-/util-0.29.10.tgz#8662daf273ffef9254b8d943d39aa396ed7225a5"
518-
519432
"@glimmer/util@^0.31.0":
520433
version "0.31.0"
521434
resolved "https://registry.npmjs.org/@glimmer/util/-/util-0.31.0.tgz#45e353a7dcaffe6df00cc3d729153ae6121cf0e4"
@@ -536,12 +449,6 @@
536449
"@glimmer/program" "^0.31.0"
537450
"@glimmer/util" "^0.31.0"
538451

539-
"@glimmer/wire-format@^0.29.10":
540-
version "0.29.10"
541-
resolved "https://registry.npmjs.org/@glimmer/wire-format/-/wire-format-0.29.10.tgz#90e82f67a6325468d6fee4f8dd7affc1070a9557"
542-
dependencies:
543-
"@glimmer/util" "^0.29.10"
544-
545452
"@glimmer/wire-format@^0.31.0":
546453
version "0.31.0"
547454
resolved "https://registry.npmjs.org/@glimmer/wire-format/-/wire-format-0.31.0.tgz#5a12e7a770a981ce9ea3a7daf737433bd6cd03d9"
@@ -1306,12 +1213,6 @@
13061213
"@types/prop-types" "*"
13071214
csstype "^2.2.0"
13081215

1309-
"@types/recursive-readdir@^2.2.0":
1310-
version "2.2.0"
1311-
resolved "https://registry.npmjs.org/@types/recursive-readdir/-/recursive-readdir-2.2.0.tgz#b39cd5474fd58ea727fe434d5c68b7a20ba9121c"
1312-
dependencies:
1313-
"@types/node" "*"
1314-
13151216
13161217
version "0.7.8"
13171218
resolved "https://registry.npmjs.org/@types/shelljs/-/shelljs-0.7.8.tgz#4b4d6ee7926e58d7bca448a50ba442fd9f6715bd"
@@ -6663,15 +6564,6 @@ gitconfiglocal@^1.0.0:
66636564
dependencies:
66646565
ini "^1.3.2"
66656566

6666-
glimmer-analyzer@^0.2.0:
6667-
version "0.2.3"
6668-
resolved "https://registry.npmjs.org/glimmer-analyzer/-/glimmer-analyzer-0.2.3.tgz#29746c3027bd342c9930d020e4e8f5902f01fc62"
6669-
dependencies:
6670-
"@glimmer/resolution-map-builder" "^0.4.0"
6671-
"@glimmer/resolver" "^0.3.0"
6672-
"@glimmer/syntax" "^0.29.1"
6673-
simple-html-tokenizer "^0.4.1"
6674-
66756567
glimmer-analyzer@^0.3.2:
66766568
version "0.3.3"
66776569
resolved "https://registry.npmjs.org/glimmer-analyzer/-/glimmer-analyzer-0.3.3.tgz#a03e202998cb93e179f04742cb9a82a4036d3f47"
@@ -9266,7 +9158,7 @@ minimalistic-crypto-utils@^1.0.0, minimalistic-crypto-utils@^1.0.1:
92669158
version "1.0.1"
92679159
resolved "https://registry.npmjs.org/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a"
92689160

9269-
"minimatch@2 || 3", minimatch@3.0.4, minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4:
9161+
"minimatch@2 || 3", minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4:
92709162
version "3.0.4"
92719163
resolved "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
92729164
dependencies:
@@ -11164,12 +11056,6 @@ [email protected]:
1116411056
dependencies:
1116511057
minimatch "3.0.3"
1116611058

11167-
recursive-readdir@^2.2.2:
11168-
version "2.2.2"
11169-
resolved "https://registry.npmjs.org/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f"
11170-
dependencies:
11171-
minimatch "3.0.4"
11172-
1117311059
redent@^1.0.0:
1117411060
version "1.0.0"
1117511061
resolved "https://registry.npmjs.org/redent/-/redent-1.0.0.tgz#cf916ab1fd5f1f16dfb20822dd6ec7f730c2afde"

0 commit comments

Comments
 (0)