From 89a40290cb7daf671f8e791215cc415480051032 Mon Sep 17 00:00:00 2001
From: Atipat Pankong <77198497+JAmoMES@users.noreply.github.com>
Date: Fri, 22 Jul 2022 17:47:18 +0700
Subject: [PATCH] feat: add entry name option (#4)
* add entry name option
* fix readme
* fix review
* fix review
* spacing
Co-authored-by: Atipat Pankong
---
README.md | 14 ++++++++++++++
src/index.ts | 10 +++++++++-
test/webpack.config.js | 21 +++++++++------------
3 files changed, 32 insertions(+), 13 deletions(-)
diff --git a/README.md b/README.md
index 9964937..e2525e3 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,20 @@ module.exports = {
}
```
+custom entry name
+
+```js
+const { SourceReplacementPlugin } = require('source-replacement-webpack-plugin')
+
+module.exports = {
+ ...
+ plugins: [
+ ...,
+ new SourceReplacementPlugin('main') // default is 'client'
+ ]
+}
+```
+
#### On your browser
Enter page with the following example
diff --git a/src/index.ts b/src/index.ts
index b959d17..2e35a7e 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -11,7 +11,15 @@ const CODE_BLOCKER_SRC = path.resolve(
'code-blocker.js',
)
+const DEFAULT_ENTRY_NAME = 'client'
+
export class SourceReplacementPlugin {
+ private readonly entryName: string
+
+ constructor(entryName = DEFAULT_ENTRY_NAME) {
+ this.entryName = entryName
+ }
+
apply(compiler: Webpack.Compiler) {
new AddAssetHtmlWebpackPlugin({
filepath: require.resolve('source-replacement'),
@@ -21,6 +29,6 @@ export class SourceReplacementPlugin {
},
}).apply(compiler)
- new InjectEntryPlugin({ entry: 'main', filepath: CODE_BLOCKER_SRC }).apply(compiler)
+ new InjectEntryPlugin({ entry: this.entryName, filepath: CODE_BLOCKER_SRC }).apply(compiler)
}
}
diff --git a/test/webpack.config.js b/test/webpack.config.js
index 5b166f6..c1c45a1 100644
--- a/test/webpack.config.js
+++ b/test/webpack.config.js
@@ -3,16 +3,13 @@ const HTMLWebpackPlugin = require('html-webpack-plugin')
const { SourceReplacementPlugin } = require('../build')
module.exports = {
- entry: path.resolve(__dirname, 'index.js'),
- output: {
- filename: 'bundle.js',
- path: path.resolve(__dirname, 'build')
- },
- plugins: [
- new HTMLWebpackPlugin(),
- new SourceReplacementPlugin(),
- ],
- target: 'node',
- mode: 'development',
- devtool: false,
+ entry: path.resolve(__dirname, 'index.js'),
+ output: {
+ filename: 'bundle.js',
+ path: path.resolve(__dirname, 'build'),
+ },
+ plugins: [new HTMLWebpackPlugin(), new SourceReplacementPlugin('main')],
+ target: 'node',
+ mode: 'development',
+ devtool: false,
}