-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the ability to use import in sass files (#1048)
* added the ability to use import in sass files * fixed one test * improved the file detection a bit so that css files etc are no longer handled
- Loading branch information
Showing
10 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
wro4j-core/src/main/java/ro/isdc/wro/model/resource/locator/SaasUriLocator.java
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2017. All rights reserved. | ||
*/ | ||
package ro.isdc.wro.model.resource.locator; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.apache.commons.io.FilenameUtils; | ||
import org.apache.commons.lang3.Validate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import ro.isdc.wro.model.resource.locator.support.LocatorProvider; | ||
|
||
/** | ||
* Adjusted loader class for saas resources "scss" files. As so imports can now the achieved using | ||
* the following configuration in the <b>wro.properties</b>: | ||
* | ||
* <pre> | ||
* managerFactoryClassName=ro.isdc.wro.manager.factory.ConfigurableWroManagerFactory | ||
* preProcessors=lessCssImport | ||
* postProcessors=rubySassCss | ||
* uriLocators=webjar,saasUri,uri,classpath | ||
* </pre> | ||
* | ||
* @author Paul Sterl | ||
* @created Created on Jun 22 2017 | ||
*/ | ||
public class SaasUriLocator implements UriLocator { | ||
private static final Logger LOG = LoggerFactory.getLogger(SaasUriLocator.class); | ||
/** | ||
* Alias used to register this locator with {@link LocatorProvider}. | ||
*/ | ||
public static final String ALIAS = "saasUri"; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public boolean accept(final String url) { | ||
if (url == null) return false; | ||
final String extension = FilenameUtils.getExtension(url); | ||
// scss file have either no extension or scss | ||
// maybe check for the "_"? | ||
if ("".equals(extension) || "scss".equals(extension)) { | ||
boolean result = getScssFile(url) != null; | ||
if (!result) { | ||
LOG.debug("Possible scss file not found {}", url); | ||
} | ||
return result; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
File getScssFile(String url) { | ||
if (url == null) return null; | ||
File result; | ||
// remove any "file:" at start | ||
if (url.startsWith("file:")) url = url.replace("file:", ""); | ||
|
||
result = new File(url); | ||
if (result.isFile()) return result; | ||
result = new File(url + ".scss"); | ||
if (result.isFile()) return result; | ||
|
||
// if we don't have a scss file end the ending isn't a / it is most likely an import | ||
if (!url.endsWith(".scss") && !url.endsWith("/")) { | ||
final int lastSlash = url.lastIndexOf('/') + 1; | ||
String cleanUrl = url.substring(0, lastSlash); | ||
cleanUrl = cleanUrl + "_" + url.substring(lastSlash, url.length()) + ".scss"; | ||
result = new File(cleanUrl); | ||
} else { | ||
result = new File(url); | ||
} | ||
|
||
if (result.isFile()) return result; | ||
else return null; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public InputStream locate(final String uri) throws IOException { | ||
Validate.notNull(uri, "URI cannot be NULL!"); | ||
LOG.debug("loading scss file: {}", uri); | ||
return new FileInputStream(getScssFile(uri)); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
wro4j-core/src/test/java/ro/isdc/wro/model/resource/locator/TestSaasUriLocator.java
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ro.isdc.wro.model.resource.locator; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.File; | ||
|
||
import org.junit.Test; | ||
|
||
public class TestSaasUriLocator { | ||
|
||
@Test | ||
public void testSaasImport() throws Exception { | ||
final String base = new File("./src/test/resources/sass/").getAbsolutePath() + "/"; | ||
SaasUriLocator saasUriLocator = new SaasUriLocator(); | ||
|
||
assertTrue(saasUriLocator.accept(base + "_sassimport")); | ||
assertTrue(saasUriLocator.accept(base + "style.scss")); | ||
assertFalse(saasUriLocator.accept(base + "style.css")); | ||
|
||
assertNotNull(saasUriLocator.locate(base + "_sassimport")); | ||
assertNotNull(saasUriLocator.locate(base + "style")); | ||
|
||
assertNotNull(saasUriLocator.locate("file:" + base + "style")); | ||
assertEquals(new File(base + "style.scss"), saasUriLocator.getScssFile("file:/" + base + "style")); | ||
} | ||
} |
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
Empty file.
Empty file.
Empty file.
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