Skip to content

Commit

Permalink
test: add an example of the custom plugin using the plugin hook to cr…
Browse files Browse the repository at this point in the history
…eate a second css bundle, processed by rtlcss
  • Loading branch information
webdiscus committed Dec 8, 2023
1 parent 7c77d4f commit de494cf
Show file tree
Hide file tree
Showing 13 changed files with 10,045 additions and 242 deletions.
10,135 changes: 9,893 additions & 242 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/material": "^5.14.19",
"@test/html-bundler-webpack-plugin": "file:./",
"@test-fixtures/js": "0.0.2",
"@test-fixtures/dius": "file:./test/fixtures/node_modules/dius/",
"@test-fixtures/lorem": "file:./test/fixtures/node_modules/lorem/",
Expand All @@ -146,6 +147,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"responsive-loader": "^3.1.2",
"rtlcss": "^4.1.1",
"sass": "1.67.0",
"sass-loader": "13.3.2",
"sharp": "^0.32.6",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions test/cases/hook-beforeEmit-rtlcss/expected/index.html
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(">> main");
8 changes: 8 additions & 0 deletions test/cases/hook-beforeEmit-rtlcss/expected/style.97b286be.css
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;
}
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;
}
Binary file added test/cases/hook-beforeEmit-rtlcss/src/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions test/cases/hook-beforeEmit-rtlcss/src/index.html
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>
1 change: 1 addition & 0 deletions test/cases/hook-beforeEmit-rtlcss/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('>> main');
8 changes: 8 additions & 0 deletions test/cases/hook-beforeEmit-rtlcss/src/style.css
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;
}
99 changes: 99 additions & 0 deletions test/cases/hook-beforeEmit-rtlcss/webpack.config.js
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]',
},
},
],
},
};
1 change: 1 addition & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ describe('plugin hooks', () => {

test('postprocess', () => compareFiles('hook-postprocess'));
test('beforeEmit', () => compareFiles('hook-beforeEmit'));
test('using beforeEmit for rtlcss', () => compareFiles('hook-beforeEmit-rtlcss'));

// TODO: add an argument as flat map of assets, w/o tree, to create a manifest
test('afterEmit', () => compareFiles('hook-afterEmit'));
Expand Down

0 comments on commit de494cf

Please sign in to comment.