2017-01-11 17:51:58 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
|
2017-01-20 02:43:42 +08:00
|
|
|
const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
|
2017-08-08 15:32:43 +08:00
|
|
|
const HarmonyInitDependency = require("./HarmonyInitDependency");
|
2017-01-11 17:51:58 +08:00
|
|
|
|
|
|
|
module.exports = class HarmonyDetectionParserPlugin {
|
|
|
|
apply(parser) {
|
|
|
|
parser.plugin("program", (ast) => {
|
|
|
|
var isHarmony = ast.body.some(statement => {
|
|
|
|
return /^(Import|Export).*Declaration$/.test(statement.type);
|
|
|
|
});
|
|
|
|
if(isHarmony) {
|
2017-02-05 07:15:47 +08:00
|
|
|
const module = parser.state.module;
|
2017-08-08 15:32:43 +08:00
|
|
|
const compatDep = new HarmonyCompatibilityDependency(module);
|
|
|
|
compatDep.loc = {
|
|
|
|
start: {
|
|
|
|
line: -1,
|
|
|
|
column: 0
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
line: -1,
|
|
|
|
column: 0
|
|
|
|
},
|
|
|
|
index: -3
|
|
|
|
};
|
|
|
|
module.addDependency(compatDep);
|
|
|
|
const initDep = new HarmonyInitDependency(module);
|
|
|
|
initDep.loc = {
|
2017-01-11 17:51:58 +08:00
|
|
|
start: {
|
|
|
|
line: -1,
|
|
|
|
column: 0
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
line: -1,
|
|
|
|
column: 0
|
|
|
|
},
|
|
|
|
index: -2
|
|
|
|
};
|
2017-08-08 15:32:43 +08:00
|
|
|
module.addDependency(initDep);
|
|
|
|
parser.state.harmonyParserScope = parser.state.harmonyParserScope || {};
|
2017-01-11 17:51:58 +08:00
|
|
|
module.meta.harmonyModule = true;
|
|
|
|
module.strict = true;
|
|
|
|
module.exportsArgument = "__webpack_exports__";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var nonHarmonyIdentifiers = ["define", "exports"];
|
|
|
|
nonHarmonyIdentifiers.forEach(identifer => {
|
|
|
|
parser.plugin(`evaluate typeof ${identifer}`, nullInHarmony);
|
|
|
|
parser.plugin(`typeof ${identifer}`, skipInHarmony);
|
|
|
|
parser.plugin(`evaluate ${identifer}`, nullInHarmony);
|
|
|
|
parser.plugin(`expression ${identifer}`, skipInHarmony);
|
|
|
|
parser.plugin(`call ${identifer}`, skipInHarmony);
|
|
|
|
});
|
|
|
|
|
|
|
|
function skipInHarmony() {
|
2017-02-05 07:15:47 +08:00
|
|
|
const module = this.state.module;
|
2017-01-11 17:51:58 +08:00
|
|
|
if(module && module.meta && module.meta.harmonyModule)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function nullInHarmony() {
|
2017-02-05 07:15:47 +08:00
|
|
|
const module = this.state.module;
|
2017-01-11 17:51:58 +08:00
|
|
|
if(module && module.meta && module.meta.harmonyModule)
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|