-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stark-build): upgrade to Angular 8
BREAKING CHANGE: Support for `htmlWebpackPlugin.options` has been removed. A new support for `starkAppMetadata`, `starkAppConfig` and `metadata` has been implemented. You can now use `starkOptions` instead of `htmlWebpackPlugin.options`. See the following example: Before: ```html <%= htmlWebpackPlugin.options.starkAppMetadata.name %> <!-- or --> <%= htmlWebpackPlugin.options.starkAppConfig.defaultLanguage %> <!-- or --> <%= htmlWebpackPlugin.options.metadata.TITLE %> ``` After: ```html <%= starkOptions.starkAppMetadata.name %> <!-- or --> <%= starkOptions.starkAppConfig.defaultLanguage %> <!-- or --> <%= starkOptions.metadata.TITLE %> ``` BREAKING CHANGE: Adapt "angular.json" file as follows: ```text { //... "projects": { "<project_name>": { "architect": { "build": { // ... // /!\ Add the following line "indexTransform": "./node_modules/@nationalbankbelgium/stark-build/config/index-html.transform.js", // ... }, "serve": { // Edit the following line. // Before: // "builder": "@angular-builders/dev-server:generic", // Now: "builder": "@angular-builders/custom-webpack:dev-server", // ... } } } } } ``` BREAKING CHANGE: Adapt the "index.html" as follows: ```html <html lang="en"> <head> <!-- ... --> <!-- Adapt the title tag as follows --> <!-- Before: --> <title><%= htmlWebpackPlugin.options.starkAppMetadata.name %></title> <!-- After: --> <title>%starkAppMetadata.name%</title> <!-- /!\ Remove the following lines --> <meta name="description" content="<%= htmlWebpackPlugin.options.starkAppMetadata.description %>" /> <% if (webpackConfig.htmlElements.headTags) { %> <!--Configured Head Tags --> <%= webpackConfig.htmlElements.headTags %> <% } %> <!-- ... --> </head> <!-- --> </html> ``` BREAKING CHANGE: Add the "config/index-head-config.js" the "description" meta in as follows: ```text { links: [ // ... ], meta: [ // ... { name: "description", content: "%starkAppMetadata.description%" }, ] } ```
- Loading branch information
1 parent
2bc701e
commit c3c83b6
Showing
6 changed files
with
3,918 additions
and
4,193 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// This code has been copied from: https://github.com/fulls1z3/html-elements-webpack-plugin/blob/master/lib/html-elements-webpack-plugin.js | ||
|
||
const RE_ENDS_WITH_BS = /\/$/; | ||
|
||
/** | ||
* Create an HTML tag with attributes from a map. | ||
* | ||
* Example: | ||
* createTag('link', { rel: "manifest", href: "/assets/manifest.json" }) | ||
* // <link rel="manifest" href="/assets/manifest.json"> | ||
* @param tagName The name of the tag | ||
* @param attrMap A Map of attribute names (keys) and their values. | ||
* @param publicPath a path to add to eh start of static asset url | ||
* @returns {string} | ||
*/ | ||
function createTag(tagName, attrMap, publicPath) { | ||
publicPath = publicPath || ""; | ||
|
||
// add trailing slash if we have a publicPath and it doesn't have one. | ||
if (publicPath && !RE_ENDS_WITH_BS.test(publicPath)) { | ||
publicPath += "/"; | ||
} | ||
|
||
const attributes = Object.getOwnPropertyNames(attrMap) | ||
.filter(function (name) { | ||
return name[0] !== "="; | ||
}) | ||
.map(function (name) { | ||
var value = attrMap[name]; | ||
|
||
if (publicPath) { | ||
// check if we have explicit instruction, use it if so (e.g: =herf: false) | ||
// if no instruction, use public path if it's href attribute. | ||
const usePublicPath = attrMap.hasOwnProperty("=" + name) ? !!attrMap["=" + name] : name === "href"; | ||
|
||
if (usePublicPath) { | ||
// remove a starting trailing slash if the value has one so we wont have // | ||
value = publicPath + (value[0] === "/" ? value.substr(1) : value); | ||
} | ||
} | ||
|
||
return `${name}="${value}"`; | ||
}); | ||
|
||
const closingTag = tagName === "script" ? "</script>" : ""; | ||
|
||
return `<${tagName} ${attributes.join(" ")}>${closingTag}`; | ||
} | ||
|
||
/** | ||
* Returns a string representing all html elements defined in a data source. | ||
* | ||
* Example: | ||
* | ||
* const ds = { | ||
* link: [ | ||
* { rel: "apple-touch-icon", sizes: "57x57", href: "/assets/icon/apple-icon-57x57.png" } | ||
* ], | ||
* meta: [ | ||
* { name: "msapplication-TileColor", content: "#00bcd4" } | ||
* ] | ||
* } | ||
* | ||
* getHeadTags(ds); | ||
* // "<link rel="apple-touch-icon" sizes="57x57" href="/assets/icon/apple-icon-57x57.png">" | ||
* "<meta name="msapplication-TileColor" content="#00bcd4">" | ||
* | ||
* @returns {string} | ||
*/ | ||
function getHtmlElementString(dataSource, publicPath) { | ||
return Object.getOwnPropertyNames(dataSource) | ||
.map(function (name) { | ||
if (Array.isArray(dataSource[name])) { | ||
return dataSource[name].map(function (attrs) { | ||
return createTag(name, attrs, publicPath); | ||
}); | ||
} else { | ||
return [createTag(name, dataSource[name], publicPath)]; | ||
} | ||
}) | ||
.reduce(function (arr, curr) { | ||
return arr.concat(curr); | ||
}, []) | ||
.join("\n\t"); | ||
} | ||
|
||
module.exports = { | ||
getHtmlElementString | ||
}; |
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,90 @@ | ||
const helpers = require("./helpers"); | ||
const fs = require("fs"); | ||
const commonData = require("./webpack.common-data.js"); // common configuration between environments | ||
const HtmlHeadElements = require("./html-head-elements"); | ||
const ngCliUtils = require("./ng-cli-utils"); | ||
const buildUtils = require("./build-utils"); | ||
|
||
const isProd = ngCliUtils.getNgCliCommandOption("prod"); | ||
const isHMR = | ||
ngCliUtils.getNgCliCommandOption("configuration") === "hmr" || | ||
ngCliUtils.hasNgCliCommandOption("hmr") || | ||
buildUtils.ANGULAR_APP_CONFIG.buildOptions.hmr; | ||
|
||
const METADATA = Object.assign({}, buildUtils.DEFAULT_METADATA, { | ||
ENV: isProd ? "production" : "development", | ||
BASE_URL: ngCliUtils.getNgCliCommandOption("baseHref") || buildUtils.ANGULAR_APP_CONFIG.buildOptions.baseHref, | ||
HMR: isHMR, | ||
AOT: ngCliUtils.hasNgCliCommandOption("aot") || buildUtils.ANGULAR_APP_CONFIG.buildOptions.aot, | ||
WATCH: | ||
!(ngCliUtils.hasNgCliCommandOption("watch") && ngCliUtils.getNgCliCommandOption("watch") === "false") && | ||
!(buildUtils.ANGULAR_APP_CONFIG.buildOptions.watch === false), // by default is true | ||
environment: isHMR ? "hmr" : "dev", | ||
IS_DEV_SERVER: helpers.hasProcessFlag("serve") && !isProd // NG CLI command 'serve" | ||
}); | ||
|
||
/** | ||
* Check if one or more placeholders are present in the generated "index.html" and replace them | ||
* with the associated value if exists in "starkAppMetadata", "starkAppConfig" or "metatada" | ||
* | ||
* The placeholder should be like this: | ||
```html | ||
<%= starkOptions.starkAppMetadata.name %> | ||
<!-- or --> | ||
<%= starkOptions.starkAppConfig.defaultLanguage %> | ||
<!-- or --> | ||
<%= starkOptions.metadata.TITLE %> | ||
``` | ||
* | ||
* @param indexHtml | ||
* @returns {string} | ||
*/ | ||
function replacePlaceholdersByValues(indexHtml) { | ||
const regex = /<%=\sstarkOptions\.(starkAppMetadata|starkAppConfig|metadata)\.\w+\s%>/g; | ||
|
||
const getRealValue = (placeholder) => { | ||
const str = placeholder.slice(4, -3).split("."); | ||
if (str.length === 3) { | ||
const configName = str[1]; | ||
const property = str[2]; | ||
|
||
let value; | ||
|
||
if (configName === "metadata") { | ||
value = METADATA[property]; | ||
} else { | ||
value = commonData[configName][property]; | ||
} | ||
|
||
if (value) { | ||
return value; | ||
} | ||
} | ||
|
||
return placeholder; | ||
}; | ||
|
||
return indexHtml.replace(regex, getRealValue); | ||
} | ||
|
||
module.exports = (targetOptions, indexHtml) => { | ||
let indexHtmlToReturn = indexHtml; | ||
|
||
/** | ||
* Generate html tags based on javascript maps. | ||
* | ||
* If a publicPath is set in the webpack output configuration, it will be automatically added to | ||
* href attributes, you can disable that by adding a "=href": false property. | ||
* You can also enable it to other attribute by settings "=attName": true. | ||
* | ||
* The configuration supplied is map between a location (key) and an element definition object | ||
*/ | ||
if (fs.existsSync(helpers.root("config/index-head-config.js"))) { | ||
const i = indexHtml.indexOf("</head>"); | ||
indexHtmlToReturn = `${indexHtml.slice(0, i)} | ||
${HtmlHeadElements.getHtmlElementString(require(helpers.root("config/index-head-config")), METADATA.BASE_URL)} | ||
${indexHtml.slice(i)}`; | ||
} | ||
|
||
return replacePlaceholdersByValues(indexHtmlToReturn); | ||
}; |
Oops, something went wrong.