Skip to content

Commit

Permalink
build command WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Aug 31, 2021
1 parent 591000a commit 2173c11
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 36 deletions.
16 changes: 8 additions & 8 deletions packages/cli/src/commands/build.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// const bundleCompilation = require('../lifecycles/bundle');
// const copyAssets = require('../lifecycles/copy');
import { bundleCompilation } from '../lifecycles/bundle.js';
import { copyAssets } from '../lifecycles/copy.js';
import { devServer } from '../lifecycles/serve.js';
import fs from 'fs';
import { generateCompilation } from '../lifecycles/compile.js';
// const { preRenderCompilation, staticRenderCompilation } = require('../lifecycles/prerender');
// const { ServerInterface } = require('../lib/server-interface');
import { preRenderCompilation, staticRenderCompilation } from '../lifecycles/prerender.js';
import { ServerInterface } from '../lib/server-interface.js';

const runProductionBuild = async () => {

Expand Down Expand Up @@ -44,7 +44,7 @@ const runProductionBuild = async () => {
return Promise.resolve(server);
}));

// await preRenderCompilation(compilation);
await preRenderCompilation(compilation);

resolve();
});
Expand All @@ -53,11 +53,11 @@ const runProductionBuild = async () => {
}
});
} else {
// wait staticRenderCompilation(compilation);
await staticRenderCompilation(compilation);
}

// await bundleCompilation(compilation);
// await copyAssets(compilation);
await bundleCompilation(compilation);
await copyAssets(compilation);

resolve();
} catch (err) {
Expand Down
17 changes: 10 additions & 7 deletions packages/cli/src/config/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable max-depth, no-loop-func */
const fs = require('fs');
const htmlparser = require('node-html-parser');
const path = require('path');
const postcss = require('postcss');
const postcssImport = require('postcss-import');
import fs from 'fs';
import htmlparser from 'node-html-parser';
import path from 'path';
import postcss from 'postcss';
import postcssImport from 'postcss-import';

const tokenSuffix = 'scratch';
const tokenNodeModules = 'node_modules';

Expand Down Expand Up @@ -506,7 +507,7 @@ function greenwoodHtmlPlugin(compilation) {
};
}

module.exports = getRollupConfig = async (compilation) => {
const getRollupConfig = async (compilation) => {
const { scratchDir, outputDir } = compilation.context;
const inputs = compilation.graph.map((page) => {
return path.normalize(`${scratchDir}${page.route}index.html`);
Expand Down Expand Up @@ -570,4 +571,6 @@ module.exports = getRollupConfig = async (compilation) => {
]
}];

};
};

export { getRollupConfig };
6 changes: 3 additions & 3 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { URL } from 'url';

import { runDevServer } from './commands/develop.js';
import { runProductionBuild } from './commands/build.js';
// const runProdServer = require('./commands/serve');
import { runProdServer } from './commands/serve.js';
// const ejectConfiguration = require('./commands/eject');

// TODO track / watch for improt json support - https://stackoverflow.com/a/62621693/417806
Expand Down Expand Up @@ -82,8 +82,8 @@ const run = async() => {
case 'serve':
process.env.__GWD_COMMAND__ = 'build';

// await runProductionBuild();
// await runProdServer();
await runProductionBuild();
await runProdServer();

break;
case 'eject':
Expand Down
12 changes: 7 additions & 5 deletions packages/cli/src/lifecycles/bundle.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const getRollupConfig = require('../config/rollup.config');
const rollup = require('rollup');
import { getRollupConfig } from '../config/rollup.config.js';
import { rollup } from 'rollup';

module.exports = bundleCompilation = async (compilation) => {
const bundleCompilation = async (compilation) => {

return new Promise(async (resolve, reject) => {
try {
// https://rollupjs.org/guide/en/#differences-to-the-javascript-api

if (compilation.graph.length > 0) {
const rollupConfigs = await getRollupConfig(compilation);
const bundle = await rollup.rollup(rollupConfigs[0]);
const bundle = await rollup(rollupConfigs[0]);

await bundle.write(rollupConfigs[0].output);
}
Expand All @@ -19,4 +19,6 @@ module.exports = bundleCompilation = async (compilation) => {
reject(err);
}
});
};
};

export { bundleCompilation };
15 changes: 8 additions & 7 deletions packages/cli/src/lifecycles/copy.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const fs = require('fs');
const fsPromises = fs.promises;
const path = require('path');
import fs from 'fs';
import path from 'path';

async function rreaddir (dir, allFiles = []) {
const files = (await fsPromises.readdir(dir)).map(f => path.join(dir, f));
const files = (await fs.promises.readdir(dir)).map(f => path.join(dir, f));

allFiles.push(...files);

await Promise.all(files.map(async f => (
await fsPromises.stat(f)).isDirectory() && rreaddir(f, allFiles
await fs.promises.stat(f)).isDirectory() && rreaddir(f, allFiles
)));

return allFiles;
Expand All @@ -33,7 +32,7 @@ async function copyFile(source, target) {
}
}

module.exports = copyAssets = (compilation) => {
const copyAssets = (compilation) => {

return new Promise(async (resolve, reject) => {
try {
Expand Down Expand Up @@ -73,4 +72,6 @@ module.exports = copyAssets = (compilation) => {
reject(err);
}
});
};
};

export { copyAssets };
8 changes: 4 additions & 4 deletions packages/cli/src/lifecycles/prerender.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const BrowserRunner = require('../lib/browser');
const fs = require('fs');
const path = require('path');
import { BrowserRunner } from '../lib/browser.js';
import fs from 'fs';
import path from 'path';

async function optimizePage(compilation, contents, route, outputDir) {
const outputPath = `${outputDir}${route}index.html`;
Expand Down Expand Up @@ -119,7 +119,7 @@ async function staticRenderCompilation(compilation) {
console.info('success, done generating all pages!');
}

module.exports = {
export {
preRenderCompilation,
staticRenderCompilation
};
4 changes: 2 additions & 2 deletions packages/plugin-postcss/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ResourceInterface } from '@greenwood/cli/src/lib/resource-interface.js'
async function getConfig (compilation, extendConfig = false) {
const { projectDirectory } = compilation.context;
const configFile = 'postcss.config.js';
const defaultConfig = (await import(new URL(configFile, import.meta.url).pathname)).default; // JSON.parse(fs.readFileSync(new URL(`${configFile}.js`, import.meta.url), 'utf-8'));
const defaultConfig = (await import(new URL(configFile, import.meta.url).pathname)).default;
const userConfig = fs.existsSync(path.join(projectDirectory, `${configFile}`))
? (await import(path.join(projectDirectory, `${configFile}`))).default
: {};
Expand Down Expand Up @@ -70,7 +70,7 @@ class PostCssResource extends ResourceInterface {
const plugins = config.plugins || [];

plugins.push(
await import('cssnano')
(await import('cssnano')).default
);

const css = plugins.length > 0
Expand Down

0 comments on commit 2173c11

Please sign in to comment.