Add magic indicator file to FS unbundles

Reviewed By: tadeuzagallo

Differential Revision: D2773427

fb-gh-sync-id: 8724f5c6e300f456e5a9c434c14e8e74b2792cf9
This commit is contained in:
David Aurelio 2015-12-18 06:36:14 -08:00 committed by facebook-github-bot-7
parent 920f71da57
commit dfdafdb733
3 changed files with 31 additions and 17 deletions

View File

@ -8,13 +8,14 @@
*/
'use strict';
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const Promise = require('promise');
const writeFile = require('../writeFile');
const writeSourceMap = require('./write-sourcemap');
const MAGIC_UNBUNDLE_NUMBER = require('./magic-number');
const MAGIC_UNBUNDLE_FILENAME = '.unbundle';
const MODULES_DIR = 'js-modules';
/**
@ -37,11 +38,15 @@ function saveAsAssets(bundle, options, log) {
log('finish');
log('Writing bundle output to:', bundleOutput);
const modulesDir = path.join(path.dirname(bundleOutput), MODULES_DIR);
const writeUnbundle =
Promise.all([
writeModules(path.dirname(bundleOutput), modules, encoding),
writeStartupFile(bundleOutput, startupCode, encoding)
]);
createDir(modulesDir).then( // create the modules directory first
Promise.all([
writeModules(modulesDir, modules, encoding),
writeFile(bundleOutput, startupCode, encoding),
writeMagicFlagFile(modulesDir),
])
);
writeUnbundle.then(() => log('Done writing unbundle output'));
return Promise.all([writeUnbundle, writeSourceMap(sourcemapOutput, '', log)]);
@ -57,13 +62,13 @@ function createDirectoriesForModules(modulesDir, modules) {
modules.map(name => {
// get all needed directory names
const dir = path.dirname(name);
return dir === '.' ? modulesDir : path.join(modulesDir, dir);
return dir === '.' || dir === '' ? null : path.join(modulesDir, dir);
})
.filter(Boolean) // remove empty directories
.sort()
.filter((dir, i, dirs) => {
// remove parent directories. After sorting, parent directories are
// located before child directories
// remove parent directories and dedupe.
// After sorting, parent directories are located before child directories
const next = dirs[i + 1];
return !next || next !== dir && !next.startsWith(dir + path.sep);
});
@ -84,20 +89,18 @@ function writeModuleFiles(modules, modulesDir, encoding) {
return Promise.all(writeFiles);
}
function writeModules(assetsDest, modules, encoding) {
const modulesDir = path.join(assetsDest, MODULES_DIR);
function writeModules(modulesDir, modules, encoding) {
return (
createDirectoriesForModules(modulesDir, modules.map(({name}) => name))
.then(() => writeModuleFiles(modules, modulesDir, encoding))
);
}
function writeStartupFile(outputFile, code, encoding) {
return new Promise((resolve, reject) => {
fs.createWriteStream(outputFile).
write(code, encoding, error => error ? reject(error) : resolve());
});
function writeMagicFlagFile(outputDir) {
/* global Buffer: true */
const buffer = Buffer(4);
buffer.writeUInt32LE(MAGIC_UNBUNDLE_NUMBER);
return writeFile(path.join(outputDir, MAGIC_UNBUNDLE_FILENAME), buffer);
}
module.exports = saveAsAssets;

View File

@ -12,7 +12,7 @@ const fs = require('fs');
const Promise = require('promise');
const writeSourceMap = require('./write-sourcemap');
const MAGIC_UNBUNDLE_FILE_HEADER = 0xFB0BD1E5;
const MAGIC_UNBUNDLE_FILE_HEADER = require('./magic-number');
const MAGIC_STARTUP_MODULE_ID = '';
/**

View File

@ -0,0 +1,11 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
module.exports = 0xFB0BD1E5;