Skip to content

Commit

Permalink
Add script maps
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSurgisonGDS committed Feb 3, 2023
1 parent 7bb6ba2 commit 8697389
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
26 changes: 22 additions & 4 deletions lib/plugins/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,32 @@ const moduleToPluginConversion = {

const readJsonFile = (filePath) => JSON.parse(fs.readFileSync(filePath, 'utf8'))

const pathIsAlreadyInAssetList = (assetPath, assets) => assets.some((asset) => asset === assetPath || assetPath.startsWith(asset + '/'))

function addScriptMaps (packageName, { scripts = [], assets = [] }) {
if (typeof scripts === 'string') {
scripts = [scripts]
}
if (typeof assets === 'string') {
assets = [assets]
}
return scripts.reduce((currentAssets, scriptPath) => {
const mapPath = scriptPath + '.map'
const pathToMapFile = getPathFromProjectRoot('node_modules', packageName, mapPath)
return fs.existsSync(pathToMapFile) && !pathIsAlreadyInAssetList(mapPath, currentAssets) ? [...currentAssets, mapPath] : currentAssets
}, assets) || assets
}

function getPackageConfig (packageName) {
if (fs.existsSync(pathToPackageConfigFile(packageName))) {
return readJsonFile(pathToPackageConfigFile(packageName))
let config
const pkgConfigFile = pathToPackageConfigFile(packageName)
if (fs.existsSync(pkgConfigFile)) {
config = readJsonFile(pkgConfigFile)
}
if (Object.prototype.hasOwnProperty.call(moduleToPluginConversion, packageName)) {
return moduleToPluginConversion[packageName]
config = moduleToPluginConversion[packageName]
}
return {}
return config ? { ...config, assets: addScriptMaps(packageName, config) } : {}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions lib/plugins/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ describe('plugins', () => {

expect(plugins.getPublicUrls('assets')).toEqual(['/plugin-assets/mine/abc%3Adef'])
})
it('should include the url for the script mapping file', () => {
testScope.fileSystem.writeFile('node_modules/govuk-frontend/govuk/all.js.map'.split('/'), 'dummy mapping file')
mockPluginConfig('mine', {})
expect(plugins.getPublicUrls('assets')).toContain('/plugin-assets/govuk-frontend/govuk/all.js.map')
})
it('should not include the url for the script mapping file', () => {
mockPluginConfig('mine', {})
expect(plugins.getPublicUrls('assets')).not.toContain('/plugin-assets/govuk-frontend/govuk/all.js.map')
})
})

describe('Lookup public URLs with file system paths', () => {
Expand Down

0 comments on commit 8697389

Please sign in to comment.