1+ const debug = require ( 'debug' ) ( 'canvas_css:compile-bundle' )
12import Promise from 'bluebird'
23const existsAsync = Promise . promisify ( require ( 'fs' ) . stat )
34const sassRender = Promise . promisify ( require ( 'node-sass' ) . render )
4- const debug = require ( 'debug' ) ( 'canvas_css:compile-bundle' )
55import path from 'path'
66import _ from 'lodash'
77import chalk from 'chalk'
88import url from 'url'
99import postcss from 'postcss'
1010import autoprefixer from 'autoprefixer'
1111import postcssUrl from 'postcss-url'
12- import PATHS from './paths'
13- import { ofFileSync } from './checksum'
12+ import { paths as PATHS } from "./config"
13+ import { BRANDABLE_VARIANTS } from './variants'
14+ import { fileChecksumSync } from './checksum'
1415import supportedBrowsers from './browser-support'
1516import cache from './cache'
16- import { relativeSassPath } from './utils'
17+ import { relativeSassPath , folderForBrandId } from './utils'
18+
19+ // If an image is in css source as url("/images/foo/bar.png"),
20+ // Rails-asset-pipeline makes it available at the url: "/assets/foo/bar-{md5}.png"
21+ function removeFirstDir ( dir ) {
22+ return dir . split ( '/' ) . slice ( 2 ) . join ( '/' )
23+ }
24+ function sprocketsFormattedUrl ( originalUrl , md5 ) {
25+ let parsedUrl = url . parse ( originalUrl )
26+ const { dir, name, ext} = path . posix . parse ( parsedUrl . pathname )
27+ parsedUrl . pathname = `/assets/${ removeFirstDir ( dir ) } /${ name } -${ md5 } ${ ext } `
28+ return url . format ( parsedUrl )
29+ }
1730
18- // Any logging from compileSingleBundle needs to go to stderr so we can use stdout to
19- // send css to rails controller when we compile a single bundle
2031function warn ( ) {
2132 console . error ( chalk . yellow ( 'canvas_css warning' , ...arguments ) )
2233}
2334
24- export default async function compileSingleBundle ( { bundleName, variant, brandVariablesFolder } ) {
35+ export default async function compileSingleBundle ( { bundleName, variant, brandId } ) {
2536 const sassFile = path . join ( PATHS . sass_dir , bundleName )
2637 let includePaths = [ PATHS . sass_dir , path . join ( PATHS . sass_dir , 'variants' , variant ) ]
2738 // pull in 'config/brand_variables.scss' if we should
28- if ( brandVariablesFolder ) {
29- if (
30- ( variant === 'new_styles_normal_contrast' || variant === 'k12_normal_contrast' ) &&
31- await existsAsync ( path . join ( brandVariablesFolder , '_brand_variables.scss' ) )
32- ) {
33- includePaths . unshift ( brandVariablesFolder )
34- } else {
35- throw new Error ( 'invalid brandVariablesFolder or you tried to include it in a legacy or high contrast bundle' )
36- }
39+ if ( brandId ) {
40+ if ( ! BRANDABLE_VARIANTS . has ( variant ) ) throw new Error ( `${ variant } is not brandable` )
41+ const fileExists = await existsAsync ( path . join ( folderForBrandId ( brandId ) , '_brand_variables.scss' ) )
42+ if ( ! fileExists ) throw new Error ( `_brand_variables.scss file not found for ${ brandId } ` )
43+ includePaths . unshift ( folderForBrandId ( brandId ) )
3744 }
3845
3946 let urlsFoundInCss = new Set ( )
@@ -47,18 +54,15 @@ export default async function compileSingleBundle ({bundleName, variant, brandVa
4754 const relativePath = relativeSassPath ( pathToFile )
4855 let md5 = cache . file_checksums . data [ relativePath ]
4956 if ( ! md5 ) {
50- md5 = ofFileSync ( pathToFile )
57+ md5 = fileChecksumSync ( pathToFile )
5158 if ( ! md5 ) {
5259 warn ( sassFile , variant , 'contains a url() to:' , originalUrl , 'which doesn\'t exist on disk' )
5360 return originalUrl
5461 }
5562 cache . file_checksums . update ( relativePath , md5 )
5663 }
5764 urlsFoundInCss . add ( pathToFile )
58-
59- const { dir, name, ext} = path . posix . parse ( parsedUrl . pathname )
60- parsedUrl . pathname = `/assets${ dir } /${ name } -${ md5 } ${ ext } `
61- return url . format ( parsedUrl )
65+ return sprocketsFormattedUrl ( originalUrl , md5 )
6266 }
6367
6468 const startTime = new Date ( )
0 commit comments