ProjectEvergreen / greenwood

Greenwood is your full-stack workbench for the web, focused on supporting modern web standards and development to help you create your next project.
https://www.greenwoodjs.io
MIT License
94 stars 9 forks source link

feature/issue 923 native import attributes for CSS and JSON #1215

Closed thescientist13 closed 2 months ago

thescientist13 commented 3 months ago

Related Issue

resolves #923

Summary of Changes

  1. Add support for Import Attributes for JSON / CSS inside the CLI and added test cases
  2. Had to add a preIntercept lifecycle hook for Resource plugins (for PostCSS to run prior to Greenwood transforming the CSS. Also updated Babel plugin to use it)
  3. Created a "raw" plugin to support legacy import behaviors
  4. Update minimum Node version to be 18.20.0
  5. Had to adjust ESLint based on markdown plugin not working with custom parsers 😢 - https://github.com/eslint/eslint-plugin-markdown/discussions/221
  6. Rename custom loaders naming convention from exp-* -> loaders-* and updated GitHub actions and the like accordingly

TL;DR

Without any plugins, you will be able to do this in Greenwood automatically

import sheet from './some.css' with { type: 'css' }; // you will get back a Constructable Stylesheet
import data from './some.json' with { type: 'json' }; // you will get back a JSON object

See https://github.com/ProjectEvergreen/greenwood/discussions/1216 for future thoughts / enhancements

TODO

  1. [x] Fix failing build (patch package failing to patch changes)
  2. [x] Restore ESLint (it doesn't support Import Attributes yet)
  3. [x] Windows tests are now failing (seems to be fine when running locally on parallels 🤔 )
  4. [x] Pattern for establishing import attributes in resource plugins as a request + header and / or URL + query params - https://github.com/ProjectEvergreen/greenwood/discussions/1216
    • Can / should greenwood convert import attributes to query params or some other signal from the browser that it is an fetch / ESM request
    • type=??? - query params are bad for node / URLs
    • fetch-sec-dest header is supported in all browsers now! - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Dest
    • refactor all standard plugins to follow the new convention
  5. [x] what is the right Content-Type to return for an import attribute? - https://github.com/ProjectEvergreen/greenwood/discussions/1216
  6. [x] Create a "raw" / text import plugin for backwards compat or to import anything as just a string
    • initial implementation
    • package.json / README
    • test cases (+matchers)
    • resolve / rewrite refactor and remove patch for eve-container.css
  7. [x] Figure out support policy for non-standard imports, e.g. import css from './eve-button.css';
    • raw loader accepts a matcher option
    • should we discourage it in anyway?
  8. [x] Minimum Node version (21.x) - - https://github.com/ProjectEvergreen/greenwood/issues/1202
  9. [x] test if we still need type !== 'json' check for greenwood-meta-import-url plugin
  10. [x] WCC Upstreams
  11. [x] See if we can not require bundling for CSS / JSON through rollup at least - https://github.com/ProjectEvergreen/greenwood/discussions/1216
    • emit import attributes as assets
    • might need acornInjectPlugins for API routes and SSR pages rollup configs
  12. [x] Any custom plugins should should adopt these new conventions (using import attributes and no longer needing ?type=xxx)
    • JSX
    • GraphQL
  13. [x] Documentation
    • Scripts and Imports
    • SSR Custom Imports
      • No minimum Node version needed (now aligns with Greenwood)
      • experimental flag not needed
    • document / track native import assertions with NodeJS and Rollup
      • 1218

      • 1216

    • Add raw plugin to plugins list
    • preIntercept lifecycle for resource plugins
  14. [x] (nice to have) test with Lit renderer plugin
  15. [x] deprecate CSS / JSON import plugins and remove from website docs
  16. [x] clean up console logs / TODOs / remove demo code

Questions / Follow Up Tasks

  1. [ ] rename shouldIntercept -> shouldTransform / shouldProcess?
  2. [ ] Seems odd we don't actually need the acorn-import-attributes plugin for Rollup?
  3. [x] Track not needing an acorn plugin - https://github.com/ProjectEvergreen/greenwood/issues/1218
  4. [x] Should greenwood not bundle, e.g. just return text instead of CSSStylesheet? - https://github.com/ProjectEvergreen/greenwood/discussions/1216
  5. [x] best way to stub out compilation in src/loader.js? - can worry about it later if needed
  6. [x] Is it experimental? - it is not!
    • technically yes because of NodeJS I suppose, even though the syntax isn't?
    • update test names if we consider it not experimental
  7. [x] Track an issue (or discussion) to refactor all plugins to use Sec-Fetch-Dest where applicable - https://github.com/ProjectEvergreen/greenwood/issues/1222
  8. [x] Track HTML Modules support / HTML import cleanup - https://github.com/ProjectEvergreen/greenwood/issues/new
  9. [ ] refactor duplicate loops over shouldIntercept, etc (good first issue)
  10. [x] Track a discussion on how to allow Greenwood to self-invoke custom loader flag - https://github.com/ProjectEvergreen/greenwood/discussions/1217
  11. [x] See how would these changes impact a CSS modules plugin implementation - https://github.com/ProjectEvergreen/www.greenwoodjs.dev/pull/11/commits/e2e5fd2ea18a6c84f3b8d47965246c8b0b61cefd