Skip to content

Commit 7a3f5b7

Browse files
arcanisgaearon
authored andcommitted
Plug'n'Play support (facebook#5136)
* Adds the PnP plugin for Webpack to find dependencies when working under PnP * Adds configuration for jest * Adds an e2e test for when using PnP * Avoids cra from crashing at the engine check * Avoids cra from crashing when initializing react-scripts * Makes the ownPath portable * Fixes linting * Bumps to [email protected], removes symlinks: false * Adds a --use-pnp option * Pin version
1 parent b547967 commit 7a3f5b7

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

config/webpack.config.dev.js

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
const path = require('path');
1212
const webpack = require('webpack');
13+
const PnpWebpackPlugin = require('pnp-webpack-plugin');
1314
const HtmlWebpackPlugin = require('html-webpack-plugin');
1415
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
1516
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
@@ -150,6 +151,9 @@ module.exports = {
150151
'react-native': 'react-native-web',
151152
},
152153
plugins: [
154+
// Adds support for installing with Plug'n'Play, leading to faster installs and adding
155+
// guards against forgotten dependencies and such.
156+
PnpWebpackPlugin,
153157
// Prevents users from importing files from outside of src/ (or node_modules/).
154158
// This often causes confusion because we only process files within src/ with babel.
155159
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
@@ -158,6 +162,13 @@ module.exports = {
158162
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
159163
],
160164
},
165+
resolveLoader: {
166+
plugins: [
167+
// Also related to Plug'n'Play, but this time it tells Webpack to load its loaders
168+
// from the current package.
169+
PnpWebpackPlugin.moduleLoader(module),
170+
],
171+
},
161172
module: {
162173
strictExportPresence: true,
163174
rules: [

config/webpack.config.prod.js

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
const path = require('path');
1212
const webpack = require('webpack');
13+
const PnpWebpackPlugin = require('pnp-webpack-plugin');
1314
const HtmlWebpackPlugin = require('html-webpack-plugin');
1415
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
1516
const TerserPlugin = require('terser-webpack-plugin');
@@ -214,6 +215,9 @@ module.exports = {
214215
'react-native': 'react-native-web',
215216
},
216217
plugins: [
218+
// Adds support for installing with Plug'n'Play, leading to faster installs and adding
219+
// guards against forgotten dependencies and such.
220+
PnpWebpackPlugin,
217221
// Prevents users from importing files from outside of src/ (or node_modules/).
218222
// This often causes confusion because we only process files within src/ with babel.
219223
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
@@ -222,6 +226,13 @@ module.exports = {
222226
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
223227
],
224228
},
229+
resolveLoader: {
230+
plugins: [
231+
// Also related to Plug'n'Play, but this time it tells Webpack to load its loaders
232+
// from the current package.
233+
PnpWebpackPlugin.moduleLoader(module),
234+
],
235+
},
225236
module: {
226237
strictExportPresence: true,
227238
rules: [

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@
4747
"html-webpack-plugin": "4.0.0-alpha.2",
4848
"identity-obj-proxy": "3.0.0",
4949
"jest": "23.6.0",
50+
"jest-pnp-resolver": "1.0.1",
51+
"jest-resolve": "23.6.0",
5052
"mini-css-extract-plugin": "0.4.3",
5153
"optimize-css-assets-webpack-plugin": "5.0.1",
54+
"pnp-webpack-plugin": "1.1.0",
5255
"postcss-flexbugs-fixes": "4.1.0",
5356
"postcss-loader": "3.0.0",
5457
"postcss-preset-env": "6.0.6",

scripts/init.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ module.exports = function(
8181
originalDirectory,
8282
template
8383
) {
84-
const ownPackageName = require(path.join(__dirname, '..', 'package.json'))
85-
.name;
86-
const ownPath = path.join(appPath, 'node_modules', ownPackageName);
84+
const ownPath = path.dirname(
85+
require.resolve(path.join(__dirname, '..', 'package.json'))
86+
);
8787
const appPackage = require(path.join(appPath, 'package.json'));
8888
const useYarn = fs.existsSync(path.join(appPath, 'yarn.lock'));
8989

scripts/utils/createJestConfig.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module.exports = (resolve, rootDir, isEjecting) => {
2222
// in Jest configs. We need help from somebody with Windows to determine this.
2323
const config = {
2424
collectCoverageFrom: ['src/**/*.{js,jsx}'],
25-
setupFiles: ['react-app-polyfill/jsdom'],
25+
resolver: require.resolve('jest-pnp-resolver'),
26+
setupFiles: [require.resolve('react-app-polyfill/jsdom')],
2627
setupTestFrameworkScriptFile: setupTestsFile,
2728
testMatch: [
2829
'<rootDir>/src/**/__tests__/**/*.{js,jsx}',

0 commit comments

Comments
 (0)