chore: generalize node6 transpilation (#1560)

This patch unifies node6 transpilation:
- instead of generating multiple top-level directories, prefixed with
  `node6-`, all transpiled code gets placed under single `node6/` folder
- transpilation doesn't change require paths of transpiled modules any
  more
This commit is contained in:
Andrey Lushnikov 2017-12-08 15:14:28 -08:00 committed by GitHub
parent 9a5086847c
commit 391d1abaa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 26 deletions

2
.gitignore vendored
View File

@ -9,5 +9,3 @@
.vscode
package-lock.json
/node6
/node6-test
/node6-testrunner

View File

@ -11,4 +11,9 @@ node_modules
*.pyc
.vscode
package-lock.json
/node6-test
/node6/test
/node6/utils
/test
/utils
/docs
yarn.lock

View File

@ -25,4 +25,4 @@ try {
if (asyncawait)
module.exports = require('./lib/Puppeteer');
else
module.exports = require('./node6/Puppeteer');
module.exports = require('./node6/lib/Puppeteer');

View File

@ -34,7 +34,8 @@ const downloadURLs = {
win64: '%s/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip',
};
const PROJECT_ROOT = path.join(__dirname, '..');
// Project root will be different for node6-transpiled code.
const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..');
class Downloader {
/**

View File

@ -18,7 +18,7 @@
"coverage": "cross-env COVERAGE=true npm run unit",
"test-node6-transformer": "node utils/node6-transform/test/test.js",
"build": "node utils/node6-transform/index.js",
"unit-node6": "node node6-test/test.js",
"unit-node6": "node node6/test/test.js",
"tsc": "tsc -p ."
},
"author": "The Chromium Authors",

View File

@ -19,8 +19,12 @@ const path = require('path');
const {helper} = require('../lib/helper');
if (process.env.COVERAGE)
helper.recordPublicAPICoverage();
console.log('Testing on Node', process.version);
const puppeteer = require('..');
const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..');
const puppeteer = require(PROJECT_ROOT);
const DeviceDescriptors = require(path.join(PROJECT_ROOT, 'DeviceDescriptors'));
const SimpleServer = require('./server/SimpleServer');
const GoldenUtils = require('./golden-utils');
@ -40,8 +44,12 @@ const HTTPS_PREFIX = 'https://localhost:' + HTTPS_PORT;
const headless = (process.env.HEADLESS || 'true').trim().toLowerCase() === 'true';
const slowMo = parseInt((process.env.SLOW_MO || '0').trim(), 10);
const executablePath = process.env.CHROME;
console.log('Testing on Node', process.version);
if (executablePath)
console.warn(`${YELLOW_COLOR}WARN: running tests with ${executablePath}${RESET_COLOR}`);
// Make sure the `npm install` was run after the chromium roll.
console.assert(fs.existsSync(puppeteer.executablePath()), `Chromium is not Downloaded. Run 'npm install' and try to re-run tests`);
const defaultBrowserOptions = {
executablePath,
@ -50,9 +58,6 @@ const defaultBrowserOptions = {
args: ['--no-sandbox']
};
// Make sure the `npm install` was run after the chromium roll.
console.assert(fs.existsSync(puppeteer.executablePath()), `Chromium is not Downloaded. Run 'npm install' and try to re-run tests`);
const timeout = process.env.DEBUG_TEST || slowMo ? 0 : 10 * 1000;
const {TestRunner, Reporter, Matchers} = require('../utils/testrunner/');
@ -228,8 +233,8 @@ describe('Puppeteer', function() {
});
describe('Page', function() {
const iPhone = require('../DeviceDescriptors')['iPhone 6'];
const iPhoneLandscape = require('../DeviceDescriptors')['iPhone 6 landscape'];
const iPhone = DeviceDescriptors['iPhone 6'];
const iPhoneLandscape = DeviceDescriptors['iPhone 6 landscape'];
let browser;
let page;

View File

@ -19,9 +19,17 @@ const path = require('path');
const removeRecursive = require('rimraf').sync;
const transformAsyncFunctions = require('./TransformAsyncFunctions');
copyFolder(path.join(__dirname, '..', '..', 'lib'), path.join(__dirname, '..', '..', 'node6'));
copyFolder(path.join(__dirname, '..', '..', 'test'), path.join(__dirname, '..', '..', 'node6-test'));
copyFolder(path.join(__dirname, '..', '..', 'utils', 'testrunner'), path.join(__dirname, '..', '..', 'node6-testrunner'));
const root = path.join(__dirname, '..', '..');
const dest = path.join(__dirname, '..', '..', 'node6');
if (fs.existsSync(dest))
removeRecursive(dest);
fs.mkdirSync(dest);
fs.mkdirSync(path.join(dest, 'utils'));
copyFolder(path.join(root, 'lib'), path.join(dest, 'lib'));
copyFolder(path.join(root, 'test'), path.join(dest, 'test'));
copyFolder(path.join(root, 'utils'), path.join(dest, 'utils'));
function copyFolder(source, target) {
if (fs.existsSync(target))
@ -31,16 +39,19 @@ function copyFolder(source, target) {
fs.readdirSync(source).forEach(file => {
const from = path.join(source, file);
const to = path.join(target, file);
if (fs.lstatSync(from).isDirectory()) {
if (fs.lstatSync(from).isDirectory())
copyFolder(from, to);
} else {
else
copyFile(from, to);
});
}
function copyFile(from, to) {
let text = fs.readFileSync(from);
if (file.endsWith('.js')) {
text = transformAsyncFunctions(text.toString());
text = text.replace(/require\('\.\.\/lib\//g, `require('../node6/`);
text = text.replace(/require\('\.\.\/utils\/testrunner\//g, `require('../node6-testrunner/`);
if (from.endsWith('.js')) {
text = text.toString();
const prefix = text.startsWith('#!') ? text.substring(0, text.indexOf('\n')) : '';
text = prefix + transformAsyncFunctions(text.substring(prefix.length));
}
fs.writeFileSync(to, text);
}
});
}