Skip to content

Commit

Permalink
adding capability for string based entry file
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekbh committed Jun 7, 2019
1 parent 5d2c99d commit 82e771d
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,26 @@ class BabelEsmPlugin {
}
}

Object.keys(compiler.options.entry).forEach(entry => {
const entryFiles = compiler.options.entry[entry];
if (Array.isArray(entryFiles)) {
new MultiEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler,
);
} else {
new SingleEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler,
);
}
});
if (typeof compiler.options.entry === 'string') {
new SingleEntryPlugin(
compiler.context,
compiler.options.entry,
'index',
).apply(childCompiler);
} else {
Object.keys(compiler.options.entry).forEach(entry => {
const entryFiles = compiler.options.entry[entry];
if (Array.isArray(entryFiles)) {
new MultiEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler,
);
} else {
new SingleEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler,
);
}
});
}

// Convert entry chunk to entry file
new JsonpTemplatePlugin().apply(childCompiler);
Expand Down
4 changes: 4 additions & 0 deletions tests/string-entry-file/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let config = {a:10, b:20};
const {a} = config;
const init = [1,2,3,4]
const final = [...init, 5];
102 changes: 102 additions & 0 deletions tests/string-entry-file/fixtures/output.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

let config = {
a: 10,
b: 20
};
const {
a
} = config;
const init = [1, 2, 3, 4];
const final = [...init, 5];

/***/ })
/******/ ]);
100 changes: 100 additions & 0 deletions tests/string-entry-file/fixtures/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

var config = {
a: 10,
b: 20
};
var a = config.a;
var init = [1, 2, 3, 4];
var final = [].concat(init, [5]);

/***/ })
/******/ ]);
47 changes: 47 additions & 0 deletions tests/string-entry-file/string-loader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import test from 'ava';
import { getCompiler, defaultConfig, runWebpack } from '../webpack-utils';
import * as fs from 'fs';
import { promisify } from 'util';

const readFile = promisify(fs.readFile);

test('string-entry-file is being tested', t => {
t.pass();
});
test('esm files are being generated', async t => {
const config = Object.assign({}, defaultConfig, {
entry: './tests/string-entry-file/fixtures/index.js',
output: {
path: `${__dirname}/output`,
filename: 'index.js',
},
});
const compiler = getCompiler(config);
await runWebpack(compiler);
const es5FixtureFileContents = await readFile(
`${__dirname}/fixtures/output.js`,
{
encoding: 'utf-8',
},
);
const es5GeneratedFileContents = await readFile(
`${__dirname}/output/index.js`,
{
encoding: 'utf-8',
},
);
const esmFixtureFileContents = await readFile(
`${__dirname}/fixtures/output.es6.js`,
{
encoding: 'utf-8',
},
);
const esmGeneratedFileContents = await readFile(
`${__dirname}/output/index.es6.js`,
{
encoding: 'utf-8',
},
);
t.is(es5FixtureFileContents, es5GeneratedFileContents);
t.is(esmFixtureFileContents, esmGeneratedFileContents);
});

0 comments on commit 82e771d

Please sign in to comment.