Coder Social home page Coder Social logo

require-fool-webpack's Introduction

require-fool-webpack

Use dynamic require() without webpack finding out

Because webpack is super annoying. ๐Ÿ’ฉ

You need to supply it an absolute path.

Install

$ npm install require-fool-webpack

Usage

Before

const foo = require(getPath());

Warning from webpack:

WARNING in ./app/node_modules/conf/index.js
1:33-43 Critical dependency: the request of a dependency is an expression
 @ ./app/node_modules/conf/index.js
 @ ./app/node_modules/electron-store/index.js
 @ ./app/config.js
 @ ./app/renderer/index.js

After

const requireFoolWebpack = require('require-fool-webpack');
const foo = requireFoolWebpack(getPath());

No warning! \o/

License

MIT ยฉ Sindre Sorhus

require-fool-webpack's People

Contributors

richienb avatar sindresorhus avatar thekashey 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

Watchers

 avatar  avatar  avatar

require-fool-webpack's Issues

Require is not defined

There is no require in real - webpack replaces it with it's own variant.
Shouldn't this work like?

module.exports = typeof __webpack_require__ !== 'undefined' ? __webpack_require__ : require

TypeError: modules[moduleId] is undefined

I'm trying to use require-fool-webpack in my frontend React application, created via create-react-app, to dynamically load a configuration file.

I'm using it because webpack normally gives me the warning, "Critical dependency: the request of a dependency is an expression", but I have no other choice than to employ a conditional dependency request for my application, and I cannot suppress the warning without "ejecting" the default webpack configuration for create-react-app.

The failing code in question looks like this:

// config-chooser.js
const requireFoolWebpack = require("require-fool-webpack");

let frontendConfig = null;
if (process.env.NODE_CONFIG) {
  frontendConfig = requireFoolWebpack(process.env.NODE_CONFIG);
} else {
  frontendConfig = requireFoolWebpack("./config.json");
}

export default frontendConfig;

And then, in another file, I import it like this:

import frontendConfig from "../../../config-chooser.js";

Here is a fuller version of the error stack:

TypeError: modules[moduleId] is undefined

__webpack_require__
/Users/msidor/Documents/fmeco/ceed/frontend/webpack/bootstrap e0ede5281dc422fd85ff:678

  675 | };
  676 | 
  677 | // Execute the module function
> 678 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
  679 | 
  680 | // Flag the module as loaded
  681 | module.l = true;

fn
/Users/msidor/Documents/fmeco/ceed/frontend/webpack/bootstrap e0ede5281dc422fd85ff:88

  85 | 		console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
  86 | 		hotCurrentParents = [];
  87 | 	}
> 88 | 	return __webpack_require__(request);
  89 | };
  90 | var ObjectFactory = function ObjectFactory(name) {
  91 | 	return {

./src/config-chooser.js
src/config-chooser.js:12

   9 | if (process.env.NODE_CONFIG) {
  10 |   frontendConfig = requireFoolWebpack(process.env.NODE_CONFIG);
  11 | } else {
> 12 |   frontendConfig = requireFoolWebpack("./config.json");
  13 | }
  14 | 
  15 | export default frontendConfig;

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.