Skip to content

Commit

Permalink
enhancement/general improvements and fixes for custom resource plugin…
Browse files Browse the repository at this point in the history
… integrations (#1234)

* feature/discussion 1117 Isolation Mode (v1) (#1206)

* isolation mode for SSR pages and API routes for greenwood serve

* documentation for isolation mode option and global config test case

* misc refactoring

* set isolation mode to true for Lit renderer plugin

* set isolation mode to true for Lit renderer plugin

* v0.30.0-alpha.0

* v0.30.0-alpha.1

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

* intial draft of import attributes support for CSS and JSON

* all test cases passing

* need patch package

* wcc patches for import attributes and CSSStylesheet shim

* bump min NodeJS version for exp specs

* temp disable ESLint

* develop based import assertion specs

* serve based import attributes specs

* add preIntercept resource plugin lifecycle and refactor PostCSS to use it

* all test cases passing for import attributes support

* refactor built in CSS and JSON intercepting

* demo code

* raw plugin docs and package.json updates

* update latest documentation for custom loaders support in NodeJS

* update custom import docs

* upgrade wcc v0.13.0

* only need Node 18 for github actions

* css imports and raw plugin interop with test cases

* lit renderer import attribute test cases and documentation

* refactor matchers support for raw plugin instead of patching and add test cases

* disable describe.only

* update usage for custom resource plugins to showcase usage of import attributes

* document preIntercept lifecycle and convert Babel to use it

* restore ESLint

* enable debug logging for failing specs

* refactor theme pack specs

* fix linting

* remove CSS and JSON packages from being publishable

* clean up console logs and comments

* rename exp test cases to loadersnaming prefix

* fix command in github actions

* remove plugin-import-css callout from plugin-postcss README

* remove demo code from website

* refine PostCSS plugin intercepting

* general enhancements and fixes for custom resource plugin integrations

* misc clean up validation testing
  • Loading branch information
thescientist13 authored Jun 22, 2024
1 parent 87cdf8d commit c08cd27
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
6 changes: 6 additions & 0 deletions packages/cli/src/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ function greenwoodResourceLoader (compilation) {
}
}

for (const plugin of resourcePlugins) {
if (plugin.shouldOptimize && await plugin.shouldOptimize(idUrl, response.clone())) {
response = await plugin.optimize(idUrl, response.clone());
}
}

return await response.text();
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/lifecycles/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ async function getDevServer(compilation) {
const merged = mergeResponse(response.clone(), current.clone());

response = merged;
break;
}
}

Expand Down
48 changes: 32 additions & 16 deletions packages/cli/src/loader.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
import { readAndMergeConfig as initConfig } from './lifecycles/config.js';
import { mergeResponse } from './lib/resource-utils.js';

const config = await initConfig();
const resourcePlugins = config.plugins.filter(plugin => plugin.type === 'resource').map(plugin => plugin.provider({
context: {
projectDirectory: new URL(`file://${process.cwd()}`)
},
config: {
devServer: {}
},
graph: []
}));

async function getCustomLoaderResponse(url, checkOnly = false) {
const resourcePlugins = config.plugins
.filter(plugin => plugin.type === 'resource')
.filter(plugin => plugin.name !== 'plugin-node-modules:resource' && plugin.name !== 'plugin-user-workspace')
.map(plugin => plugin.provider({
context: {
projectDirectory: new URL(`file://${process.cwd()}/`),
scratchDir: new URL(`file://${process.cwd()}/.greenwood/`)
},
config: {
devServer: {}
},
graph: []
}));

async function getCustomLoaderResponse(initUrl, checkOnly = false) {
const headers = {
'Accept': 'text/javascript',
'Sec-Fetch-Dest': 'empty'
};
const request = new Request(url, { headers });
const initResponse = new Response('');
let request = new Request(initUrl, { headers });
let url = initUrl;
let response = initResponse.clone();
let shouldHandle = false;

for (const plugin of resourcePlugins) {
if (plugin.shouldServe && await plugin.shouldServe(url, request)) {
if (initUrl.protocol === 'file:' && plugin.shouldResolve && await plugin.shouldResolve(initUrl, request)) {
shouldHandle = true;

if (!checkOnly) {
response = await plugin.serve(url, request);
url = new URL((await plugin.resolve(initUrl, request)).url);
}
}
}

for (const plugin of resourcePlugins) {
if (plugin.shouldServe && await plugin.shouldServe(initUrl, request)) {
shouldHandle = true;

if (!checkOnly) {
response = mergeResponse(response, await plugin.serve(initUrl, request));
}
}
}
Expand All @@ -36,15 +52,15 @@ async function getCustomLoaderResponse(url, checkOnly = false) {
shouldHandle = true;

if (!checkOnly) {
response = await plugin.preIntercept(url, request, response.clone());
response = mergeResponse(response, await plugin.preIntercept(url, request, response.clone()));
}
}

if (plugin.shouldIntercept && await plugin.shouldIntercept(url, request, response.clone())) {
shouldHandle = true;

if (!checkOnly) {
response = await plugin.intercept(url, request, response.clone());
response = mergeResponse(response, await plugin.intercept(url, request, response.clone()));
}
}
}
Expand Down

0 comments on commit c08cd27

Please sign in to comment.