-
Notifications
You must be signed in to change notification settings - Fork 166
Use Webpack manifest to associate asset references per pack #6198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
69aaeed
Create manifest to associate asset references per Webpack pack
aduth 762f1d5
Emit as assets.json
aduth 1f1ac90
Retrieve assets from asset.json manifest output
aduth 36e2d0a
Use existing manifest to append assets
aduth 197533c
Omit nil values from AssetSources.get_assets
aduth 0f406a1
Add getAssetPath specs
aduth 30ec119
Add specs for AssetsWebpackPlugin
aduth 81a0bb3
Clean up unused references
aduth e7dd39c
Remove more remnants of asset-checker
aduth 71c243b
Add specs for AssetSources.get_assets
aduth 2ac7181
Stub CSP method for test
aduth 1600b40
Render JavaScript tag with script text argument
aduth 79e54a9
Add specs for render_javascript_pack_once_tags assets behavior
aduth 57be65e
Avoid polluting global namespace with content_security_policy_nonce
aduth bed0542
Add inline comment describing publicPath customization
aduth b970eba
Emit asset map as JSON
aduth 52a6223
bump circleci
aduth aed7e73
Add type signature for getAssetPath
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,32 @@ | ||
| import { getAssetPath } from './index'; | ||
|
|
||
| describe('getAssetPath', () => { | ||
| beforeEach(() => { | ||
| delete getAssetPath.cache; | ||
| }); | ||
|
|
||
| it('returns undefined', () => { | ||
| expect(getAssetPath('foo.svg')).to.be.undefined(); | ||
| }); | ||
|
|
||
| context('with global assets not including the provided asset', () => { | ||
| beforeEach(() => { | ||
| document.body.innerHTML = '<script type="application/json" data-asset-map>{}</script>'; | ||
| }); | ||
|
|
||
| it('returns undefined for missing assets', () => { | ||
| expect(getAssetPath('foo.svg')).to.be.undefined(); | ||
| }); | ||
| }); | ||
|
|
||
| context('with global assets including the provided asset', () => { | ||
| beforeEach(() => { | ||
| document.body.innerHTML = | ||
| '<script type="application/json" data-asset-map>{"foo.svg":"bar.svg"}</script>'; | ||
| }); | ||
|
|
||
| it('returns the mapped asset path', () => { | ||
| expect(getAssetPath('foo.svg')).to.equal('bar.svg'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,16 @@ | ||
| type AssetPaths = Record<string, string>; | ||
|
|
||
| export function getAssetPath(path: string): string | undefined { | ||
| if (!getAssetPath.cache) { | ||
| try { | ||
| const script = document.querySelector('[data-asset-map]') as HTMLScriptElement; | ||
| getAssetPath.cache = JSON.parse(script.textContent!); | ||
| } catch { | ||
| getAssetPath.cache = {}; | ||
| } | ||
| } | ||
|
|
||
| return getAssetPath.cache![path]; | ||
| } | ||
|
|
||
| getAssetPath.cache = undefined as AssetPaths | undefined; |
This file contains hidden or 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,9 @@ | ||
| { | ||
| "name": "@18f/identity-assets", | ||
| "private": true, | ||
| "version": "1.0.0", | ||
| "main": "index.js", | ||
| "peerDependencies": { | ||
| "webpack": ">=5" | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| actual* |
This file contains hidden or 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,3 @@ | ||
| const getAssetPath = () => {}; | ||
|
|
||
| globalThis.path = getAssetPath('foo.svg'); |
This file contains hidden or 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,72 @@ | ||
| const { Compilation } = require('webpack'); | ||
|
|
||
| /** @typedef {import('webpack/lib/ChunkGroup')} ChunkGroup */ | ||
| /** @typedef {import('webpack/lib/Entrypoint')} Entrypoint */ | ||
|
|
||
| /** | ||
| * Webpack plugin name. | ||
| */ | ||
| const PLUGIN = 'AssetsWebpackPlugin'; | ||
|
|
||
| /** | ||
| * Regular expression matching calls to retrieve asset path. | ||
| */ | ||
| const GET_ASSET_CALL = /getAssetPath\)?\(\s*['"](.+?)['"]/g; | ||
|
|
||
| /** | ||
| * Given a file name, returns true if the file is a JavaScript file, or false otherwise. | ||
| * | ||
| * @param {string} filename | ||
| * | ||
| * @return {boolean} | ||
| */ | ||
| const isJavaScriptFile = (filename) => filename.endsWith('.js'); | ||
|
|
||
| /** | ||
| * Given a string of source code, returns array of asset paths. | ||
| * | ||
| * @param source Source code. | ||
| * | ||
| * @return {string[]} Asset paths. | ||
| */ | ||
| const getAssetPaths = (source) => | ||
| Array.from(source.matchAll(GET_ASSET_CALL)).map(([, path]) => path); | ||
|
|
||
| /** | ||
| * Adds the given asset file name to the list of files of the group's parent entrypoint. | ||
| * | ||
| * @param {string[]} filenames Asset filename. | ||
| * @param {ChunkGroup|Entrypoint} group Chunk group. | ||
| */ | ||
| const addFilesToEntrypoint = (filenames, group) => | ||
| typeof group.getEntrypointChunk === 'function' | ||
| ? filenames.forEach((filename) => group.getEntrypointChunk().files.add(filename)) | ||
| : group.parentsIterable.forEach((parent) => addFilesToEntrypoint(filenames, parent)); | ||
|
|
||
| class AssetsWebpackPlugin { | ||
| /** | ||
| * @param {import('webpack').Compiler} compiler | ||
| */ | ||
| apply(compiler) { | ||
| compiler.hooks.compilation.tap('compile', (compilation) => { | ||
| compilation.hooks.processAssets.tap( | ||
| { name: PLUGIN, stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS }, | ||
| () => { | ||
| compilation.chunks.forEach((chunk) => { | ||
| [...chunk.files].filter(isJavaScriptFile).forEach((filename) => { | ||
| const source = compilation.assets[filename].source(); | ||
| const assetPaths = getAssetPaths(source); | ||
| if (assetPaths.length) { | ||
| Array.from(chunk.groupsIterable).forEach((group) => { | ||
| addFilesToEntrypoint(assetPaths, group); | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| module.exports = AssetsWebpackPlugin; |
This file contains hidden or 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,48 @@ | ||
| const path = require('path'); | ||
| const { promises: fs } = require('fs'); | ||
| const webpack = require('webpack'); | ||
| const WebpackAssetsManifest = require('webpack-assets-manifest'); | ||
| const AssetsWebpackPlugin = require('./webpack-plugin'); | ||
|
|
||
| describe('AssetsWebpackPlugin', () => { | ||
| it('generates expected output', (done) => { | ||
| webpack( | ||
| { | ||
| mode: 'development', | ||
| devtool: false, | ||
| entry: path.resolve(__dirname, 'spec/fixtures/in.js'), | ||
| plugins: [ | ||
| new AssetsWebpackPlugin(), | ||
| new WebpackAssetsManifest({ | ||
| entrypoints: true, | ||
| publicPath: true, | ||
| writeToDisk: true, | ||
| output: 'actualmanifest.json', | ||
| }), | ||
| ], | ||
| output: { | ||
| path: path.resolve(__dirname, 'spec/fixtures'), | ||
| filename: 'actual[name].js', | ||
| }, | ||
| }, | ||
| async (webpackError) => { | ||
| try { | ||
| expect(webpackError).to.be.null(); | ||
|
|
||
| const manifest = JSON.parse( | ||
| await fs.readFile( | ||
| path.resolve(__dirname, 'spec/fixtures/actualmanifest.json'), | ||
| 'utf-8', | ||
| ), | ||
| ); | ||
|
|
||
| expect(manifest.entrypoints.main.assets.svg).to.include.all.members(['foo.svg']); | ||
|
|
||
| done(); | ||
| } catch (error) { | ||
| done(error); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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
21 changes: 0 additions & 21 deletions
21
app/javascript/packages/document-capture/hooks/use-asset.js
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect I'll need to revisit this in future work, where we'll want to use this for the design system icon sprite, which can't be loaded across domains, so we'll need to have a way to pass
host: asset_hostlike we do inIconComponent:identity-idp/app/components/icon_component.rb
Line 261 in 5a8af56
Thinking we might need to have some sort of allowlist, e.g.
(
hostoption uses falsey assignment for defaulting)