-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Treat a missing sourcemap as a warning (#2959)
* Missing sourcemap warning * Linting
- Loading branch information
1 parent
cef23f1
commit 97fc646
Showing
5 changed files
with
168 additions
and
21 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
38 changes: 38 additions & 0 deletions
38
packages/workbox-build/src/lib/translate-url-to-sourcemap-paths.ts
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright 2021 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
import fse from 'fs-extra'; | ||
import upath from 'upath'; | ||
|
||
import {errors} from './errors'; | ||
|
||
export function translateURLToSourcemapPaths( | ||
url: string, | ||
swSrc: string, | ||
swDest: string, | ||
): { | ||
destPath: string | undefined; | ||
srcPath: string | undefined; | ||
warning: string | undefined; | ||
} { | ||
let destPath: string | undefined = undefined; | ||
let srcPath: string | undefined = undefined; | ||
let warning: string | undefined = undefined; | ||
|
||
if (url && !url.startsWith('data:')) { | ||
const possibleSrcPath = upath.resolve(upath.dirname(swSrc), url); | ||
if (fse.existsSync(possibleSrcPath)) { | ||
srcPath = possibleSrcPath; | ||
destPath = upath.resolve(upath.dirname(swDest), url); | ||
} else { | ||
warning = `${errors['cant-find-sourcemap']} ${possibleSrcPath}`; | ||
} | ||
} | ||
|
||
return {destPath, srcPath, warning}; | ||
} |
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
87 changes: 87 additions & 0 deletions
87
test/workbox-build/node/lib/translate-url-to-sourcemap-paths.js
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Copyright 2018 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
const expect = require('chai').expect; | ||
const proxyquire = require('proxyquire'); | ||
|
||
const {errors} = require('../../../../packages/workbox-build/build/lib/errors'); | ||
|
||
describe(`[workbox-build] lib/translate-url-to-sourcemap-paths.ts`, function () { | ||
const MODULE_PATH = | ||
'../../../../packages/workbox-build/build/lib/translate-url-to-sourcemap-paths'; | ||
const URL = 'sw.js.map'; | ||
const SWSRC = 'src/sw.js'; | ||
const SWDEST = 'dist/sw.js'; | ||
|
||
it(`should return undefined paths when url is undefined`, function () { | ||
const {translateURLToSourcemapPaths} = require(MODULE_PATH); | ||
|
||
const {destPath, srcPath, warning} = translateURLToSourcemapPaths( | ||
undefined, | ||
SWSRC, | ||
SWDEST, | ||
); | ||
|
||
expect(destPath).to.be.undefined; | ||
expect(srcPath).to.be.undefined; | ||
expect(warning).to.be.undefined; | ||
}); | ||
|
||
it(`should return undefined paths when url starts with data:`, function () { | ||
const {translateURLToSourcemapPaths} = require(MODULE_PATH); | ||
|
||
const {destPath, srcPath, warning} = translateURLToSourcemapPaths( | ||
`data:${URL}`, | ||
SWSRC, | ||
SWDEST, | ||
); | ||
|
||
expect(destPath).to.be.undefined; | ||
expect(srcPath).to.be.undefined; | ||
expect(warning).to.be.undefined; | ||
}); | ||
|
||
it(`should return undefined paths and a warning when the resolved URL path doesn't exist`, function () { | ||
const {translateURLToSourcemapPaths} = proxyquire(MODULE_PATH, { | ||
'fs-extra': { | ||
existsSync: () => false, | ||
}, | ||
}); | ||
|
||
const {destPath, srcPath, warning} = translateURLToSourcemapPaths( | ||
URL, | ||
SWSRC, | ||
SWDEST, | ||
); | ||
|
||
expect(destPath).to.be.undefined; | ||
expect(srcPath).to.be.undefined; | ||
expect(warning).to.include(errors['cant-find-sourcemap']); | ||
}); | ||
|
||
it(`should return valid paths and no warning when the resolved URL path exists`, function () { | ||
const {translateURLToSourcemapPaths} = proxyquire(MODULE_PATH, { | ||
'fs-extra': { | ||
existsSync: () => true, | ||
}, | ||
'upath': { | ||
resolve: (...args) => args.join('/'), | ||
}, | ||
}); | ||
|
||
const {destPath, srcPath, warning} = translateURLToSourcemapPaths( | ||
URL, | ||
SWSRC, | ||
SWDEST, | ||
); | ||
|
||
expect(destPath).to.eql('dist/sw.js.map'); | ||
expect(srcPath).to.eq('src/sw.js.map'); | ||
expect(warning).to.be.undefined; | ||
}); | ||
}); |
Oops, something went wrong.