Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the options logic #21

Merged
merged 2 commits into from Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -27,7 +27,7 @@
"travis:coverage": "npm run test:coverage -- --runInBand"
},
"dependencies": {
"google-closure-compiler": "^20171118.0.0-webpack-beta",
"google-closure-compiler": "20171119.0.0-webpack-beta",
"webpack-sources": "^1.0.1"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions src/basic-runtime.js
@@ -0,0 +1,10 @@
/* eslint-env browser */
/* eslint no-underscore-dangle: "off", camelcase: "off", no-var: "off" */
// webpackBootstrap
var __webpack_require__;
if (typeof __webpack_require__ === 'undefined') {
__webpack_require__ = function (m) {}; // eslint-disable-line func-names, no-unused-vars
}

/** @define {string} public path */
__webpack_require__.p = '';
33 changes: 18 additions & 15 deletions src/index.js
Expand Up @@ -15,11 +15,6 @@ function toSafePath(originalPath) {
class ClosureCompilerPlugin {
constructor(options, compilerFlags) {
this.options = options || {};
if (!('mode' in this.options)) {
this.options.mode = 'STANDARD';
} else if (this.options.mode !== 'STANDARD' && this.options.mode !== 'AGGRESSIVE_BUNDLE') {
this.options.mode = 'STANDARD';
}
this.compilerFlags = compilerFlags || {};
}

Expand Down Expand Up @@ -57,6 +52,11 @@ class ClosureCompilerPlugin {
}
});
});
} else if (this.options.mode && this.options.mode !== 'STANDARD') {
this.reportErrors(compilation, [{
level: 'warn',
description: 'invalid plugin mode',
}]);
}

compilation.plugin('optimize-chunk-assets', (originalChunks, cb) => {
Expand Down Expand Up @@ -158,7 +158,7 @@ class ClosureCompilerPlugin {

const allSources = [{
path: '__webpack__base_module__',
src: ClosureCompilerPlugin.renderRuntime(scriptSrcPath),
src: '',
}, {
path: require.resolve('./aggressive-bundle-externs.js'),
src: fs.readFileSync(require.resolve('./aggressive-bundle-externs.js'), 'utf8'),
Expand All @@ -167,7 +167,7 @@ class ClosureCompilerPlugin {
const BASE_MODULE_NAME = 'required-base';
const moduleDefs = [`${BASE_MODULE_NAME}:2`];
let uniqueId = 1;
let runtimeNeeded = false;
let jsonpRuntimeRequired = false;
const entryPoints = new Set();
entryPoints.add(allSources[0].path);
originalChunks.forEach((chunk) => {
Expand All @@ -187,10 +187,10 @@ class ClosureCompilerPlugin {
compilation, chunk, allSources, BASE_MODULE_NAME, moduleDefs, uniqueId);
}

// The runtime must be injected if at least one module
// The jsonp runtime must be injected if at least one module
// is late loaded (doesn't include the runtime)
if (!chunk.hasRuntime()) {
runtimeNeeded = true;
jsonpRuntimeRequired = true;
}
});

Expand Down Expand Up @@ -218,9 +218,7 @@ Use the CommonsChunkPlugin to ensure a module exists in only one bundle.`,
return;
}

if (!runtimeNeeded) {
allSources[0].src = '';
}
allSources[0].src = ClosureCompilerPlugin.renderRuntime(scriptSrcPath, jsonpRuntimeRequired);

const defines = [];
if (this.compilerFlags.define) {
Expand All @@ -235,9 +233,12 @@ Use the CommonsChunkPlugin to ensure a module exists in only one bundle.`,
.filter(entryPoint => allSources.find(source => source.path === entryPoint));

const moduleWrappers = moduleDefs.map((moduleDef) => {
if (/^required-base:/.test(moduleDef)) {
return 'required-base:var __wpcc;if(typeof __wpcc === "undefined")__wpcc={};(function(__wpcc){%s}).call(this, __wpcc);';
}
const defParts = moduleDef.split(':');
const chunkIdParts = /^chunk-(\d+)$/.exec(defParts[0]);
if (runtimeNeeded) {
if (jsonpRuntimeRequired) {
if (chunkIdParts) {
return `${defParts[0]}:webpackJsonp([${chunkIdParts[1]}], function(__wpcc){%s});`;
}
Expand Down Expand Up @@ -458,10 +459,12 @@ Use the CommonsChunkPlugin to ensure a module exists in only one bundle.`,
* runtime used by AGGRESSIVE_BUNDLE mode.
*
* @param {string} scriptSrcPath
* @param {boolean} lateLoadingSupport
* @return {string}
*/
static renderRuntime(scriptSrcPath) {
return `${fs.readFileSync(require.resolve('./runtime.js'), 'utf8')}
static renderRuntime(scriptSrcPath, lateLoadingSupport = false) {
const runtimePath = lateLoadingSupport ? './basic-runtime.js' : './runtime.js';
return `${fs.readFileSync(require.resolve(runtimePath), 'utf8')}
__webpack_require__.src = function(chunkId) {
return __webpack_require__.p + ${scriptSrcPath};
}
Expand Down