Skip to content

Commit fec42e4

Browse files
Tim Lindvalltimlindvall
Tim Lindvall
authored andcommittedSep 3, 2020
feat: End-to-end sourcemaps for ember v2 pipeline.
- Use raw Compiled CSS contents in the build pipeline. Trying to use something else causes misaligment with embedded sourcemap char data. - Remove some sourcemap logic from the prev. commit that's no longer necessary.
1 parent da47ce6 commit fec42e4

File tree

9 files changed

+35
-30
lines changed

9 files changed

+35
-30
lines changed
 

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

+1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ export class BlockFactory {
364364

365365
// Merge the rules from the CSS contents into the Block.
366366
block.precompiledStylesheet = cssContentsAst;
367+
block.precompiledStylesheetUnedited = file.rawCssContents;
367368
this._mergeCssRulesIntoDefinitionBlock(block, cssContentsAst, file);
368369

369370
// And we're done!

‎packages/@css-blocks/core/src/BlockTree/Block.ts

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ export class Block
8383
* If this is set, the stylesheet property is only the definition file contents.
8484
*/
8585
public precompiledStylesheet: postcss.Root | undefined;
86+
/**
87+
* The full contents of the compiled CSS this block was built from, if available.
88+
* This content is copied verbatim from the file system and has not been transformed
89+
* or processed by PostCSS in any way.
90+
*/
91+
public precompiledStylesheetUnedited: string | undefined;
8692
public blockReferencePaths: Map<string, Block>;
8793
private _resolveImplementedBlocksResult: Set<Block> | undefined;
8894

‎packages/@css-blocks/core/src/PrecompiledDefinitions/compiled-comments.ts

-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ export const REGEXP_COMMENT_DEFINITION_REF = /^\/\*#blockDefinitionURL=([\S]+?)\
1616
*/
1717
export const REGEXP_COMMENT_FOOTER = /^\/\*#css-blocks end\*\/\r?\n?/m;
1818

19-
/**
20-
* A regular expression that can be used to find the included sourcemap in the file.
21-
*/
22-
export const REGEXP_SOURCEMAP_COMMENT = /(^\/\*#\s*sourceMappingURL=\S+\s*\*\/$|^\/\/#\s*sourceMappingURL=\S+\s*$)/m;
23-
2419
/**
2520
* Determines if the given URL for a definition is valid.
2621
*

‎packages/@css-blocks/core/src/importing/BaseImporter.ts

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Syntax } from "../BlockParser";
22
import { ResolvedConfiguration } from "../configuration";
3-
import { REGEXP_COMMENT_DEFINITION_REF, REGEXP_COMMENT_FOOTER, REGEXP_COMMENT_HEADER, REGEXP_SOURCEMAP_COMMENT } from "../PrecompiledDefinitions/compiled-comments";
3+
import { REGEXP_COMMENT_DEFINITION_REF, REGEXP_COMMENT_FOOTER, REGEXP_COMMENT_HEADER } from "../PrecompiledDefinitions/compiled-comments";
44

55
import { FileIdentifier, ImportedCompiledCssFile, ImportedCompiledCssFileContents, ImportedFile, Importer } from "./Importer";
66

@@ -103,32 +103,14 @@ export abstract class BaseImporter implements Importer {
103103
}
104104
const [definitionFullMatch, definitionUrl] = definitionRegexpResult;
105105
const blockCssContents = fullBlockContents.replace(definitionFullMatch, "");
106-
const blockContentsWithSourcemap = this.mergeSourcemapIntoCompiledCssContents(blockCssContents, post);
107106

108107
return {
109108
pre,
110109
blockId,
111-
blockCssContents: blockContentsWithSourcemap,
110+
blockCssContents,
112111
definitionUrl,
113112
post,
113+
raw: contents,
114114
};
115115
}
116-
117-
/**
118-
* Look for sourcemap data in given post-blocks content. If found, append it to the given CSS contents.
119-
* @param cssContents - The CSS contents of the Compiled CSS file (what appears between the CSS Blocks comments).
120-
* @param postContent - Content that appears in the content after the closing CSS Blocks comment.
121-
* @returns The CSS Contents with sourcemap appended, if found. Returns CSS contents alone if not found.
122-
*/
123-
private mergeSourcemapIntoCompiledCssContents(cssContents: string, postContent: string) {
124-
const sourcemapRegexpResult = postContent.match(REGEXP_SOURCEMAP_COMMENT);
125-
126-
// No sourcemap? Just return the CSS contents.
127-
if (sourcemapRegexpResult === null) {
128-
return cssContents;
129-
}
130-
131-
// Otherwise, append the sourcemap to the end of the CSS contents.
132-
return `${cssContents}\n${sourcemapRegexpResult[0]}`;
133-
}
134116
}

‎packages/@css-blocks/core/src/importing/Importer.ts

+15
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ export interface ImportedCompiledCssFileContents {
8686
* File contents after the CSS Blocks footer comment.
8787
*/
8888
post: string;
89+
90+
/**
91+
* The unedited full file contents. This content will be added to the
92+
* ImportedCompiledCssFile object so that it's accessible to build
93+
* plugins later on.
94+
*/
95+
raw: string;
8996
}
9097

9198
/**
@@ -141,6 +148,14 @@ export interface ImportedCompiledCssFile {
141148
* an error.
142149
*/
143150
defaultName: string;
151+
152+
/**
153+
* The unedited file contents of the Compiled CSS file.
154+
* In many cases when integrating blocks into a build pipeline,
155+
* you can use this to build instead of rebuilding from the
156+
* Block representation or the cssContents property.
157+
*/
158+
rawCssContents: string;
144159
}
145160

146161
/**

‎packages/@css-blocks/core/src/importing/NodeJsImporter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ export class NodeJsImporter extends BaseImporter {
197197
definitionContents: dfnData,
198198
definitionIdentifier,
199199
defaultName: this.defaultName(identifier, config),
200+
rawCssContents: contents,
200201
};
201202
} else {
202203
return {

‎packages/@css-blocks/core/test/util/MockImportRegistry.ts

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class MockImporter extends NodeJsImporter {
6161
definitionContents: source.dfnContents,
6262
blockId: blockId,
6363
defaultName: this.defaultName(resolvedPath, configuration),
64+
rawCssContents: source.contents,
6465
};
6566
} else if (source.hasEmbeddedDfnData) {
6667
const parsedSourceContents = this.segmentizeCompiledBlockCSS(source.contents);
@@ -77,6 +78,7 @@ export class MockImporter extends NodeJsImporter {
7778
definitionContents,
7879
blockId: blockId,
7980
defaultName: this.defaultName(resolvedPath, configuration),
81+
rawCssContents: source.contents,
8082
};
8183
} else {
8284
return {

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ export class CSSBlocksApplicationPlugin extends Filter {
6464
let compiler = new BlockCompiler(postcss, config);
6565
let reservedClassnames = analyzer.reservedClassNames();
6666
for (let block of blocksUsed) {
67-
let content: postcss.Result;
67+
let content: postcss.Result | string;
6868
let filename = importer.debugIdentifier(block.identifier, config);
69-
if (block.precompiledStylesheet) {
70-
debug(`Optimizing precompiled stylesheet for ${filename}`);
71-
content = block.precompiledStylesheet.toResult();
69+
if (block.precompiledStylesheetUnedited) {
70+
// We don't need to do any processing on the compiled css content, just
71+
// add it to the output.
72+
debug(`Looking up unedited Compiled CSS content for ${filename}`);
73+
content = block.precompiledStylesheetUnedited;
7274
} else {
7375
debug(`Compiling stylesheet for optimization of ${filename}`);
7476
// XXX Do we need to worry about reservedClassnames here?

‎packages/@css-blocks/ember-utils/src/BroccoliTreeImporter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export class BroccoliTreeImporter extends BaseImporter {
129129
definitionContents: dfnData,
130130
definitionIdentifier,
131131
defaultName: this.defaultName(identifier, config),
132+
rawCssContents: contents,
132133
};
133134
} else {
134135
return {

0 commit comments

Comments
 (0)
Please sign in to comment.