Skip to content

Commit

Permalink
feat: Process script only asset
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 Aug 15, 2022
1 parent 421d6d5 commit 7cd18e5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export class DefineAfterBundleWebpackPlugin {

compilation.hooks.processAssets.tap(DefineAfterBundleWebpackPlugin.name, assets => {
for (const index in assets) {
if (!/\.(js|jsx|ts|tsx)$/i.test(index)) continue

const asset = compilation.getAsset(index)
const source = asset.source

Expand Down
Binary file added test/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
console.log(process.env.TEST)
const image = require('./image.png')

console.log(process.env.TEST)
20 changes: 17 additions & 3 deletions test/test-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@ const path = require('path')

const rootPath = process.cwd()
const buildScriptPath = path.join('build', 'index.js')
const buildImagePath = path.join('build', 'media/image.png')

const testPath = path.join(rootPath, 'test')
const outputScriptPath = path.join(testPath, buildScriptPath)
const sourceMapPath = `${outputScriptPath}.map`

const inputImagePath = 'test/image.png'

const outputImagePath = path.join(testPath, buildImagePath)

const outputText = fs.readFileSync(outputScriptPath).toString()

const inputImageText = fs.readFileSync(inputImagePath).toString()

const outputImageText = fs.readFileSync(outputImagePath).toString()

if (inputImageText !== outputImageText) {
throw new Error('Generated asset is incorrect')
}

if (!outputText.includes('"New Value"')) {
throw new Error('No replacement found in build')
throw new Error('No replacement found in build')
}

const sourceMapText = fs.readFileSync(sourceMapPath).toString()

if (!sourceMapText.includes('console.log(process.env.TEST)')) {
throw new Error('Generated Sourcemap is incorrect')
throw new Error('Generated Sourcemap is incorrect')
}

console.log('This plugin is fine to be used.')
console.log('This plugin is fine to be used.')
39 changes: 25 additions & 14 deletions test/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ const path = require('path')
const { DefineAfterBundleWebpackPlugin } = require('../build')

module.exports = {
entry: path.resolve(__dirname, 'index.js'),
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'build')
},
plugins: [
new DefineAfterBundleWebpackPlugin({
'process.env.TEST': JSON.stringify('New Value'),
}),
],
target: 'node',
mode: 'production',
devtool: 'source-map',
}
entry: path.resolve(__dirname, 'index.js'),
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'build'),
},
plugins: [
new DefineAfterBundleWebpackPlugin({
'process.env.TEST': JSON.stringify('New Value'),
}),
],
module: {
rules: [
{
test: /\.png$/,
type: 'asset/resource',
generator: {
filename: 'media/[name][ext]',
},
},
],
},
target: 'node',
mode: 'production',
devtool: 'source-map',
}

0 comments on commit 7cd18e5

Please sign in to comment.