Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processModuleFilter and remove postProcessModules #215

Closed
wants to merge 5 commits into from
Closed

Add processModuleFilter and remove postProcessModules #215

wants to merge 5 commits into from

Conversation

cliberal
Copy link
Contributor

@cliberal cliberal commented Aug 5, 2018

Summary
remove postProcessModules hook since it is not working now. Add a processModuleFilter hook to skip some modules before code generating. see #210

Test plan
pass

use processModuleFilter to skip some modules before code generating
@codecov-io
Copy link

codecov-io commented Aug 5, 2018

Codecov Report

Merging #215 into master will increase coverage by 0.03%.
The diff coverage is 82.35%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #215      +/-   ##
==========================================
+ Coverage   85.79%   85.83%   +0.03%     
==========================================
  Files         137      137              
  Lines        4406     4411       +5     
  Branches      685      686       +1     
==========================================
+ Hits         3780     3786       +6     
+ Misses        554      553       -1     
  Partials       72       72
Impacted Files Coverage Δ
...ro/src/DeltaBundler/Serializers/sourceMapObject.js 100% <ø> (ø) ⬆️
...etro/src/DeltaBundler/Serializers/plainJSBundle.js 100% <ø> (ø) ⬆️
...ro/src/DeltaBundler/Serializers/sourceMapString.js 100% <ø> (ø) ⬆️
packages/metro-config/src/oldConfig.js 82.75% <0%> (ø) ⬆️
packages/metro/src/Server.js 83.44% <0%> (ø) ⬆️
packages/metro-config/src/convertConfig.js 61.53% <0%> (ø) ⬆️
...etro/src/DeltaBundler/Serializers/deltaJSBundle.js 100% <100%> (ø) ⬆️
...es/metro/src/DeltaBundler/Serializers/getAssets.js 100% <100%> (ø) ⬆️
...o/src/DeltaBundler/Serializers/getRamBundleInfo.js 90.24% <100%> (+0.24%) ⬆️
.../metro/src/DeltaBundler/Serializers/getAllFiles.js 95% <100%> (+0.55%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fde0642...84e119d. Read the comment docs.

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 5, 2018
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

Copy link
Contributor

@rafeca rafeca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! having a filtering function is much better than the previous method 👍

Please update the rest of the serializers and we're good to go 😃

@@ -104,4 +104,4 @@ These options are only useful with React Native projects.
| `getPolyfills` | `({platform: ?string}) => $ReadOnlyArray<string>` | An optional list of polyfills to include in the bundle. The list defaults to a set of common polyfills for Number, String, Array, Object... |
| `postProcessBundleSourcemap` | `PostProcessBundleSourcemap` | An optional function that can modify the code and source map of the bundle before it is written. Applied once for the entire bundle. |
| `getModulesRunBeforeMainModule` | `(entryFilePath: string) => Array<string>` | An array of modules to be required before the entry point. It should contain the absolute path of each module. |
| `postProcessModules` | `(modules: Array<Module>, entryFiles: Array<string>) => Array<Module>` | A function that can change the resulting modules output. |
| `processModuleFilter` | `(module: Array<Module>) => boolean` | A function that can change the resulting modules output. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the description to: A filter function to discard specific modules from the output?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it's better

@@ -42,6 +43,7 @@ function plainJSBundle(
...getAppendScripts(entryPoint, graph, options),
]
.filter(isJsModule)
.filter(options.processModuleFilter)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be done to each of the serializers to have consistent results no matter the type of output that the user is generating (it's specially important to update the sourcemap serializers to not end up with incorrect sourcemaps).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 I'll have a try. Since I haven't read these files, it may take a while.

@cliberal
Copy link
Contributor Author

cliberal commented Aug 7, 2018

Thanks for review

  1. I did not change getAllFiles.js since it does't seem to be used.
  2. There some redundant code but I'm not sure whether can I remove it.
// sourceMapObject.js

function fullSourceMapObject(
  pre: $ReadOnlyArray<Module<>>,
  graph: Graph<>,
  options: {|
    +excludeSource: boolean,
    +processModuleFilter: (module: Module<>) => boolean,
  |},
): BabelSourceMap {
  const modules = [...pre, ...graph.dependencies.values()]
    .filter(isJsModule)
    .filter(options.processModuleFilter) // this line can be removed if we don't use it in `_sourceMapForURL` function below
    .map(module => {
      return {
        ...getJsOutput(module).data,
        path: module.path,
        source: options.excludeSource ? '' : module.getSource(),
      };
    });

  return fromRawMappings(modules).toMap(undefined, {
    excludeSource: options.excludeSource,
  });
}

// Server.js
async _sourceMapForURL(reqUrl: string): Promise<MetroSourceMap> {
    const options: DeltaOptions = this._getOptionsFromUrl(reqUrl);

    const {graph, prepend} = await this._getGraphInfo(options, {
      rebuild: false,
    });

    return sourceMapObject(prepend, graph, {
      excludeSource: options.excludeSource,
      processModuleFilter: this._config.serializer.processModuleFilter,
    });
 }

@rafeca
Copy link
Contributor

rafeca commented Aug 7, 2018

  1. I did not change getAllFiles.js since it does't seem to be used.

getAllFiles() sadly is being used by Server.getOrderedDependencyPaths(), which sadly is being used by the react-native cli (there's a cli command to get all the files in the bundle).

  1. There some redundant code but I'm not sure whether can I remove it.

The sourceMapObject() method is being used by the Server symbolication entrypoint. This is an http entrypoint http://localhost:8081/symbolicate that allows simulators to display the original file/line number of errors/warnings (you may have noticed that RN development error boxes initially display the raw line number and after a small delay it shows the original file/line/column: this is because the app is making a request to the symbolication entrypoint).

@cliberal
Copy link
Contributor Author

cliberal commented Aug 7, 2018

@rafeca thanks for explain that. It's very helpful

Copy link
Contributor

@rafeca rafeca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the comments so fast! Looks great now! (sorry for all the repetitive work on the serializers: we're planning create some helpers to be able to reuse logic there.

I'm going to import this PR to our systems

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rafeca has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants