webpack-contrib/expose-loader

Uncaught ReferenceError: global is not defined

Closed this issue · 3 comments

I'm using the expose loader like this:

var React = require('expose?React!react');

With the following webpack.config:

module.exports = {
    entry: 'app.js',
    output: {
        path: path.join(__dirname, 'dist/www'),
        filename: '[chunkhash].entry.js',
        chunkFilename: '[chunkhash].[name].js'
    },
    module: {
        noParse: /bower_components/,
        loaders: [
            // { test: require.resolve('react'), loader: 'expose?React' },
            { test: /\.js$/, loader: 'jsx-loader?harmony' },
            { test: /\.css$/, loader: 'style-loader!css-loader' },
            { test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192' }
        ]
    },
    resolve: {
        root: [
            path.join(__dirname, 'www/bower_components'),
            path.join(__dirname, 'www/app'),
            path.join(__dirname, 'www/assets')
        ]
    },
    plugins: [
        new webpack.ResolverPlugin(
            new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin('bower.json', ['main'])
        ),
        new StatsPlugin(path.join(__dirname, 'dist/server', 'stats.json'), {
            chunkModules: true
        })
    ]
};

The module compiles down to:

module.exports = global["React"] = require("-!/Users/Rich/Projects/react-webpack-boilerplate/node_modules/jsx-loader/index.js?harmony!/Users/Rich/Projects/react-webpack-boilerplate/www/bower_components/react/react.js");

so even if I change global to window it'll error out on the require call.

What I imagine it should compile down to is:

module.exports = window["React"] = __webpack_require__(10); // or whatever the react id is.

I'm struggling to work out how I'd achieve this or what I'm doing wrong, any ideas?

Might be worth noting I also get a similar problem when using the import loader:

var Router = require('imports?React=react!react-router'); // Uncaught ReferenceError: require is not defined

because it compiles down to:

/*** IMPORTS FROM imports-loader ***/
var React = require("react");

I've found the problem, it's because of noParse: /bower_components/ which prevented the loader output from being being transpiled...

@richardscarrott
Thanks you very much for posting you solution, it worked for me.

@sokra
@d3viant0ne
Could please add this to the readme as a caveat, saying that this loader will of course not work if the module is excluded by noParse option (which is not ideal btw.)