diff --git a/greenwood.config.js b/greenwood.config.js index 0eab90367..970f3e119 100644 --- a/greenwood.config.js +++ b/greenwood.config.js @@ -1,6 +1,6 @@ const path = require('path'); -// const pluginGoogleAnalytics = require('./packages/plugin-google-analytics/src/index'); -// const pluginPolyfills = require('./packages/plugin-polyfills/src/index'); +const pluginGoogleAnalytics = require('./packages/plugin-google-analytics/src/index'); +const pluginPolyfills = require('./packages/plugin-polyfills/src/index'); const META_DESCRIPTION = 'A modern and performant static site generator supporting Web Component based development'; const FAVICON_HREF = '/assets/favicon.ico'; @@ -20,13 +20,12 @@ module.exports = { { rel: 'icon', href: FAVICON_HREF }, { name: 'google-site-verification', content: '4rYd8k5aFD0jDnN0CCFgUXNe4eakLP4NnA18mNnK5P0' } ], - // TODO - // plugins: [ - // ...pluginGoogleAnalytics({ - // analyticsId: 'UA-147204327-1' - // }), - // ...pluginPolyfills() - // ], + plugins: [ + pluginGoogleAnalytics({ + analyticsId: 'UA-147204327-1' + }), + pluginPolyfills() + ], markdown: { plugins: [ '@mapbox/rehype-prism', diff --git a/packages/cli/src/config/rollup.config.js b/packages/cli/src/config/rollup.config.js index a8ea9b94a..ca0eaab51 100644 --- a/packages/cli/src/config/rollup.config.js +++ b/packages/cli/src/config/rollup.config.js @@ -37,9 +37,28 @@ function greenwoodHtmlPlugin(compilation) { return { name: 'greenwood-html-plugin', - load(id) { - if (path.extname(id) === '.html') { - return ''; + async load(id) { + const extension = path.extname(id); + + if (extension === '.html') { + return Promise.resolve(''); + } + + // handle custom user file extensions + const customResources = compilation.config.plugins.filter((plugin) => { + return plugin.type === 'resource'; + }).map((plugin) => { + return plugin.provider(compilation); + }).filter((resource) => { + if (resource.shouldServe(id)) { + return resource; + } + }); + + if (customResources.length) { + const response = await customResources[0].serve(id); + + return response.body; } }, // TODO do this during load instead? @@ -59,7 +78,7 @@ function greenwoodHtmlPlugin(compilation) { const srcPath = src.replace('../', './'); const source = fs.readFileSync(path.join(userWorkspace, srcPath), 'utf-8'); - + that.emitFile({ type: 'chunk', id: srcPath, diff --git a/packages/cli/src/lib/resource-interface.js b/packages/cli/src/lib/resource-interface.js new file mode 100644 index 000000000..5430f0dca --- /dev/null +++ b/packages/cli/src/lib/resource-interface.js @@ -0,0 +1,62 @@ +const path = require('path'); + +class ResourceInterface { + constructor(compilation, options = {}) { + this.compilation = compilation; + this.options = options; + this.extensions = []; + this.contentType = ''; + } + + // hidden API? + shouldResolve(url) { + const { extensions } = this; + + return extensions.length && extensions.length > 0 + || extensions[0] === '*' + || extensions.indexOf(path.extname(url) >= 0); + } + + async resolve(url) { + return Promise.resolve(url); + } + + // introduce a new resource type to the browser, on the fly, ex: ` + + `); + + resolve(newContents); + } catch (e) { + reject(e); + } + }); + } +} + +module.exports = { + type: 'resource', + name: 'plugin-node-modules', + provider: (compilation, options) => new NodeModulesResource(compilation, options) +}; \ No newline at end of file diff --git a/packages/cli/src/transforms/transform.css.js b/packages/cli/src/plugins/resource/plugin-standard-css.js similarity index 55% rename from packages/cli/src/transforms/transform.css.js rename to packages/cli/src/plugins/resource/plugin-standard-css.js index 8430d65b7..e940d88c1 100644 --- a/packages/cli/src/transforms/transform.css.js +++ b/packages/cli/src/plugins/resource/plugin-standard-css.js @@ -1,26 +1,28 @@ +/* + * + * Manages web standard resource related operations for CSS. + * This is a Greenwood default plugin. + * + */ const fs = require('fs'); const path = require('path'); const postcss = require('postcss'); -const TransformInterface = require('./transform.interface'); +const { ResourceInterface } = require('../../lib/resource-interface'); -class CSSTransform extends TransformInterface { - - constructor(req) { - super(req, ['.css'], 'text/css'); +class StandardCssResource extends ResourceInterface { + constructor(compilation, options) { + super(compilation, options); + this.extensions = ['.css']; + this.contentType = 'text/css'; } - async applyTransform() { - // do stuff with path + async serve(url, header) { return new Promise(async (resolve, reject) => { try { - const { url, header } = this.request; - const destHeader = header['sec-fetch-dest']; - const cssPath = url.indexOf('/node_modules') >= 0 - ? path.join(process.cwd(), url) - : path.join(this.workspace, url); - - let css = await fs.promises.readFile(cssPath, 'utf-8'); - let body = '', contentType = ''; + const destHeader = header['sec-fetch-dest']; + let css = await fs.promises.readFile(url, 'utf-8'); + let body = ''; + let contentType = ''; // TODO try and use context.projectDirectory if (fs.existsSync(path.join(process.cwd(), 'postcss.config.js'))) { @@ -29,7 +31,7 @@ class CSSTransform extends TransformInterface { if (userPostcssPlugins.length > 0) { const result = await postcss(userPostcssPlugins) - .process(css, { from: cssPath }); + .process(css, { from: url }); css = result.css; } @@ -37,19 +39,19 @@ class CSSTransform extends TransformInterface { //