Skip to content

Commit

Permalink
fix: issue with multiple webpackJsonp & runtime
Browse files Browse the repository at this point in the history
Now every webpack config will have different webpackJsonp and
there will only be a single runtime.

See #13
  • Loading branch information
swashata committed Oct 20, 2018
1 parent ed5edb1 commit 846c2c0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/plugin/src/reactapp/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render } from 'react-dom';
import App from './App';

document.addEventListener('DOMContentLoaded', () => {
console.log('Hello from React');
const entry = document.querySelector('#wpackio-reactapp');
render(<App />, entry);
});
8 changes: 6 additions & 2 deletions packages/scripts/__tests__/config/WebpackConfigHelper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ describe('CreateWebPackConfig', () => {

// getOptimization()
describe('getOptimization', () => {
test('returns undefined when not in use', () => {
test('returns just runtimeChunk when not in use', () => {
const cwc = new WebpackConfigHelper(
projectConfig.files[0],
{
Expand All @@ -427,7 +427,7 @@ describe('CreateWebPackConfig', () => {
'/foo/bar',
false
);
expect(cwc.getOptimization()).toBeUndefined();
expect(cwc.getOptimization()).toEqual({ runtimeChunk: 'single' });
});

test('returns object when in use', () => {
Expand All @@ -444,6 +444,10 @@ describe('CreateWebPackConfig', () => {
false
);
expect(cwc.getOptimization()).not.toBeUndefined();
expect(
(cwc.getOptimization() as webpack.Options.Optimization)
.runtimeChunk
).toBe('single');
expect(cwc.getOptimization()).toMatchSnapshot();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,25 @@ Object {

exports[`CreateWebPackConfig getOptimization returns object when in use 1`] = `
Object {
"runtimeChunk": "single",
"splitChunks": Object {
"cacheGroups": Object {
"default": Object {
"minChunks": 2,
"priority": -20,
"reuseExistingChunk": true,
},
"vendors": Object {
"priority": -10,
"test": /\\[\\\\\\\\\\\\/\\]node_modules\\[\\\\\\\\\\\\/\\]/,
},
},
"chunks": "all",
"maxAsyncRequests": 5,
"maxInitialRequests": 3,
"minChunks": 1,
"minSize": 30000,
"name": true,
},
}
`;
Expand Down Expand Up @@ -186,6 +203,9 @@ A zero setup Webpack Bundler Script for WordPress",

exports[`CreateWebPackConfig getPlugins has proper plugins for dev mode 1`] = `
Array [
TimeFixPlugin {
"watchOffset": 11000,
},
DefinePlugin {
"definitions": Object {
"__WPACKIO__": Object {
Expand Down Expand Up @@ -223,8 +243,5 @@ Array [
WatchMissingNodeModulesPlugin {
"nodeModulesPath": "/foo/bar/node_modules",
},
TimeFixPlugin {
"watchOffset": 11000,
},
]
`;
41 changes: 35 additions & 6 deletions packages/scripts/src/config/WebpackConfigHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ export class WebpackConfigHelper {
// leave blank because we would handle with free variable
// __webpack_public_path__ in runtime.
publicPath: '',
// we need different jsonpFunction, it has to
// be unique for every webpack config, otherwise
// the later will override the previous
// having combination of appName and file.name
// kind of ensures that billions of devs, don't
// override each other!!!!
jsonpFunction: `wpackio${this.config.appName}${
this.file.name
}Jsonp`,
};
// Add the publicPath if it is in devMode
if (this.isDev) {
Expand All @@ -219,7 +228,7 @@ export class WebpackConfigHelper {
*/
public getPlugins(): webpack.Plugin[] {
// Add common plugins
const plugins: webpack.Plugin[] = [
let plugins: webpack.Plugin[] = [
// Define env
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(this.env),
Expand Down Expand Up @@ -266,7 +275,7 @@ export class WebpackConfigHelper {
);
// Add timewatch plugin to avoid multiple successive build
// https://github.com/webpack/watchpack/issues/25
plugins.push(new TimeFixPlugin());
plugins = [new TimeFixPlugin(), ...plugins];
} else {
// Add Production specific plugins
const { bannerConfig } = this.config;
Expand Down Expand Up @@ -462,15 +471,35 @@ ${bannerConfig.copyrightText}${bannerConfig.credit ? creditNote : ''}`,
*/
public getOptimization(): webpack.Options.Optimization | undefined {
const { optimizeSplitChunks } = this.config;
const optimization: webpack.Options.Optimization = {
// We set runtimeChunk to be single
// because user can (and probably should)
// have multiple entry-point on the same page
runtimeChunk: 'single',
};
if (optimizeSplitChunks) {
return {
splitChunks: {
chunks: 'all',
optimization.splitChunks = {
chunks: 'all',
minSize: 30000,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
name: true,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
};
}

return undefined;
return optimization;
}

/**
Expand Down

0 comments on commit 846c2c0

Please sign in to comment.