Skip to content

Commit

Permalink
Accept Dynamic Entries and RuntimeChunks (#21)
Browse files Browse the repository at this point in the history
* Accept Dynamic Entries

* Duplicate runtimeChunk also to childCompiler

* dynamic-entries tests

* runtimeChunk tests
  • Loading branch information
azizhk authored and prateekbh committed Jun 17, 2019
1 parent b3b2b8c commit 032b16b
Show file tree
Hide file tree
Showing 19 changed files with 1,169 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
const MultiEntryPlugin = require('webpack/lib/MultiEntryPlugin');
const JsonpTemplatePlugin = require('webpack/lib/web/JsonpTemplatePlugin');
const SplitChunksPlugin = require('webpack/lib/optimize/SplitChunksPlugin');
const RuntimeChunkPlugin = require('webpack/lib/optimize/RuntimeChunkPlugin');
const chalk = require('chalk');

const PLUGIN_NAME = 'BabelEsmPlugin';
Expand All @@ -24,7 +25,7 @@ class BabelEsmPlugin {
}

apply(compiler) {
compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
compiler.hooks.make.tapAsync(PLUGIN_NAME, async (compilation, callback) => {
const outputOptions = deepcopy(compiler.options);
this.babelLoaderConfigOptions_ = this.getBabelLoaderOptions(
outputOptions,
Expand Down Expand Up @@ -66,14 +67,18 @@ class BabelEsmPlugin {
}
}

if (typeof compiler.options.entry === 'string') {
compiler.options.entry = {
index: compiler.options.entry,
let entries = compiler.options.entry;
if (typeof entries === 'function') {
entries = await entries();
}
if (typeof entries === 'string') {
entries = {
index: entries,
};
}

Object.keys(compiler.options.entry).forEach(entry => {
const entryFiles = compiler.options.entry[entry];
Object.keys(entries).forEach(entry => {
const entryFiles = entries[entry];
if (Array.isArray(entryFiles)) {
new MultiEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler,
Expand All @@ -94,6 +99,11 @@ class BabelEsmPlugin {
Object.assign({}, compiler.options.optimization.splitChunks),
).apply(childCompiler);
}
if (compiler.options.optimization.runtimeChunk) {
new RuntimeChunkPlugin(
Object.assign({}, compiler.options.optimization.runtimeChunk),
).apply(childCompiler);
}
}

compilation.hooks.additionalAssets.tapAsync(
Expand Down
122 changes: 122 additions & 0 deletions tests/dynamic-entries/dynamic-entries.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
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('dynamic-entries single file string', async t => {
const config = Object.assign({}, defaultConfig, {
entry: () => './tests/dynamic-entries/fixtures/first.js',
output: {
path: `${__dirname}/output`,
filename: 'index.js',
},
});
const compiler = getCompiler(config);
await runWebpack(compiler);
const es5FixtureFileContents = await readFile(
`${__dirname}/fixtures/string.js`,
{
encoding: 'utf-8',
},
);
const es5GeneratedFileContents = await readFile(
`${__dirname}/output/index.js`,
{
encoding: 'utf-8',
},
);
const esmFixtureFileContents = await readFile(
`${__dirname}/fixtures/string.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);
});

test('dynamic-entries single file object', async t => {
const config = Object.assign({}, defaultConfig, {
entry: () => ({
object: './tests/dynamic-entries/fixtures/first.js',
}),
output: {
path: `${__dirname}/output`,
},
});
const compiler = getCompiler(config);
await runWebpack(compiler);
const es5FixtureFileContents = await readFile(
`${__dirname}/fixtures/object.js`,
{
encoding: 'utf-8',
},
);
const es5GeneratedFileContents = await readFile(
`${__dirname}/output/object.js`,
{
encoding: 'utf-8',
},
);
const esmFixtureFileContents = await readFile(
`${__dirname}/fixtures/object.es6.js`,
{
encoding: 'utf-8',
},
);
const esmGeneratedFileContents = await readFile(
`${__dirname}/output/object.es6.js`,
{
encoding: 'utf-8',
},
);
t.is(es5FixtureFileContents, es5GeneratedFileContents);
t.is(esmFixtureFileContents, esmGeneratedFileContents);
});

test('dynamic-entries single file promise', async t => {
const config = Object.assign({}, defaultConfig, {
entry: async () => ({
promise: './tests/dynamic-entries/fixtures/first.js',
}),
output: {
path: `${__dirname}/output`,
},
});
const compiler = getCompiler(config);
await runWebpack(compiler);
const es5FixtureFileContents = await readFile(
`${__dirname}/fixtures/promise.js`,
{
encoding: 'utf-8',
},
);
const es5GeneratedFileContents = await readFile(
`${__dirname}/output/promise.js`,
{
encoding: 'utf-8',
},
);
const esmFixtureFileContents = await readFile(
`${__dirname}/fixtures/promise.es6.js`,
{
encoding: 'utf-8',
},
);
const esmGeneratedFileContents = await readFile(
`${__dirname}/output/promise.es6.js`,
{
encoding: 'utf-8',
},
);
t.is(es5FixtureFileContents, es5GeneratedFileContents);
t.is(esmFixtureFileContents, esmGeneratedFileContents);
});
2 changes: 2 additions & 0 deletions tests/dynamic-entries/fixtures/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let config = {a:10, b:20};
const {a} = config;
100 changes: 100 additions & 0 deletions tests/dynamic-entries/fixtures/object.es6.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) {

let config = {
a: 10,
b: 20
};
const {
a
} = config;

/***/ })
/******/ ]);
98 changes: 98 additions & 0 deletions tests/dynamic-entries/fixtures/object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/******/ (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;

/***/ })
/******/ ]);
Loading

0 comments on commit 032b16b

Please sign in to comment.