Giter Club home page Giter Club logo

Comments (25)

sokra avatar sokra commented on April 19, 2024

Maybe it's just writing a loader for component.json and setup component.json as default module file. I have to check it.

from webpack.

jhnns avatar jhnns commented on April 19, 2024

As far as I understood everything a module needs is defined in the component.json. A require() within a module can simply be resolved by examining the component.json. For instance:

component.json

{
  "name": "test",
  "repo": "jhnns/test",
  "dependencies": {
      "component/emitter": "*"
  },
  "development": {},
  "scripts": [
    "index.js",
    "template.js"
  ]
}

index.js

// requires starting with a dot are located relative to the current module (like node)
var template = require("./template.js"),

// requires not starting with a dot are expected to be under ./components
// the / is replaced by a -
// this module for instance would be located at ./components/component-emitter
    emitter = require("component/emitter"),

// this is not valid as the referenced module is not included in the scripts-array
// currently 'component build' prints no warning, but by runtime require() will
// throw an exception
    bla = require("./bla.js");

As I mentioned in #47 component uses a flat dependency hierarchy. This means that every component an application uses is installed directly under ./components/<user>-<project>.

Regarding styles webpack should not automatically include the styles via the style-loader. This decision should be up to the developer imho.

You can find more information in the spec. I'm currently not sure how this will turn out but components brings some good ideas (e.g. simplicity, no opinionated styling, no or just a few dependencies). On the other hand it adds another fragmentation to the community.

from webpack.

sokra avatar sokra commented on April 19, 2024

Thanks for the explaination. The resolving process is more complex than i thought. We may need some plugin system for the resolver...

from webpack.

jhnns avatar jhnns commented on April 19, 2024

Yes. This could work well with your latest changes on webpack. πŸ˜„

from webpack.

sokra avatar sokra commented on April 19, 2024

webpack 0.9 is nearly a complete rewrite ;)

from webpack.

jhnns avatar jhnns commented on April 19, 2024

I think it can be quite easy to make webpack work with component. I'm not sure but maybe webpack just has to add ./components as a lookup path (like node_modules and web_modules). Relative requires require("../b.js") inside the modules should work like expected (as they are the same as in node) and dependency requires require("moduleA") should resolve than too - oh wait ... there is this thing that dependencies are stored differently within the components folder.

Hopefully they'll change this πŸ˜„ componentjs/component#212

from webpack.

vendethiel avatar vendethiel commented on April 19, 2024
emitter = require("component/emitter"),

That may be how they're namespaced (github-style) but that's not how they're requirable. You have to use emitter = require("component-emitter"), from root namespace (where you're usually not).
When you're, say, in index.js of your component (with index.js being referenced from scripts: [] property), it's just require('emitter') because it's aliased.

from webpack.

jhnns avatar jhnns commented on April 19, 2024

Ah ok. Didn't get that πŸ˜„

from webpack.

sokra avatar sokra commented on April 19, 2024

I think he want to point out, that you write require("emitter"), which checks the dependencies and load component/emitter if you depend on it, or sokra/emitter if you depend on it.

from webpack.

vendethiel avatar vendethiel commented on April 19, 2024

I do mean that, but never sokra/emitter. That just won't work. It is sokra-emitter.
ie my components/ folder : http://i.imgur.com/QaGgy5G.png

from webpack.

sokra avatar sokra commented on April 19, 2024

Ok thanks for making that clear.

Another point, @jhnns mentioned: Should be stylesheets applied to the DOM on require? Why not? I didn't find anything about that in the spec.

from webpack.

vendethiel avatar vendethiel commented on April 19, 2024

You just <link /> build.css

from webpack.

sokra avatar sokra commented on April 19, 2024

ok... I think I will make it configurable how the mapping between fields in component.json and loaders should be:

{
  // field in component.json ":" require request with "[file]" replaced
  scripts: "[file]", // normal require
  styles: "style!css![file]", // apply styles
  less: "style!css!val!less![file]", // apply also less styles
  coffee: "coffee![file]", // load also coffeescript
}

default:

{
  scripts: "[file]",
  styles: "style!css![file]"
}

so you can opt-out from applying styles by: {scripts: "[file]"}

from webpack.

jhnns avatar jhnns commented on April 19, 2024

Mhmm I don't think that the component should decide whether to apply styles or not. It's the developers decision who uses the component in an application.

Also relying on the loader-syntax will break other module loaders / bundlers who don't support this syntax.

from webpack.

sokra avatar sokra commented on April 19, 2024

So I refactored the enhanced-resolve module to support plugins.

So it's now possible to write a ComponentsPlugin for webpack, which

  • adds resolving logic for components
  • registers a component-loader for component.json

The component-loader process the component.json and applies fields.


new ComponentPlugin({
  styles: "!style!css![file]",
});
require("emitter");
  • Lookup the nearest component.json
  • Reads the component.json and the the full name of the dependency
  • Resolves to .../components/sokra-emitter/component.json
  • The component-loader is applied. And generates something like this:
// component.json
{
  "main": "main.js",
  "styles": [
    "style.css"
  ]
}
// !component-loader!component.json
require("!style!css!.../components/sokra-emitter/style.css");
module.exports = require(".../components/sokra-emitter/main.js");
  • The remaining process is as normal...

from webpack.

jhnns avatar jhnns commented on April 19, 2024

Ah cool. So requiring components should work with webpack 0.9?

from webpack.

sokra avatar sokra commented on April 19, 2024

I think less than 200 LOC... :)

from webpack.

jhnns avatar jhnns commented on April 19, 2024

Wow. Let's hope that they don't change the specification too often.

from webpack.

jhnns avatar jhnns commented on April 19, 2024

Where can I find the ComponentPlugin?

from webpack.

sokra avatar sokra commented on April 19, 2024

I haven't made it yet....

from webpack.

jhnns avatar jhnns commented on April 19, 2024

Ah sorry, I misunderstood your comment. πŸ˜„

from webpack.

sokra avatar sokra commented on April 19, 2024

Wolla... here it is: component-webpack-plugin

example

from webpack.

sokra avatar sokra commented on April 19, 2024

as promised less then 200 LOC πŸ˜„

from webpack.

vendethiel avatar vendethiel commented on April 19, 2024

Great !

from webpack.

jhnns avatar jhnns commented on April 19, 2024

Awesome!!! πŸ‘

from webpack.

Related Issues (20)

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.