-
Notifications
You must be signed in to change notification settings - Fork 0
RFC: Define preProcessors API #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
629d1e3
Add initial preProcessors API
69f37e9
Add initial template for a preProcessor
2bb4abc
Expand on types for preProcessor template
955d083
Update API
b14c3a2
Change source input/output type
076ffb9
Update preprocessor template
3c4411a
Add initial prototype of workflow
1376fc4
Expand on workflow explanation
a3d6ee0
Add prettier and eslint
1f01c23
Implement YAML loader for prototype
dba39d2
Merge pull request #5 from googleinterns/loader-prototype
55b7686
Update workflow for on-the-fly loading
0d749e6
Update eslint
9c588c3
Update files using prettier
c82365d
Update dummy resource provider
b25241b
Update api/API.md
0995054
Update YAML loader with module wrapper
28d48b6
Update module wrapper solution
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "extends": "./node_modules/gts/" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module.exports = { | ||
| ...require('gts/.prettierrc.json') | ||
| } | ||
|
julian-londono marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export const sourceFormatTypes = ['module']; | ||
|
|
||
| export function getPostProcessor(options = {}) { | ||
| return { | ||
| async process(source) { | ||
| return { | ||
| source: | ||
| source + | ||
| "\nconsole.log('This line was added by a post processor!!');", | ||
| }; | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import yaml from 'yaml'; | ||
|
|
||
| export const sourceExtensionTypes = ['.yaml']; | ||
|
|
||
| export const outputExtensionTypes = ['.json']; | ||
|
|
||
| export function getPreProcessor(options = {}) { | ||
| return { | ||
| async process(source) { | ||
| const yamlSource = yaml.parse(source); | ||
| return { | ||
| source: JSON.stringify(yamlSource), | ||
| }; | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import * as fs from 'fs'; | ||
|
|
||
| export let prefixes = ['file:']; | ||
|
|
||
| export let suffixes = []; | ||
|
|
||
| // Following was adapted from the example in the Node.js docs | ||
| // https://nodejs.org/api/esm.html#esm_code_resolve_code_hook | ||
| export async function resolve(specifier, context, defaultResolve) { | ||
| console.log( | ||
| `### Resolving resource in dummy resourceProvider for ${specifier}\n` | ||
| ); | ||
|
|
||
| return defaultResolve(specifier, context, defaultResolve); | ||
| } | ||
|
|
||
| export function getResourceProvider() { | ||
| return { | ||
| async getResource(url) { | ||
| console.log( | ||
| `### Getting resource in dummy resourceProvider for ${url}\n` | ||
| ); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
|
julian-londono marked this conversation as resolved.
Outdated
|
||
| fs.readFile(new URL(url), 'utf8', (err, data) => { | ||
| resolve(data); | ||
| }); | ||
| }); | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import yamlFile from './yamlExample.yaml'; | ||
|
|
||
| console.log('\nHello from test.mjs'); | ||
|
|
||
| console.log('YAML Contents as JSON'); | ||
| console.log(yamlFile); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Some example YAML data | ||
| - John Doe: | ||
| job: SWE | ||
| skills: | ||
| - python | ||
| - java | ||
| - Jane Doe: | ||
| job: SWE | ||
| skills: | ||
| - java | ||
| - python | ||
| - php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| export default { | ||
| outputPrefix: './dist', | ||
| resourceProviders: [ | ||
| { | ||
| type: '../examples/loaders/resourceprovider-dummy-fs.mjs', | ||
| base: './src', | ||
| }, | ||
| ], | ||
| preProcessors: [ | ||
| { | ||
| name: '../examples/loaders/preprocessor-yaml.mjs', | ||
| options: {}, | ||
| }, | ||
| ], | ||
| postProcessors: [ | ||
| { | ||
| name: '../examples/loaders/postprocessor-consolelog.mjs', | ||
| options: {}, | ||
| }, | ||
| ], | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import config from '../loaderconfig.mjs'; | ||
|
|
||
| // console.log("Config File:"); | ||
| // console.log(config); | ||
|
|
||
| // Load all resourceProviders, preProcessors, and postProcessors as specified in config file | ||
|
|
||
| let resourceProviders = await Promise.all( | ||
| config.resourceProviders.map((resourceProvider, i) => | ||
| import(resourceProvider.type) | ||
| ) | ||
| ); | ||
|
|
||
| let preProcessors = await Promise.all( | ||
| config.preProcessors.map((preProcessor, i) => import(preProcessor.name)) | ||
| ); | ||
|
|
||
| let postProcessors = await Promise.all( | ||
| config.postProcessors.map((postProcessor, i) => import(postProcessor.name)) | ||
| ); | ||
|
|
||
| export let resolve = resourceProviders[0].resolve; | ||
|
|
||
| // Dummy getFormat, effectively eliminating this step | ||
| export async function getFormat(url, context, defaultGetFormat) { | ||
| if (url.endsWith('.yaml') || url.endsWith('.yml')) { | ||
| return { | ||
| format: 'json', | ||
| }; | ||
| } else { | ||
| return { | ||
| format: 'module', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // This getSource hook executes chained resourceProviders, preProcessors, and postProcessors | ||
| export async function getSource(url, context, defaultGetSource) { | ||
| const {format} = context; | ||
|
|
||
| let source; | ||
|
|
||
| // Get source using any resouce preovider that accepts this type of URL ("file:") | ||
| for (const resourceProvider of resourceProviders) { | ||
| if (resourceProvider.prefixes.some(prefix => url.startsWith(prefix))) { | ||
| let resourceProviderInstance = resourceProvider.getResourceProvider(); | ||
| source = await resourceProviderInstance.getResource(url); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // Redefine source for every preProcessor that exists | ||
| for (const preProcessor of preProcessors) { | ||
| if (preProcessor.sourceExtensionTypes.some(ext => url.endsWith(ext))) { | ||
| let preProcessorInstance = preProcessor.getPreProcessor(); | ||
| source = (await preProcessorInstance.process(source)).source; | ||
| } | ||
| } | ||
|
|
||
| // Redefine source for every postProcessor that exists | ||
| for (const postProcessor of postProcessors) { | ||
| if (postProcessor.sourceFormatTypes.includes(format)) { | ||
| let postProcessorInstance = postProcessor.getPostProcessor(); | ||
| source = (await postProcessorInstance.process(source)).source; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| source: source, | ||
| }; | ||
|
|
||
| // Defer to Node.js for all other URLs. | ||
| // return defaultGetSource(url, context, defaultGetSource); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.