Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
fix: using own inject entry code (#5)
Browse files Browse the repository at this point in the history
* fix conflict

* fix inject code

* fix check

Co-authored-by: Atipat Pankong <[email protected]>
  • Loading branch information
JAmoMES and Atipat Pankong authored Jul 25, 2022
1 parent 89a4029 commit dbe7b10
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
},
"dependencies": {
"add-asset-html-webpack-plugin": "5.0.1",
"source-replacement": "2.0.1",
"webpack-inject-entry-plugin": "0.0.3"
"source-replacement": "2.0.1"
}
}
29 changes: 27 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import InjectEntryPlugin from 'webpack-inject-entry-plugin'
import Webpack from 'webpack'
import AddAssetHtmlWebpackPlugin from 'add-asset-html-webpack-plugin'
import path from 'path'
import { Compiler } from 'webpack'

const CODE_BLOCKER_SRC = path.resolve(
process.cwd(),
Expand All @@ -13,6 +13,31 @@ const CODE_BLOCKER_SRC = path.resolve(

const DEFAULT_ENTRY_NAME = 'client'

function injectEntry(
options: Compiler['options'],
entryName: string,
injectFilepath: string,
): void {
const entry: any =
typeof options.entry === 'function' ? options.entry() : Promise.resolve(options.entry)

options.entry = () =>
entry.then((e: any) => {
const injectEntry: typeof e[string] | undefined = e[entryName]

if (!injectEntry?.import) {
throw new Error(
`Could not find an entry named '${entryName}'. See https://webpack.js.org/concepts/entry-points/ for an overview of webpack entries.`,
)
}

if (!injectEntry.import.includes(injectFilepath)) {
injectEntry.import.unshift(injectFilepath)
}
return e
})
}

export class SourceReplacementPlugin {
private readonly entryName: string

Expand All @@ -29,6 +54,6 @@ export class SourceReplacementPlugin {
},
}).apply(compiler)

new InjectEntryPlugin({ entry: this.entryName, filepath: CODE_BLOCKER_SRC }).apply(compiler)
injectEntry(compiler.options, this.entryName, CODE_BLOCKER_SRC)
}
}
7 changes: 0 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5360,13 +5360,6 @@ [email protected]:
rechoir "^0.7.0"
webpack-merge "^5.7.3"

[email protected]:
version "0.0.3"
resolved "https://registry.yarnpkg.com/webpack-inject-entry-plugin/-/webpack-inject-entry-plugin-0.0.3.tgz#3e8fafa6ce1ef7b7105f0ad9fcc621420d7c9626"
integrity sha512-yG/+XUD9r2saj7oLzxFl278rnPhY+IZSZ9Win3KNr9dek2lqpo4HPBrwF3r1tEFX5B2a8I5reNdG1hdI11ekWw==
dependencies:
schema-utils "^3.0.0"

webpack-merge@^5.7.3:
version "5.8.0"
resolved "https://registry.yarnpkg.com/webpack-merge/-/webpack-merge-5.8.0.tgz#2b39dbf22af87776ad744c390223731d30a68f61"
Expand Down

0 comments on commit dbe7b10

Please sign in to comment.