How to avoid this hardcoded index.js when use webpack?


Tags: javascript,css,node.js,sass,webpack

Problem :

Tasks:

  • CSS: compile .scss, add prefixes, minify and concat into main.css
  • JS: minify and concat into main.js

We are using webpack and BEM.

Project structure:

static/
├── build
│   ├── main.css
│   └── main.js
└── src
    ├── blocks
    │   ├── a
    │   │   ├── a.js
    │   │   └── a.scss
    │   ├── b
    │   │   └── b.scss
    │   ├── c
    │   │   └── b.scss
    │   └── d
    │       ├── d.js
    │       └── d.scss
    └── index.js

index.js is entry point for webpack, this content:

// javascript
require("./a/a.js");
require("./d/d.js");

// scss
require('./a/a.scss');
require('./b/b.scss');
require('./c/c.scss');
require('./d/d.scss');

This approach is very bad, how to avoid this hardcoded index.js and automatically detect new .js and .scss files in blocks dir?

Can we define some globs (as in Gulp.js) for .js or .scss?



Solution :

index.js:

require.context('./blocks', true, /\.js$/);
require.context('./blocks', true, /\.scss$/);

    CSS Howto..

    How to remove spacing between tags?

    How do I push inline elements onto the next line

    How can I get the seletced filename from file button?

    How do I add transition to the effect of hide() and show() on these elements?

    how to do display none using css

    How to achieve vertical centering with optional extra element and adjacent matching column, in pure CSS, no tables?

    SVG, how to evaluate the property of the svg object

    How do I tell a page to ignore internal CSS in favor of external CSS?

    How can I display links in 2 rows?

    How to continue CSS list on new page?

    How to override default CSS file location in a Qml Webview?

    How to filter elements on click in js?

    CSS : How to make two column right fluid left fluid depend on right

    How to style Text input in jQuery editable

    CSS - how to make image container width fixed and height auto stretched

    How to generate image sprites in ember-cli using compass?

    how to center and Crop an image to square with CSS

    How can I click a specific li without an id or class?

    How to position a drop down list nav bar inside header in html css

    How to prevent fixed image from scrolling down?

    why is the space (shown in image) is added below when using vertical-align? How to fix it?

    How do I get an infinite vertically scrolling image in CSS?

    CSS styling of a line: how to get the line to move with the header above it

    How can I align a button at the bottom of a div?

    How to add percentage-of-height top/bottom margin in CSS

    How to display the :after content outside element

    How to change the width of an HTML upload field with CSS?

    Pure CSS3 slideshow repeats the last 4 slides?

    How to make right and bottom css properties work on a textarea in IE? (IE9 and below)

    How to center checkbox vertically in a table-cell?