UNPKG

webpack-browser-extension-locales

Version:
2 lines (1 loc) 2.95 kB
"use strict";var M=Object.create;var u=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var A=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var _=(t,s)=>{for(var e in s)u(t,e,{get:s[e],enumerable:!0})},w=(t,s,e,o)=>{if(s&&typeof s=="object"||typeof s=="function")for(let r of C(s))!F.call(t,r)&&r!==e&&u(t,r,{get:()=>s[r],enumerable:!(o=x(s,r))||o.enumerable});return t};var m=(t,s,e)=>(e=t!=null?M(A(t)):{},w(s||!t||!t.__esModule?u(e,"default",{value:t,enumerable:!0}):e,t)),N=t=>w(u({},"__esModule",{value:!0}),t);var R={};_(R,{default:()=>h});module.exports=N(R);var b=m(require("path")),f=m(require("fs")),l=require("webpack"),y=m(require("browser-extension-manifest-fields"));var p=m(require("webpack"));function O(t){let s="A manifest file is required for this plugin to run.";t.errors.push(new p.default.WebpackError(`[manifest.json]: ${s}`))}function D(t,s,e){let o=`Check the \`${s}\` field in your \`manifest.json\` file.`,r=`File path \`${e}\` not found. ${o}`;t.warnings.push(new p.default.WebpackError(`[manifest.json]: ${r}`))}function $(t){let o="Default locale was specified, but `_locales` subtree is missing. Ensure the `_locales` folder is valid and available at the root of your project. or remove the `default_locale` field from your `manifest.json` file.";t.errors.push(new p.default.WebpackError(`[_locales]: ${o}`))}var d={manifestNotFoundError:O,entryNotFoundWarn:D,noValidFolderError:$};function E(t,s){return s.some(e=>t.includes(e))}var h=class{manifestPath;exclude;constructor(s){this.manifestPath=s.manifestPath}apply(s){s.hooks.thisCompilation.tap("LocalesPlugin (module)",e=>{e.hooks.processAssets.tap({name:"LocalesPlugin (module)",stage:l.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS},o=>{if(!f.default.existsSync(this.manifestPath)){d.manifestNotFoundError(e);return}if(e.errors.length>0)return;let r=o["manifest.json"]?JSON.parse(o["manifest.json"].source().toString()):require(this.manifestPath),a=(0,y.default)(this.manifestPath,r).locales;if(r.default_locale&&!a?.length){d.noValidFolderError(e);return}for(let g of Object.entries(a||[])){let[S,c]=g,n=c;if(n){if(!f.default.existsSync(n)){d.entryNotFoundWarn(e,S,n);return}let i=f.default.readFileSync(n),P=new l.sources.RawSource(i),k=s.options.context||b.default.dirname(this.manifestPath);if(!E(n,this.exclude||[])){let j=b.default.relative(k,n);e.emitAsset(j,P)}}}})}),s.hooks.thisCompilation.tap("LocalesPlugin (module)",e=>{e.hooks.processAssets.tap({name:"LocalesPlugin (module)",stage:l.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS},o=>{if(e.errors?.length)return;let r=o["manifest.json"]?JSON.parse(o["manifest.json"].source().toString()):require(this.manifestPath),a=(0,y.default)(this.manifestPath,r).locales;for(let g of Object.entries(a||[])){let[,S]=g;if(S){let c=new Set(e.fileDependencies),n=a||[];for(let i of n)f.default.existsSync(i)&&(c.has(i)||(c.add(i),e.fileDependencies.add(i)))}}})})}};