-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add an example of the custom plugin using the plugin hook to cr…
…eate a second css bundle, processed by rtlcss
- Loading branch information
Showing
13 changed files
with
10,045 additions
and
242 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Test</title> | ||
<link href="style.97b286be.css" rel="stylesheet" /><link href="style.97b286be.rtl.css" rel="stylesheet" /> | ||
<script src="main.5317c1f6.js" defer="defer"></script> | ||
</head> | ||
<body> | ||
<h1>Hello World!</h1> | ||
<img src="img/image.697ef306.png" alt="picture"> | ||
</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 @@ | ||
console.log(">> main"); |
8 changes: 8 additions & 0 deletions
8
test/cases/hook-beforeEmit-rtlcss/expected/style.97b286be.css
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 @@ | ||
body { | ||
background-image: url(img/image.697ef306.png); | ||
direction: ltr; | ||
} | ||
|
||
h1 { | ||
color: red; | ||
} |
8 changes: 8 additions & 0 deletions
8
test/cases/hook-beforeEmit-rtlcss/expected/style.97b286be.rtl.css
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 @@ | ||
body { | ||
background-image: url(img/image.697ef306.png); | ||
direction: rtl; | ||
} | ||
|
||
h1 { | ||
color: red; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Test</title> | ||
<link href="./style.css" rel="stylesheet" /> | ||
<script src="./main.js" defer="defer"></script> | ||
</head> | ||
<body> | ||
<h1>Hello World!</h1> | ||
<img src="./image.png" alt="picture"> | ||
</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 @@ | ||
console.log('>> main'); |
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 @@ | ||
body { | ||
background-image: url('./image.png'); | ||
direction: ltr; | ||
} | ||
|
||
h1 { | ||
color: red; | ||
} |
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,99 @@ | ||
const path = require('path'); | ||
const HtmlBundlerPlugin = require('@test/html-bundler-webpack-plugin'); | ||
const rtlCss = require('rtlcss'); | ||
|
||
module.exports = { | ||
mode: 'production', | ||
|
||
output: { | ||
path: path.join(__dirname, 'dist/'), | ||
}, | ||
|
||
plugins: [ | ||
new HtmlBundlerPlugin({ | ||
entry: { | ||
index: './src/index.html?lang=en', | ||
}, | ||
|
||
js: { | ||
filename: '[name].[contenthash:8].js', | ||
chunkFilename: '[name].[contenthash:8].chunk.js', | ||
}, | ||
|
||
css: { | ||
filename: '[name].[contenthash:8].css', | ||
chunkFilename: '[name].[contenthash:8].chunk.css', | ||
}, | ||
}), | ||
|
||
// The example of the plugin to create a second css bundle, processed by rtlcss. | ||
{ | ||
apply(compiler) { | ||
const pluginName = 'myPlugin'; | ||
const { RawSource } = compiler.webpack.sources; | ||
|
||
// the Webpack hook | ||
compiler.hooks.compilation.tap(pluginName, (compilation) => { | ||
const hooks = HtmlBundlerPlugin.getHooks(compilation); | ||
|
||
// the Bundler Plugin hook | ||
hooks.beforeEmit.tap(pluginName, (content, entry) => { | ||
// explain the structure of the `entry` object | ||
//console.dir({ hook: 'beforeEmit', entry }, { depth: 5 }); | ||
|
||
entry.assets.forEach((asset) => { | ||
const { type, assetFile, inline } = asset; | ||
|
||
if (type !== 'style') return; | ||
|
||
let cssSource = compilation.assets[assetFile].source(); | ||
let rtlResult = rtlCss.process(cssSource); | ||
let rtlAssetFile = assetFile.replace('.css', '.rtl.css'); | ||
|
||
if (!inline) { | ||
// add new CSS file into compilation | ||
compilation.assets[rtlAssetFile] = new RawSource(rtlResult); | ||
|
||
// find inject pos: after the last <link> tag | ||
// TODO: implement your logic to find an inject pos | ||
let pos = content.lastIndexOf('<link'); | ||
if (pos > -1) { | ||
let injectPos = content.indexOf('>', pos); | ||
// inject the style tag with rtl CSS into HTML | ||
if (injectPos > -1) { | ||
injectPos++; | ||
content = | ||
content.slice(0, injectPos) + | ||
`<link href="${rtlAssetFile}" rel="stylesheet" />` + | ||
content.slice(injectPos); | ||
} | ||
} | ||
} else { | ||
// TODO: if CSS asset is inlined inject the <style> tag into HTML | ||
} | ||
}); | ||
|
||
return content; | ||
}); | ||
}); | ||
}, | ||
}, | ||
], | ||
|
||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
use: ['css-loader'], | ||
}, | ||
|
||
{ | ||
test: /\.(png|jpe?g|ico|svg)$/, | ||
type: 'asset/resource', | ||
generator: { | ||
filename: 'img/[name].[hash:8][ext]', | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
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