Skip to content

Commit

Permalink
add pnp-webpack-plugin plugin to our default wp config
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Luo committed Jul 21, 2021
1 parent 6d3ec2d commit 7333a7b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5,545 deletions.
28 changes: 28 additions & 0 deletions npm/webpack-batteries-included-preprocessor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ const addTypeScriptConfig = (file, options) => {
options.__typescriptSupportAdded = true
}

/**
* Config yarn pnp plugin for webpack 4
* @param {*} file file to be processed
* @param {*} options
*/
const addYarnPnpConfig = (file, options) => {
const { makeResolver } = require('pnp-webpack-plugin/resolver')
const findPnpApi = require('module').findPnpApi

if (findPnpApi && file.filePath) {
const pnpapi = findPnpApi(file.filePath)

if (pnpapi) {
const PnpPlugin = {
apply: makeResolver({ pnpapi }),
}

options.webpackOptions.resolve.plugins.push(PnpPlugin)
}
}
}

const getDefaultWebpackOptions = () => {
return {
mode: 'development',
Expand Down Expand Up @@ -125,6 +147,7 @@ const getDefaultWebpackOptions = () => {
'repl': require.resolve('./empty'),
'tls': require.resolve('./empty'),
},
plugins: [],
},
}
}
Expand All @@ -143,6 +166,11 @@ const preprocessor = (options = {}) => {
addTypeScriptConfig(file, options)
}

if (process.versions.pnp) {
// pnp path
addYarnPnpConfig(file, options)
}

return webpackPreprocessor(options)(file)
}
}
Expand Down
3 changes: 2 additions & 1 deletion npm/webpack-batteries-included-preprocessor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"babel-plugin-add-module-exports": "^1.0.2",
"coffee-loader": "^0.9.0",
"coffeescript": "^1.12.7",
"pnp-webpack-plugin": "^1.7.0",
"ts-loader": "^8.0.2",
"tsconfig-package": "npm:tsconfig@^7.0.0",
"tsconfig-paths-webpack-plugin": "^3.3.0",
Expand Down Expand Up @@ -65,4 +66,4 @@
"publishConfig": {
"access": "public"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
const wp = require('@cypress/webpack-preprocessor')
const PnpWebpackPlugin = require('pnp-webpack-plugin')

const options = {
webpackOptions: {
module: {
rules: [
{
test: /\.(j|t)sx?$/,
use: [{
loader: require.resolve('babel-loader'),
options: {
presets: [require.resolve('@babel/preset-env'), require.resolve('@babel/preset-typescript')],
},
}],
},
],
},
resolve: {
extensions: ['.js', '.ts'],
plugins: [
PnpWebpackPlugin,
],
},
resolveLoader: {
plugins: [
PnpWebpackPlugin.moduleLoader(module),
],
},

},
}
import * as head from 'lodash/head'

// Default Cypress plugin function
export default (on, config) => {
on('file:preprocessor', wp(options))
// make sure plugin can access dependencies
head([1, 2, 3])

return config
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/core": "^7.14.6",
"@babel/preset-env": "^7.14.7",
"@babel/preset-typescript": "^7.14.5",
"@cypress/webpack-preprocessor": "^5.9.1",
"babel-loader": "^8.0.6",
"pnp-webpack-plugin": "^1.6.4",
"typescript": "^4.2.4",
"webpack": "^4"
"typescript": "^4.2.4"
},
"license": "MIT"
}
Loading

0 comments on commit 7333a7b

Please sign in to comment.