Exemplo n.º 1
0
  load() {
    if (this._loading) {
      return this._loading;
    }

    const mw = this._opts.maxWorkers;
    this._haste = new JestHasteMap({
      extensions: this._opts.extensions.concat(this._opts.assetExts),
      forceNodeFilesystemAPI: this._opts.forceNodeFilesystemAPI,
      ignorePattern: {test: this._opts.ignoreFilePath},
      maxWorkers: typeof mw === 'number' && mw >= 1 ? mw : getMaxWorkers(),
      mocksPattern: '',
      name: 'react-native-packager',
      platforms: Array.from(this._opts.platforms),
      providesModuleNodeModules: this._opts.providesModuleNodeModules,
      resetCache: this._opts.resetCache,
      retainAllFiles: true,
      roots: this._opts.roots,
      useWatchman: this._opts.useWatchman,
      watch: this._opts.watch,
    });

    this._loading = this._haste.build().then(hasteMap => {
      const initializingPackagerLogEntry =
        print(log(createActionStartEntry('Initializing Packager')));

      const hasteFSFiles = hasteMap.hasteFS.getAllFiles();

      this._fastfs = new Fastfs(
        'JavaScript',
        this._opts.roots,
        hasteFSFiles,
        {
          ignore: this._opts.ignoreFilePath,
        }
      );

      this._moduleCache = new ModuleCache({
        fastfs: this._fastfs,
        cache: this._cache,
        transformCode: this._opts.transformCode,
        transformCacheKey: this._opts.transformCacheKey,
        depGraphHelpers: this._helpers,
        assetDependencies: this._assetDependencies,
        moduleOptions: this._opts.moduleOptions,
      }, this._opts.platforms);

      this._hasteMap = new HasteMap({
        fastfs: this._fastfs,
        extensions: this._opts.extensions,
        moduleCache: this._moduleCache,
        preferNativePlatform: this._opts.preferNativePlatform,
        helpers: this._helpers,
        platforms: this._opts.platforms,
      });

      this._haste.on('change', ({eventsQueue}) =>
        eventsQueue.forEach(({type, filePath, stat}) =>
          this.processFileChange(type, filePath, stat)
        )
      );

      const buildingHasteMapLogEntry =
        print(log(createActionStartEntry('Building Haste Map')));

      return this._hasteMap.build().then(
        map => {
          print(log(createActionEndEntry(buildingHasteMapLogEntry)));
          print(log(createActionEndEntry(initializingPackagerLogEntry)));
          return map;
        },
        err => {
          const error = new Error(
            `Failed to build DependencyGraph: ${err.message}`
          );
          /* $FlowFixMe: monkey-patching */
          error.type = ERROR_BUILDING_DEP_GRAPH;
          error.stack = err.stack;
          throw error;
        }
      );
    });

    return this._loading;
  }
Exemplo n.º 2
0
  load() {
    if (this._loading) {
      return this._loading;
    }

    const mw = this._opts.maxWorkers;
    const haste = new JestHasteMap({
      extensions: this._opts.extensions.concat(this._opts.assetExts),
      ignorePattern: {test: this._opts.ignoreFilePath},
      maxWorkers: typeof mw === 'number' && mw >= 1 ? mw : getMaxWorkers(),
      mocksPattern: '',
      name: 'react-native-packager',
      platforms: Array.from(this._opts.platforms),
      providesModuleNodeModules: this._opts.providesModuleNodeModules,
      resetCache: this._opts.resetCache,
      retainAllFiles: true,
      roots: this._opts.roots.concat(this._opts.assetRoots_DEPRECATED),
      useWatchman: this._opts.useWatchman,
    });

    this._loading = haste.build().then(hasteMap => {
      const {activity} = this._opts;
      const depGraphActivity = activity.startEvent(
        'Initializing Packager',
        null,
        {
          telemetric: true,
        },
      );

      const hasteFSFiles = hasteMap.hasteFS.getAllFiles();

      this._fastfs = new Fastfs(
        'JavaScript',
        this._opts.roots,
        this._opts.fileWatcher,
        hasteFSFiles,
        {
          ignore: this._opts.ignoreFilePath,
          activity: activity,
        }
      );

      this._fastfs.on('change', this._processFileChange.bind(this));

      this._moduleCache = new ModuleCache({
        fastfs: this._fastfs,
        cache: this._cache,
        extractRequires: this._opts.extractRequires,
        transformCode: this._opts.transformCode,
        depGraphHelpers: this._helpers,
        assetDependencies: this._assetDependencies,
        moduleOptions: this._opts.moduleOptions,
      }, this._opts.platforms);

      this._hasteMap = new HasteMap({
        fastfs: this._fastfs,
        extensions: this._opts.extensions,
        moduleCache: this._moduleCache,
        preferNativePlatform: this._opts.preferNativePlatform,
        helpers: this._helpers,
        platforms: this._opts.platforms,
      });

      const escapePath = (p: string) => {
        return (path.sep === '\\')  ? p.replace(/(\/|\\(?!\.))/g, '\\\\') : p;
      };

      const assetPattern =
        new RegExp('^' + this._opts.assetRoots_DEPRECATED.map(escapePath).join('|'));

      const assetFiles = hasteMap.hasteFS.matchFiles(assetPattern);

      this._deprecatedAssetMap = new DeprecatedAssetMap({
        helpers: this._helpers,
        assetExts: this._opts.assetExts,
        platforms: this._opts.platforms,
        files: assetFiles,
      });

      this._fastfs.on('change', (type, filePath, root, fstat) => {
        if (assetPattern.test(path.join(root, filePath))) {
          this._deprecatedAssetMap.processFileChange(type, filePath, root, fstat);
        }
      });

      const hasteActivity = activity.startEvent(
        'Building Haste Map',
        null,
        {
          telemetric: true,
        },
      );
      return this._hasteMap.build().then(
        map => {
          activity.endEvent(hasteActivity);
          activity.endEvent(depGraphActivity);
          return map;
        },
        err => {
          const error = new Error(
            `Failed to build DependencyGraph: ${err.message}`
          );
          error.type = ERROR_BUILDING_DEP_GRAPH;
          error.stack = err.stack;
          throw error;
        }
      );
    });

    return this._loading;
  }
Exemplo n.º 3
0
test('exits the process after test are done but before timers complete', async () => {
  writeFiles(DIR, {
    'file.android.js': '"foo android"',
    'file.ios.js': '"foo ios"',
    'file.js': '"foo default"',
    'fileWithExtension.ignored': '"ignored file"',
    'node_modules/bar/fileWithExtension.ignored': '"ignored node modules"',
    'node_modules/bar/image.png': '"an image"',
    'node_modules/bar/index.js': '"node modules bar"',
  });

  const haste = new JestHasteMap({
    computeSha1: true,
    extensions: ['js', 'json', 'png'],
    forceNodeFilesystemAPI: true,
    ignorePattern: / ^/,
    maxWorkers: 2,
    mocksPattern: '',
    name: 'tmp',
    platforms: ['ios', 'android'],
    retainAllFiles: true,
    rootDir: DIR,
    roots: [DIR],
    useWatchman: false,
    watch: false,
  });

  const {hasteFS} = await haste.build();

  expect(hasteFS.getSha1(path.join(DIR, 'file.android.js'))).toBe(
    'e376f9fd9a96d000fa019020159f996a8855f8bc',
  );

  expect(hasteFS.getSha1(path.join(DIR, 'file.ios.js'))).toBe(
    '1271b4db2a5f47ae46cb01a1d0604a94d401e8f7',
  );

  expect(hasteFS.getSha1(path.join(DIR, 'file.js'))).toBe(
    'c26c852220977244418f17a9fdc4ae9c192b3188',
  );

  expect(hasteFS.getSha1(path.join(DIR, 'node_modules/bar/image.png'))).toBe(
    '8688f7e11f63d8a7eac7cb87af850337fabbd400',
  );

  expect(hasteFS.getSha1(path.join(DIR, 'node_modules/bar/index.js'))).toBe(
    'ee245b9fbd45e1f6ad300eb2f5484844f6b5a34c',
  );

  // Ignored files do not get the SHA-1 computed.

  expect(hasteFS.getSha1(path.join(DIR, 'fileWithExtension.ignored'))).toBe(
    null,
  );

  expect(
    hasteFS.getSha1(
      path.join(DIR, 'node_modules/bar/fileWithExtension.ignored'),
    ),
  ).toBe(null);
});