@studyportals/bob-manifest-generator
Advanced tools
Comparing version 3.0.0-beta.3 to 3.0.0-beta.4
{ | ||
"name": "@studyportals/bob-manifest-generator", | ||
"version": "3.0.0-beta.3", | ||
"version": "3.0.0-beta.4", | ||
"description": "A Webpack plugin that generates a manifest file for your microservice.", | ||
@@ -5,0 +5,0 @@ "main": "src/main.js", |
@@ -6,5 +6,5 @@ const ManifestGenerator = require('./plugins/manifest-generator'); | ||
module.exports = { | ||
ManifestGenerator, | ||
DllReferencePlugin, | ||
DllManifestGenerator | ||
ManifestGenerator, | ||
DllReferencePlugin, | ||
DllManifestGenerator | ||
} |
@@ -5,162 +5,162 @@ const fs = require('fs'); | ||
constructor() { | ||
constructor() { | ||
this.manifest = { | ||
name: "", | ||
version: "", | ||
assets: { | ||
js: [], | ||
css: [] | ||
} | ||
}; | ||
} | ||
this.manifest = { | ||
name: "", | ||
version: "", | ||
assets: { | ||
js: [], | ||
css: [] | ||
} | ||
}; | ||
} | ||
apply(compiler) { | ||
apply(compiler) { | ||
const compilationHook = (compilation, callback) => { | ||
this.gatherPackageInfo(compilation); | ||
this.gatherAssets(compilation.assets); | ||
this.emitManifest(compilation); | ||
const compilationHook = (compilation, callback) => { | ||
this.gatherPackageInfo(compilation); | ||
this.gatherAssets(compilation.assets); | ||
this.emitManifest(compilation); | ||
callback(); | ||
}; | ||
callback(); | ||
}; | ||
if(typeof compiler.hooks !== 'undefined') { | ||
if(typeof compiler.hooks !== 'undefined') { | ||
compiler.hooks.emit.tapAsync( | ||
'BobManifestGenerator', | ||
compilationHook | ||
); | ||
compiler.hooks.emit.tapAsync( | ||
'BobManifestGenerator', | ||
compilationHook | ||
); | ||
return; | ||
} | ||
return; | ||
} | ||
compiler.plugin( | ||
'emit', | ||
compilationHook | ||
); | ||
} | ||
compiler.plugin( | ||
'emit', | ||
compilationHook | ||
); | ||
} | ||
gatherPackageInfo(compilation) { | ||
gatherPackageInfo(compilation) { | ||
let packageFile; | ||
let packageFile; | ||
try { | ||
try { | ||
const data = fs.readFileSync( | ||
'./package.json', | ||
'utf-8' | ||
); | ||
const data = fs.readFileSync( | ||
'./package.json', | ||
'utf-8' | ||
); | ||
packageFile = JSON.parse(data); | ||
} catch(e) { | ||
packageFile = JSON.parse(data); | ||
} catch(e) { | ||
throw new Error('BobManifestGenerator: Cannot find package.json for this package'); | ||
} | ||
throw new Error('BobManifestGenerator: Cannot find package.json for this package'); | ||
} | ||
this.manifest.name = packageFile.name; | ||
this.manifest.version = packageFile.version; | ||
this.manifest.name = packageFile.name; | ||
this.manifest.version = packageFile.version; | ||
if(typeof packageFile.style !== 'undefined') { | ||
this.emitStylesheet(compilation, packageFile.style); | ||
this.emitSourcemap(compilation, packageFile.style); | ||
if(typeof packageFile.style !== 'undefined') { | ||
this.emitStylesheet(compilation, packageFile.style); | ||
this.emitSourcemap(compilation, packageFile.style); | ||
this.manifest.assets.css.push({ | ||
url: packageFile.style | ||
}); | ||
} | ||
} | ||
this.manifest.assets.css.push({ | ||
url: packageFile.style | ||
}); | ||
} | ||
} | ||
gatherAssets(files) { | ||
for(let file in files) { | ||
gatherAssets(files) { | ||
for(let file in files) { | ||
if(this.shouldProcessExtension(file, 'js')) { | ||
this.manifest.assets.js.push({ | ||
url: file.replace(".min", "") | ||
}); | ||
} | ||
} | ||
} | ||
if(this.shouldProcessExtension(file, 'js')) { | ||
this.manifest.assets.js.push({ | ||
url: file.replace(".min", "") | ||
}); | ||
} | ||
} | ||
} | ||
emitManifest(compilation) { | ||
emitManifest(compilation) { | ||
compilation.assets['manifest.json'] = { | ||
source: () => { | ||
compilation.assets['manifest.json'] = { | ||
source: () => { | ||
return JSON.stringify(this.manifest); | ||
}, | ||
size: () => { | ||
return JSON.stringify(this.manifest); | ||
}, | ||
size: () => { | ||
return JSON.stringify(this.manifest).length; | ||
} | ||
} | ||
} | ||
return JSON.stringify(this.manifest).length; | ||
} | ||
} | ||
} | ||
emitStylesheet(compilation, file) { | ||
emitStylesheet(compilation, file) { | ||
try { | ||
try { | ||
const stylesheet = fs.readFileSync( | ||
'../dist/' + file, | ||
'utf-8' | ||
); | ||
const stylesheet = fs.readFileSync( | ||
'../dist/' + file, | ||
'utf-8' | ||
); | ||
compilation.assets[file] = { | ||
source: () => { | ||
compilation.assets[file] = { | ||
source: () => { | ||
return stylesheet.toString(); | ||
}, | ||
size: () => { | ||
return stylesheet.toString(); | ||
}, | ||
size: () => { | ||
return stylesheet.toString().length; | ||
} | ||
} | ||
} catch(e) { | ||
return stylesheet.toString().length; | ||
} | ||
} | ||
} catch(e) { | ||
throw new Error('BobManifestGenerator: Cannot make a copy of the package stylesheet'); | ||
} | ||
} | ||
throw new Error('BobManifestGenerator: Cannot make a copy of the package stylesheet'); | ||
} | ||
} | ||
emitSourcemap(compilation, file) { | ||
emitSourcemap(compilation, file) { | ||
try { | ||
try { | ||
const sourcemap = fs.readFileSync( | ||
'../dist/' + file + ".map", | ||
'utf-8' | ||
); | ||
const sourcemap = fs.readFileSync( | ||
'../dist/' + file + ".map", | ||
'utf-8' | ||
); | ||
compilation.assets[file + ".map"] = { | ||
source: () => { | ||
compilation.assets[file + ".map"] = { | ||
source: () => { | ||
return sourcemap.toString(); | ||
}, | ||
size: () => { | ||
return sourcemap.toString(); | ||
}, | ||
size: () => { | ||
return sourcemap.toString().length; | ||
} | ||
} | ||
} catch(e) { | ||
return sourcemap.toString().length; | ||
} | ||
} | ||
} catch(e) { | ||
console.warn('BobManifestGenerator: No CSS sourcemap was found'); | ||
} | ||
} | ||
console.warn('BobManifestGenerator: No CSS sourcemap was found'); | ||
} | ||
} | ||
shouldProcessExtension(file, extension) { | ||
shouldProcessExtension(file, extension) { | ||
return this.fileType(file) === extension; | ||
} | ||
return this.fileType(file) === extension; | ||
} | ||
fileType(file) { | ||
fileType(file) { | ||
const parts = file.split('.'); | ||
const extension = parts[parts.length - 1]; | ||
const parts = file.split('.'); | ||
const extension = parts[parts.length - 1]; | ||
return extension; | ||
} | ||
return extension; | ||
} | ||
} | ||
module.exports = DllManifestGenerator; |
@@ -7,195 +7,195 @@ const path = require('path'); | ||
constructor(options) { | ||
constructor(options) { | ||
this.manifest = { | ||
name: options.name, | ||
html: options.html, | ||
assets: { | ||
js: [], | ||
css: [] | ||
}, | ||
dllDependencies: [] | ||
}; | ||
this.baseURL = options.baseURL || ''; | ||
this.exclude = options.exclude || []; | ||
this.async = options.async || []; | ||
this.defer = options.defer || []; | ||
} | ||
this.manifest = { | ||
name: options.name, | ||
html: options.html, | ||
assets: { | ||
js: [], | ||
css: [] | ||
}, | ||
dllDependencies: [] | ||
}; | ||
this.baseURL = options.baseURL || ''; | ||
this.exclude = options.exclude || []; | ||
this.async = options.async || []; | ||
this.defer = options.defer || []; | ||
} | ||
apply(compiler) { | ||
apply(compiler) { | ||
const compilationHook = (compilation, callback) => { | ||
const compilationHook = (compilation, callback) => { | ||
const assets = compilation.getAssets(); | ||
this.validateOptions(compilation); | ||
this.gatherAssets(assets, compilation.chunks); | ||
this.emitManifest(compilation); | ||
const assets = compilation.getAssets(); | ||
this.validateOptions(compilation); | ||
this.gatherAssets(assets, compilation.chunks); | ||
this.emitManifest(compilation); | ||
callback(); | ||
}; | ||
callback(); | ||
}; | ||
const checkPlugins = (compilation) => { | ||
const checkPlugins = (compilation) => { | ||
compilation.options.plugins.forEach((plugin) => { | ||
this.findDLLDependencies(plugin); | ||
}); | ||
}; | ||
compilation.options.plugins.forEach((plugin) => { | ||
this.findDLLDependencies(plugin); | ||
}); | ||
}; | ||
if(typeof compiler.hooks !== 'undefined') { | ||
if(typeof compiler.hooks !== 'undefined') { | ||
compiler.hooks.afterPlugins.tap( | ||
'BobManifestGenerator', | ||
checkPlugins | ||
); | ||
compiler.hooks.afterPlugins.tap( | ||
'BobManifestGenerator', | ||
checkPlugins | ||
); | ||
compiler.hooks.emit.tapAsync( | ||
'BobManifestGenerator', | ||
compilationHook | ||
); | ||
compiler.hooks.emit.tapAsync( | ||
'BobManifestGenerator', | ||
compilationHook | ||
); | ||
return; | ||
} | ||
return; | ||
} | ||
compiler.plugin( | ||
'emit', | ||
compilationHook | ||
); | ||
} | ||
compiler.plugin( | ||
'emit', | ||
compilationHook | ||
); | ||
} | ||
findDLLDependencies(plugin) { | ||
findDLLDependencies(plugin) { | ||
if(plugin instanceof DllReferencePlugin) { | ||
if(plugin instanceof DllReferencePlugin) { | ||
try { | ||
try { | ||
const data = fs.readFileSync( | ||
path.resolve('node_modules/@studyportals/' + plugin.name) + '/dist/manifest.json', | ||
'utf-8' | ||
); | ||
const data = fs.readFileSync( | ||
path.resolve('node_modules/@studyportals/' + plugin.name) + '/dist/manifest.json', | ||
'utf-8' | ||
); | ||
const manifest = JSON.parse(data); | ||
const manifest = JSON.parse(data); | ||
this.manifest.dllDependencies.push({ | ||
name: manifest.name, | ||
version: manifest.version, | ||
assets: manifest.assets | ||
}) | ||
} catch(e) { | ||
this.manifest.dllDependencies.push({ | ||
name: manifest.name, | ||
version: manifest.version, | ||
assets: manifest.assets | ||
}) | ||
} catch(e) { | ||
throw new Error('BobManifestGenerator: Cannot find manifest for @studyportals/' + plugin.name); | ||
} | ||
} | ||
} | ||
throw new Error('BobManifestGenerator: Cannot find manifest for @studyportals/' + plugin.name); | ||
} | ||
} | ||
} | ||
validateOptions(compilation) { | ||
validateOptions(compilation) { | ||
let valid = true; | ||
let valid = true; | ||
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.manifest.html === 'undefined') valid = false; | ||
if(!valid) { | ||
if(!valid) { | ||
compilation.errors.push( | ||
new Error('BobManifestGenerator: Couldn\'t build a manifest without all required properties.') | ||
); | ||
} | ||
} | ||
compilation.errors.push( | ||
new Error('BobManifestGenerator: Couldn\'t build a manifest without all required properties.') | ||
); | ||
} | ||
} | ||
gatherAssets(files, chunks) { | ||
gatherAssets(files, chunks) { | ||
// Make sure there are no assets from previous compilations in the assets object by clearing it | ||
this.manifest.assets = { | ||
js: [], | ||
css: [] | ||
} | ||
// Make sure there are no assets from previous compilations in the assets object by clearing it | ||
this.manifest.assets = { | ||
js: [], | ||
css: [] | ||
} | ||
let chunkFiles = []; | ||
let chunkFiles = []; | ||
chunks.forEach(chunk => { | ||
chunk.files.forEach(file => { | ||
if (this.isFileMainChunk(file)) { | ||
return; | ||
} | ||
chunks.forEach(chunk => { | ||
chunk.files.forEach(file => { | ||
if (this.isFileMainChunk(file)) { | ||
return; | ||
} | ||
chunkFiles.push(file); | ||
}); | ||
}); | ||
chunkFiles.push(file); | ||
}); | ||
}); | ||
files.forEach(file => { | ||
if (chunkFiles.includes(file.name)) { | ||
return; | ||
} | ||
files.forEach(file => { | ||
if (chunkFiles.includes(file.name)) { | ||
return; | ||
} | ||
if (this.shouldProcessExtension(file.name, 'js')) { | ||
this.manifest.assets.js.push({ | ||
url: this.baseURL + '/' + file.name, | ||
async: this.matchFile(file.name, this.async) | ||
}); | ||
} | ||
if (this.shouldProcessExtension(file.name, 'js')) { | ||
this.manifest.assets.js.push({ | ||
url: this.baseURL + '/' + file.name, | ||
async: this.matchFile(file.name, this.async) | ||
}); | ||
} | ||
if (this.shouldProcessExtension(file.name, 'css')) { | ||
this.manifest.assets.css.push({ | ||
url: this.baseURL + '/' + file.name, | ||
defer: this.matchFile(file.name, this.defer) | ||
}); | ||
} | ||
}); | ||
} | ||
if (this.shouldProcessExtension(file.name, 'css')) { | ||
this.manifest.assets.css.push({ | ||
url: this.baseURL + '/' + file.name, | ||
defer: this.matchFile(file.name, this.defer) | ||
}); | ||
} | ||
}); | ||
} | ||
shouldProcessExtension(file, extension) { | ||
shouldProcessExtension(file, extension) { | ||
return this.fileType(file) === extension && !this.matchFile(file, this.exclude); | ||
} | ||
return this.fileType(file) === extension && !this.matchFile(file, this.exclude); | ||
} | ||
emitManifest(compilation) { | ||
emitManifest(compilation) { | ||
compilation.assets['manifest.json'] = { | ||
source: () => { | ||
compilation.assets['manifest.json'] = { | ||
source: () => { | ||
return JSON.stringify(this.manifest); | ||
}, | ||
size: () => { | ||
return JSON.stringify(this.manifest); | ||
}, | ||
size: () => { | ||
return JSON.stringify(this.manifest).length; | ||
} | ||
} | ||
} | ||
return JSON.stringify(this.manifest).length; | ||
} | ||
} | ||
} | ||
fileType(file) { | ||
const parts = file.split('.'); | ||
const extension = parts[parts.length - 1]; | ||
fileType(file) { | ||
const parts = file.split('.'); | ||
const extension = parts[parts.length - 1]; | ||
return extension; | ||
} | ||
return extension; | ||
} | ||
isFileMainChunk(file) { | ||
const parts = file.split('.'); | ||
const firstPartAsNumber = parseInt(parts[0]); | ||
return isNaN(firstPartAsNumber); | ||
} | ||
isFileMainChunk(file) { | ||
const parts = file.split('.'); | ||
const firstPartAsNumber = parseInt(parts[0]); | ||
return isNaN(firstPartAsNumber); | ||
} | ||
matchFile(file, namesToMatch) { | ||
if (namesToMatch === true) return true; | ||
matchFile(file, namesToMatch) { | ||
if (namesToMatch === true) return true; | ||
let matched = false; | ||
const parts = file.split('.'); | ||
const filename = parts[0]; | ||
let matched = false; | ||
const parts = file.split('.'); | ||
const filename = parts[0]; | ||
namesToMatch.forEach(name => { | ||
namesToMatch.forEach(name => { | ||
if (filename === name || file === name) { | ||
matched = true; | ||
} | ||
}); | ||
if (filename === name || file === name) { | ||
matched = true; | ||
} | ||
}); | ||
return matched; | ||
} | ||
return matched; | ||
} | ||
} | ||
module.exports = ManifestGenerator; |
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
14689