Skip to content

Commit

Permalink
Merge branch 'master' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Nov 20, 2017
2 parents 5f372e7 + 46a6c34 commit c02ca77
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<a href="https://opencollective.com/webpack#sponsors">
<img src="https://opencollective.com/webpack/sponsors/badge.svg">
</a>
<a href="https://github.com/webpack/webpack/graphs/contributors">
<img src="https://img.shields.io/github/contributors/webpack/webpack.svg">
</a>
<a href="https://gitter.im/webpack/webpack">
<img src="https://badges.gitter.im/webpack/webpack.svg">
</a>
Expand Down
2 changes: 1 addition & 1 deletion lib/DllReferencePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DllReferencePlugin {
manifest = params["dll reference " + manifest];
}
const name = this.options.name || manifest.name;
const sourceType = this.options.sourceType || "var";
const sourceType = this.options.sourceType || (manifest && manifest.type) || "var";
const externals = {};
const source = "dll-reference " + name;
externals[source] = name;
Expand Down
6 changes: 3 additions & 3 deletions lib/dependencies/AMDDefineDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate {
return {
f: [
"var __WEBPACK_AMD_DEFINE_RESULT__;",
`!(__WEBPACK_AMD_DEFINE_RESULT__ = #.call(exports, __webpack_require__, exports, module),
`!(__WEBPACK_AMD_DEFINE_RESULT__ = (#).call(exports, __webpack_require__, exports, module),
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`
],
o: [
Expand All @@ -42,7 +42,7 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate {
],
af: [
"var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;",
`!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_RESULT__ = #.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
`!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_RESULT__ = (#).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`
],
ao: [
Expand Down Expand Up @@ -70,7 +70,7 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate {
],
laf: [
"var __WEBPACK_AMD_DEFINE_ARRAY__, XXX;",
"!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, XXX = (#.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)))"
"!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, XXX = ((#).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)))"
],
lao: [
"var XXX;",
Expand Down
24 changes: 18 additions & 6 deletions lib/dependencies/AMDDefineDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ const isBoundFunctionExpression = expr => {
return true;
};

function isUnboundFunctionExpression(expr) {
if(expr.type === "FunctionExpression") return true;
if(expr.type === "ArrowFunctionExpression") return true;
return false;
}

function isCallable(expr) {
if(isUnboundFunctionExpression(expr)) return true;
if(isBoundFunctionExpression(expr)) return true;
return false;
}

class AMDDefineDependencyParserPlugin {
constructor(options) {
this.options = options;
Expand All @@ -38,7 +50,7 @@ class AMDDefineDependencyParserPlugin {
let array, fn, obj, namedModule;
switch(expr.arguments.length) {
case 1:
if(expr.arguments[0].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[0])) {
if(isCallable(expr.arguments[0])) {
// define(f() {...})
fn = expr.arguments[0];
} else if(expr.arguments[0].type === "ObjectExpression") {
Expand All @@ -54,7 +66,7 @@ class AMDDefineDependencyParserPlugin {
if(expr.arguments[0].type === "Literal") {
namedModule = expr.arguments[0].value;
// define("...", ...)
if(expr.arguments[1].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[1])) {
if(isCallable(expr.arguments[1])) {
// define("...", f() {...})
fn = expr.arguments[1];
} else if(expr.arguments[1].type === "ObjectExpression") {
Expand All @@ -67,7 +79,7 @@ class AMDDefineDependencyParserPlugin {
}
} else {
array = expr.arguments[0];
if(expr.arguments[1].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[1])) {
if(isCallable(expr.arguments[1])) {
// define([...], f() {})
fn = expr.arguments[1];
} else if(expr.arguments[1].type === "ObjectExpression") {
Expand All @@ -84,7 +96,7 @@ class AMDDefineDependencyParserPlugin {
// define("...", [...], f() {...})
namedModule = expr.arguments[0].value;
array = expr.arguments[1];
if(expr.arguments[2].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[2])) {
if(isCallable(expr.arguments[2])) {
// define("...", [...], f() {})
fn = expr.arguments[2];
} else if(expr.arguments[2].type === "ObjectExpression") {
Expand All @@ -102,7 +114,7 @@ class AMDDefineDependencyParserPlugin {
let fnParams = null;
let fnParamsOffset = 0;
if(fn) {
if(fn.type === "FunctionExpression") fnParams = fn.params;
if(isUnboundFunctionExpression(fn)) fnParams = fn.params;
else if(isBoundFunctionExpression(fn)) {
fnParams = fn.callee.object.params;
fnParamsOffset = fn.arguments.length - 1;
Expand Down Expand Up @@ -134,7 +146,7 @@ class AMDDefineDependencyParserPlugin {
});
}
let inTry;
if(fn && fn.type === "FunctionExpression") {
if(fn && isUnboundFunctionExpression(fn)) {
inTry = parser.scope.inTry;
parser.inScope(fnParams, () => {
parser.scope.renames = fnRenames;
Expand Down
8 changes: 4 additions & 4 deletions lib/dependencies/AMDRequireDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ AMDRequireDependency.Template = class AMDRequireDependencyTemplate {

source.replace(depBlock.outerRange[0], depBlock.arrayRange[0] - 1, startBlock);
source.insert(depBlock.arrayRange[0] + 0.9, "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; (");
source.insert(depBlock.functionRange[1], ".apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; ((");
source.insert(depBlock.functionRange[1], ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
source.replace(depBlock.functionRange[1], depBlock.errorCallbackRange[0] - 1, errorRangeBlock);
source.replace(depBlock.errorCallbackRange[1], depBlock.outerRange[1] - 1, endBlock);
return;
Expand All @@ -57,8 +57,8 @@ AMDRequireDependency.Template = class AMDRequireDependencyTemplate {
const endBlock = `}${depBlock.functionBindThis ? ".bind(this)" : ""}${wrapper[1]}__webpack_require__.oe${wrapper[2]}`;
source.replace(depBlock.outerRange[0], depBlock.arrayRange[0] - 1, startBlock);
source.insert(depBlock.arrayRange[0] + 0.9, "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; (");
source.insert(depBlock.functionRange[1], ".apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; ((");
source.insert(depBlock.functionRange[1], ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
source.replace(depBlock.functionRange[1], depBlock.outerRange[1] - 1, endBlock);
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/Schemas.lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const glob = require("glob");
const rootDir = path.resolve(__dirname, "..");

describe("Schemas", () => {
const schemas = glob.sync("schemas/**.json", {
const schemas = glob.sync("schemas/**/*.json", {
cwd: rootDir
});

Expand Down Expand Up @@ -38,6 +38,7 @@ describe("Schemas", () => {
const allowedProperties = [
"definitions",
"$ref",
"id",
"items",
"properties",
"additionalProperties",
Expand Down
1 change: 1 addition & 0 deletions test/cases/parsing/extract-amd.nominimize/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "a";
3 changes: 3 additions & 0 deletions test/cases/parsing/extract-amd.nominimize/amdmodule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define((require) => {
return require("./a");
});
1 change: 1 addition & 0 deletions test/cases/parsing/extract-amd.nominimize/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "c";
2 changes: 2 additions & 0 deletions test/cases/parsing/extract-amd.nominimize/circular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = 1;
module.exports = require("./circular");
3 changes: 3 additions & 0 deletions test/cases/parsing/extract-amd.nominimize/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(value) {
this.value = value;
}
1 change: 1 addition & 0 deletions test/cases/parsing/extract-amd.nominimize/d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "d";
118 changes: 118 additions & 0 deletions test/cases/parsing/extract-amd.nominimize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
var should = require("should");

it("should parse fancy function calls with arrow functions", function() {
("function"==typeof define && define.amd ?
define :
(e,t) => {return t()}
)(["./constructor"], (c) => {
return new c(1324);
});
module.exports.should.have.property("value").be.eql(1324);
(("function"==typeof define && define.amd ?
define :
(e,t) => {return t()}
)(["./constructor"], (c) => {
return new c(4231);
}));
module.exports.should.have.property("value").be.eql(4231);
});

it("should parse fancy AMD calls with arrow functions", function() {
require("./constructor ./a".split(" "));
require("-> module module exports *constructor *a".replace("module", "require").substr(3).replace(/\*/g, "./").split(" "), (require, module, exports, constructor, a) => {
(typeof require).should.be.eql("function");
(typeof module).should.be.eql("object");
(typeof exports).should.be.eql("object");
(typeof require("./constructor")).should.be.eql("function");
(typeof constructor).should.be.eql("function");
a.should.be.eql("a");
});
define("-> module module exports *constructor *a".replace("module", "require").substr(3).replace(/\*/g, "./").split(" "), (require, module, exports, constructor, a) => {
(typeof require).should.be.eql("function");
(typeof module).should.be.eql("object");
(typeof exports).should.be.eql("object");
(typeof require("./constructor")).should.be.eql("function");
(typeof constructor).should.be.eql("function");
a.should.be.eql("a");
});
});

it("should be able to use AMD-style require with arrow functions", function(done) {
var template = "b";
require(["./circular", "./templates/" + template, true ? "./circular" : "fail"], (circular, testTemplate, circular2) => {
circular.should.be.eql(1);
circular2.should.be.eql(1);
testTemplate.should.be.eql("b");
done();
});
});

it("should be able to use require.js-style define with arrow functions", function(done) {
define("name", ["./circular"], (circular) => {
circular.should.be.eql(1);
done();
});
});

it("should be able to use require.js-style define, optional dependancies, not exist, with arrow function", function(done) {
define("name", ["./optional"], (optional) => {
should(optional.b).not.exist;
done();
});
});

it("should be able to use require.js-style define, special string, with arrow function", function(done) {
define(["require"], (require) => {
require("./circular").should.be.eql(1);
done();
});
});

it("should be able to use require.js-style define, without name, with arrow function", function(done) {
true && define(["./circular"], (circular) => {
circular.should.be.eql(1);
done();
});
});

it("should be able to use require.js-style define, with empty dependencies, with arrow function", function(done) {
define("name", [], () => {
done();
});
});

it("should be able to use require.js-style define, without dependencies, with arrow function", function(done) {
true && define("name", () => {
done();
});
});

it("should offer AMD-style define for CommonJs with arrow function", function(done) {
var _test_exports = exports;
var _test_module = module;
define((require, exports, module) => {
(typeof require).should.be.eql("function");
exports.should.be.equal(_test_exports);
module.should.be.equal(_test_module);
require("./circular").should.be.eql(1);
done();
});
});

it("should pull in all dependencies of an AMD module with arrow function", function(done) {
define((require) => {
require("./amdmodule").should.be.eql("a");
done();
});
});

it("should create a chunk for require.js require, with arrow function", function(done) {
var sameTick = true;
require(["./c"], (c) => {
sameTick.should.be.eql(false);
c.should.be.eql("c");
require("./d").should.be.eql("d");
done();
});
sameTick = false;
});
3 changes: 3 additions & 0 deletions test/cases/parsing/extract-amd.nominimize/optional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = 2;
try { module.exports.a = require("./a"); } catch (e) {};
try { module.exports.b = require("./b"); } catch (e) {};
1 change: 1 addition & 0 deletions test/cases/parsing/extract-amd.nominimize/templates/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "a";
1 change: 1 addition & 0 deletions test/cases/parsing/extract-amd.nominimize/templates/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "b";
1 change: 1 addition & 0 deletions test/cases/parsing/extract-amd.nominimize/templates/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "c";
5 changes: 5 additions & 0 deletions test/cases/parsing/extract-amd.nominimize/test.filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var supportsES6 = require("../../../helpers/supportsES6");

module.exports = function(config) {
return !config.minimize && supportsES6();
};
3 changes: 3 additions & 0 deletions test/cases/parsing/extract-amd.nominimize/warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = [
[/Module not found/, /Can't resolve '\.\/b' /, /b\.js/]
];

0 comments on commit c02ca77

Please sign in to comment.