Skip to content

Commit

Permalink
feat: initial checkin gatsby bundler
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 23, 2020
1 parent c6e2e99 commit d7ea3a0
Show file tree
Hide file tree
Showing 62 changed files with 4,235 additions and 453 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
"root": true,
extends: [
'plugin:react/recommended',
'plugin:react-hooks/recommended',
'plugin:prettier/recommended',
'plugin:@typescript-eslint/recommended',
'plugin:mdx/recommended',
Expand Down Expand Up @@ -29,6 +31,8 @@ module.exports = {
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-object-literal-type-assertion': 'off',
'@typescript-eslint/no-parameter-properties': 'off',
"no-restricted-globals":'error',
'react-hooks/rules-of-hooks': 'off',
},
parserOptions: {
ecmaVersion: 2018,
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ dist
.cache
.rpt2_cache
*.log
docs
docs
public
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
},
{
"name": "Gatsby build",
"type": "node",
"request": "launch",
"protocol": "inspector",
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby",
"args": ["develop"],
"cwd": "${workspaceFolder}/examples/gatsby",
"stopOnEntry": false,
"runtimeArgs": ["--nolazy"],
"sourceMaps": false
}
]
}
1 change: 1 addition & 0 deletions core/config/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
35 changes: 26 additions & 9 deletions core/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import { sync as globSync } from 'glob';
import yargs from 'yargs';
import { Configuration } from '@component-controls/specification';

export const configFileNames = ['main.js', 'main.json', 'main.ts'];
export const configFileNames = [
'main.js',
'main.json',
'main.ts',
'controls.js',
];

export interface ConfigrationResult {
config: Configuration;
configPath: string;
}
/**
* return the configration object
* return the configration folder from command-line parameters
* command line accepts -c/ -config parameter for config path
* the config file is assumed named main.js/main.ts
* @param baseFolder project folder to start the searh with
* @param baseFolder project folder to start the search with
*/
export const getConfiguration = (
baseFolder: string,
export const getConfigurationArg = (
args: string[] = process.argv,
): ConfigrationResult | undefined => {
): string | undefined => {
const argv = yargs(args)
.option('config', {
alias: 'c',
Expand All @@ -28,10 +32,23 @@ export const getConfiguration = (
})
.help()
.alias('help', 'h').argv;
return argv.config;
};

const configPath = argv.config
? path.resolve(baseFolder, argv.config)
: baseFolder;
/**
* given a base project folder and a configuration folder, returns the configuration file
*
* @param baseFolder project folder to start the search with
* @param configFolder folder where the configuration file is located
* @param args optional arguments
*/
export const loadConfiguration = (
baseFolder: string,
configFolder?: string,
args?: string[],
): ConfigrationResult | undefined => {
const folder = configFolder ?? getConfigurationArg(args);
const configPath = folder ? path.resolve(baseFolder, folder) : baseFolder;
const configFiles = fs.readdirSync(configPath);
const configFile = configFiles.find(file =>
configFileNames.includes(file.toLowerCase()),
Expand Down
16 changes: 13 additions & 3 deletions core/config/test/config-folder.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { getConfiguration } from '../src/index';
import { loadConfiguration } from '../src/index';

describe('config-folder', () => {
it('config file short option', () => {
expect(
getConfiguration(__dirname, ['file', 'name', '-c', 'fixtures']),
loadConfiguration(__dirname, undefined, [
'file',
'name',
'-c',
'fixtures',
]),
).toMatchSnapshot();
});
it('config file long option', () => {
expect(
getConfiguration(__dirname, ['file', 'name', '--config', 'fixtures']),
loadConfiguration(__dirname, undefined, [
'file',
'name',
'--config',
'fixtures',
]),
).toMatchSnapshot();
});
});
16 changes: 13 additions & 3 deletions core/config/test/stories.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { getConfiguration, extractStories } from '../src/index';
import { loadConfiguration, extractStories } from '../src/index';

describe('config-folder', () => {
it('config file short option', () => {
expect(
extractStories(
getConfiguration(__dirname, ['file', 'name', '-c', 'fixtures']),
loadConfiguration(__dirname, undefined, [
'file',
'name',
'-c',
'fixtures',
]),
),
).toMatchSnapshot();
});

it('config file long option', () => {
expect(
extractStories(
getConfiguration(__dirname, ['file', 'name', '--config', 'fixtures']),
loadConfiguration(__dirname, undefined, [
'file',
'name',
'--config',
'fixtures',
]),
),
).toMatchSnapshot();
});
Expand Down
11 changes: 8 additions & 3 deletions core/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export const getControlValue = (
}
break;
}
default:
return value;
}
return value;
}
Expand All @@ -149,7 +151,7 @@ export const getControlValues = (controls?: ComponentControls): ControlValues =>
export const visibleControls = (
controls?: ComponentControls,
): ComponentControls =>
controls && typeof controls == 'object'
controls && typeof controls === 'object'
? Object.keys(controls)
.filter(key => !controls[key].hidden)
.map((key, index) => ({
Expand All @@ -175,7 +177,7 @@ export const visibleControls = (
: {};

export const hasControls = (controls?: ComponentControls): boolean =>
controls && typeof controls == 'object'
controls && typeof controls === 'object'
? !!Object.keys(controls).filter(key => !controls[key].hidden).length
: false;
export const newControlValues = (
Expand Down Expand Up @@ -249,7 +251,10 @@ export const newControlValues = (
break;
}
default:
break;
return {
name,
value: control.defaultValue,
};
}
return {
name,
Expand Down
14 changes: 12 additions & 2 deletions core/loader/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import * as webpack from 'webpack';
import { createHash } from 'crypto';
import { store } from './store';

export interface LoaderPluginOptions {
config?: string;
}
class LoaderPlugin {
public static pluginName = 'component-controls-loader-plugin';
private readonly compilationHash: string;
private readonly options: LoaderPluginOptions;

constructor() {
constructor(options: LoaderPluginOptions) {
const hash = createHash('md5')
.update(new Date().getTime().toString())
.digest('hex');
this.compilationHash = `__${hash.substr(0, 6)}__`;
this.options = {
...options,
};
}

apply(compiler: webpack.Compiler) {
Expand Down Expand Up @@ -40,7 +47,10 @@ class LoaderPlugin {
if (resource.resource) {
resource.loaders.push({
loader: path.join(__dirname, 'runtimeLoader.js'),
options: JSON.stringify({ compilationHash: this.compilationHash }),
options: JSON.stringify({
compilationHash: this.compilationHash,
...this.options,
}),
});
}
},
Expand Down
5 changes: 2 additions & 3 deletions core/loader/src/runtimeLoader.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { getConfiguration, extractStories } from '@component-controls/config';
import { loadConfiguration, extractStories } from '@component-controls/config';
import { stringifyRequest } from 'loader-utils';
import { replaceSource, StoryPath } from './replaceSource';

module.exports = function(content: string) {
//@ts-ignore
const params = JSON.parse(this.query.slice(1));
//@ts-ignore
const config = getConfiguration(this.rootContext);
const config = loadConfiguration(this.rootContext, params.config);
const stories: StoryPath[] = (config ? extractStories(config) || [] : []).map(
fileName => ({
absPath: fileName,
//@ts-ignore
relPath: stringifyRequest(this, fileName),
}),
);

content = replaceSource(
stories,
`__COMPILATION_HASH__${params.compilationHash}`,
Expand Down
2 changes: 1 addition & 1 deletion core/loader/story-store-data.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default null;
module.exports = null;
2 changes: 1 addition & 1 deletion core/specification/src/stories/controls-files.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
export const overview = ({ images }) => (
<div>
{images.map((image, index) => (
<img src={image} key={`image_${index}`} />
<img src={image} key={`image_${index}`} alt={`run time control`} />
))}
</div>
);
Expand Down
4 changes: 3 additions & 1 deletion core/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"live_store.js",
"live_store.d.ts",
"static_store.js",
"static_store.d.ts"
"static_store.d.ts",
"singleton_store.js",
"singleton_store.d.ts"
],
"scripts": {
"build": "yarn cross-env NODE_ENV=production rollup -c",
Expand Down
7 changes: 6 additions & 1 deletion core/store/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { config } from '../../rollup-config';

export default config({
input: ['./src/index.ts', './src/live_store.ts', './src/static_store.ts'],
input: [
'./src/index.ts',
'./src/live_store.ts',
'./src/static_store.ts',
'./src/singleton_store.ts',
],
});
1 change: 1 addition & 0 deletions core/store/singleton_store.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist/singleton_store';
1 change: 1 addition & 0 deletions core/store/singleton_store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/singleton_store');
33 changes: 19 additions & 14 deletions core/store/src/Store/Store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { StoriesStore } from '@component-controls/specification';

import { BroadcastChannel } from 'broadcast-channel';
export { BroadcastChannel };
import {
StoreObserver,
StoryStore,
Expand All @@ -10,6 +8,7 @@ import {
} from '../types';
import { readStore, updateStory } from '../serialization/StoreStorage';

export { BroadcastChannel };
export { StoreObserver, StoryStore };

export interface StoreOptions {
Expand All @@ -28,7 +27,7 @@ export interface StoreOptions {
export class Store implements StoryStore {
private loadedStore: StoriesStore | undefined;
private updateLocalStorage: boolean = true;
private channel: BroadcastChannel;
private channel: BroadcastChannel | undefined;
private observers: StoreObserver[];
private moduleId: number;

Expand All @@ -40,18 +39,24 @@ export class Store implements StoryStore {
this.moduleId = Math.random();
this.loadedStore = store;
this.updateLocalStorage = updateLocalStorage;
this.channel = new BroadcastChannel<MessageType>(UPDATE_STORY_MSG, {
type: 'localstorage',
});
this.observers = [];
this.channel.onmessage = ({ storyId, moduleId, propName }: MessageType) => {
if (storyId && moduleId) {
if (this.moduleId !== moduleId) {
this.readData(storyId, propName);
this.notifyObservers(storyId, propName);
if (updateLocalStorage) {
this.channel = new BroadcastChannel<MessageType>(UPDATE_STORY_MSG, {
type: 'localstorage',
});
this.channel.onmessage = ({
storyId,
moduleId,
propName,
}: MessageType) => {
if (storyId && moduleId) {
if (this.moduleId !== moduleId) {
this.readData(storyId, propName);
this.notifyObservers(storyId, propName);
}
}
}
};
};
}
}
/**
* add observer callback function
Expand Down Expand Up @@ -118,7 +123,7 @@ export class Store implements StoryStore {
this.updateLocalStorage,
);
if (this.loadedStore) {
if (this.updateLocalStorage) {
if (this.channel) {
const message: MessageType = {
storyId,
moduleId: this.moduleId,
Expand Down
2 changes: 1 addition & 1 deletion core/store/src/serialization/StoreStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const encodeFn = (name: string, val: any) => {
};
export const saveStore = (store: StoriesStore) => {
for (var key in localStorage) {
if (key.indexOf(COMPONENT_CONTROLS_STORAGE) == 0) {
if (key.indexOf(COMPONENT_CONTROLS_STORAGE) === 0) {
localStorage.removeItem(key);
}
}
Expand Down
1 change: 1 addition & 0 deletions core/store/src/serialization/load-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-unused-vars */
import {
StoriesStore,
Story,
Expand Down
Loading

0 comments on commit d7ea3a0

Please sign in to comment.