Closed michealengland closed 4 years ago
@michealengland Thank you for your work on this. Would you be willing to create a branch off of feature/conditional-css-imports
with an additional commit that adds multiple blocks in the src
directory (perhaps the branch could be called feature/conditional-css-imports-demo
).
Then all of the reviewers could pull down that branch, run npm install && npm run build
and see this updated webpack config in action?
I know this is asking extra of you (and I very much appreciate all you have already done). I do think overall it would streamline the review process so reviewers aren't duplicating this work.
@michealengland Thank you for your work on this. Would you be willing to create a branch off of
feature/conditional-css-imports
with an additional commit that adds multiple blocks in thesrc
directory (perhaps the branch could be calledfeature/conditional-css-imports-demo
).
This is a great idea, I'll get a test branch created and posted shortly.
@salcode Here is a quick test branch I put together.
example/conditional-css-imports-demo
src/example-block-1/style.scss
style.css
and edtior.css
that happens automatically with the use of glob.sync()
.example-block-2
Note: While creating this branch I learned that // comments
in the .scss files were throwing errors and not compiling. This is unrelated to this PR and will PostCSS configuration is finalized.
Include Style & Editor Webpack Entries
Note: This is a temporary solution until Webpack 5 is release which we will resume using splitChunks as recommended by
mini-css-extract-plugin
.Special props shoutout to @salcode for helping simplify this PR dramatically.
This update is a workaround for extracting all CSS into a singular file based on entry name. The recommended method from
mini-css-extract-plugin
is to use splitChunks. However, this feature breaks the main entry JS file. According to this thread https://github.com/webpack-contrib/mini-css-extract-plugin/issues/147 the feature is fixed for Webpack 5 and is not being worked on for Webpack 4 which is our current version. There is not an official release Webpack 5 at this time.Proposed Solution
Using
glob.sync()
we'll conditionally check the/src
directory for allstyle.scss
oreditor.scss
files and add to a glob which will be used as an entry point. If no files are located for eitherstyle.scss
oreditor.scss
then no entry points will be added.Example Directory Structure 📦src ┣ 📂directory-1 ┃ ┣ 📜editor.scss ┃ ┣ 📜index.js ┃ ┗ 📜style.scss ┣ 📂directory-2 ┃ ┣ 📜editor.scss ┃ ┣ 📜index.js ┃ ┗ 📜style.scss ┣ 📂directory-3 ┃ ┣ 📂d3-nested ┃ ┃ ┣ 📜editor.scss ┃ ┃ ┗ 📜index.js ┃ ┣ 📜index.js ┃ ┗ 📜style.scss ┣ 📜index.js ┗ 📜readme.md
Once all of the files have been located the entry points will transcode into a singular
editor.css
andstyle.css
in the/build
directory. It's worth mentioning that additional files are generated and ignored with theEmit
plugin. See ignored files.Example Build Files 📦build ┣ 📜editor.css ┣ 📜index.js ┗ 📜style.css
Ignored Files
Additional Notes
glob.sync()
stylesheets or sass files with a name ofstyle
oreditor
will be auto-loaded if they're located in/src
directory. Each file will be transcoded into astyle.css
oreditor.css
based on the name. This could be an unwanted feature since it's common practice for styles to be imported in the JS.glob.sync()
is not as performant assplitChunks
.How to Test
npm run start
ornpm run build
will generate build files.style.scss
oreditor.scss
and verify that your assets are being generated as expected.If you have any questions or need help, I'm available.