romeobleonor / ui_demo

0 stars 1 forks source link

Embed stylesheet into a webpack javacript. (w/o any plugins) #2

Closed romeobleonor closed 8 years ago

romeobleonor commented 8 years ago

Embed stylesheet into main.js (bundle) using css-loader and style-loader.

@igMarketingLeonor

igMarketingLeonor commented 8 years ago

Good idea! try that first and later come up with a better approach like extracting the css file, so your style is no longer inline into the javascript. It tells webpack to load css file into a separate file. Also, we have to resolve assets in our css like fonts and images. Embed small png as data urls, jpeg images as file and use sass instead of css.

In this example, your main js will have a require to the css file right?

// main.js
require('./css/main.css');
document.write("text is added to the DOM and render the style by webpack");
romeobleonor commented 8 years ago

Yes! You are right! I've tweaked your code a little bit and practice my DOM skills. :)

Here's the structure of our files. structure

Here's what I got.

require('../css/main.css');

// native document ready
document.body.onload = addElement;

function addElement() {
  var newElem, newElemContent, mainCont;

  newElem = document.createElement('h2');
  newElemContent = document.createTextNode('Investors Group');
  newElem.appendChild(newElemContent);

  mainCont = document.getElementsByClassName('wrapper')[0];
  mainCont.appendChild(newElem);
}