This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Bundle with Source Replacement v2
BREAKING CHANGE: Change Source Replacement Engine to v2 and new webpack plugin engine inside
- Loading branch information
Showing
6 changed files
with
605 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
import Webpack from 'webpack' | ||
import AddAssetHtmlWebpackPlugin from 'add-asset-html-webpack-plugin' | ||
import InjectPlugin, { ENTRY_ORDER } from 'webpack-inject-plugin' | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { BundleMoreWebpackPlugin } from 'bundle-more-webpack-plugin' | ||
|
||
const MODULE_NAME = path.resolve(process.cwd(), 'node_modules', 'source-replacement', 'build', 'index.js') | ||
export class SourceReplacementPlugin extends BundleMoreWebpackPlugin { | ||
constructor() { | ||
super([ | ||
MODULE_NAME, | ||
]) | ||
} | ||
const CODE_BLOCKER_SRC = path.resolve( | ||
process.cwd(), | ||
'node_modules', | ||
'source-replacement', | ||
'build', | ||
'code-blocker.js', | ||
) | ||
|
||
export class SourceReplacementPlugin { | ||
apply(complier: Webpack.Compiler) { | ||
new AddAssetHtmlWebpackPlugin({ | ||
filepath: require.resolve('source-replacement'), | ||
attributes: { | ||
type: 'module', | ||
async: true, | ||
}, | ||
}).apply(complier) | ||
|
||
new InjectPlugin(() => fs.readFileSync(CODE_BLOCKER_SRC, { encoding: 'utf8' }), { | ||
entryOrder: ENTRY_ORDER.First, | ||
}).apply(complier) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,63 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const BUNDLE_FILE_NAME = 'bundle.js' | ||
|
||
const SOURCE_REPLACEMENT_FILE_NAME = 'index.js' | ||
|
||
const rootPath = process.cwd() | ||
const buildScriptPath = path.join('build', 'index.js') | ||
const testPath = path.join(rootPath, 'test') | ||
const outputScriptPath = path.join(testPath, buildScriptPath) | ||
const buildPath = path.join(testPath, 'build') | ||
|
||
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 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 PREVENT_RENDER_CONTENT = 'preventPerformExistingScriptDuringInjection()' | ||
|
||
if (!outputText.includes(PREVENT_RENDER_CONTENT)) { | ||
throw new Error('No prevent render source found in build') | ||
} | ||
|
||
const outputText = fs.readFileSync(outputScriptPath).toString() | ||
if (outputText.indexOf(PREVENT_RENDER_CONTENT) > outputText.indexOf(SOURCE_CONTENT)) { | ||
throw new Error('Invalid source order') | ||
} | ||
|
||
if (!outputText.includes('./test/index.js')) { | ||
throw new Error('No input source found in build') | ||
if (!htmlSource.includes(BUNDLE_FILE_NAME)) { | ||
throw new Error('No bundle source found in output') | ||
} | ||
} | ||
|
||
if (!outputText.includes("console.log('Source Replacement is running');")) { | ||
throw new Error('No source replacement source found in build') | ||
function testInjectSourceReplacementScript() { | ||
const sourceReplacementPath = path.join(buildPath, SOURCE_REPLACEMENT_FILE_NAME) | ||
|
||
const originalFilePath = path.join(rootPath, 'node_modules', 'source-replacement', 'build', 'index.js') | ||
|
||
const sourceContent = fs.readFileSync(sourceReplacementPath, { encoding: 'utf8' }) | ||
|
||
const sourceOrigin = fs.readFileSync(originalFilePath, { encoding: 'utf8' }) | ||
|
||
if (!sourceContent.includes(sourceOrigin)) { | ||
throw new Error('Unmatched source replacement source') | ||
} | ||
|
||
if (!htmlSource.includes(`<script src="${SOURCE_REPLACEMENT_FILE_NAME}" type="module" async></script>`)) { | ||
throw new Error('No bundle source found in output') | ||
} | ||
} | ||
|
||
console.log('This plugin is fine to be used.') | ||
testBundlePreventExecuteSourceWithExistingSource() | ||
|
||
testInjectSourceReplacementScript() | ||
|
||
console.log('Test Passed') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.