Skip to content

Commit

Permalink
Making UtilScript Optional And dynamically importing libphonenumber.j…
Browse files Browse the repository at this point in the history
…s if utilscript is not there (#224)

utilScript optional
  • Loading branch information
superhit0 committed Jul 15, 2018
1 parent bda8e94 commit bb09300
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
10 changes: 7 additions & 3 deletions __tests__/FlagDropDown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names
};
const subject = await this.makeSubject();

expect(subject.state().countryCode).toBe('jp');
window.localStorage.clear();
subject.instance().utilsScriptDeferred.then(() => {
expect(subject.state().countryCode).toBe('jp');
window.localStorage.clear();
});
});

it('should has .separate-dial-code class when with separateDialCode = true', () => {
Expand Down Expand Up @@ -160,7 +162,9 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names
};
const subject = await this.makeSubject();

expect(subject.state().countryCode).toBe('jp');
subject.instance().utilsScriptDeferred.then(() => {
expect(subject.state().countryCode).toBe('jp');
});
});

describe('with original ReactTestUtils', () => {
Expand Down
12 changes: 11 additions & 1 deletion config/babel.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ module.exports = {
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in OS temporary directory for faster rebuilds.
cacheDirectory: true,
env: {
test: {
plugins: [
// to enable dynamic import in tests
require.resolve('babel-plugin-dynamic-import-node')
]
}
},
presets: [
// Latest stable ECMAScript features
['latest'],
['env'],
// JSX, Flow
require.resolve('babel-preset-react'),
],
Expand All @@ -17,6 +25,8 @@ module.exports = {
require.resolve('babel-plugin-transform-class-properties'),
// { ...todo, completed: true }
require.resolve('babel-plugin-transform-object-rest-spread'),
// to enable import() [dynamic import]
require.resolve('babel-plugin-syntax-dynamic-import'),
// function* () { yield 42; yield 43; }
[require.resolve('babel-plugin-transform-regenerator'), {
// Async functions are converted to generators by babel-preset-latest
Expand Down
4 changes: 3 additions & 1 deletion config/babel.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
babelrc: false,
presets: [
// Latest stable ECMAScript features
['latest', { es2015: { 'modules': false } }],
['env', { es2015: { 'modules': false } }],
// JSX, Flow
require.resolve('babel-preset-react')
],
Expand Down Expand Up @@ -36,5 +36,7 @@ module.exports = {
// * https://phabricator.babeljs.io/search/query/pCNlnC2xzwzx/
// TODO: Enable again when these issues are resolved.
// require.resolve('babel-plugin-transform-react-constant-elements')
// to enable import() [dynamic import]
require.resolve('babel-plugin-syntax-dynamic-import'),
]
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"babel-plugin-transform-react-remove-prop-types": "^0.4.13",
"babel-plugin-transform-regenerator": "^6.21.0",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-latest": "^6.16.0",
"babel-plugin-dynamic-import-node": "^2.0.0",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-preset-env": "^1.1.8",
"babel-preset-react": "^6.16.0",
"babel-preset-react-app": "^2.0.1",
"babel-runtime": "^6.20.0",
Expand Down
5 changes: 4 additions & 1 deletion src/components/IntlTelInputApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@ class IntlTelInputApp extends Component {
});
}
} else {
this.utilsScriptDeferred.resolve();
import('../libphonenumber').then(() => {
this.loadUtils();
this.utilsScriptDeferred.resolve();
});
}

if (this.tempCountry === 'auto') {
Expand Down
2 changes: 0 additions & 2 deletions src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class DemoComponent extends Component {
value={ this.state.phone1 }
geoIpLookup={ lookup }
css={ ['intl-tel-input', 'form-control'] }
utilsScript="libphonenumber.js"
format
/>
<div>Phone Number: {this.state.phone1 }</div>
Expand All @@ -91,7 +90,6 @@ class DemoComponent extends Component {
defaultCountry={ 'jp' }
value={ this.state.phone2 }
css={ ['intl-tel-input', 'form-control'] }
utilsScript="libphonenumber.js"
/>
<div>Phone Number: {this.state.phone2}</div>
</div>
Expand Down

0 comments on commit bb09300

Please sign in to comment.