Giter Site home page Giter Site logo

doc-js-webpack-simple's Introduction

Webpack via command line

  • https://leanpub.com/doc-js
  • If you want to contribute to the book or join me as a coauthor pool, please get in contact mgalli at mgalli dot com subject "doc-js book"

Bundlers and HTML app start points

  • npm install webpack --save-dev
  • npm install webpack-cli -save-dev

Create a directory for your unbundled sources, and another for the distribution HTML that will attempt to load a bundle.js (generated output of webpack):

  • ./src/index.js
  • ./public/dist/ (target will will receive the bundle.js)
  • ./public/page.html (HTML file that will load the ./public/dist/bundle.js)

Let's write a JS file with almost nothing in it:

(./src/index.js)

alert(1);

Now setup your config to help web pack to load your js file and generate the bundle.

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'public', 'dist'),
    filename: 'bundle.js'
  }
};

Le't run:

./node_modules/webpack-cli/bin/cli.js --config ./webpack.config.js

Now make a simple page.html:

<!doctype html>
<html>
  <head>
    ...
  </head>
  <body>
    ...
    <script src="dist/bundle.js"></script>
  </body>
</html>

And load it from the file system in your browser. Expect the simple alert box.

Now let's evolve your webpack-based example so your initial JS executes a code from a module:

(./src/index.js)

import moduleTest from './moduleTest'
moduleTest();

And make your 'moduleTest.js':

export default function something() {

  alert(1)

}

Again, execute the webpack to generate your bundle:

./node_modules/webpack-cli/bin/cli.js --config ./webpack.config.js

And expect, again an alert box.

References

doc-js-webpack-simple's People

Contributors

taboca avatar

Watchers

 avatar  avatar  avatar

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.