-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ParagonWebpackPlugin and clean up
- Loading branch information
1 parent
e832cbc
commit 4745a57
Showing
9 changed files
with
224 additions
and
66 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
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,27 +1,24 @@ | ||
import React from 'react'; | ||
|
||
const ParagonPreview = () => { | ||
if (!PARAGON) { | ||
return null; | ||
} | ||
return ( | ||
<> | ||
<h2>Paragon</h2> | ||
<ul> | ||
<li> | ||
<a href={`/${PARAGON.themeUrls.core.outputChunkName}.css`} target="_blank" rel="noopener noreferrer"> | ||
{`${PARAGON.themeUrls.core.outputChunkName}.css`} | ||
</a> | ||
</li> | ||
<li> | ||
<a href={`/${PARAGON.themeUrls.variants.light.outputChunkName}.css`} target="_blank" rel="noopener noreferrer"> | ||
{`${PARAGON.themeUrls.variants.light.outputChunkName}.css`} | ||
</a> | ||
</li> | ||
</ul> | ||
<pre>{JSON.stringify(PARAGON, null, 2)}</pre> | ||
</> | ||
); | ||
}; | ||
const ParagonPreview = () => ( | ||
<> | ||
<h2>Paragon</h2> | ||
<h3>Exposed Theme CSS</h3> | ||
<ul> | ||
<li> | ||
<a href={`/${PARAGON.themeUrls.core}`} target="_blank" rel="noopener noreferrer"> | ||
{PARAGON.themeUrls.core} | ||
</a> | ||
</li> | ||
<li> | ||
<a href={`/${PARAGON.themeUrls.variants.light}`} target="_blank" rel="noopener noreferrer"> | ||
{PARAGON.themeUrls.variants.light} | ||
</a> | ||
</li> | ||
</ul> | ||
<h3>Contents of <code>PARAGON</code> global variable</h3> | ||
<pre>{JSON.stringify(PARAGON, null, 2)}</pre> | ||
</> | ||
); | ||
|
||
export default ParagonPreview; |
123 changes: 123 additions & 0 deletions
123
lib/plugins/paragon-webpack-plugin/ParagonWebpackPlugin.js
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,123 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
const { Compilation, sources } = require('webpack'); | ||
const parse5 = require('parse5'); | ||
const { | ||
getParagonVersion, | ||
getParagonThemeCss, | ||
} = require('../../../config/data/paragonUtils'); | ||
|
||
const paragonVersion = getParagonVersion(process.cwd()); | ||
const paragonThemeCss = getParagonThemeCss(process.cwd()); | ||
|
||
class ParagonWebpackPlugin { | ||
constructor() { | ||
this.version = paragonVersion; | ||
if (paragonThemeCss) { | ||
this.coreEntryName = paragonThemeCss.core.entryName; | ||
this.themeVariantEntryNames = {}; | ||
Object.entries(paragonThemeCss.variants).forEach(([key, value]) => { | ||
this.themeVariantEntryNames[key] = value.entryName; | ||
}); | ||
} | ||
} | ||
|
||
logger(message) { | ||
console.log('[ParagonWebpackPlugin]', message); | ||
} | ||
|
||
getDescendantByTag(node, tag) { | ||
for (let i = 0; i < node.childNodes?.length; i++) { | ||
if (node.childNodes[i].tagName === tag) { | ||
return node.childNodes[i]; | ||
} | ||
const result = this.getDescendantByTag(node.childNodes[i], tag); | ||
if (result) { | ||
return result; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
apply(compiler) { | ||
compiler.hooks.thisCompilation.tap('ParagonWebpackPlugin', (compilation) => { | ||
compilation.hooks.processAssets.tap( | ||
{ | ||
name: 'ParagonWebpackPlugin', | ||
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS, | ||
additionalAssets: true, | ||
}, | ||
() => { | ||
const file = compilation.getAsset('index.html'); | ||
if (!file) { | ||
return; | ||
} | ||
|
||
// get paragon assets | ||
const paragonAssets = compilation.getAssets().filter((asset) => asset.name.includes('paragon') && asset.name.endsWith('.css')); | ||
const coreCssAsset = paragonAssets.find((asset) => asset.name.includes(this.coreEntryName))?.name; | ||
const themeVariantLightAsset = paragonAssets.find((asset) => ( | ||
asset.name.includes(this.themeVariantEntryNames.light) | ||
))?.name; | ||
if (!coreCssAsset || !themeVariantLightAsset) { | ||
this.logger('Unable to find paragon assets in compilation. Skipping.'); | ||
return; | ||
} | ||
|
||
const originalSource = file.source.source(); | ||
const newSource = new sources.ReplaceSource( | ||
new sources.RawSource(originalSource), | ||
'index.html', | ||
); | ||
|
||
// parse file as html document | ||
const document = parse5.parse(originalSource, { | ||
sourceCodeLocationInfo: true, | ||
}); | ||
|
||
// find the body element | ||
const bodyElement = this.getDescendantByTag(document, 'body'); | ||
if (!bodyElement) { | ||
throw new Error('Missing body element in index.html'); | ||
} | ||
|
||
// determine script insertion point | ||
let scriptInsertionPoint; | ||
if (bodyElement.sourceCodeLocation?.endTag) { | ||
scriptInsertionPoint = bodyElement.sourceCodeLocation.endTag.startOffset; | ||
} else { | ||
// less accurate fallback | ||
scriptInsertionPoint = originalSource.indexOf('</body>'); | ||
} | ||
|
||
// update index.html with new content | ||
const paragonScript = ` | ||
<script type="text/javascript"> | ||
var PARAGON = { | ||
version: '${this.version}', | ||
themeUrls: { | ||
core: '${coreCssAsset}', | ||
variants: { | ||
light: '${themeVariantLightAsset}', | ||
}, | ||
}, | ||
}; | ||
</script> | ||
` | ||
// minify the above script | ||
.replace(/>[\r\n ]+</g, '><') | ||
.replace(/(<.*?>)|\s+/g, (m, $1) => { | ||
if ($1) { return $1; } | ||
return ' '; | ||
}) | ||
.trim(); | ||
|
||
// insert the Paragon script into the HTML document | ||
newSource.insert(scriptInsertionPoint, paragonScript); | ||
compilation.updateAsset('index.html', new sources.RawSource(newSource.source())); | ||
}, | ||
); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ParagonWebpackPlugin; |
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,3 @@ | ||
const ParagonWebpackPlugin = require('./ParagonWebpackPlugin'); | ||
|
||
module.exports = ParagonWebpackPlugin; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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