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 per-root module imports #386

Merged
merged 3 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion editor/components/toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classNames from 'classnames';
* Internal dependencies
*/
import './style.scss';
import Dashicon from '../dashicon';
import Dashicon from 'components/dashicon';

function Toolbar( { controls } ) {
if ( ! controls || ! controls.length ) {
Expand Down
2 changes: 1 addition & 1 deletion editor/header/mode-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { connect } from 'react-redux';
* Internal dependencies
*/
import './style.scss';
import Dashicon from '../../components/dashicon';
import Dashicon from 'components/dashicon';

class ModeSwitcher extends wp.element.Component {
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion editor/inserter/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import Inserter from './';
import Dashicon from '../components/dashicon';
import Dashicon from 'components/dashicon';

class InserterButton extends wp.element.Component {
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion editor/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import './style.scss';
import Dashicon from '../components/dashicon';
import Dashicon from 'components/dashicon';

function Inserter() {
const blocks = wp.blocks.getBlocks();
Expand Down
6 changes: 3 additions & 3 deletions editor/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { connect } from 'react-redux';
/**
* Internal dependencies
*/
import Header from '../header';
import TextEditor from '../modes/text-editor';
import VisualEditor from '../modes/visual-editor';
import Header from 'header';
import TextEditor from 'modes/text-editor';
import VisualEditor from 'modes/visual-editor';

function Layout( { mode } ) {
return (
Expand Down
2 changes: 1 addition & 1 deletion editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
/**
* Internal dependencies
*/
import Toolbar from '../../components/toolbar';
import Toolbar from 'components/toolbar';

function VisualEditorBlock( props ) {
const { block } = props;
Expand Down
2 changes: 1 addition & 1 deletion editor/modes/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { connect } from 'react-redux';
* Internal dependencies
*/
import './style.scss';
import InserterButton from '../../inserter/button';
import InserterButton from 'inserter/button';
import VisualEditorBlock from './block';

function VisualEditor( { blocks } ) {
Expand Down
38 changes: 38 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,47 @@
* External dependencies
*/

const path = require( 'path' );
const glob = require( 'glob' );
const webpack = require( 'webpack' );
const ExtractTextPlugin = require( 'extract-text-webpack-plugin' );

/**
* Webpack plugin that adds the containing directory of each entry point as an
* effective `resolve.modules` root for all module resolution within that path.
*/
class EntryResolvePlugin {
apply( compiler ) {
// Map entries into array of absolute paths
const entryRoots = Object.keys( compiler.options.entry ).map( ( entry ) => {
return path.dirname( path.resolve( compiler.options.entry[ entry ] ) );
} );

compiler.plugin( 'after-resolvers', () => {
compiler.resolvers.normal.apply( {
apply( resolver ) {
resolver.plugin( 'module', ( request, callback ) => {
// Find entry root which contains the requesting path
const resolvePath = entryRoots.find( ( entryRoot ) => {
return request.path.startsWith( entryRoot );
Copy link
Member

Choose a reason for hiding this comment

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

If I'm understanding this correctly, it would break if you had two directories named like prefix and prefix-and-more-stuff - there should be a test for a path separator somewhere.

} );

if ( ! resolvePath ) {
return callback();
}

// Add entry root as resolve base path
resolver.doResolve( 'resolve', Object.assign( {}, request, {
path: resolvePath,
request: './' + request.request
} ), '', callback );
} );
}
} );
} );
}
}

const config = {
entry: {
i18n: './i18n/index.js',
Expand Down Expand Up @@ -55,6 +92,7 @@ const config = {
]
},
plugins: [
new EntryResolvePlugin(),
new ExtractTextPlugin( {
filename: './[name]/build/style.css'
} ),
Expand Down