-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Serve theme static files from VAADIN/static (#9451)
Static files in META-INF/VAADIN/static will now be served on request to VAADIN/static. Added new webpack loader that changes app theme css urls targeting ./ and ../ to be VAADIN/static/ instead. Where ../ can not go outside of the application theme folder. Fixes #9405 # Conflicts: # flow-server/src/main/java/com/vaadin/flow/server/StaticFileServer.java # flow-server/src/main/java/com/vaadin/flow/server/frontend/NodeUpdater.java # flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskUpdateWebpack.java
- Loading branch information
Showing
28 changed files
with
328 additions
and
58 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
5 changes: 3 additions & 2 deletions
5
flow-server/src/main/resources/plugins/application-theme-plugin/package.json
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
19 changes: 19 additions & 0 deletions
19
flow-server/src/main/resources/plugins/theme-loader/package.json
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,19 @@ | ||
{ | ||
"description": "theme-loader updates css urls targeting './' to instead be 'VAADIN/static/' as used by app theme", | ||
"keywords": [ | ||
"plugin", | ||
"application theme" | ||
], | ||
"repository": "vaadin/flow", | ||
"name": "@vaadin/theme-loader", | ||
"version": "0.0.1", | ||
"main": "theme-loader.js", | ||
"author": "Vaadin Ltd", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/vaadin/flow/issues" | ||
}, | ||
"files": [ | ||
"theme-loader.js" | ||
] | ||
} |
45 changes: 45 additions & 0 deletions
45
flow-server/src/main/resources/plugins/theme-loader/theme-loader.js
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,45 @@ | ||
const loaderUtils = require("loader-utils"); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
// Collect groups [url(] [ |'|"]optional './', file part and end of url | ||
const urlMatcher = /(url\()(\'|\")?(\.\/|\.\.\/)(\S*)(\2\))/g; | ||
|
||
/** | ||
* This custom loader handles rewriting urls for the application theme css files. | ||
* URLs starting with ./ or ../ are checked against the filesystem and converted if a file exists. | ||
* URLs going outside of the application theme folder are not accepted and will not be rewritten. | ||
* | ||
* @param source file contents to handle | ||
* @param map source map for file | ||
*/ | ||
module.exports = function (source, map) { | ||
const options = loaderUtils.getOptions(this); | ||
const handledResourceFolder = path.dirname(this._module.resource); | ||
const logger = this.getLogger("theme-loader"); | ||
|
||
let themeFolder = handledResourceFolder; | ||
while (themeFolder && path.basename(path.resolve(themeFolder, "..")) !== "theme") { | ||
themeFolder = path.resolve(themeFolder, ".."); | ||
} | ||
logger.log("Using '", themeFolder, "' for the application theme base folder."); | ||
|
||
source = source.replace(urlMatcher, function (match, url, quoteMark, replace, fileUrl, endString) { | ||
let absolutePath = path.resolve(handledResourceFolder, replace, fileUrl); | ||
if (fs.existsSync(absolutePath) && absolutePath.startsWith(themeFolder)) { | ||
logger.debug("Updating url for file '", replace, fileUrl, "' to use 'VAADIN/static'"); | ||
const pathResolved = absolutePath.substring(themeFolder.length).replace(/\\/g, '/'); | ||
|
||
// keep the url the same except replace the ./ to VAADIN/static | ||
if (quoteMark) { | ||
return url + quoteMark + 'VAADIN/static' + pathResolved + endString; | ||
} | ||
return url + 'VAADIN/static' + pathResolved + endString; | ||
} else if (options.devMode) { | ||
logger.info("No rewrite for '", match, "' as the file was not found."); | ||
} | ||
return match; | ||
}); | ||
|
||
this.callback(null, source, map); | ||
} |
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,6 +1,7 @@ | ||
{ | ||
"plugins": [ | ||
"stats-plugin", | ||
"application-theme-plugin" | ||
"application-theme-plugin", | ||
"theme-loader" | ||
] | ||
} |
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
File renamed without changes.
File renamed without changes.
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
13 changes: 13 additions & 0 deletions
13
...hemes-legacy/src/main/webapp/frontend/bower_components/my-component/src/my-component.html
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,13 @@ | ||
<link rel="import" href="../../../bower_components/polymer/polymer-element.html"> | ||
|
||
<dom-module id="my-component"> | ||
<template> | ||
<div id="component">My component</div> | ||
</template> | ||
<script> | ||
class MyComponent extends Polymer.Element { | ||
static get is() { return 'my-component' } | ||
} | ||
customElements.define(MyComponent.is, MyComponent); | ||
</script> | ||
</dom-module> |
22 changes: 22 additions & 0 deletions
22
...cy/src/main/webapp/frontend/bower_components/my-component/theme/myTheme/my-component.html
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,22 @@ | ||
<link rel="import" href="../../../../bower_components/polymer/polymer-element.html"> | ||
<link rel="import" href="../../src/my-component.html"> | ||
|
||
<dom-module id="my-component"> | ||
<script> | ||
customElements.whenDefined('my-component').then(() => { | ||
let element = document.createElement("div"); | ||
element.setAttribute("id", "theme-component"); | ||
element.innerHTML = "My themed component"; | ||
|
||
function _append(){ | ||
if ( document.body ){ | ||
document.body.appendChild(element); | ||
} | ||
else { | ||
setTimeout(_append); | ||
} | ||
} | ||
_append(); | ||
}); | ||
</script> | ||
</dom-module> |
35 changes: 35 additions & 0 deletions
35
...acy/src/main/webapp/frontend/bower_components/themed-template/src/com/ThemedTemplate.html
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,35 @@ | ||
<!-- | ||
~ Copyright 2000-2019 Vaadin Ltd. | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
~ use this file except in compliance with the License. You may obtain a copy of | ||
~ the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
~ License for the specific language governing permissions and limitations under | ||
~ the License. | ||
--> | ||
<!-- Using an absolute URL is a bad practice, but the nesting depth here makes it incovenient traverse up with ../.. --> | ||
<link rel="import" href="/frontend/bower_components/polymer/polymer-element.html"> | ||
<link rel="import" href="relative1.html"> | ||
<link rel="import" href="../relative2.html"> | ||
<link rel="import" href="/frontend/absolute.html"> | ||
|
||
<dom-module id="themed-template"> | ||
<template> | ||
<div id='div'>Themed Template</div> | ||
</template> | ||
|
||
<script> | ||
class ThemedTemplate extends Polymer.Element { | ||
static get is() { | ||
return 'themed-template' | ||
} | ||
} | ||
customElements.define(ThemedTemplate.is, ThemedTemplate); | ||
</script> | ||
</dom-module> |
35 changes: 35 additions & 0 deletions
35
...in/webapp/frontend/bower_components/themed-template/theme/myTheme/com/ThemedTemplate.html
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,35 @@ | ||
<!-- | ||
~ Copyright 2000-2019 Vaadin Ltd. | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
~ use this file except in compliance with the License. You may obtain a copy of | ||
~ the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
~ License for the specific language governing permissions and limitations under | ||
~ the License. | ||
--> | ||
<!-- Using an absolute URL is a bad practice, but the nesting depth here makes it incovenient traverse up with ../.. --> | ||
<link rel="import" href="/frontend/bower_components/polymer/polymer-element.html"> | ||
|
||
<dom-module id="themed-template"> | ||
<template> | ||
<div id='div'>Themed Template</div> | ||
<themed-relative1 id='relative1'></themed-relative1> | ||
<themed-relative2 id='relative2'></themed-relative2> | ||
<themed-absolute id='absolute'></themed-absolute> | ||
</template> | ||
|
||
<script> | ||
class ThemedTemplate extends Polymer.Element { | ||
static get is() { | ||
return 'themed-template' | ||
} | ||
} | ||
customElements.define(ThemedTemplate.is, ThemedTemplate); | ||
</script> | ||
</dom-module> |
Oops, something went wrong.