Update transform to generate ES-2015 modules
This commit is contained in:
@@ -310,4 +310,4 @@ package: build/timestamps/node-modules-timestamp
|
|||||||
@cp -r package build
|
@cp -r package build
|
||||||
@cd ./src && cp -r ol/* ../build/package
|
@cd ./src && cp -r ol/* ../build/package
|
||||||
@rm build/package/typedefs.js
|
@rm build/package/typedefs.js
|
||||||
./node_modules/.bin/jscodeshift --transform transforms/common.js build/package
|
./node_modules/.bin/jscodeshift --transform transforms/module.js build/package
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "ol",
|
"name": "ol",
|
||||||
"version": "3.21.0-beta.4",
|
"version": "3.21.0-beta.5",
|
||||||
"description": "OpenLayers",
|
"description": "OpenLayers",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
"module": "index.js",
|
||||||
"license": "BSD-2-Clause",
|
"license": "BSD-2-Clause",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"pbf": "3.0.5",
|
"pbf": "3.0.5",
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ function resolve(fromName, toName) {
|
|||||||
}
|
}
|
||||||
return packageName;
|
return packageName;
|
||||||
}
|
}
|
||||||
var commonDepth = 1;
|
const fromLength = fromParts.length;
|
||||||
var fromLength = fromParts.length;
|
let commonDepth = 1;
|
||||||
while (commonDepth < fromLength - 2) {
|
while (commonDepth < fromLength - 2) {
|
||||||
if (fromParts[commonDepth] === toParts[commonDepth]) {
|
if (fromParts[commonDepth] === toParts[commonDepth]) {
|
||||||
++commonDepth;
|
++commonDepth;
|
||||||
@@ -44,8 +44,12 @@ function resolve(fromName, toName) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var back = new Array(fromLength - commonDepth).join('../') || './';
|
const back = new Array(fromLength - commonDepth).join('../') || './';
|
||||||
return back + toParts.slice(commonDepth).join('/').toLowerCase();
|
let relative = back + toParts.slice(commonDepth).join('/').toLowerCase();
|
||||||
|
if (relative.endsWith('/')) {
|
||||||
|
relative += 'index';
|
||||||
|
}
|
||||||
|
return relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getGoogExpressionStatement(identifier) {
|
function getGoogExpressionStatement(identifier) {
|
||||||
@@ -127,7 +131,7 @@ module.exports = function(info, api) {
|
|||||||
|
|
||||||
const replacements = {};
|
const replacements = {};
|
||||||
|
|
||||||
// replace assignments for boolean defines (e.g. ol.FOO = true -> global.OL_FOO = true)
|
// replace assignments for boolean defines (e.g. ol.FOO = true -> window.OL_FOO = true)
|
||||||
root.find(j.AssignmentExpression, defineAssignment)
|
root.find(j.AssignmentExpression, defineAssignment)
|
||||||
.filter(path => {
|
.filter(path => {
|
||||||
const node = path.value;
|
const node = path.value;
|
||||||
@@ -139,7 +143,7 @@ module.exports = function(info, api) {
|
|||||||
const defineName = `${node.left.object.name}.${node.left.property.name}`;
|
const defineName = `${node.left.object.name}.${node.left.property.name}`;
|
||||||
return j.assignmentExpression(
|
return j.assignmentExpression(
|
||||||
'=',
|
'=',
|
||||||
j.memberExpression(j.identifier('global'), j.identifier(renameDefine(defineName))),
|
j.memberExpression(j.identifier('window'), j.identifier(renameDefine(defineName))),
|
||||||
j.literal(node.right.value));
|
j.literal(node.right.value));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -172,21 +176,27 @@ module.exports = function(info, api) {
|
|||||||
}
|
}
|
||||||
replacements[provide] = rename(provide);
|
replacements[provide] = rename(provide);
|
||||||
|
|
||||||
// replace goog.require()
|
// replace `goog.require('foo')` with `import foo from 'foo'`
|
||||||
|
const imports = [];
|
||||||
root.find(j.ExpressionStatement, getGoogExpressionStatement('require'))
|
root.find(j.ExpressionStatement, getGoogExpressionStatement('require'))
|
||||||
.replaceWith(path => {
|
.forEach(path => {
|
||||||
const name = path.value.expression.arguments[0].value;
|
const name = path.value.expression.arguments[0].value;
|
||||||
if (name in replacements) {
|
if (name in replacements) {
|
||||||
throw new Error(`Duplicate require found in ${info.path}: ${name}`);
|
throw new Error(`Duplicate require found in ${info.path}: ${name}`);
|
||||||
}
|
}
|
||||||
const renamed = rename(name);
|
const renamed = rename(name);
|
||||||
replacements[name] = renamed;
|
replacements[name] = renamed;
|
||||||
return j.variableDeclaration('var', [
|
imports.push(
|
||||||
j.variableDeclarator(j.identifier(renamed), j.callExpression(
|
j.importDeclaration(
|
||||||
j.identifier('require'), [j.literal(resolve(provide, name))]
|
[j.importDefaultSpecifier(j.identifier(renamed))],
|
||||||
))
|
j.literal(resolve(provide, name))
|
||||||
]);
|
)
|
||||||
});
|
);
|
||||||
|
})
|
||||||
|
.remove();
|
||||||
|
|
||||||
|
const body = root.find(j.Program).get('body');
|
||||||
|
body.unshift.apply(body, imports);
|
||||||
|
|
||||||
// replace all uses of required or provided names with renamed identifiers
|
// replace all uses of required or provided names with renamed identifiers
|
||||||
Object.keys(replacements).sort().reverse().forEach(name => {
|
Object.keys(replacements).sort().reverse().forEach(name => {
|
||||||
@@ -199,14 +209,10 @@ module.exports = function(info, api) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// add module.exports
|
// add export declaration
|
||||||
root.find(j.Program).get('body').push(j.expressionStatement(
|
root.find(j.Program).get('body').push(
|
||||||
j.assignmentExpression(
|
j.exportDefaultDeclaration(j.identifier(rename(provide)))
|
||||||
'=',
|
);
|
||||||
j.memberExpression(j.identifier('module'), j.identifier('exports')),
|
|
||||||
j.identifier(rename(provide))
|
|
||||||
)
|
|
||||||
));
|
|
||||||
|
|
||||||
// replace any initial comments
|
// replace any initial comments
|
||||||
root.get().node.comments = comments;
|
root.get().node.comments = comments;
|
||||||
Reference in New Issue
Block a user