Giter VIP home page Giter VIP logo

component-lab's Introduction

Component Lab

A component development and testing tool built for Angular, inspired by Storybook

Getting Started

Installation

npm install component-lab --save-dev

OR

yarn add component-lab --dev

Experiments

Experiments are used to run your components, directives and pipes in many different isolated scenarios. Create an experiment in the directory where your component is located.

button.component.exp.ts

import { experimentOn } from 'component-lab';

export default experimentOn('Component Experiment Name')
  .case('Experiment 1 Name', {
    template: `
      <app-button>
        Foo
      </app-button>
    `
  })
  .case('Experiment 2 Name', {
    template: `
      <app-button>
        Bar
      </app-button>
    `
  });

Experiments can also provide both a template context object and an array of styles. Some cases can be ignored by using xcase instead of case

import { experimentOn } from 'component-lab';


export default experimentOn('My Button')
  .case('Normal Button', {
    template: `
      <app-button></app-button>
    `
  })
  .case('Warning Button', {
    context: {
      buttonLabel: 'Warning!',
      onClick() {
        console.log('Clicked!');
      }
    },
    styles: [`
      :host {
        text-align: center;
      }
    `],
    template: `
      <app-button (click)="onClick()">
        {{ buttonLabel }}
      </app-button>
    `
  })
  .xcase('Not Yet Implemented', {
    template: `
      <app-button raised>Raised Button</app-button>
    `
  });

Usage

Angular CLI
Custom Webpack Config

Angular CLI
  1. Create some experiments for your component, directive or pipe.

  2. Create a lab.ts file in your src folder for your lab.

import { NgModule } from '@angular/core';
import { createLab } from 'component-lab';

import { ButtonComponent } from './app/button/button.component';

declare var require: any;

@NgModule({
  declarations: [
    ButtonComponent
  ],
  exports: [
    ButtonComponent
  ],
  imports: [
  ],
  providers: [],
})
export class LabModule { }

createLab({
  /**
  * NgModule to import. All components and pipes must be exported
  * by this module to be useable in your experiments. Use an existing
  * NgModule from your application or create a custom one.
  */  
  ngModule: LabModule,

  loadExperiments() {
    /**
     * Function that returns an array of experiments.
     *
     * Here is an example using webpack's `require.context` to
     * load all modules ending in `.exp.ts` and returning thier
     * default exports as an array:
     */
    const context = require.context('./', true, /\.exp\.ts/);
    return context.keys().map(context).map(mod => mod.default);
  }
});
  1. Create a lab.html in your src folder to use for the component-lab app.
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Component Lab</title>
  <base href="/">
</head>
<body>
  <component-lab>Loading...</component-lab>
</body>
</html>
  1. Create a tsconfig.lab.json in your src folder. This file should be a copy of your tsconfig.app.json with the angularCompilerOptions added to specify an entryModule with the path to the lab.
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "types": []
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ],
  "angularCompilerOptions": {
    "entryModule": "lab#LabModule"
  }
}
  1. Create another app in your .angular-cli.json file in the apps array to point it to the lab files.
    {
      "name": "component-lab",
      "root": "src",
      "outDir": "dist-lab",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "lab.html",
      "main": "lab.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.lab.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  1. Update your tsconfig.app.json to exclude your lab files.
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "types": []
  },
  "exclude": [
    "lab.ts",
    "test.ts",
    "**/*.spec.ts"
  ]
}
  1. Run the CLI with your lab app.
npm start -- --app component-lab --port 8080

OR

yarn start -- --app component-lab --port 8080
Custom Webpack Config
  1. Create some experiments for your component, directive or pipe.

  2. Create a lab.ts file for your configuration.

import { NgModule } from '@angular/core';
import { createLab } from 'component-lab';

import { ButtonComponent } from './src/app/button/button.component';

declare var require: any;

@NgModule({
  declarations: [
    ButtonComponent
  ],
  exports: [
    ButtonComponent
  ],
  imports: [
  ],
  providers: [],
})
export class LabModule { }

createLab({
  /**
  * NgModule to import. All components and pipes must be exported
  * by this module to be useable in your experiments
  */  
  ngModule: LabModule,

  loadExperiments() {
    /**
     * Function that returns an array of experiments.
     *
     * Here is an example using webpack's `require.context` to
     * load all modules ending in `.exp.ts` and returning thier
     * default exports as an array:
     */
    const context = require.context('./', true, /\.exp\.ts/);
    return context.keys().map(context).map(mod => mod.default);
  }
});
  1. Create a component-lab.config.js file in the root of your project
  /**
    * Export a single configuration object
    */
  module.exports = {
    /**
      * Webpack configuration object used to load your experiments
      */
    webpackConfig: { ... },
    /**
      *  Host and port of the Component Lab webpack development server
      */
    host: 'localhost',
    port: 8080,
    /**
      * Additional list of files to include in the bundle
      */
    include: [],
    /**
      * Dictionary of suites. Each suite should be a lab configuration 
      * module (see "Experiments") 
      */
    suites: {
      feature: './src/lab.ts'
    }
  };
  1. In the scripts section of your package.json add a script to start Component Lab.
{
  "scripts": {
    "component-lab": "component-lab"
  }
}
  1. Start the Component Lab with the suite name.
npm run component-lab -- feature

OR

yarn run component-lab -- feature

component-lab's People

Contributors

brandonroberts avatar dpdotjs avatar intellix avatar isaacplmann avatar kyleryanbanks avatar mikeryandev avatar ndelangen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

component-lab's Issues

Multiple experiments require unique IDs

Created an exp file for 2x components and noticed that I only have a single entry in the navigation. Was debugging why and found that I need to specify a module.id per experiment otherwise the ID names for each experiment defaults to exp:

this.id = `exp${module ? module.id : ''}`;

README.md says to create experiments like:

export default experimentOn('My Button')

But in order to have multiple, they need to include a unique ID:

export default experimentOn('My Button', { id: 'button' })

My guess is that with the webpack configuration included, an ID is always provided, but with Angular CLI there isn't one.

Any idea of how to tackle this?

Update to latest Webpack

Update Webpack and Webpack-Dev-Server to the latest versions. Beta is missing some configuration options such as performance which throws errors on compilation.

Also, it might be good to make Webpack a peer dependency since the user will be supplying their own webpack.config. Not sure though, haven't used peer dependencies before.

Support HMR for Experiments

Experiment cases should already be mostly stateless and the case/experiment APIs already accept the local node module. Supporting HMR would be great!

Execute project locally

First thanks for the great package.

I clone this project and I want to execute it locally.
I didn't found in the packag.json any serve script.

How can I execute it locally .

Thanks

Error starting with custom webpack config

I've configured my setup as described in the redme, except for one line ( webpackConfig: require('./webpack.config'), ) for custom webpack configuration file and recieved an error:

for(const module of chunk.modulesIterable)
TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined

research resolves, it's a problem with yarn and the webpack-dev-server and it's recommended to install it with npm (webpack/webpack-dev-server#947), but stuck at the same error.

I'm using angular cli and extracted the webpack config with ng eject, use yarn instead of npm.

Any ideas?

Sample webpack config

Can you post a sample webpack config just to get this running with an angular cli app? I'd like to get this running, but it's not worth writing a whole webpack config just for the component lab.

Component Lab does not have applicationRef

I have a component which inject applicationRef in the constructor, constructor(private applicationRef: ApplicationRef); after I put it into component Lab, applicationRef is undefined when debugging.

Can someone let me know why it happens.

Thanks

module.id undefined in ExperimentBuilder

If undefined is passed to the ExperimentBuilder constructor, an error occurs when it attempts to build a string with the module id property (index.ts:ln:21).

This is probably some sort of misconfiguration on my part. I've tried adding module: module.id to the component decorator, and that didn't fix it. So I'm assuming its something in with my webpack configuration.

As a workaround I've passed an anonymous object {id: 'test'} as the second parameter to the function.

ComponentLab style overrides own styles

My components look different inside the component lab compared to the actual app. That's because there're some styles inside the component lab that apply to my components.

Here's what it looks like in the component lab. The highlighted style entry on the right is from ComponentLab.
With component lab style

That's what it's supposed to look like (I deleted the component lab style entry in the header)
Without component lab style

Uncaught ReferenceError: webpackJsonp is not defined

For some reason my polyfills are not beeing loaded.

I tried to import them as in the directly into the lab component as suggested here #11 but with no luck.

My config has this:

    entry: {
        polyfills: './src/polyfills.browser.ts',
        vendor: './src/vendor.browser.ts',
        main: './src/main.browser.ts'
    }

But the scripts beeing included looks like this:

<script type="text/javascript" src="vendor.ccf5d7ddcba0e5d3b0f2.bundle.js" defer></script>
<script type="text/javascript" src="main.0e4b0dce2fdb009c3c65.bundle.js" defer></script>
</body>

Problem with visualization components from another module

Hello. After installing and including library i have next problem, where i import component from another module (creating in module ui-components) in console write next message: 'app-search-input' is not a known element:

  1. If 'app-search-input' is an Angular component, then verify that it is part of this module.
  2. If 'app-search-input' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
    [ERROR ->]
    "): ng:///ExperimentModule/ExperimentCaseComponent.html@1:12

How i can use this tool with another module component?

P.S. Included module working with app.module.
P.P.S. app and modules was generated by angular-cli

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.