Skip to content

Commit

Permalink
remove inject plugin (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Atipat Pankong <[email protected]>
  • Loading branch information
JAmoMES and Atipat Pankong authored Jul 27, 2022
1 parent ee64073 commit fb2bdb7
Show file tree
Hide file tree
Showing 5 changed files with 484 additions and 1,946 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,20 @@ module.exports = {
]
}
```

custom entry name

```js
const { BundleMoreWebpackPlugin } = require('bundle-more-webpack-plugin')

module.exports = {
...
plugins: [
...,
new BundleMoreWebpackPlugin(
['file-path-to-bundle']
, 'main' // default 'client'
)
]
}
```
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
"rollup-plugin-visualizer": "5.5.0",
"semantic-release": "18.0.0",
"typescript": "4.4.4",
"webpack": "4",
"webpack-cli": "4.9.1"
"webpack": "5.74.0",
"webpack-cli": "4.10.0"
},
"dependencies": {
"webpack-inject-plugin": "1.5.5"
}
"dependencies": {}
}
49 changes: 34 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
import Webpack from 'webpack'
import InjectPlugin from 'webpack-inject-plugin'
import fs from 'fs'
import { Compiler } from 'webpack'

const PLUGIN_NAME = 'BundleMoreWebpackPlugin'

export class BundleMoreWebpackPlugin{
paths: string[]
const DEFAULT_ENTRY_NAME = 'client'

constructor(paths: string[]) {
this.paths = paths
}
function injectEntry(entryImport: string[] | undefined, newEntrys: string[]) {
if (!entryImport) {
throw new Error(
`Could not find an entry named. See https://webpack.js.org/concepts/entry-points/ for an overview of webpack entries.`,
)
}
entryImport.unshift(...newEntrys)
}

export class BundleMoreWebpackPlugin {
entryName: string
paths: string[]

constructor(paths: string[], entryName = DEFAULT_ENTRY_NAME) {
this.paths = paths
this.entryName = entryName
}

apply(compiler: Compiler) {
compiler.hooks.entryOption.tap('BundleMoreWebpackPlugin', (_, entry): any => {
if (typeof entry !== 'function') {
injectEntry(entry[this.entryName].import, this.paths)
} else if (typeof entry === 'function') {
const entryPromise = entry()

apply(compiler: Webpack.Compiler) {
this.paths.forEach(path => {
new InjectPlugin(() => {
return fs.readFileSync(path, 'utf8')
}).apply(compiler)
})
}
entry = () =>
entryPromise.then(e => {
injectEntry(e[this.entryName].import, this.paths)
return e
})
}
})
}
}
37 changes: 20 additions & 17 deletions test/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ const path = require('path')
const { BundleMoreWebpackPlugin } = require('../build')

module.exports = {
entry: path.resolve(__dirname, 'index.js'),
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'build')
},
plugins: [
new BundleMoreWebpackPlugin([
path.resolve(__dirname, 'externals', 'script1.js'),
path.resolve(__dirname, 'externals', 'script2.js')
]),
],
target: 'node',
mode: 'development',
devtool: false,
resolve: {
modules: [path.resolve(__dirname, 'externals')]
}
entry: path.resolve(__dirname, 'index.js'),
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'build'),
},
plugins: [
new BundleMoreWebpackPlugin(
[
path.resolve(__dirname, 'externals', 'script1.js'),
path.resolve(__dirname, 'externals', 'script2.js'),
],
'main',
),
],
target: 'node',
mode: 'development',
devtool: false,
resolve: {
modules: [path.resolve(__dirname, 'externals')],
},
}
Loading

0 comments on commit fb2bdb7

Please sign in to comment.