diff --git a/lib/client-assets.php b/lib/client-assets.php index b8f8eb024c973d..5424313886d064 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -1059,7 +1059,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) { $script .= <<

Code is Poetry.

diff --git a/packages/library-export-default-webpack-plugin/package.json b/packages/library-export-default-webpack-plugin/package.json new file mode 100644 index 00000000000000..30e9d662dd715d --- /dev/null +++ b/packages/library-export-default-webpack-plugin/package.json @@ -0,0 +1,35 @@ +{ + "name": "@wordpress/library-export-default-webpack-plugin", + "version": "0.0.1", + "description": "Webpack plugin for exporting default property for selected libraries", + "author": "WordPress", + "license": "GPL-2.0-or-later", + "keywords": [ + "wordpress", + "webpack", + "webpack-plugin" + ], + "homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/library-export-default-webpack-plugin/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": { + "lodash": "4.17.5", + "webpack-sources": "1.1.0" + }, + "devDependencies": { + "rimraf": "2.6.2", + "webpack": "4.8.3" + }, + "peerDependencies": { + "webpack": "^4.0.0" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/library-export-default-webpack-plugin/src/index.js b/packages/library-export-default-webpack-plugin/src/index.js new file mode 100644 index 00000000000000..16b9dfb3eef7e7 --- /dev/null +++ b/packages/library-export-default-webpack-plugin/src/index.js @@ -0,0 +1,36 @@ +/** + * External dependencies + */ +const { includes } = require( 'lodash' ); +const { ConcatSource } = require( 'webpack-sources' ); + +module.exports = class LibraryExportDefaultPlugin { + constructor( entryPointNames ) { + this.entryPointNames = entryPointNames; + } + + apply( compiler ) { + compiler.hooks.compilation.tap( 'LibraryExportDefaultPlugin', ( compilation ) => { + const { mainTemplate, chunkTemplate } = compilation; + + const onRenderWithEntry = ( source, chunk ) => { + if ( ! includes( this.entryPointNames, chunk.name ) ) { + return source; + } + return new ConcatSource( source, '["default"]' ); + }; + + for ( const template of [ mainTemplate, chunkTemplate ] ) { + template.hooks.renderWithEntry.tap( + 'LibraryExportDefaultPlugin', + onRenderWithEntry + ); + } + + mainTemplate.hooks.hash.tap( 'LibraryExportDefaultPlugin', ( hash ) => { + hash.update( 'export property' ); + hash.update( 'default' ); + } ); + } ); + } +}; diff --git a/packages/library-export-default-webpack-plugin/test/fixtures/boo/index.js b/packages/library-export-default-webpack-plugin/test/fixtures/boo/index.js new file mode 100644 index 00000000000000..7a3f6c4baab50a --- /dev/null +++ b/packages/library-export-default-webpack-plugin/test/fixtures/boo/index.js @@ -0,0 +1,3 @@ +export default function boo() { + return 'boo'; +} diff --git a/packages/library-export-default-webpack-plugin/test/fixtures/foo/index.js b/packages/library-export-default-webpack-plugin/test/fixtures/foo/index.js new file mode 100644 index 00000000000000..7e78ec110b1bec --- /dev/null +++ b/packages/library-export-default-webpack-plugin/test/fixtures/foo/index.js @@ -0,0 +1,3 @@ +export default function foo() { + return 'foo'; +} diff --git a/packages/library-export-default-webpack-plugin/test/fixtures/webpack.config.js b/packages/library-export-default-webpack-plugin/test/fixtures/webpack.config.js new file mode 100644 index 00000000000000..b0c48fb101ba4f --- /dev/null +++ b/packages/library-export-default-webpack-plugin/test/fixtures/webpack.config.js @@ -0,0 +1,22 @@ +/** + * Internal dependencies + */ +const LibraryExportDefaultPlugin = require( '../../' ); + +module.exports = { + mode: 'development', + context: __dirname, + entry: { + boo: './boo', + foo: './foo', + }, + output: { + filename: 'build/[name].js', + path: __dirname, + library: [ 'wp', '[name]' ], + libraryTarget: 'global', + }, + plugins: [ + new LibraryExportDefaultPlugin( [ 'boo' ] ), + ], +}; diff --git a/packages/library-export-default-webpack-plugin/test/index.js b/packages/library-export-default-webpack-plugin/test/index.js new file mode 100644 index 00000000000000..83f839791ec91a --- /dev/null +++ b/packages/library-export-default-webpack-plugin/test/index.js @@ -0,0 +1,33 @@ +/** + * External dependencies + */ +const path = require( 'path' ); +const { promisify } = require( 'util' ); +const rimraf = promisify( require( 'rimraf' ) ); +const webpack = promisify( require( 'webpack' ) ); + +/** + * Internal dependencies + */ +const config = require( './fixtures/webpack.config.js' ); + +describe( 'LibraryExportDefaultPlugin', () => { + const buildDir = path.join( __dirname, '/fixtures/build' ); + + beforeAll( async () => { + await rimraf( buildDir ); + await webpack( config ); + } ); + + test( 'the default export of boo entry point is assigned to the library target', () => { + require( './fixtures/build/boo' ); + + expect( window.wp.boo() ).toBe( 'boo' ); + } ); + + test( 'the default export of foo entry point is not assigned to the library target', () => { + require( './fixtures/build/foo' ); + + expect( window.wp.foo.default() ).toBe( 'foo' ); + } ); +} ); diff --git a/webpack.config.js b/webpack.config.js index 87e3e939c64a98..ea455672100dc7 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -3,6 +3,7 @@ */ const ExtractTextPlugin = require( 'extract-text-webpack-plugin' ); const WebpackRTLPlugin = require( 'webpack-rtl-plugin' ); + const { get } = require( 'lodash' ); const { basename } = require( 'path' ); @@ -10,6 +11,7 @@ const { basename } = require( 'path' ); * WordPress dependencies */ const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' ); +const LibraryExportDefaultPlugin = require( './packages/library-export-default-webpack-plugin' ); // Main CSS loader for everything but blocks.. const mainCSSExtractTextPlugin = new ExtractTextPlugin( { @@ -273,6 +275,7 @@ const config = { return path; }, } ), + new LibraryExportDefaultPlugin( [ 'dom-ready' ].map( camelCaseDash ) ), ], stats: { children: false,