Enable babel translation to enable ES module support.

This commit is contained in:
Yury Delendik 2017-03-06 08:42:48 -06:00
parent 02370f952a
commit 25873e92f0
10 changed files with 65 additions and 18 deletions

View File

@ -8,6 +8,8 @@ external/webL10n/
external/cmapscompress/ external/cmapscompress/
external/builder/fixtures/ external/builder/fixtures/
external/builder/fixtures_esprima/ external/builder/fixtures_esprima/
src/shared/cffStandardStrings.js
src/shared/fonts_utils.js
test/tmp/ test/tmp/
test/features/ test/features/
test/pdfs/ test/pdfs/

View File

@ -1,6 +1,7 @@
{ {
"parserOptions": { "parserOptions": {
"ecmaVersion": 5, "ecmaVersion": 6,
"sourceType": "module"
}, },
"env": { "env": {
@ -78,7 +79,7 @@
}], }],
// Strict Mode // Strict Mode
"strict": ["error", "global"], "strict": ["off", "global"],
// Variables // Variables
"no-catch-shadow": "error", "no-catch-shadow": "error",

View File

@ -0,0 +1,9 @@
{
"extends": [
../../.eslintrc
],
"parserOptions": {
"sourceType": "script"
},
}

View File

@ -4,7 +4,8 @@
], ],
"parserOptions": { "parserOptions": {
"ecmaVersion": 6 "ecmaVersion": 6,
"sourceType": "script"
}, },
"rules": { "rules": {

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var esprima = require('esprima'); var acorn = require('acorn');
var escodegen = require('escodegen'); var escodegen = require('escodegen');
var vm = require('vm'); var vm = require('vm');
var fs = require('fs'); var fs = require('fs');
@ -49,7 +49,7 @@ function handlePreprocessorAction(ctx, actionName, args, loc) {
return {type: 'Literal', value: result, loc: loc}; return {type: 'Literal', value: result, loc: loc};
} }
if (typeof result === 'object') { if (typeof result === 'object') {
var parsedObj = esprima.parse('(' + JSON.stringify(result) + ')'); var parsedObj = acorn.parse('(' + JSON.stringify(result) + ')');
parsedObj.body[0].expression.loc = loc; parsedObj.body[0].expression.loc = loc;
return parsedObj.body[0].expression; return parsedObj.body[0].expression;
} }
@ -66,7 +66,7 @@ function handlePreprocessorAction(ctx, actionName, args, loc) {
jsonPath.substring(ROOT_PREFIX.length)); jsonPath.substring(ROOT_PREFIX.length));
} }
var jsonContent = fs.readFileSync(jsonPath).toString(); var jsonContent = fs.readFileSync(jsonPath).toString();
var parsedJSON = esprima.parse('(' + jsonContent + ')'); var parsedJSON = acorn.parse('(' + jsonContent + ')');
parsedJSON.body[0].expression.loc = loc; parsedJSON.body[0].expression.loc = loc;
return parsedJSON.body[0].expression; return parsedJSON.body[0].expression;
} }
@ -354,16 +354,21 @@ function preprocessPDFJSCode(ctx, code) {
adjustMultilineComment: saveComments, adjustMultilineComment: saveComments,
} }
}; };
var comments;
var parseComment = { var parseComment = {
loc: true, locations: true,
attachComment: saveComments onComments: saveComments || (comments = []),
sourceType: 'module',
}; };
var codegenOptions = { var codegenOptions = {
format: format, format: format,
comment: saveComments, comment: saveComments,
parse: esprima.parse parse: acorn.parse,
}; };
var syntax = esprima.parse(code, parseComment); var syntax = acorn.parse(code, parseComment);
if (saveComments) {
escodegen.attachComments(syntax, comments);
}
traverseTree(ctx, syntax); traverseTree(ctx, syntax);
return escodegen.generate(syntax, codegenOptions); return escodegen.generate(syntax, codegenOptions);
} }

View File

@ -109,6 +109,12 @@ function createWebpackConfig(defines, output) {
}, },
module: { module: {
loaders: [ loaders: [
{
loader: 'babel-loader',
options: {
plugins: ['transform-es2015-modules-commonjs']
}
},
{ {
loader: path.join(__dirname, 'external/webpack/pdfjsdev-loader.js'), loader: path.join(__dirname, 'external/webpack/pdfjsdev-loader.js'),
options: { options: {
@ -116,7 +122,7 @@ function createWebpackConfig(defines, output) {
saveComments: false, saveComments: false,
defines: bundleDefines defines: bundleDefines
} }
} },
] ]
} }
}; };
@ -928,6 +934,10 @@ gulp.task('jsdoc', function (done) {
gulp.task('lib', ['buildnumber'], function () { gulp.task('lib', ['buildnumber'], function () {
function preprocess(content) { function preprocess(content) {
content = preprocessor2.preprocessPDFJSCode(ctx, content); content = preprocessor2.preprocessPDFJSCode(ctx, content);
content = babel.transform(content, {
sourceType: 'module',
plugins: ['transform-es2015-modules-commonjs'],
}).code;
var removeCjsSrc = var removeCjsSrc =
/^(var\s+\w+\s*=\s*require\('.*?)(?:\/src)(\/[^']*'\);)$/gm; /^(var\s+\w+\s*=\s*require\('.*?)(?:\/src)(\/[^']*'\);)$/gm;
content = content.replace(removeCjsSrc, function (all, prefix, suffix) { content = content.replace(removeCjsSrc, function (all, prefix, suffix) {
@ -935,6 +945,7 @@ gulp.task('lib', ['buildnumber'], function () {
}); });
return licenseHeader + content; return licenseHeader + content;
} }
var babel = require('babel-core');
var versionInfo = getVersionJSON(); var versionInfo = getVersionJSON();
var ctx = { var ctx = {
rootPath: __dirname, rootPath: __dirname,

View File

@ -19,9 +19,8 @@
try { try {
require('shelljs/make'); require('shelljs/make');
} catch (e) { } catch (e) {
console.log('ShellJS is not installed. Run "npm install" to install ' + throw new Error('ShellJS is not installed. Run "npm install" to install ' +
'all dependencies.'); 'all dependencies.');
return;
} }
var fs = require('fs'); var fs = require('fs');

View File

@ -2,9 +2,12 @@
"name": "pdf.js", "name": "pdf.js",
"version": "0.8.0", "version": "0.8.0",
"devDependencies": { "devDependencies": {
"acorn": "^4.0.11",
"babel-core": "^6.23.1",
"babel-loader": "^6.4.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.23.0",
"escodegen": "^1.8.0", "escodegen": "^1.8.0",
"eslint": "^3.11.1", "eslint": "^3.11.1",
"esprima": "^2.7.2",
"gulp": "^3.9.1", "gulp": "^3.9.1",
"gulp-rename": "^1.2.2", "gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4", "gulp-replace": "^0.5.4",
@ -22,6 +25,7 @@
"shelljs": "~0.4.0", "shelljs": "~0.4.0",
"streamqueue": "^1.1.1", "streamqueue": "^1.1.1",
"systemjs": "^0.20.7", "systemjs": "^0.20.7",
"systemjs-plugin-babel": "0.0.21",
"typogr": "~0.6.5", "typogr": "~0.6.5",
"uglify-js": "^2.6.1", "uglify-js": "^2.6.1",
"webpack": "^2.2.1", "webpack": "^2.2.1",

View File

@ -29,10 +29,13 @@
throw new Error('Cannot configure SystemJS'); throw new Error('Cannot configure SystemJS');
} }
var PluginBabelPath = 'node_modules/systemjs-plugin-babel/plugin-babel.js';
var SystemJSPluginBabelPath =
'node_modules/systemjs-plugin-babel/systemjs-babel-browser.js';
SystemJS.config({ SystemJS.config({
packages: { packages: {
'': { '': {
format: 'amd',
defaultExtension: 'js', defaultExtension: 'js',
} }
}, },
@ -40,6 +43,18 @@
'pdfjs': new URL('src', baseLocation).href, 'pdfjs': new URL('src', baseLocation).href,
'pdfjs-web': new URL('web', baseLocation).href, 'pdfjs-web': new URL('web', baseLocation).href,
'pdfjs-test': new URL('test', baseLocation).href, 'pdfjs-test': new URL('test', baseLocation).href,
} },
meta: {
'*': {
scriptLoad: false,
esModule: true
}
},
map: {
'plugin-babel': new URL(PluginBabelPath, baseLocation).href,
'systemjs-babel-build':
new URL(SystemJSPluginBabelPath, baseLocation).href,
},
transpiler: 'plugin-babel'
}); });
})(); })();

View File

@ -502,7 +502,7 @@ var Stats = (function Stats() {
})(); })();
// Manages all the debugging tools. // Manages all the debugging tools.
var PDFBug = (function PDFBugClosure() { window.PDFBug = (function PDFBugClosure() {
var panelWidth = 300; var panelWidth = 300;
var buttons = []; var buttons = [];
var activePanel = null; var activePanel = null;