@studyportals/bob-manifest-generator
Advanced tools
Comparing version 3.0.0-beta.6 to 3.0.0-beta.7
{ | ||
"name": "@studyportals/bob-manifest-generator", | ||
"version": "3.0.0-beta.6", | ||
"version": "3.0.0-beta.7", | ||
"description": "A Webpack plugin that generates a manifest file for your microservice.", | ||
@@ -14,3 +14,3 @@ "main": "src/main.js", | ||
"engines": { | ||
"node": "8.12.*" | ||
"node": ">=10.13.0" | ||
}, | ||
@@ -17,0 +17,0 @@ "peerDependencies": { |
@@ -51,2 +51,3 @@ # BobManifestGenerator | ||
html: string, | ||
embedChunk: string, | ||
baseURL: string, | ||
@@ -56,3 +57,4 @@ crossorigin: boolean, | ||
async: string[], | ||
defer: string[] | ||
defer: string[], | ||
priority: boolean | ||
}) | ||
@@ -113,3 +115,4 @@ ] | ||
| `name` | The name of your microservice. | yes | | ||
| `html` | The HTML that should be injected on the page the microservice is loaded in. | yes | | ||
| `html` | The HTML that should be injected on the page the microservice is loaded in. | no | | ||
| `embedChunk` | The name of the chunk that should be injected into the manifest html. This should only be used as a **last resort** where javascript need's be injected inline. Please **discuss** this within the Frontend tribe before implementation. | no | | ||
| `baseURL` | The base URL for your microservice's assets. This will be prepended to the filenames. | no (default: `''`) | | ||
@@ -120,2 +123,3 @@ | `crossorigin` | Determines if JS assets will be inserted on the page with a `crossorigin="anonymous"` tag. | no (default: `true`) | | ||
| `defer` | An array of CSS filenames to be lazy-loaded. This can be just a filename or a filename including extension. Keep in mind that only CSS assets may be lazy-loaded. When `true` is passed as a value, or when the value is omitted, all CSS assets will be lazy-loaded.| no (default: `[]`) | | ||
| `priority` | Whenever a microservice should be loaded with priority or not. This is usually the case when other microservices depend on your priority microservice. Such as StudentJS and AnonymousStudent | no (default: `false`) | | ||
@@ -122,0 +126,0 @@ **IMPORTANT: Assets that are generated from dynamic imports will be automatically excluded from the manifest and therefore don't need to be excluded using the `exclude` option.** |
@@ -12,3 +12,3 @@ const path = require('path'); | ||
name: options.name, | ||
html: options.html, | ||
html: undefined, | ||
assets: { | ||
@@ -18,3 +18,4 @@ js: [], | ||
}, | ||
dllDependencies: [] | ||
dllDependencies: [], | ||
priority: options.priority === true | ||
}; | ||
@@ -25,2 +26,5 @@ this.baseURL = options.baseURL || ''; | ||
this.defer = options.defer || []; | ||
this.embedChunk = options.embedChunk || undefined; | ||
this.html = options.html; | ||
this.loadConfiguration = options.loadConfiguration || {}; | ||
} | ||
@@ -32,6 +36,6 @@ | ||
const assets = compilation.getAssets(); | ||
this.validateOptions(compilation); | ||
this.gatherAssets(assets, compilation.chunks); | ||
GenericManifestGenerator.emitFile(compilation, 'manifest.json', this.manifest); | ||
this.gatherAssets(compilation.chunks); | ||
this.setHtml(compilation.getAssets(), compilation.chunks) | ||
GenericManifestGenerator.emitFile(compilation, 'manifest.json', this.manifest); | ||
@@ -44,3 +48,3 @@ callback(); | ||
compilation.options.plugins.forEach((plugin) => { | ||
this.findDLLDependencies(plugin); | ||
@@ -50,3 +54,3 @@ }); | ||
if(typeof compiler.hooks !== 'undefined') { | ||
if (typeof compiler.hooks !== 'undefined') { | ||
@@ -74,3 +78,3 @@ compiler.hooks.afterPlugins.tap( | ||
if(plugin instanceof DllReferencePlugin) { | ||
if (plugin instanceof DllReferencePlugin) { | ||
@@ -80,3 +84,3 @@ try { | ||
const data = fs.readFileSync( | ||
path.resolve('node_modules/@studyportals/' + plugin.name) + '/dist/manifest.json', | ||
path.resolve('node_modules/@studyportals/' + plugin.name) + '/dist/manifest.json', | ||
'utf-8' | ||
@@ -92,3 +96,3 @@ ); | ||
}) | ||
} catch(e) { | ||
} catch (e) { | ||
@@ -104,15 +108,26 @@ throw new Error('BobManifestGenerator: Cannot find manifest for @studyportals/' + plugin.name); | ||
if(typeof this.manifest.name === 'undefined') valid = false; | ||
if(typeof this.manifest.html === 'undefined') valid = false; | ||
if (typeof this.manifest.name === 'undefined') valid = false; | ||
if (typeof this.html !== 'undefined' && this.html === '') valid = false; | ||
if (typeof this.html !== 'undefined' && typeof this.embedChunk !== 'undefined') valid = false; | ||
if(!valid) { | ||
if (!valid) { | ||
compilation.errors.push( | ||
compilation.errors.push( | ||
new Error('BobManifestGenerator: Couldn\'t build a manifest without all required properties.') | ||
); | ||
return; | ||
} | ||
if (typeof this.embedChunk !== 'undefined') { | ||
const chunkExists = compilation.chunks.some(chunk => chunk.name === this.embedChunk); | ||
if (!chunkExists) { | ||
compilation.errors.push( | ||
new Error('BobManifestGenerator: Couldn\'t find embedChunk in webpack chunks.') | ||
); | ||
} | ||
} | ||
} | ||
gatherAssets(files, chunks) { | ||
gatherAssets(chunks) { | ||
// Make sure there are no assets from previous compilations in the assets object by clearing it | ||
@@ -124,35 +139,63 @@ this.manifest.assets = { | ||
let chunkFiles = []; | ||
for (const chunk of chunks) { | ||
if (!chunk.entryModule) continue; | ||
chunks.forEach(chunk => { | ||
chunk.files.forEach(file => { | ||
if (this.isFileMainChunk(file)) { | ||
return; | ||
for (const fileName of chunk.files) { | ||
if (this.shouldProcessExtension(fileName, 'js')) { | ||
this.manifest.assets.js.push( | ||
this.createJSAsset(chunk, fileName) | ||
); | ||
} | ||
chunkFiles.push(file); | ||
}); | ||
}); | ||
if (this.shouldProcessExtension(fileName, 'css')) { | ||
this.manifest.assets.css.push({ | ||
url: this.baseURL + '/' + fileName, | ||
defer: this.matchFile(fileName, this.defer) | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
files.forEach(file => { | ||
if (chunkFiles.includes(file.name)) { | ||
return; | ||
createJSAsset(chunk, fileName) { | ||
const asset = { | ||
url: this.baseURL + '/' + fileName, | ||
async: this.matchFile(fileName, this.async) | ||
}; | ||
if (typeof this.loadConfiguration[chunk.name] !== 'undefined') { | ||
const config = this.loadConfiguration[chunk.name]; | ||
if (config.identifier) { | ||
asset.identifier = config.identifier; | ||
} | ||
if (this.shouldProcessExtension(file.name, 'js')) { | ||
this.manifest.assets.js.push({ | ||
url: `${this.baseURL}/${file.name}`, | ||
async: this.matchFile(file.name, this.async) | ||
}); | ||
if (config.onLoad) { | ||
asset.onLoad = config.onLoad; | ||
} | ||
if (this.shouldProcessExtension(file.name, 'css')) { | ||
this.manifest.assets.css.push({ | ||
url: `${this.baseURL}/${file.name}`, | ||
defer: this.matchFile(file.name, this.defer) | ||
}); | ||
if (config.onError) { | ||
asset.onError = config.onError; | ||
} | ||
}); | ||
} | ||
return asset; | ||
} | ||
setHtml(assets, chunks) { | ||
if (this.html) { | ||
this.manifest.html = this.html; | ||
} | ||
if (this.embedChunk !== undefined) { | ||
const chunk = chunks.find(chunk => chunk.name === this.embedChunk); | ||
if (chunk) { | ||
const fileName = chunk.files[0]; | ||
this.manifest.html = `<script>${assets[fileName].source()}</script>`; | ||
} | ||
} | ||
} | ||
shouldProcessExtension(file, extension) { | ||
@@ -164,3 +207,3 @@ | ||
fileType(file) { | ||
const parts = file.split('.'); | ||
@@ -172,11 +215,4 @@ const extension = parts[parts.length - 1]; | ||
isFileMainChunk(file) { | ||
const parts = file.split('.'); | ||
const firstPartAsNumber = parseInt(parts[0]); | ||
return isNaN(firstPartAsNumber); | ||
} | ||
matchFile(file, namesToMatch) { | ||
matchFile(file, namesToMatch) { | ||
if (namesToMatch === true) return true; | ||
@@ -191,3 +227,3 @@ | ||
if (filename === name || file === name) { | ||
matched = true; | ||
@@ -194,0 +230,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16264
286
216