Skip to content

Instantly share code, notes, and snippets.

@rossta
Last active November 12, 2020 19:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rossta/5a9edcd7ba37416f8c6f7ed383200b0d to your computer and use it in GitHub Desktop.
Save rossta/5a9edcd7ba37416f8c6f7ed383200b0d to your computer and use it in GitHub Desktop.
Webpack with require.context
console.log('initializer hello.js');
const importAll = (r) => r.keys().forEach(r)
const context = require.context('.', true, /\.js$/);
importAll(context);
console.log('initializer index.js');
/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb
import '../initializers';
console.log('Hello World from Webpacker')
// EXCERPT
/* 25 */
/*!*********************************************!*\
!*** ./app/javascript/packs/application.js ***!
\*********************************************/
/*! no exports provided */
/*! all exports used */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__initializers__ = __webpack_require__(/*! ../initializers */ 26);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__initializers___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__initializers__);
/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb
console.log('Hello World from Webpacker');
/***/ }),
/* 26 */
/*!**********************************************!*\
!*** ./app/javascript/initializers/index.js ***!
\**********************************************/
/*! dynamic exports provided */
/*! all exports used */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
var importAll = function importAll(r) {
return r.keys().forEach(r);
};
var context = __webpack_require__(/*! . */ 27);
importAll(context);
console.log('initializer index.js');
/***/ }),
/* 27 */
/*!*******************************************!*\
!*** ./app/javascript/initializers \.js$ ***!
\*******************************************/
/*! dynamic exports provided */
/*! all exports used */
/***/ (function(module, exports, __webpack_require__) {
var map = {
"./hello.js": 28,
"./index.js": 26
};
function webpackContext(req) {
return __webpack_require__(webpackContextResolve(req));
};
function webpackContextResolve(req) {
var id = map[req];
if(!(id + 1)) // check for number or string
throw new Error("Cannot find module '" + req + "'.");
return id;
};
webpackContext.keys = function webpackContextKeys() {
return Object.keys(map);
};
webpackContext.resolve = webpackContextResolve;
module.exports = webpackContext;
webpackContext.id = 27;
/***/ }),
/* 28 */
/*!**********************************************!*\
!*** ./app/javascript/initializers/hello.js ***!
\**********************************************/
/*! dynamic exports provided */
/*! all exports used */
/***/ (function(module, exports) {
console.log('initializer hello.js');
/***/ })
/******/ ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment