From 742ed3d1c9c19cab92956d8e6b416c25bcbfb4d5 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Sun, 9 Jul 2017 15:19:16 +0200 Subject: [PATCH] Remove __pdfjsdev_webpack__, use webpack options `__pdfjsdev_webpack__` was used to skip evaluating part of an AST, in order to not mangle some `require` symbols. This commit removes `__pdfjsdev_webpack__`, and: - Uses `__non_webpack_require__` when one wants the output to contain `require` instead of `__webpack_require__`. - Adds options to the webpack config to prevent "polyfills" for some Node.js-specific APIs to be added. - Use `// eslint-disable-next-line no-undef` instead of `/* globals ... */` for variables that are not meant to be used globally. --- external/webpack/block-require.js | 50 ------------------------------- gulpfile.js | 36 ++++++++++++++++++++-- src/display/api.js | 20 ++++++------- src/shared/util.js | 9 ++---- web/pdfjs.js | 17 +++++------ 5 files changed, 52 insertions(+), 80 deletions(-) delete mode 100644 external/webpack/block-require.js diff --git a/external/webpack/block-require.js b/external/webpack/block-require.js deleted file mode 100644 index 203ca00df..000000000 --- a/external/webpack/block-require.js +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2017 Mozilla Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* eslint-env node */ - -'use strict'; - -function isPDFJSDevCheck(test) { - // Is it something like `typeof __pdfjsdev_webpack__ === 'undefined'`? - return test.type === 'BinaryExpression' && - (test.operator === '===' || test.operator === '!==' || - test.operator === '==' || test.operator === '!=') && - test.left.type === 'UnaryExpression' && - test.left.operator === 'typeof' && - test.left.argument.type === 'Identifier' && - test.left.argument.name === '__pdfjsdev_webpack__' && - test.right.type === 'Literal' && test.right.value === 'undefined'; -} - -function isPDFJSDevEnabled(test) { - return test.operator[0] === '!'; -} - -function BlockRequirePlugin() {} - -BlockRequirePlugin.prototype.apply = function(compiler) { - compiler.plugin('compilation', function(compilation, data) { - data.normalModuleFactory.plugin('parser', function (parser, options) { - parser.plugin('statement if', function (ifNode) { - if (isPDFJSDevCheck(ifNode.test)) { - return isPDFJSDevEnabled(ifNode.test); - } - return undefined; - }); - }); - }); -}; - -module.exports = BlockRequirePlugin; diff --git a/gulpfile.js b/gulpfile.js index eb6c99298..36d31664a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -121,7 +121,6 @@ function createStringSource(filename, content) { function createWebpackConfig(defines, output) { var path = require('path'); - var BlockRequirePlugin = require('./external/webpack/block-require.js'); var versionInfo = getVersionJSON(); var bundleDefines = builder.merge(defines, { @@ -138,7 +137,6 @@ function createWebpackConfig(defines, output) { output: output, plugins: [ new webpack2.BannerPlugin({ banner: licenseHeader, raw: true, }), - new BlockRequirePlugin() ], resolve: { alias: { @@ -168,6 +166,19 @@ function createWebpackConfig(defines, output) { }, ], }, + // Avoid shadowing actual Node.js variables with polyfills, by disabling + // polyfills/mocks - https://webpack.js.org/configuration/node/ + node: { + console: false, + global: false, + process: false, + __filename: false, + __dirname: false, + Buffer: false, + setImmediate: false, + }, + // If we upgrade to Webpack 3.0+, the above can be replaced with: + // node: false, }; } @@ -975,13 +986,32 @@ gulp.task('jsdoc', function (done) { }); gulp.task('lib', ['buildnumber'], function () { + // When we create a bundle, webpack is run on the source and it will replace + // require with __webpack_require__. When we want to use the real require, + // __non_webpack_require__ has to be used. + // In this target, we don't create a bundle, so we have to replace the + // occurences of __non_webpack_require__ ourselves. + function babelPluginReplaceNonWebPackRequire(babel) { + return { + visitor: { + Identifier(path, state) { + if (path.node.name === '__non_webpack_require__') { + path.replaceWith(babel.types.identifier('require')); + } + }, + }, + }; + } function preprocess(content) { var noPreset = /\/\*\s*no-babel-preset\s*\*\//.test(content); content = preprocessor2.preprocessPDFJSCode(ctx, content); content = babel.transform(content, { sourceType: 'module', presets: noPreset ? undefined : ['es2015'], - plugins: ['transform-es2015-modules-commonjs'], + plugins: [ + 'transform-es2015-modules-commonjs', + babelPluginReplaceNonWebPackRequire, + ], }).code; var removeCjsSrc = /^(var\s+\w+\s*=\s*require\('.*?)(?:\/src)(\/[^']*'\);)$/gm; diff --git a/src/display/api.js b/src/display/api.js index ed74c722d..6a2a2ef57 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals requirejs, __pdfjsdev_webpack__ */ +/* globals requirejs, __non_webpack_require__ */ import { createPromiseCapability, deprecated, getVerbosityLevel, globalScope, @@ -43,21 +43,19 @@ var pdfjsFilePath = var fakeWorkerFilesLoader = null; var useRequireEnsure = false; -// The if below protected by __pdfjsdev_webpack__ check from webpack parsing. if (typeof PDFJSDev !== 'undefined' && - PDFJSDev.test('GENERIC && !SINGLE_FILE') && - typeof __pdfjsdev_webpack__ === 'undefined') { + PDFJSDev.test('GENERIC && !SINGLE_FILE')) { // For GENERIC build we need add support of different fake file loaders // for different frameworks. if (typeof window === 'undefined') { // node.js - disable worker and set require.ensure. isWorkerDisabled = true; - if (typeof require.ensure === 'undefined') { - require.ensure = require('node-ensure'); + if (typeof __non_webpack_require__.ensure === 'undefined') { + __non_webpack_require__.ensure = __non_webpack_require__('node-ensure'); } useRequireEnsure = true; - } else if (typeof require !== 'undefined' && - typeof require.ensure === 'function') { + } else if (typeof __non_webpack_require__ !== 'undefined' && + typeof __non_webpack_require__.ensure === 'function') { useRequireEnsure = true; } if (typeof requirejs !== 'undefined' && requirejs.toUrl) { @@ -66,12 +64,12 @@ if (typeof PDFJSDev !== 'undefined' && var dynamicLoaderSupported = typeof requirejs !== 'undefined' && requirejs.load; fakeWorkerFilesLoader = useRequireEnsure ? (function (callback) { - require.ensure([], function () { + __non_webpack_require__.ensure([], function () { var worker; if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) { - worker = require('../pdf.worker.js'); + worker = __non_webpack_require__('../pdf.worker.js'); } else { - worker = require('./pdf.worker.js'); + worker = __non_webpack_require__('./pdf.worker.js'); } callback(worker.WorkerMessageHandler); }); diff --git a/src/shared/util.js b/src/shared/util.js index 31ac174b8..22d70f5e8 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -12,13 +12,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals global, process, __pdfjsdev_webpack__ */ import './compatibility'; import { ReadableStream } from '../../external/streams/streams-lib'; var globalScope = (typeof window !== 'undefined' && window.Math === Math) ? window : + // eslint-disable-next-line no-undef (typeof global !== 'undefined' && global.Math === Math) ? global : (typeof self !== 'undefined' && self.Math === Math) ? self : this; @@ -1092,11 +1092,8 @@ function isSpace(ch) { } function isNodeJS() { - // The if below protected by __pdfjsdev_webpack__ check from webpack parsing. - if (typeof __pdfjsdev_webpack__ === 'undefined') { - return typeof process === 'object' && process + '' === '[object process]'; - } - return false; + // eslint-disable-next-line no-undef + return typeof process === 'object' && process + '' === '[object process]'; } /** diff --git a/web/pdfjs.js b/web/pdfjs.js index 62c4f8ba9..1248f753b 100644 --- a/web/pdfjs.js +++ b/web/pdfjs.js @@ -12,19 +12,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals module, __pdfjsdev_webpack__ */ +/* globals module, __non_webpack_require__ */ 'use strict'; var pdfjsLib; -// The if below protected by __pdfjsdev_webpack__ check from webpack parsing. -if (typeof __pdfjsdev_webpack__ === 'undefined') { - if (typeof window !== 'undefined' && window['pdfjs-dist/build/pdf']) { - pdfjsLib = window['pdfjs-dist/build/pdf']; - } else if (typeof require === 'function') { - pdfjsLib = require('../build/pdf.js'); - } else { - throw new Error('Neither `require` nor `window` found'); - } +if (typeof window !== 'undefined' && window['pdfjs-dist/build/pdf']) { + pdfjsLib = window['pdfjs-dist/build/pdf']; +} else if (typeof __non_webpack_require__ === 'function') { + pdfjsLib = __non_webpack_require__('../build/pdf.js'); +} else { + throw new Error('Neither `require` nor `window` found'); } module.exports = pdfjsLib;