Skip to content

Commit

Permalink
task: address mutability of compilation object param
Browse files Browse the repository at this point in the history
  • Loading branch information
hutchgrant committed Nov 9, 2020
1 parent a2997ba commit a83d8fc
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
21 changes: 11 additions & 10 deletions packages/cli/src/lifecycles/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ const JSONTransform = require('../transforms/transform.json.js');
const AssetTransform = require('../transforms/transform.assets');

// create an array of transforms for all expected file types
function getTransforms(request, compilation) {
function getTransforms(request) {
// default transforms
const defaultTransforms = [
new HTMLTransform(request, compilation),
new MarkdownTransform(request, compilation),
new CSSTransform(request, compilation),
new JSTransform(request, compilation),
new JSONTransform(request, compilation),
new AssetTransform(request, compilation)
new HTMLTransform(request),
new MarkdownTransform(request),
new CSSTransform(request),
new JSTransform(request),
new JSONTransform(request),
new AssetTransform(request)
];

// custom greenwood configured transform plugins
const transformPlugins = compilation.config.plugins.filter(plugin => plugin.type === 'transform') || [];
const transformPlugins = request.compilation.config.plugins.filter(plugin => plugin.type === 'transform') || [];

// combine arrays and remove duplicates
return defaultTransforms.concat(transformPlugins.filter(({ extension }) =>
Expand Down Expand Up @@ -72,12 +72,13 @@ function getServer(compilation, productionEnv = false) {

let request = {
header: ctx.request.header,
url: ctx.request.url
url: ctx.request.url,
compilation
};

try {

const allTransforms = getTransforms(request, compilation);
const allTransforms = getTransforms(request);

if (productionEnv && ctx.request.url.endsWith('/')) {
ctx.redirect(`http://localhost:8080${ctx.request.url}index.html`);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/transforms/transform.assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const TransformInterface = require('./transform.interface');

class AssetTransform extends TransformInterface {

constructor(req, compilation) {
super(req, compilation, ['.woff2', '.woff', '.ttf', '.jpg', '.png', '.gif', '.svg'], '');
constructor(req) {
super(req, ['.woff2', '.woff', '.ttf', '.jpg', '.png', '.gif', '.svg'], '');
}

async serveFileByType(workspace, url) {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/transforms/transform.css.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const TransformInterface = require('./transform.interface');

class CSSTransform extends TransformInterface {

constructor(req, compilation) {
super(req, compilation, ['.css'], 'text/css');
constructor(req) {
super(req, ['.css'], 'text/css');
}

async applyTransform() {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/transforms/transform.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { getAppTemplate, getAppTemplateScripts, getUserScripts, getMetaContent }

class HTMLTransform extends TransformInterface {

constructor(req, compilation) {
super(req, compilation, ['.html'], 'text/html');
constructor(req) {
super(req, ['.html'], 'text/html');
}

shouldTransform() {
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/transforms/transform.interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ const { promises: fsp } = require('fs');
// tranform.interface.js
module.exports = class TransformInterface {

constructor(request, { context, config }, extensions, contentType) {
constructor(request, extensions, contentType) {
const { config, context } = request.compilation;
this.extensions = extensions; // ['.foo', '.bar']
this.contentType = contentType || ''; // text/html, text/javascript etc
this.workspace = context.userWorkspace; // greenwood
this.outputDir = context.outputDir; // public dir
this.scratchDir = context.scratchDir;
this.request = request;
this.request = { url: request.url, header: request.header };
this.config = config;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/transforms/transform.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const TransformInterface = require('./transform.interface');

class JSTransform extends TransformInterface {

constructor(req, compilation) {
super(req, compilation, ['.js'], 'text/javascript');
constructor(req) {
super(req, ['.js'], 'text/javascript');
}

shouldTransform() {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/transforms/transform.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const TransformInterface = require('./transform.interface');

class TransformJSON extends TransformInterface {

constructor(req, compilation) {
super(req, compilation, ['.json'], 'application/json');
constructor(req) {
super(req, ['.json'], 'application/json');
}

async applyTransform() {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/transforms/transform.md.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const { getAppTemplateScripts, getUserScripts, getMetaContent } = require('./tra

class MDTransform extends TransformInterface {

constructor(req, compilation) {
super(req, compilation, ['.md']);
constructor(req) {
super(req, ['.md']);
}

shouldTransform() {
Expand Down

0 comments on commit a83d8fc

Please sign in to comment.