-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: Move
InlineManifestPlugin
into the project. (#33)
* Start on new plugin. * Add tests. * Fix tests.
- Loading branch information
Showing
11 changed files
with
138 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ logs/ | |
# Directories | ||
node_modules/ | ||
lib/ | ||
public/ | ||
|
||
# Configs | ||
.babelrc | ||
|
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
97 changes: 97 additions & 0 deletions
97
packages/config-webpack/src/plugins/InlineManifestPlugin.ts
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,97 @@ | ||
/* eslint-disable no-param-reassign, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, import/no-extraneous-dependencies */ | ||
|
||
import webpack from 'webpack'; | ||
import HtmlWebpackPlugin from 'html-webpack-plugin'; | ||
import sourceMappingURL from 'source-map-url'; | ||
|
||
function getAssetName(chunks: webpack.compilation.Chunk[], chunkName: string) { | ||
return chunks.filter((chunk) => chunk.name === chunkName)?.[0]?.files[0]; | ||
} | ||
|
||
function inlineWhenMatched( | ||
compilation: webpack.compilation.Compilation, | ||
scripts: HtmlWebpackPlugin.HtmlTagObject[], | ||
manifestAssetName: string, | ||
) { | ||
return scripts.map((script) => { | ||
const isManifestScript = | ||
script.tagName === 'script' && | ||
typeof script.attributes.src === 'string' && | ||
script.attributes.src?.includes(manifestAssetName); | ||
|
||
if (isManifestScript) { | ||
return { | ||
tagName: 'script', | ||
voidTag: true, | ||
attributes: { | ||
type: 'text/javascript', | ||
}, | ||
innerHTML: sourceMappingURL.removeFrom(compilation.assets[manifestAssetName].source()), | ||
}; | ||
} | ||
|
||
return script; | ||
}); | ||
} | ||
|
||
export default class InlineManifestPlugin implements webpack.Plugin { | ||
name: string; | ||
|
||
constructor(name: string = 'runtime') { | ||
this.name = name; | ||
} | ||
|
||
apply(compiler: webpack.Compiler) { | ||
const { name } = this; | ||
|
||
compiler.hooks.emit.tap('InlineManifestWebpackPlugin', (compilation) => { | ||
const assetName = getAssetName(compilation.chunks, name); | ||
|
||
if (assetName) { | ||
delete compilation.assets[assetName]; | ||
} | ||
}); | ||
|
||
compiler.hooks.compilation.tap('InlineManifestWebpackPlugin', (compilation) => { | ||
const hooks = HtmlWebpackPlugin.getHooks(compilation); | ||
|
||
hooks.alterAssetTags.tapAsync('InlineManifestWebpackPlugin', (data, cb) => { | ||
const assetName = getAssetName(compilation.chunks, name); | ||
|
||
if (assetName) { | ||
data.assetTags.scripts = inlineWhenMatched( | ||
compilation, | ||
data.assetTags.scripts, | ||
assetName, | ||
); | ||
} | ||
|
||
cb(null, data); | ||
}); | ||
|
||
hooks.beforeAssetTagGeneration.tapAsync( | ||
'InlineManifestWebpackPlugin', | ||
(htmlPluginData, cb) => { | ||
const assetName = getAssetName(compilation.chunks, name); | ||
|
||
// @ts-ignore Option exists | ||
if (assetName && htmlPluginData.plugin.options.inject === false) { | ||
const { assets } = htmlPluginData; | ||
const runtime = `<script>${sourceMappingURL.removeFrom( | ||
compilation.assets[assetName].source(), | ||
)}</script>`; | ||
const runtimeIndex = assets.js.indexOf(assets.publicPath + assetName); | ||
|
||
if (runtimeIndex >= 0) { | ||
assets.js.splice(runtimeIndex, 1); | ||
} | ||
|
||
assets.js.push(runtime); | ||
} | ||
|
||
cb(null, htmlPluginData); | ||
}, | ||
); | ||
}); | ||
} | ||
} |
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,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en" dir="ltr"> | ||
<head> | ||
<title>Test</title> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
</body> | ||
</html> |
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,2 @@ | ||
// eslint-disable-next-line no-console | ||
console.log('Webpack build test!'); |
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,6 @@ | ||
const { getConfig } = require('../lib'); | ||
|
||
module.exports = getConfig({ | ||
root: __dirname, | ||
srcFolder: 'src', | ||
}); |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
declare module 'source-map-url' { | ||
class SMU { | ||
removeFrom(code: string): string; | ||
} | ||
|
||
const smu: SMU; | ||
export default smu; | ||
} |
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