Git Product home page Git Product logo

webpack-book's Introduction

build status Join the chat at https://gitter.im/survivejs/webpack

SurviveJS - Webpack 5

IMPORTANT! If you want to support the development of this book, you can purchase a copy at Leanpub. A part (~30%) of the proceedings will go directly to the author of webpack to support its development.

Webpack, a module bundler, solves a significant problem for web developers. It can be daunting to learn but once you grok it, life gets easier. The purpose of this book is to make it easier to pick up the tool while learning more advanced techniques as well. You can read the book online.

How to Use This Repository?

You can read the content easily through the book site. It is also available within the manuscript directory of the repository.

Please note that the repository defaults to the dev branch of the book. master contains the source of the most recent version. Each version has been tagged so that it's easy for you to find the source matching to the version of the book you are reading.

Getting Support

As no book is perfect, you will likely come by issues and might have some questions related to the content. There are a couple of options to deal with this:

If you post questions to Stack Overflow, tag them using survivejs so I will get notified of them. You can also use the hashtag #survivejs at Twitter.

Announcements

I announce SurviveJS related news through a couple of channels:

Feel free to subscribe.

Contributing

Feedback and PRs are welcome! See CONTRIBUTING.md for more information.

License

Creative Commons License

webpack-book's People

Contributors

adi-zz avatar alex-sokolov avatar azizpunjani avatar bahtou avatar bebraw avatar bengale avatar billfienberg avatar bjrmatos avatar cdoublev avatar cheerazar avatar chrisblossom avatar christianalfoni avatar coryhouse avatar dcarral avatar dependabot[bot] avatar fakefish avatar felipe-gdr avatar gforcada avatar henrikra avatar lex111 avatar mac10688 avatar nklatt avatar purezen avatar quasiyoke avatar replaceafill avatar sapegin avatar stefanfrede avatar thebriancui avatar undistraction avatar youbicode 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  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  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

webpack-book's Issues

Generating index.html for SPAs

This is something that I struggled with when I first picked up webpack as well, since I wanted my production and development scripts to be as similar as possible.

Currently I use a custom plugin that will receive the stats object, extract the JS and CSS list, and pass it to an Html.jsx file.

I recently found html-webpack-plugin, but haven't tried it.

Explain the meaning of app.entry 'webpack/hot/dev-server'

Link http://webpack.github.io/docs/webpack-dev-server.html contains this code

module.exports = {
  entry: {
    app: ['webpack/hot/dev-server', './app/main.js']
  },
  ...
};

With this message

And make sure you have the special webpack/hot/dev-server entry point in your configuration

This is undocumented. You also use the same chunk in your own examples.
But the funniest thing that official react-starter from webpack does not contain this. And they have both live and hot reload enabled...

Here is their entry (single place in code):

var entry = {
  main: options.prerender ? "./config/mainPrerenderer" : "./config/mainApp"
  // second: options.prerender ? "./config/secondPrerenderer" : "./config/secondApp"
};

Did you manage to understand what's about this webpack/hot/dev-server line?
What exactly is does? Why everyone uses it except Webpack itself ๐Ÿ˜ƒ ?

Transfer below survivejs organization

Just opening this so we can track the status of migration:

  • Figure out how to deal with redirects. Each page should point to a new one on survivejs.com site so we don't break the internet. Basically each old page needs something like <meta http-equiv="refresh" content="0; url=http://example.com/" />.
  • Make sure survivejs site can pick up the material. We'll probably end up with /webpack_cookbook and pages below that.
  • Assuming the first two have been completed, switch over. After that I'll go through the issues and try to resolve as many as I can. The content needs some other tweaks as well.

@christianalfoni Sounds good?

How to structure the cookbook?

I did some initial stab at the wiki. I guess it can grow from that. Basic structure:

  • General
  • React.js
  • <tool/environment specific>

Recipes should go beyond each. One alternative would be to think up a format that allows recipes to be tagged (can belong to multiple). Then you could implement a filtering UI based on that. But that's not needed yet and can be thought about later on.

Update to BabelJS 6

BabelJS just launched new major version 6. It makes breaking changes, especially how to compile React and ES6. I was getting this error:

ERROR in ./app/main.jsx
Module build failed: SyntaxError: /home/edward/learn/react-webpack-cookbook/app/main.jsx: Unexpected token (7:17)
  5 | 
  6 | function main() {
> 7 |     React.render(<Hello />, document.getElementById('app'));
    |                  ^
  8 | }
  9 | 

I wrote a post in here to solve that: https://edwardsamuel.wordpress.com/2015/11/01/react-syntax-error-unexpected-token/.

Multi entry points with react hot loader

Hi,

I found your post about multi entry points really useful (https://github.com/christianalfoni/react-webpack-cookbook/wiki/Multiple-entry-points).

But in your post about hot loading components (https://github.com/christianalfoni/react-webpack-cookbook/wiki/Hot-loading-components), no more mentions of multi entry points. Would you have an example laying around about this one ? :) Can't find a way to make it work. It seems that my HMR is never triggered.

Thanks !

Using inline styles instead of stylesheets

I was reading the cookbook, and just noticed this section.

Unless Webpack is doing something really sneaky here (which I don't think is the case, but do correct me if I'm wrong), these styles will end up being rendered into the page as inline styles on DOM elements.

This is generally poor for application security. Aspects of inline styles can be used in various attacks, including XSS attacks, and it's generally preferable to disable them with a Content Security Policy if possible.

I advise adding a warning to this section to say that it could cause security issues, and that it is not advised to use it in production.

Using .min react alias in dev prevents warning from propTypes validation

Hi,

If I setup the recommended alias for react in dev mode all works fine except any warning you may receive prom react propTypes do no occur.

For example I had this for an ImageItem component:

ImageItem.propTypes = {
src: React.PropTypes.string.isRequired
};

and purposely used this: (no props)

With the alias in place it all works fine but you get no warnings.

When I remove the alias I get the expected warning:
Warning: Failed propType: Required prop src was not specified in ImageItem. Check the render method of Root.

I thought I'd just mention this because if using the minified version suppresses warnings it kinda defeats the purpose since you want to see these things in dev mode.

Cheers

Show how to process within LESS/SASS and such

Apparently something like @import "~theo?less!./file.json" should work within LESS files. The idea here is that ./file/json gets processed by a tool known as theo. After processing it is something less loader can take care of.

Loading SVG page

Hey @christianalfoni great work with this cookbook, it has been very useful. One thing we could add underneath "Loading LESS or SASS" is the "Loading SVG" page.

I know we have a few ways to achieve this using with svg-loader but the simple way to do that is through file-loader.

module: {
  loaders: [
  ...
  { 
    test: /\.svg$/, 
    loader: "file-loader" 
  },
  ...
  ]
}

Then in your CSS:

.MyIcon {
  ...
  background-image: url(../assets/my-icon.svg);
}

I know I lost a few minutes yesterday looking, it might be useful for someone else as well.

Thoughts?

Cheers

Hot Module Replacement is disabled

I got this error when use weppack-dev-server for automatically refresh.
Uncaught Error: [HMR] Hot Module Replacement is disabled.
dev in package.json is
"dev": "webpack-dev-server --devtool eval --progress --colors --hot --content-base build"
entry in webpack.config.js is
entry: ['webpack/hot/dev-server', path.resolve(__dirname, 'main.js')],
How to deal with that?

loader for jsx

Hi there I have been following this cookbook, I know babel-loader is used for parsing jsx but in section 3.1 Loading CSS, on the the webpack.config.js loader for jsx is changed to 'jsx' from 'babel', and I get error when running npm run dev. But if i put 'babel', it work fine. Do I need to install jsx-loader but I guess we dont need that as we already have bable-loader doing same thing. Am i missing something or should be it 'babel' instead of 'jsx'. Thanks.

Show file to edit

On Automatic-browser-refresh.html, it mentions

We added a script that refreshes the application when a change occurs. You will also need to add an entry point to your configuration:

without specifying the file to be edited.

Dealing with global imports/exports

Just to quote @jeffijoe at Gitter (sample conf):

Loaders:

    {
      test: /moxie\-plupload/,
      loader: 'imports?mOxie=moxie!exports?window.plupload'
    }, {
      test: /moxie/i,
      loader: 'exports?this.mOxie'
    }

Alias:

      moxie: 'plupload/js/moxie.js',
      'moxie-plupload': 'plupload/js/plupload.dev.js',

ProvidePlugin:

mOxie: 'moxie'

Optimizing-development.md not working with new react-router package

Hi,

I have been struggling for a bit, trying to optimize my dev environment.
In my package.json I import the following libraries as dependencies:

    "react": "0.13.3",
    "react-router": "0.13.3"

and taking clues from your Optimizing-development.md document I end up with this in my deps array (I also tried just to import React only but also get an error with React Router):

  deps= [
    'react/dist/react-with-addons.min.js',
    'react-router/umd/ReactRouter.min.js'
  ];

I also tried with and without the following line in my loaders list:

    loaders: [
      {test: path.resolve(modulesPath, 'react/dist/react-with-addons.min.js'), loader: 'expose?React'}
]

The problem is that is that I just get errors of this kind (all works well if I don't use aliases):

Uncaught ReferenceError: require is not defined

I understand somewhat that the UMD React Router package does a require('react') but Webpack require is not exposed and I have no clue how to even do that at this stage.
Could you point me to the right direction ? My current alternative which not to use any aliases and noParse adds several seconds to rebundling.
Is there an alternative option (and please no Bower) ?

Thanks.

4.3. Split app and vendors - adding vendors

In the sample you have

vendors: ['react']

but no where in that sample page do you define react. On your site you define it as

config.addVendor('react', bower_dir + '/react/react.min.js');

so I expect it to be the same, if so also need to add the addVendor method.

Is bundling style via webpack a good practice?

Hi,
I read this page: https://christianalfoni.github.io/react-webpack-cookbook/Loading-CSS.html
I made some tests, a virtual css style-sheet was appended to <head> via javascript.
I tested style-loader, css-loader and less-loader chaining style!css!less and it worked as expected.

Wow...
But I have a lot of questions flying in my head... I really dunno if I want to do this...
If not out of scope, can you add some thoughts about embedding css this way?

Thanks
V

Split app and vendors : ensure loading order

Hi,

thanks for the cookbook !
I have an issue with the receipt spliting app and vendor : my vendor.js is way larger than my app.js and so is loaded after the application code, triggering of course a "webpackJsonp is not defined" exception. Is there a Webpack way to deal with this ?

Thanks !

Changed menu structure

Hi there,

You said that change of menu structure would probably break gitbook. I suggested a change now as it felt a bit weird to have those headers as links. There was not really anything to put in there :-)

It would be great, if you agree, to spread some fairy dust on gitbook and make this structure work.

Adding content whenever I get time. Things are starting to come a bit more together now. Looking forward to spreading it like crazy, I think this will be extremely valuable to developers :-)

Lazy load + multiple end points

I have yet to see an example that combines...

  • Multiple end points (main, Home, Admin)
  • Common.js pulled out of the multiple end points (Shared)
  • Lazy load the endpoints that you created (Home, Admin)

SEE: 7, 8, 9 https://github.com/petehunt/webpack-howto

Looking into some of your issues/questions/articles and they all point to the same things.

Home/Admin has duplicate code and there is only ever one entry point.

Do you know if this is possible?

Thanks!

Line 1: Unexpected reserved word

Hi,
the following javascript code

import React from 'react';

export default React.createClass({
  render: function () {
    return React.createElement('h1', null, 'Hello world');
  }
});
;

found in https://github.com/christianalfoni/react-webpack-cookbook/wiki/Configuring-react-js

gives me the following error when launching webpack

ERROR in ./app/Component.js
Module parse failed: C:\inetpub\wwwroot\webpack\app\Component.js Line 1: Unexpected reserved word
You may need an appropriate loader to handle this file type.
| import React from 'react';
|
| export default React.createClass({
 @ ./app/main.js 3:16-41

Can you help me?
I'm a noob ;)

Issues Getting Started

I'm running Ubuntu 14.04

Here is a screenshot

image

I made the file structure as the book described and copy and pasted the code. I tried many variations to try to get it to find the main.js file but it won't. I have made this work before by putting the entry file in the root folder.

inlining fonts: nothing supports .svg fonts

https://christianalfoni.github.io/react-webpack-cookbook/Inlining-fonts.html says "Depending on your project you might be able to get away with one font format. If you exclude Opera Mini, all browsers support the .woff and .svg format" but the SVG format is a dead format, and virtually everyone stopped supporting it quite a while ago. Only safari still supports it, but we found really obvious flaws in the SVG format fairly long ago, and it was shelved as "useless compared to real fonts". http://caniuse.com/#search=svg%20font is a good resource to check, here.

Everything does support WOFF though, so at this point (early 2016), with Microsoft having literally abandoned all IE versions that don't support WOFF, you just need one format. (and WOFF2 if you want to be ahead of the curve)

Error while parsing es6 import syntax

Hi

In my main.s js I am using the import syntax

import './Component.jsx';

When I run 'npm run dev' I get the following error:

ERROR in ./app/main.js
Module parse failed: /Users/aoshi_1/Desktop/test_webpack/app/main.js Line 2: Unexpected reserved word
You may need an appropriate loader to handle this file type.
| 'use strict';
| import './Component.jsx';
|
|

This is my config setup

var path = require('path');
var config = {
entry: path.resolve(__dirname, 'app/main.js'),
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js'
},
module: {
loaders: [{
test: /.jsx$/, // A regexp to test the require path
loader: 'jsx' // The module to load. "jsx" is short for "jsx-loader"
}]
}
};

module.exports = config;

thanks

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.