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

Enable strict mode for transform-es2015-modules-commonjs #5796

Closed
wants to merge 1 commit into from

Conversation

janicduplessis
Copy link
Contributor

Since #5422 react-native works with strict mode modules but the transform was not updated since Facebook has some non strict mode compatible internal modules. Now that #5214 has landed and it is easy to change the babel config I think we should enable it by default to make es2015 modules spec compliant.

Someone at Facebook will have to make the internal changes necessary to disable strict mode modules for their projects that use non strict mode compatible modules by including a .babelrc file with

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["transform-es2015-modules-commonjs", { "strict": false, "allowTopLevelThis": true }]
  ]
}

before merging this.

We might also want to mention this in the breaking change section for the next release.

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @skevy to be a potential reviewer.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Feb 7, 2016
@janicduplessis
Copy link
Contributor Author

cc @davidaurelio @vjeux

@skevy
Copy link
Contributor

skevy commented Feb 8, 2016

cc @martinbigio

@facebook-github-bot
Copy link
Contributor

@janicduplessis updated the pull request.

1 similar comment
@facebook-github-bot
Copy link
Contributor

@janicduplessis updated the pull request.

@satya164
Copy link
Contributor

Would be awesome to get it merged.

@corbt
Copy link
Contributor

corbt commented Feb 18, 2016

Looks good to me, just need someone at Facebook to make the changes to their .babelrc's as necessary.

@bestander
Copy link
Contributor

@martinbigio this is related to #6403 that I asked you to check.

@janicduplessis
Copy link
Contributor Author

I retested this recently and it need #6255 to be merged to work properly or keep the allowTopLevelThis.

@facebook-github-bot
Copy link
Contributor

@janicduplessis updated the pull request.

@bestander
Copy link
Contributor

@janicduplessis does this need to be rebased?

@facebook-github-bot
Copy link
Contributor

@janicduplessis updated the pull request.

@facebook-github-bot
Copy link
Contributor

@janicduplessis updated the pull request.

@facebook-github-bot
Copy link
Contributor

@janicduplessis updated the pull request.

@janicduplessis
Copy link
Contributor Author

I removed the allowTopLevelThis since it's already addressed in #6255 and rebased on master. All tests should pass now.

@bestander
Copy link
Contributor

@facebook-github-bot shipit

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to Phabricator to review.

@bestander
Copy link
Contributor

@janicduplessis have you tested this with a manual babel transform?

Our apps internally use babel imperatively:

  var result = babel.transform(srcTxt, {
    retainLines: true,
    compact: true,
    comments: false,
    filename: filename,
    presets: ['react-native'],
    plugins: plugins,
    sourceFileName: filename,
    sourceMaps: options.generateSourceMaps,
  });

If I do

var var presetPlugins = require('babel-preset-react-native/plugins');
plugins.push([presetPlugins['babel-plugin-transform-es2015-modules-commonjs'], { strict: false, allowTopLevelThis: true }]);

before calling transform I still get "use strict" injected automatically

@janicduplessis
Copy link
Contributor Author

No I haven't tested that. Did you clear the packager cache to make sure the transforms are all run again?

@janicduplessis
Copy link
Contributor Author

Tested this and it worked:

var presetPlugins = require('babel-preset-react-native/plugins');
var babel = require('babel-core');
var plugins = [];
plugins.push([presetPlugins['babel-plugin-transform-es2015-modules-commonjs'], { strict: false, allowTopLevelThis: true }]);

var result = babel.transform('const a = 0;', {
  retainLines: true,
  compact: true,
  comments: false,
  presets: ['react-native'],
  plugins: plugins,
});

console.log(result.code);

It prints var a=0; when strict is false and "use strict";var a=0; when strict is true.

@bestander
Copy link
Contributor

Thanks a lot, mate.
I'll dig deeper into packager unknowns.

@janicduplessis
Copy link
Contributor Author

Good luck :)

jeffchienzabinet pushed a commit to jeffchienzabinet/react-native that referenced this pull request Mar 21, 2016
Summary:Since facebook#5422 react-native works with strict mode modules but the transform was not updated since Facebook has some non strict mode compatible internal modules. Now that facebook#5214 has landed and it is easy to change the babel config I think we should enable it by default to make es2015 modules spec compliant.

Someone at Facebook will have to make the internal changes necessary to disable strict mode modules for their projects that use non strict mode compatible modules by including a .babelrc file with
``` json
{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["transform-es2015-modules-commonjs", { "strict": false, "allowTopLevelThis": true }]
  ]
}
```
before merging this.

We might also want to mention this in the breaking change section for the next release.
Closes facebook#5796

Differential Revision: D3075802

fb-gh-sync-id: e807b67401107e1e944db38453e254025ce0a6c7
shipit-source-id: e807b67401107e1e944db38453e254025ce0a6c7
@janicduplessis
Copy link
Contributor Author

Ok, we should probably revert this. Not sure what changed in babel but changing a plugin option by defining it a second time doesn't work anymore. Since we transform all the code in node_modules by default there are a lot of issues with libraries that don't support strict mode. Right now the only way to disable it is to not use babel-preset-react-native and define all the transforms manually so it is not very convenient.

Let's wait until there is a supported way to pass options to presets in babel so disabling strict mode is easy. Also I know there was already some discussion about that but shipping react-native and libraries already compiled would help a lot here.

cc @bestander

@bestander
Copy link
Contributor

Sounds good to me.
Will you send a pr?

On Sunday, 27 March 2016, Janic Duplessis notifications@github.com wrote:

Ok, we should probably revert this. Not sure what changed in babel but
changing a plugin option by defining it a second time doesn't work anymore.
Since we transform all the code in node_modules by default there are a lot
of issues with libraries that don't support strict mode. Right now the only
way to disable it is to not use babel-preset-react-native and define all
the transforms manually so it is not very convenient.

Let's wait until there is a supported way to pass options to presets in
babel so disabling strict mode is easy.


You are receiving this because you were assigned.
Reply to this email directly or view it on GitHub
#5796 (comment)

@satya164
Copy link
Contributor

There's always a risk of breaking third party code since we're transpiling things under node_modules. What would be great is if we could precompile RN before distributing (doesn't need to be a bundle, just transpile with babel, and rename source files to *.flow). Then we could avoid transpiling node_modules altogether and anyone can use any preset they want.

@bestander
Copy link
Contributor

Makes sense actually.
So just make it commonjs es5 code?

On Sunday, 27 March 2016, Satyajit Sahoo notifications@github.com wrote:

There's always a risk of breaking third party code since we're transpiling
things under node_modules. What would be great is if we could precompile
RN before distributing (doesn't need to be a bundle, just transpile with
babel, and rename source files to *.flow). Then we could avoid
transpiling node_modules altogether and anyone can use any preset they want.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#5796 (comment)

@satya164
Copy link
Contributor

@bestander Yeah. And keep the original files as *.flow files, so flow works for consumers.

@bestander
Copy link
Contributor

I know David Aurelio wanted to get rid of haste code with transpiling
before npm publish.
You propose the next logical step.

A discussion in Core Contributors group maybe?

On Sunday, 27 March 2016, Satyajit Sahoo notifications@github.com wrote:

@bestander https://github.com/bestander Yeah. And keep the original
files as .flow files, so flow works for consumers.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#5796 (comment)

@satya164
Copy link
Contributor

@bestander Yeah, will post in the group. :)

@janicduplessis
Copy link
Contributor Author

@bestander #6686

ghost pushed a commit that referenced this pull request Mar 29, 2016
Summary:See discussion here #5796 (comment)

**Test plan (required)**
Change the babel-preset-react-native dependency to `"babel-preset-react-native": "file:./babel-preset",` so it uses the local babel-preset instead of the one from npm.
```
./packager/packager.sh --reset-cache
```
open `http://localhost:8081/Examples/UIExplorer/UIExplorerApp.android.bundle?platform=android&dev=true&hot=false&minify=false` and there should not be any added 'strict mode'.
Closes #6686

Reviewed By: mkonicek

Differential Revision: D3103122

Pulled By: bestander

fb-gh-sync-id: 85658ee01bb73f13dacb2b6a48ab121324c13118
fbshipit-source-id: 85658ee01bb73f13dacb2b6a48ab121324c13118
zebulgar pushed a commit to nightingale/react-native that referenced this pull request Jun 18, 2016
Summary:See discussion here facebook#5796 (comment)

**Test plan (required)**
Change the babel-preset-react-native dependency to `"babel-preset-react-native": "file:./babel-preset",` so it uses the local babel-preset instead of the one from npm.
```
./packager/packager.sh --reset-cache
```
open `http://localhost:8081/Examples/UIExplorer/UIExplorerApp.android.bundle?platform=android&dev=true&hot=false&minify=false` and there should not be any added 'strict mode'.
Closes facebook#6686

Reviewed By: mkonicek

Differential Revision: D3103122

Pulled By: bestander

fb-gh-sync-id: 85658ee01bb73f13dacb2b6a48ab121324c13118
fbshipit-source-id: 85658ee01bb73f13dacb2b6a48ab121324c13118
This pull request was closed.
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.

6 participants