Support requiring stylesheets without an extension

Stylesheets will attempt to be resolved with both css
and less extensions if no extension is included in the
path specified to requireStylesheet().
This commit is contained in:
Kevin Sawicki
2013-03-25 23:34:51 -04:00
parent 844f5343c2
commit fab5842651
3 changed files with 32 additions and 11 deletions

View File

@@ -94,7 +94,7 @@ describe "Window", ->
$('head style[id*="css.css"]').remove()
it "synchronously loads and parses less files at the given path and installs a style tag for it in the head", ->
it "synchronously loads and parses less files at the given path and installs a style tag for it in the head", ->
lessPath = project.resolve('sample.less')
lengthBefore = $('head style').length
requireStylesheet(lessPath)
@@ -117,7 +117,16 @@ describe "Window", ->
expect($('head style').length).toBe lengthBefore + 1
$('head style[id*="sample.less"]').remove()
describe ".disableStyleSheet(path)", ->
it "supports requiring css and less stylesheets without an explicit extension", ->
requireStylesheet 'fixtures/css'
expect($('head style[id*="css.css"]').attr('id')).toBe project.resolve('css.css')
requireStylesheet 'fixtures/sample'
expect($('head style[id*="sample.less"]').attr('id')).toBe project.resolve('sample.less')
$('head style[id*="css.css"]').remove()
$('head style[id*="sample.less"]').remove()
describe ".removeStylesheet(path)", ->
it "removes styling applied by given stylesheet path", ->
cssPath = require.resolve(fs.join("fixtures", "css.css"))

View File

@@ -24,14 +24,14 @@ window.setUpEnvironment = ->
$(document).on 'keydown', keymap.handleKeyEvent
keymap.bindDefaultKeys()
requireStylesheet 'reset.less'
requireStylesheet 'atom.less'
requireStylesheet 'overlay.less'
requireStylesheet 'popover-list.less'
requireStylesheet 'notification.less'
requireStylesheet 'markdown.less'
requireStylesheet 'reset'
requireStylesheet 'atom'
requireStylesheet 'overlay'
requireStylesheet 'popover-list'
requireStylesheet 'notification'
requireStylesheet 'markdown'
if nativeStylesheetPath = fs.resolveOnLoadPath("#{process.platform}.css")
if nativeStylesheetPath = fs.resolveOnLoadPath(process.platform, ['css', 'less'])
requireStylesheet(nativeStylesheetPath)
# This method is only called when opening a real application window
@@ -111,8 +111,14 @@ window.deserializeWindowState = ->
window.stylesheetElementForId = (id) ->
$("head style[id='#{id}']")
window.resolveStylesheet = (path) ->
if fs.extension(path).length > 0
fs.resolveOnLoadPath(path)
else
fs.resolveOnLoadPath(path, ['css', 'less'])
window.requireStylesheet = (path) ->
if fullPath = require.resolve(path)
if fullPath = window.resolveStylesheet(path)
content = window.loadStylesheet(fullPath)
window.applyStylesheet(fullPath, content)
else
@@ -128,7 +134,7 @@ window.loadStylesheet = (path) ->
content
window.removeStylesheet = (path) ->
unless fullPath = require.resolve(path)
unless fullPath = window.resolveStylesheet(path)
throw new Error("Could not find a file at path '#{path}'")
window.stylesheetElementForId(fullPath).remove()

View File

@@ -218,6 +218,12 @@ module.exports =
pathToResolve = args.pop()
loadPaths = args
if pathToResolve[0] is '/'
if extensions and resolvedPath = @resolveExtension(pathToResolve, extensions)
return resolvedPath
else
return pathToResolve if @exists(pathToResolve)
for loadPath in loadPaths
candidatePath = @join(loadPath, pathToResolve)
if extensions