Skip to content

Commit

Permalink
Throw when using 2 different builds in dev
Browse files Browse the repository at this point in the history
Fixes #6361
  • Loading branch information
mjackson committed Feb 22, 2019
1 parent 130f7ec commit b2c6fa0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/react-router-dom/.size-snapshot.json
Expand Up @@ -14,9 +14,9 @@
}
},
"umd/react-router-dom.js": {
"bundled": 161325,
"minified": 57478,
"gzipped": 16483
"bundled": 162042,
"minified": 57823,
"gzipped": 16684
},
"umd/react-router-dom.min.js": {
"bundled": 97926,
Expand Down
12 changes: 6 additions & 6 deletions packages/react-router/.size-snapshot.json
@@ -1,8 +1,8 @@
{
"esm/react-router.js": {
"bundled": 22391,
"minified": 12617,
"gzipped": 3438,
"bundled": 23114,
"minified": 13111,
"gzipped": 3670,
"treeshaked": {
"rollup": {
"code": 2267,
Expand All @@ -14,9 +14,9 @@
}
},
"umd/react-router.js": {
"bundled": 101474,
"minified": 35902,
"gzipped": 11344
"bundled": 102208,
"minified": 36247,
"gzipped": 11540
},
"umd/react-router.min.js": {
"bundled": 63839,
Expand Down
21 changes: 21 additions & 0 deletions packages/react-router/modules/index.js
@@ -1,3 +1,24 @@
if (__DEV__) {
const global = (1, eval)("this");
const key = "__react_router_build__";
const buildNames = { cjs: "CommonJS", esm: "ES modules", umd: "UMD" };

if (global[key] && global[key] !== process.env.BUILD_FORMAT) {
const initialBuildName = buildNames[global[key]];
const secondaryBuildName = buildNames[process.env.BUILD_FORMAT];

// TODO: Add link to article that explains in detail how to avoid
// loading 2 different builds.
throw new Error(
`You are loading the ${secondaryBuildName} build of React Router ` +
`on a page that is already running the ${initialBuildName} ` +
`build, so things won't work right.`
);
}

global[key] = process.env.BUILD_FORMAT;
}

export { default as MemoryRouter } from "./MemoryRouter";
export { default as Prompt } from "./Prompt";
export { default as Redirect } from "./Redirect";
Expand Down
21 changes: 17 additions & 4 deletions packages/react-router/rollup.config.js
Expand Up @@ -18,7 +18,10 @@ const cjs = [
external: isBareModuleId,
plugins: [
babel({ exclude: /node_modules/ }),
replace({ "process.env.NODE_ENV": JSON.stringify("development") })
replace({
"process.env.NODE_ENV": JSON.stringify("development"),
"process.env.BUILD_FORMAT": JSON.stringify("cjs")
})
]
},
{
Expand All @@ -27,7 +30,10 @@ const cjs = [
external: isBareModuleId,
plugins: [
babel({ exclude: /node_modules/ }),
replace({ "process.env.NODE_ENV": JSON.stringify("production") }),
replace({
"process.env.NODE_ENV": JSON.stringify("production"),
"process.env.BUILD_FORMAT": JSON.stringify("cjs")
}),
uglify()
]
}
Expand All @@ -44,6 +50,7 @@ const esm = [
runtimeHelpers: true,
plugins: [["@babel/transform-runtime", { useESModules: true }]]
}),
replace({ "process.env.BUILD_FORMAT": JSON.stringify("esm") }),
sizeSnapshot()
]
}
Expand Down Expand Up @@ -74,7 +81,10 @@ const umd = [
"node_modules/react-is/index.js": ["isValidElementType"]
}
}),
replace({ "process.env.NODE_ENV": JSON.stringify("development") }),
replace({
"process.env.NODE_ENV": JSON.stringify("development"),
"process.env.BUILD_FORMAT": JSON.stringify("umd")
}),
sizeSnapshot()
]
},
Expand All @@ -100,7 +110,10 @@ const umd = [
"node_modules/react-is/index.js": ["isValidElementType"]
}
}),
replace({ "process.env.NODE_ENV": JSON.stringify("production") }),
replace({
"process.env.NODE_ENV": JSON.stringify("production"),
"process.env.BUILD_FORMAT": JSON.stringify("umd")
}),
sizeSnapshot(),
uglify()
]
Expand Down

0 comments on commit b2c6fa0

Please sign in to comment.