-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Packages: Move data module to the package maintained by Lerna #6828
Changes from 9 commits
39e01f4
dcdbcc4
3a6450e
fa597be
f7afc46
decc7b5
fdb107e
889b45f
3dcc655
3eae5a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { isArray, map } = require( 'lodash' ); | ||
const babelPluginTransformReactJSX = require( 'babel-plugin-transform-react-jsx' ); | ||
const babelPresetEnv = require( 'babel-preset-env' ); | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
const babelDefaultConfig = require( '@wordpress/babel-preset-default' ); | ||
|
||
const plugins = map( babelDefaultConfig.plugins, ( plugin ) => { | ||
if ( isArray( plugin ) && plugin[ 0 ] === babelPluginTransformReactJSX ) { | ||
// TODO: It should become the default value when all modules are moved to packages. | ||
return [ babelPluginTransformReactJSX, { pragma: 'createElement' } ]; | ||
} | ||
|
||
return plugin; | ||
} ); | ||
|
||
const babelConfigs = { | ||
main: { | ||
...babelDefaultConfig, | ||
babelrc: false, | ||
plugins, | ||
presets: map( babelDefaultConfig.presets, ( preset ) => { | ||
if ( isArray( preset ) && preset[ 0 ] === babelPresetEnv ) { | ||
return [ babelPresetEnv, Object.assign( | ||
{}, | ||
preset[ 1 ], | ||
{ modules: 'commonjs' } | ||
) ]; | ||
} | ||
return preset; | ||
} ), | ||
}, | ||
module: { | ||
...babelDefaultConfig, | ||
babelrc: false, | ||
plugins, | ||
}, | ||
}; | ||
|
||
function getBabelConfig( environment ) { | ||
return babelConfigs[ environment ]; | ||
} | ||
|
||
module.exports = getBabelConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@wordpress/data", | ||
"version": "0.0.1", | ||
"description": "Data module for WordPress", | ||
"author": "WordPress", | ||
"license": "GPL-2.0-or-later", | ||
"keywords": [ | ||
"wordpress", | ||
"data", | ||
"redux" | ||
], | ||
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/data/README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/WordPress/gutenberg.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/WordPress/gutenberg/issues" | ||
}, | ||
"main": "src/index.js", | ||
"dependencies": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that we are going to move those packages elsewhere one day, let's keep all dependencies listed. I will add them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should be explicit :) If we use |
||
"@wordpress/deprecated": "0.0.1", | ||
"@wordpress/element": "0.0.1", | ||
"@wordpress/is-shallow-equal": "1.0.2", | ||
"equivalent-key-map": "0.2.0", | ||
"lodash": "4.17.5", | ||
"redux": "3.7.2" | ||
}, | ||
"devDependencies": { | ||
"deep-freeze": "0.0.1" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,18 @@ import deprecated from '@wordpress/deprecated'; | |
*/ | ||
import { get } from 'lodash'; | ||
|
||
// Defaults to the local storage. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we default to a broken (error-throwing) state for Node environments where this package is used? It's probably a larger dependency than we'd want, but something like Store.js is a good abstraction for this sort of thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really, you can make it work by using setter method introduced in this PR: import store from 'store';
setPersistenceStorage( store ); It isn't ideal, needs to be documented but should allow us to move forward. I will add the following example in the README file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's tackle it seperately as we don't have any docs for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #7015 to solve it. |
||
let persistenceStorage = window.localStorage; | ||
|
||
/** | ||
* Sets a different persistence storage. | ||
* | ||
* @param {Object} storage Persistence storage. | ||
*/ | ||
export function setPersistenceStorage( storage ) { | ||
persistenceStorage = storage; | ||
} | ||
|
||
/** | ||
* Adds the rehydration behavior to redux reducers. | ||
* | ||
|
@@ -66,7 +78,7 @@ export function withRehydratation( reducer, reducerKey, storageKey ) { | |
*/ | ||
export function loadAndPersist( store, reducer, reducerKey, storageKey ) { | ||
// Load initially persisted value | ||
const persistedString = window.localStorage.getItem( storageKey ); | ||
const persistedString = persistenceStorage.getItem( storageKey ); | ||
if ( persistedString ) { | ||
const persistedState = { | ||
...get( reducer( undefined, { type: '@@gutenberg/init' } ), reducerKey ), | ||
|
@@ -87,7 +99,7 @@ export function loadAndPersist( store, reducer, reducerKey, storageKey ) { | |
if ( newStateValue !== currentStateValue ) { | ||
currentStateValue = newStateValue; | ||
const stateToSave = get( reducer( store.getState(), { type: 'SERIALIZE' } ), reducerKey ); | ||
window.localStorage.setItem( storageKey, JSON.stringify( stateToSave ) ); | ||
persistenceStorage.setItem( storageKey, JSON.stringify( stateToSave ) ); | ||
} | ||
} ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we still need a similar config for babel to generate the Right JS (instead of using the global)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed, I will look into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be fixed with f7afc46.