Skip to content
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

Add common Webpack Build Config for NextJS projects and Storybook for a shared SVGR integration. #37

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*.d.ts
*.js
!/@types/*.d.ts
!/src/build-config/*.js

# don't ignore test files
!/tests/**/*.js
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.3.0
=====

* (feature) Add common Webpack Build Config for NextJS projects and Storybook for a shared SVGR integration.


2.2.1
=====

Expand Down
4 changes: 4 additions & 0 deletions finalize-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ console.log("");

console.log("Copying additional files");


await Promise.all(
[
copy("src/build-config/webpack.js", `dist/build-config/webpack.js`),
],
[
"CHANGELOG.md",
"README.md",
Expand Down
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@
"xtend": "^4.0.2"
},
"optionalDependencies": {
"next": "^14.1",
"react": "^18.0",
"react-dom": "^18.0"
"@svgr/webpack": "^8.1.0",
"next": "^14.2.3",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/node": "^18.15.1",
"@types/qunit": "^2.19.4",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@types/xtend": "^4.0.4",
"fs-extra": "^11.1.0",
"@types/node": "^18.19.33",
"@types/qunit": "^2.19.10",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/xtend": "^4.0.6",
"fs-extra": "^11.2.0",
"next": "^14.1.3",
"prettier-package-json": "^2.8.0",
"publint": "^0.2.7",
"qunit": "^2.19.4",
"publint": "^0.2.8",
"qunit": "^2.20.1",
"react": "^18.0",
"react-dom": "^18.0",
"typescript": "^5.4.2"
"typescript": "^5.4.5"
},
"publishConfig": {
"access": "public"
Expand Down
48 changes: 48 additions & 0 deletions src/build-config/webpack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const registerSvgWebpackLoader = (config) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should write it as TS now, so we don't need any new infrastructure.
Type-wise that might get hard, but using simplified types here is fine.

{
// Grab the existing rule that handles SVG imports
const fileLoaderRule = config.module.rules.find((rule) => rule.test?.test?.(".svg") && rule.resourceQuery?.toString() !== "/raw/");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the deal about the /raw/?

// Modify the file loader rule to ignore *.svg, since we have it handled now.
fileLoaderRule.exclude = /\.svg$/i;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should

  • add this exclude to every rule, not just the first we find (there might be multiple ones)
  • should merge the exclude with a maybe existing exclude
  • find out why we tested for /raw/ above


// Convert all other *.svg imports to React components
config.module.rules.push({
test: /\.svg$/i,
issuer: { not: /\.(css|scss|sass)$/ },
resourceQuery: { not: /url/ }, // exclude if *.svg?url
use: [{
loader: "@svgr/webpack",
options: {
dimensions: true,
svgo: true,
svgoConfig: {
plugins: [
{
name: "preset-default",
params: {
overrides: {
// ensure unique IDs when embedding multiple times
cleanupIds: false,
// don't remove the viewbox, to avoid scaling issues
removeViewBox: false,
},
},
},
{
name: "removeAttrs",
params: {
attrs: "data-.*",
},
},
{
// ensure unique IDs when embedding multiple times
name: "prefixIds",
},
],
},
},
}],
});

return config;
};