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

Commit

Permalink
fix: using inject entry instead add asset html plugin (#6)
Browse files Browse the repository at this point in the history
* remove asset html plugin

* remove log

* remove log

Co-authored-by: Atipat Pankong <[email protected]>
  • Loading branch information
JAmoMES and Atipat Pankong authored Jul 26, 2022
1 parent dbe7b10 commit 8f87380
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 94 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"webpack-cli": "4.9.1"
},
"dependencies": {
"add-asset-html-webpack-plugin": "5.0.1",
"source-replacement": "2.0.1"
}
}
30 changes: 7 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
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(),
'node_modules',
'source-replacement',
'build',
'code-blocker.js',
)

const DEFAULT_ENTRY_NAME = 'client'

function injectEntry(
options: Compiler['options'],
entryName: string,
injectFilepath: string,
injectFilepath: string[],
): void {
const entry: any =
typeof options.entry === 'function' ? options.entry() : Promise.resolve(options.entry)
Expand All @@ -31,9 +21,8 @@ function injectEntry(
)
}

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

return e
})
}
Expand All @@ -46,14 +35,9 @@ export class SourceReplacementPlugin {
}

apply(compiler: Webpack.Compiler) {
new AddAssetHtmlWebpackPlugin({
filepath: require.resolve('source-replacement'),
attributes: {
type: 'module',
async: true,
},
}).apply(compiler)

injectEntry(compiler.options, this.entryName, CODE_BLOCKER_SRC)
injectEntry(compiler.options, this.entryName, [
require.resolve('source-replacement'),
require.resolve('source-replacement/build/code-blocker.js'),
])
}
}
69 changes: 36 additions & 33 deletions test/test-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const path = require('path')

const BUNDLE_FILE_NAME = 'bundle.js'

const SOURCE_REPLACEMENT_FILE_NAME = 'index.js'

const rootPath = process.cwd()
const testPath = path.join(rootPath, 'test')
const buildPath = path.join(testPath, 'build')
Expand All @@ -13,51 +11,56 @@ const htmlPath = path.join(testPath, 'build', 'index.html')
const htmlSource = fs.readFileSync(htmlPath, { encoding: 'utf8' })

function testBundlePreventExecuteSourceWithExistingSource() {
const outputScriptPath = path.join(buildPath, BUNDLE_FILE_NAME)
const outputScriptPath = path.join(buildPath, BUNDLE_FILE_NAME)

const outputText = fs.readFileSync(outputScriptPath, { encoding: 'utf8' })
const outputText = fs.readFileSync(outputScriptPath, { encoding: 'utf8' })

const SOURCE_CONTENT = './test/index.js'

if (!outputText.includes(SOURCE_CONTENT)) {
throw new Error('No input source found in build')
}
const SOURCE_CONTENT = './test/index.js'

const PREVENT_RENDER_CONTENT = 'preventPerformExistingScriptDuringInjection()'

if (!outputText.includes(PREVENT_RENDER_CONTENT)) {
throw new Error('No prevent render source found in build')
}
if (!outputText.includes(SOURCE_CONTENT)) {
throw new Error('No input source found in build')
}

if (outputText.indexOf(PREVENT_RENDER_CONTENT) > outputText.indexOf(SOURCE_CONTENT)) {
throw new Error('Invalid source order')
}
PREVENT_RENDER_CONTENT = 'preventPerformExistingScriptDuringInjection()'

if (!htmlSource.includes(BUNDLE_FILE_NAME)) {
throw new Error('No bundle source found in output')
}
}
const REQUIRE_SOURCE_REPLACEMENT =
'__webpack_require__("./node_modules/source-replacement/build/index.js")'

function testInjectSourceReplacementScript() {
const sourceReplacementPath = path.join(buildPath, SOURCE_REPLACEMENT_FILE_NAME)
const REQUIRE_CODE_BLOCKER =
'__webpack_require__("./node_modules/source-replacement/build/code-blocker.js")'

const REQUIRE_MAIN_SCRIPT = '__webpack_require__("./test/index.js")'

const originalFilePath = path.join(rootPath, 'node_modules', 'source-replacement', 'build', 'index.js')
if (!outputText.includes(PREVENT_RENDER_CONTENT)) {
throw new Error('No prevent render source found in build')
}

const sourceContent = fs.readFileSync(sourceReplacementPath, { encoding: 'utf8' })
if (
outputText.indexOf(PREVENT_RENDER_CONTENT) > outputText.indexOf(SOURCE_CONTENT) &&
outputText.indexOf(SOURCE_CONTENT) > outputText.indexOf(REQUIRE_MAIN_SCRIPT)
) {
throw new Error('Invalid source order')
}

if (!htmlSource.includes(BUNDLE_FILE_NAME)) {
throw new Error('No bundle source found in output')
}
}

function testInjectSourceReplacementScript() {
const sourceReplacementPath = path.join(buildPath, BUNDLE_FILE_NAME)

const sourceOrigin = fs.readFileSync(originalFilePath, { encoding: 'utf8' })
const sourceContent = fs.readFileSync(sourceReplacementPath, { encoding: 'utf8' })

if (!sourceContent.includes(sourceOrigin)) {
throw new Error('Unmatched source replacement source')
}
const FIRST_FUNCTION_REPLACEMENT_SCRIPT = 'getAugmentedNamespace(n)'

if (!htmlSource.includes(`<script src="${SOURCE_REPLACEMENT_FILE_NAME}" type="module" async></script>`)) {
throw new Error('No bundle source found in output')
}
if (!sourceContent.includes(FIRST_FUNCTION_REPLACEMENT_SCRIPT)) {
throw new Error('Unmatched source replacement source')
}
}

testBundlePreventExecuteSourceWithExistingSource()

testInjectSourceReplacementScript()

console.log('Test Passed')
console.log('Test Passed')
38 changes: 1 addition & 37 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,6 @@ acorn@^8.0.4, acorn@^8.5.0:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.0.tgz#88c0187620435c7f6015803f5539dae05a9dbea8"
integrity sha512-QOxyigPVrpZ2GXT+PFyZTl6TtOFc5egxHIP9IlQ+RbupQuX4RkT/Bee4/kQuC02Xkzg84JcT7oLYtDIQxp+v7w==

[email protected]:
version "5.0.1"
resolved "https://registry.yarnpkg.com/add-asset-html-webpack-plugin/-/add-asset-html-webpack-plugin-5.0.1.tgz#1e83e1a54a2e093c62888d9134b90eca66c0b707"
integrity sha512-KdTDqG0I1OXYfiMuDkjUWJf7zjhrqdnSfKfX+ZD51hyEmyUOxubJIHhSu6Ri3MURWzqBKWNFMqdBTkr2yNFimA==
dependencies:
globby "^11.1.0"
micromatch "^4.0.4"

agent-base@6, agent-base@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
Expand Down Expand Up @@ -1998,17 +1990,6 @@ fast-glob@^3.1.1:
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-glob@^3.2.9:
version "3.2.11"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.11.tgz#a1172ad95ceb8a16e20caa5c5e56480e5129c1d9"
integrity sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==
dependencies:
"@nodelib/fs.stat" "^2.0.2"
"@nodelib/fs.walk" "^1.2.3"
glob-parent "^5.1.2"
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-json-stable-stringify@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
Expand Down Expand Up @@ -2280,18 +2261,6 @@ globby@^11.0.0, globby@^11.0.1:
merge2 "^1.3.0"
slash "^3.0.0"

globby@^11.1.0:
version "11.1.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b"
integrity sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==
dependencies:
array-union "^2.1.0"
dir-glob "^3.0.1"
fast-glob "^3.2.9"
ignore "^5.2.0"
merge2 "^1.4.1"
slash "^3.0.0"

graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.3, graceful-fs@^4.2.4, graceful-fs@^4.2.8:
version "4.2.8"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.8.tgz#e412b8d33f5e006593cbd3cee6df9f2cebbe802a"
Expand Down Expand Up @@ -2507,11 +2476,6 @@ ignore@^5.1.4:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.9.tgz#9ec1a5cbe8e1446ec60d4420060d43aa6e7382fb"
integrity sha512-2zeMQpbKz5dhZ9IwL0gbxSW5w0NK/MSAMtNuhgIHEPmaU3vPdKPL0UdvUCXs5SS4JAwsBxysK5sFMW8ocFiVjQ==

ignore@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.2.0.tgz#6d3bac8fa7fe0d45d9f9be7bac2fc279577e345a"
integrity sha512-CmxgYGiEPCLhfLnpPp1MoRmifwEIOgjcHXxOBjv7mY96c+eWScsOP9c112ZyLdWHi0FxHjI+4uVhKYp/gcdRmQ==

import-fresh@^3.0.0, import-fresh@^3.2.1:
version "3.3.0"
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
Expand Down Expand Up @@ -3336,7 +3300,7 @@ merge-stream@^2.0.0:
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==

merge2@^1.3.0, merge2@^1.4.1:
merge2@^1.3.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"
integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==
Expand Down

0 comments on commit 8f87380

Please sign in to comment.