-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved syntax for including styles
- Loading branch information
Erik Rasmussen
committed
Jul 31, 2015
1 parent
72cf1f8
commit 44f0d5d
Showing
8 changed files
with
47 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,39 @@ | ||
import path from 'path'; | ||
|
||
const pathToSrc = path.resolve(__dirname, '.'); | ||
const readStats = () => { | ||
// don't cache the `webpack-stats.json` on dev so we read the file on each request. | ||
// on production, use simple `require` to cache the file | ||
const stats = require('../webpack-stats.json'); | ||
if (__DEVELOPMENT__) { | ||
delete require.cache[require.resolve('../webpack-stats.json')]; | ||
} | ||
return stats; | ||
} | ||
|
||
export function relativeToSrc(value) { | ||
return value.slice(pathToSrc.length); | ||
export function requireServerCss(cssPath) { | ||
if (__CLIENT__) { | ||
throw new Error('image-resolver called on browser'); | ||
} | ||
return readStats().css.modules[cssPath.slice(__dirname.length)]; | ||
} | ||
|
||
export function requireServerImage(imagePath) { | ||
if (!imagePath) { | ||
return ''; | ||
} | ||
if (__CLIENT__) { | ||
throw new Error('image-resolver called on browser'); | ||
} else { | ||
// Load images compiled from `webpack-stats` | ||
// don't cache the `webpack-stats.json` on dev | ||
// so we gonna read the file on each request | ||
// on production, use simple `require` to cache the file | ||
let images = require('../webpack-stats.json').images; | ||
if (__DEVELOPMENT__) { | ||
delete require.cache[require.resolve('../webpack-stats.json')]; | ||
} | ||
if (!images) { | ||
return ''; | ||
} | ||
throw new Error('server-side only resolver called on client'); | ||
} | ||
const images = readStats().images; | ||
if (!images) { | ||
return ''; | ||
} | ||
|
||
// Find the correct image | ||
const regex = new RegExp(`${imagePath}$`); | ||
const image = images.find(img => regex.test(img.original)); | ||
// Find the correct image | ||
const regex = new RegExp(`${imagePath}$`); | ||
const image = images.find(img => regex.test(img.original)); | ||
|
||
// Serve image. | ||
if (image) return image.compiled; | ||
// Serve image. | ||
if (image) return image.compiled; | ||
|
||
// Serve a not-found asset maybe? | ||
return ''; | ||
} | ||
// Serve a not-found asset maybe? | ||
return ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This should probably be removed?