Skip to content

Commit a2fb9e8

Browse files
committed
Update to Webpack 5 🎉
But got a deprecation warning: > BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
1 parent 14f8c9b commit a2fb9e8

5 files changed

Lines changed: 297 additions & 66 deletions

File tree

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@
5050
"devDependencies": {
5151
"@size-limit/preset-small-lib": "^4.10.2",
5252
"@types/fs-extra": "^9.0.11",
53-
"@types/webpack": "4.41.27",
53+
"@types/webpack": "5.28.0",
54+
"file-loader": "^6.2.0",
5455
"fs-extra": "^9.1.0",
5556
"size-limit": "^4.10.2",
5657
"tsdx": "^0.14.1",
5758
"tslib": "^2.2.0",
5859
"typescript": "^4.2.4",
59-
"webpack": "^4.1.1"
60+
"webpack": "^5.28.0"
6061
}
6162
}

src/build-file.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface BuildFileOptions {
1010
}
1111

1212
export default function buildFile({ compiler, gitWorkTree, command, replacePattern, asset }: BuildFileOptions) {
13-
let data: string | undefined = ''
13+
let data: string = ''
1414

1515
compiler.hooks.compilation.tap('GitRevisionWebpackPlugin', compilation => {
1616
compilation.hooks.optimizeTree.tapAsync('optimize-tree', (_, __, callback) => {
@@ -24,12 +24,7 @@ export default function buildFile({ compiler, gitWorkTree, command, replacePatte
2424
})
2525
})
2626

27-
// we are missing the type definition for assetPath
28-
// TODO: create a PR to DefinitelyTyped
29-
const mainTemplate = compilation.mainTemplate as any
30-
31-
// TODO remove `any` once we get the type definitions
32-
mainTemplate.hooks.assetPath.tap('GitRevisionWebpackPlugin', (assetPath: any, chunkData: any) => {
27+
compilation.hooks.assetPath.tap('GitRevisionWebpackPlugin', (assetPath: any, chunkData: any) => {
3328
const path = typeof assetPath === 'function' ? assetPath(chunkData) : assetPath
3429

3530
if (!data) return path
@@ -45,6 +40,16 @@ export default function buildFile({ compiler, gitWorkTree, command, replacePatte
4540
size: function() {
4641
return data ? data.length : 0
4742
},
43+
buffer: function() {
44+
return Buffer.from(data)
45+
},
46+
map: function() {
47+
return {}
48+
},
49+
sourceAndMap: function() {
50+
return { source: data, map: {} }
51+
},
52+
updateHash: function() {},
4853
}
4954
})
5055
}

src/helpers/run-git-command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'path'
44
import removeEmptyLines from './remove-empty-lines'
55

66
interface Cb {
7-
(err: Error | null, output?: string): void
7+
(err: Error | null, output: string): void
88
}
99

1010
export default function(gitWorkTree: string | undefined, command: string, callback?: Cb) {
@@ -15,7 +15,7 @@ export default function(gitWorkTree: string | undefined, command: string, callba
1515
if (callback) {
1616
exec(gitCommand, function(err, stdout) {
1717
if (err) {
18-
return callback(err)
18+
return callback(err, '')
1919
}
2020
callback(null, removeEmptyLines(stdout))
2121
})

test/integration.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const targetGitRepository = path.join(__dirname, '../tmp/project/.git')
1313
const targetBuild = path.join(__dirname, '../tmp/build')
1414

1515
describe('git-revision-webpack-plugin default', function() {
16-
beforeEach(function(done) {
16+
beforeAll(function(done) {
1717
fs.emptyDirSync(targetProject)
1818
fs.copySync(sourceProject, targetProject)
1919

@@ -95,7 +95,7 @@ describe('git-revision-webpack-plugin default', function() {
9595
})
9696

9797
describe('git-revision-webpack-plugin with lightweightTags option', function() {
98-
beforeEach(function(done) {
98+
beforeAll(function(done) {
9999
fs.emptyDirSync(targetProject)
100100
fs.copySync(sourceProject, targetProject)
101101

@@ -153,7 +153,7 @@ describe('git-revision-webpack-plugin with lightweightTags option', function() {
153153
const mainJs = fs.readFileSync(versionPath, 'utf8')
154154

155155
const expectedPublicPath =
156-
'r.p="http://cdn.com/assets/master/v2.0.0-beta/9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2"'
156+
'o.p="http://cdn.com/assets/master/v2.0.0-beta/9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2"'
157157

158158
expect(mainJs.indexOf(expectedPublicPath) !== -1).toEqual(true)
159159
})
@@ -178,7 +178,7 @@ describe('git-revision-webpack-plugin with lightweightTags option', function() {
178178
})
179179

180180
describe('git-revision-webpack-plugin without branch option', function() {
181-
beforeEach(function(done) {
181+
beforeAll(function(done) {
182182
fs.emptyDirSync(targetProject)
183183
fs.copySync(sourceProject, targetProject)
184184

0 commit comments

Comments
 (0)