-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,378 additions
and
749 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
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 |
---|---|---|
@@ -1,9 +1,38 @@ | ||
import { Configuration } from 'webpack'; | ||
import { WordPack } from '../services'; | ||
import { merge } from 'webpack-merge'; | ||
import * as fs from 'fs-extra'; | ||
import * as Svc from '../services'; | ||
import { WordPackEnv } from '../config'; | ||
|
||
export async function buildConfig( | ||
env: Record<string, string>, | ||
filepath?: string, | ||
webpackEnv: Record<string, string> | WordPackEnv, | ||
wpwpConfig: string = 'wpwp.config.ts', | ||
): Promise<Configuration[]> { | ||
return await new WordPack(env).buildConfig(filepath); | ||
try { | ||
const res: Configuration[] = []; | ||
const env = Svc.UserConfig.env(webpackEnv); | ||
const cfg = await Svc.UserConfig.load(wpwpConfig, env); | ||
|
||
cfg.bundles.forEach((bundle) => | ||
res.push( | ||
merge( | ||
Svc.SharedConfig.build(cfg), | ||
Svc.ManifestConfig.build(cfg), | ||
Svc.EntryConfig.build(cfg, bundle), | ||
Svc.CompileConfig.build(cfg, bundle), | ||
Svc.OptimizeConfig.build(cfg, bundle), | ||
cfg.override, | ||
bundle.override, | ||
), | ||
), | ||
); | ||
|
||
res.push(Svc.AssetConfig.build(cfg)); | ||
|
||
fs.emptyDirSync(cfg.distPath); | ||
|
||
return res; | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
} |
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,49 @@ | ||
import WebpackAssetsManifest, { Entry } from 'webpack-assets-manifest'; | ||
import * as path from 'node:path'; | ||
import * as fs from 'fs-extra'; | ||
|
||
export function manifestEntryFormatter({ key, value }: Entry): Entry | false { | ||
const base = (p: string) => path.basename(path.dirname(p)); | ||
|
||
const src = base(key); | ||
const dst = base(value); | ||
|
||
if (src !== dst) { | ||
key = `${dst}/${key}`; | ||
} | ||
|
||
return { key, value }; | ||
} | ||
|
||
export function manifestFileWriter(mfs: WebpackAssetsManifest): void { | ||
const tmpl = ` | ||
<?php | ||
/** | ||
* Asset manifest file. | ||
* | ||
* Auto-generated via WordPack. | ||
* | ||
* @package eXtended WordPress | ||
* @subpackage Assets | ||
*/ | ||
return array( | ||
{{{entries}}} | ||
); | ||
`; | ||
const assets = Object.keys(mfs.assets); | ||
const length = findLongest(assets); | ||
const entries = assets.map((k) => parseEntry(k, mfs.assets[k], length)); | ||
|
||
const content = tmpl.replace('{{{entries}}}', entries.join('\n')); | ||
|
||
fs.writeFileSync(mfs.getOutputPath().replace('.json', '.php'), content); | ||
} | ||
|
||
function findLongest(entries: string[]): number { | ||
return entries.reduce((l, k) => Math.max(l, k.length), 0); | ||
} | ||
|
||
function parseEntry(key: string, val: unknown, length: number): string { | ||
return ` '${key}'${' '.repeat(length - key.length)} => '${val}',`; | ||
} |
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,79 @@ | ||
import { Configuration } from 'webpack'; | ||
import { WordPackConfig } from '../config'; | ||
import * as fs from 'fs-extra'; | ||
import CopyPlugin from 'copy-webpack-plugin'; | ||
import merge from 'webpack-merge'; | ||
import { OptimizeConfig } from './optimize-config.service'; | ||
import { ManifestConfig } from './manifest-config.service'; | ||
import { manifestFileWriter } from '../functions/manifest-utils'; | ||
import WebpackBarPlugin from 'webpackbar'; | ||
|
||
export class AssetConfig { | ||
static build(cfg: WordPackConfig): Partial<Configuration> { | ||
return merge( | ||
AssetConfig.getCoreConfig(cfg), | ||
AssetConfig.getManifestConfig(cfg), | ||
AssetConfig.getOptimizeConfig(cfg), | ||
); | ||
} | ||
|
||
private static getCoreConfig(cfg: WordPackConfig): Configuration { | ||
return { | ||
name: 'AssetCopy', | ||
context: cfg.resolve(cfg.srcDir), | ||
mode: cfg.mode, | ||
entry: {}, | ||
output: { | ||
path: cfg.resolve(cfg.distRoot), | ||
}, | ||
stats: false, | ||
plugins: [ | ||
new CopyPlugin({ | ||
patterns: [ | ||
{ | ||
from: `${cfg.images}/**/*`, | ||
to: `[path]${cfg.asset}[ext]`, | ||
force: false, | ||
noErrorOnMissing: true, | ||
filter: (rp) => | ||
!fs.existsSync(rp.replace(cfg.srcDir, cfg.distRoot)), | ||
}, | ||
], | ||
}), | ||
new WebpackBarPlugin({ | ||
fancy: !cfg.isCI, | ||
basic: cfg.isCI, | ||
name: 'AssetCopy', | ||
}), | ||
], | ||
dependencies: cfg.bundles.map(({ name }) => name), | ||
}; | ||
} | ||
|
||
private static getOptimizeConfig(cfg: WordPackConfig): Configuration { | ||
if (!cfg.prod) { | ||
return {}; | ||
} | ||
|
||
return { | ||
optimization: { | ||
minimize: true, | ||
minimizer: OptimizeConfig.getImageMinimizers(), | ||
}, | ||
}; | ||
} | ||
private static getManifestConfig(cfg: WordPackConfig): Configuration { | ||
if (!cfg.prod || !cfg.manifest) { | ||
return {}; | ||
} | ||
return { | ||
plugins: [ | ||
ManifestConfig.getManifestPlugin({ | ||
output: cfg.manifest, | ||
writeToDisk: true, | ||
done: manifestFileWriter, | ||
}), | ||
], | ||
}; | ||
} | ||
} |
Oops, something went wrong.