Differential D5241 Diff 18802 swh/web/assets/config/webpack-plugins/generate-weblabels-webpack-plugin/index.js
Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/config/webpack-plugins/generate-weblabels-webpack-plugin/index.js
Show All 25 Lines | constructor(opts) { | ||||
// check that provided options match JSON schema | // check that provided options match JSON schema | ||||
validate(schema, opts, pluginName); | validate(schema, opts, pluginName); | ||||
this.options = opts || {}; | this.options = opts || {}; | ||||
this.weblabelsDirName = this.options['outputDir'] || 'jssources'; | this.weblabelsDirName = this.options['outputDir'] || 'jssources'; | ||||
this.outputType = this.options['outputType'] || 'html'; | this.outputType = this.options['outputType'] || 'html'; | ||||
// source file extension handled by webpack and compiled to js | // source file extension handled by webpack and compiled to js | ||||
this.srcExts = ['js', 'ts', 'coffee', 'lua']; | this.srcExts = ['js', 'ts', 'coffee', 'lua']; | ||||
this.srcExtsRegexp = new RegExp('^.*.(' + this.srcExts.join('|') + ')$'); | this.srcExtsRegexp = new RegExp('^.*.(' + this.srcExts.join('|') + ')$'); | ||||
this.chunkIdToName = {}; | |||||
this.chunkNameToJsAsset = {}; | this.chunkNameToJsAsset = {}; | ||||
this.chunkJsAssetToSrcFiles = {}; | this.chunkJsAssetToSrcFiles = {}; | ||||
this.srcIdsInChunkJsAsset = {}; | this.srcIdsInChunkJsAsset = {}; | ||||
this.packageJsonCache = {}; | this.packageJsonCache = {}; | ||||
this.packageLicenseFile = {}; | this.packageLicenseFile = {}; | ||||
this.exclude = []; | this.exclude = []; | ||||
this.copiedFiles = new Set(); | this.copiedFiles = new Set(); | ||||
this.logger = log({name: pluginName}); | this.logger = log({name: pluginName}); | ||||
Show All 15 Lines | compiler.hooks.done.tap(pluginName, statsObj => { | ||||
// get the stats object in JSON format | // get the stats object in JSON format | ||||
let stats = statsObj.toJson(); | let stats = statsObj.toJson(); | ||||
this.stats = stats; | this.stats = stats; | ||||
// set output folder | // set output folder | ||||
this.weblabelsOutputDir = path.join(stats.outputPath, this.weblabelsDirName); | this.weblabelsOutputDir = path.join(stats.outputPath, this.weblabelsDirName); | ||||
this.recursiveMkdir(this.weblabelsOutputDir); | this.recursiveMkdir(this.weblabelsOutputDir); | ||||
stats.assets.forEach(asset => { | |||||
for (let i = 0; i < asset.chunks.length; ++i) { | |||||
this.chunkIdToName[asset.chunks[i]] = asset.chunkNames[i]; | |||||
} | |||||
}); | |||||
// map each generated webpack chunk to its js asset | // map each generated webpack chunk to its js asset | ||||
Object.keys(stats.assetsByChunkName).forEach((chunkName, i) => { | Object.keys(stats.assetsByChunkName).forEach((chunkName, i) => { | ||||
if (Array.isArray(stats.assetsByChunkName[chunkName])) { | if (Array.isArray(stats.assetsByChunkName[chunkName])) { | ||||
for (let asset of stats.assetsByChunkName[chunkName]) { | for (let asset of stats.assetsByChunkName[chunkName]) { | ||||
if (asset.endsWith('.js')) { | if (asset.endsWith('.js')) { | ||||
this.chunkNameToJsAsset[chunkName] = asset; | this.chunkNameToJsAsset[chunkName] = asset; | ||||
this.chunkNameToJsAsset[i] = asset; | this.chunkNameToJsAsset[i] = asset; | ||||
break; | break; | ||||
Show All 30 Lines | compiler.hooks.done.tap(pluginName, statsObj => { | ||||
// remove webpack loader call if any | // remove webpack loader call if any | ||||
let loaderEndPos = srcFilePath.indexOf('!'); | let loaderEndPos = srcFilePath.indexOf('!'); | ||||
if (loaderEndPos !== -1) { | if (loaderEndPos !== -1) { | ||||
srcFilePath = srcFilePath.slice(loaderEndPos + 1); | srcFilePath = srcFilePath.slice(loaderEndPos + 1); | ||||
} | } | ||||
// iterate on all chunks containing the module | // iterate on all chunks containing the module | ||||
mod.chunks.forEach(chunk => { | mod.chunks.forEach(chunk => { | ||||
let chunkName = this.chunkIdToName[chunk]; | |||||
let chunkJsAsset = stats.publicPath + this.chunkNameToJsAsset[chunk]; | let chunkJsAsset = stats.publicPath + this.chunkNameToJsAsset[chunkName]; | ||||
// init the chunk to source files mapping if needed | // init the chunk to source files mapping if needed | ||||
if (!this.chunkJsAssetToSrcFiles.hasOwnProperty(chunkJsAsset)) { | if (!this.chunkJsAssetToSrcFiles.hasOwnProperty(chunkJsAsset)) { | ||||
this.chunkJsAssetToSrcFiles[chunkJsAsset] = []; | this.chunkJsAssetToSrcFiles[chunkJsAsset] = []; | ||||
this.srcIdsInChunkJsAsset[chunkJsAsset] = new Set(); | this.srcIdsInChunkJsAsset[chunkJsAsset] = new Set(); | ||||
} | } | ||||
// check if the source file needs to be replaces | // check if the source file needs to be replaces | ||||
if (this.options['srcReplace'] && this.options['srcReplace'].hasOwnProperty(srcFilePath)) { | if (this.options['srcReplace'] && this.options['srcReplace'].hasOwnProperty(srcFilePath)) { | ||||
▲ Show 20 Lines • Show All 282 Lines • Show Last 20 Lines |