|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fse = require('fs-extra'); |
| 4 | +const path = require('path'); |
| 5 | +const findUp = require('find-up'); |
| 6 | +const glob = require('glob'); |
| 7 | + |
| 8 | +const loadPackageJson = packagePath => { |
| 9 | + try { |
| 10 | + const packageObj = fse.readJsonSync(packagePath); |
| 11 | + return packageObj; |
| 12 | + } catch (err) { |
| 13 | + throw err; |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +const getWorkspacesRootConfig = dir => { |
| 18 | + const packageJsonUp = findUp.sync('package.json', {cwd: dir}); |
| 19 | + |
| 20 | + if (packageJsonUp === null) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + const packageObj = loadPackageJson(packageJsonUp); |
| 25 | + |
| 26 | + if (Reflect.has(packageObj, 'workspaces')) { |
| 27 | + const workspacesRootConfig = { |
| 28 | + root: path.dirname(packageJsonUp), |
| 29 | + workspaces: packageObj.workspaces |
| 30 | + }; |
| 31 | + return workspacesRootConfig; |
| 32 | + } |
| 33 | + |
| 34 | + const dirUp = path.dirname(dir); |
| 35 | + return getWorkspacesRootConfig(dirUp); |
| 36 | +}; |
| 37 | + |
| 38 | +const getPackagePaths = (root, workspacesList) => { |
| 39 | + const packageList = []; |
| 40 | + |
| 41 | + workspacesList.forEach(workspace => { |
| 42 | + const workspaceDir = path.dirname(workspace); |
| 43 | + const workspaceAbsDir = path.join(root, workspaceDir); |
| 44 | + const packageJsonGlob = path.join('**!(node_modules)', 'package.json'); |
| 45 | + const packageJsonAbsPaths = glob |
| 46 | + .sync(packageJsonGlob, {cwd: workspaceAbsDir}) |
| 47 | + .map(pkgPath => path.join(workspaceAbsDir, pkgPath)); |
| 48 | + |
| 49 | + packageList.push(...packageJsonAbsPaths); |
| 50 | + }); |
| 51 | + |
| 52 | + return packageList; |
| 53 | +}; |
| 54 | + |
| 55 | +const getDeep = (obj, keyChain) => { |
| 56 | + const nextKey = keyChain.shift(); |
| 57 | + const has = Reflect.has(obj, nextKey); |
| 58 | + const val = obj[nextKey]; |
| 59 | + |
| 60 | + if (keyChain.length === 0) { |
| 61 | + return val; |
| 62 | + } |
| 63 | + |
| 64 | + if (has) { |
| 65 | + return getDeep(val, keyChain); |
| 66 | + } |
| 67 | + |
| 68 | + return false; |
| 69 | +}; |
| 70 | + |
| 71 | +const resolveBabelLoaderPaths = ({root, workspacesList}, packageEntry) => { |
| 72 | + const packageJsonPaths = getPackagePaths(root, workspacesList); |
| 73 | + const babelLoaderPaths = []; |
| 74 | + |
| 75 | + packageJsonPaths.map(absPkgPath => { |
| 76 | + const packageJson = loadPackageJson(absPkgPath); |
| 77 | + const mainSrcFile = getDeep(packageJson, [packageEntry]); |
| 78 | + |
| 79 | + if (mainSrcFile) { |
| 80 | + const mainSrcPath = path.dirname(mainSrcFile); |
| 81 | + const packageAbsDir = path.dirname(absPkgPath); |
| 82 | + const absSrcPath = path.join(packageAbsDir, mainSrcPath); |
| 83 | + babelLoaderPaths.push(absSrcPath); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + return babelLoaderPaths; |
| 88 | +}; |
| 89 | + |
| 90 | +const loadAppSettings = appPackageJson => { |
| 91 | + const result = {workspaces: {}, dependencies: {}}; |
| 92 | + |
| 93 | + const appPackageObj = loadPackageJson(appPackageJson); |
| 94 | + |
| 95 | + const dependencies = getDeep(appPackageObj, ['dependencies']); |
| 96 | + const devDependencies = getDeep(appPackageObj, ['devDependencies']); |
| 97 | + |
| 98 | + if (!dependencies && !devDependencies) return result; |
| 99 | + |
| 100 | + if (dependencies) { |
| 101 | + result.dependencies = Object.assign(result.dependencies, dependencies); |
| 102 | + } |
| 103 | + |
| 104 | + if (devDependencies) { |
| 105 | + result.dependencies = Object.assign( |
| 106 | + result.dependencies, |
| 107 | + devDependencies |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + const reactScripts = getDeep(appPackageObj, ['react-scripts']); |
| 112 | + if (!reactScripts) return result; |
| 113 | + |
| 114 | + const workspaces = getDeep(reactScripts, ['workspaces']); |
| 115 | + result.workspaces = workspaces; |
| 116 | + if (!workspaces) return result; |
| 117 | + |
| 118 | + return workspaces; |
| 119 | +}; |
| 120 | + |
| 121 | +const guard = (appDirectory, appPackageJson) => { |
| 122 | + if (!appDirectory) { |
| 123 | + throw new Error('appDirectory not provided'); |
| 124 | + } |
| 125 | + |
| 126 | + if (typeof appDirectory !== 'string') { |
| 127 | + throw new Error('appDirectory should be a string'); |
| 128 | + } |
| 129 | + |
| 130 | + if (!appPackageJson) { |
| 131 | + throw new Error('appPackageJson not provided'); |
| 132 | + } |
| 133 | + |
| 134 | + if (typeof appPackageJson !== 'string') { |
| 135 | + throw new Error('appPackageJson should be a string'); |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +const getPkg = path => { |
| 140 | + const pkgPath = findUp.sync('package.json', {cwd: path}); |
| 141 | + const pkg = loadPackageJson(pkgPath); |
| 142 | + return pkg; |
| 143 | +}; |
| 144 | + |
| 145 | +const getDeps = pkg => { |
| 146 | + const deps = getDeep(pkg, ['dependencies']); |
| 147 | + const devDeps = getDeep(pkg, ['devDependencies']); |
| 148 | + |
| 149 | + let dependencies = {}; |
| 150 | + |
| 151 | + if (deps) { |
| 152 | + dependencies = Object.assign(dependencies, deps); |
| 153 | + } |
| 154 | + |
| 155 | + if (devDeps) { |
| 156 | + dependencies = Object.assign(dependencies, devDeps); |
| 157 | + } |
| 158 | + |
| 159 | + return dependencies; |
| 160 | +}; |
| 161 | + |
| 162 | +const depsTable = {}; |
| 163 | + |
| 164 | +const filterDeps = deps => |
| 165 | + Reflect.ownKeys(deps).filter(dep => Reflect.has(depsTable, dep)); |
| 166 | + |
| 167 | +const filterDepsTable = () => { |
| 168 | + Reflect.ownKeys(depsTable).forEach(depName => { |
| 169 | + const depsList = depsTable[depName].deps; |
| 170 | + const workspacesOnlyDeps = filterDeps(depsList); |
| 171 | + depsTable[depName].deps = workspacesOnlyDeps; |
| 172 | + }); |
| 173 | +}; |
| 174 | + |
| 175 | +const buildDepsTable = srcPaths => { |
| 176 | + srcPaths.forEach(path => { |
| 177 | + const pkg = getPkg(path); |
| 178 | + const name = pkg.name; |
| 179 | + const deps = getDeps(pkg); |
| 180 | + depsTable[name] = {path, deps}; |
| 181 | + }); |
| 182 | +}; |
| 183 | + |
| 184 | +const filterSrcPaths = (srcPaths, dependencies) => { |
| 185 | + const filteredPaths = []; |
| 186 | + |
| 187 | + srcPaths.forEach(path => { |
| 188 | + const pkg = getPkg(path); |
| 189 | + |
| 190 | + if (dependencies && Reflect.has(dependencies, pkg.name)) { |
| 191 | + filteredPaths.push(path); |
| 192 | + |
| 193 | + const subDeps = depsTable[pkg.name].deps; |
| 194 | + const subPaths = filterSrcPaths(srcPaths, subDeps); |
| 195 | + filteredPaths.push(...subPaths); |
| 196 | + } |
| 197 | + }); |
| 198 | + |
| 199 | + return filteredPaths; |
| 200 | +}; |
| 201 | + |
| 202 | +const init = paths => { |
| 203 | + guard(paths.appPath, paths.appPackageJson); |
| 204 | + |
| 205 | + const config = { |
| 206 | + root: null, |
| 207 | + paths: [], |
| 208 | + packageEntry: 'main:src', |
| 209 | + development: true, |
| 210 | + production: true |
| 211 | + }; |
| 212 | + |
| 213 | + const {root, workspaces} = getWorkspacesRootConfig(paths.appPath); |
| 214 | + const workspacesList = []; |
| 215 | + |
| 216 | + // Normally "workspaces" in package.json is an array |
| 217 | + if (Array.isArray(workspaces)) { |
| 218 | + workspacesList.push(...workspaces); |
| 219 | + } |
| 220 | + |
| 221 | + // Sometimes "workspaces" in package.json is an object |
| 222 | + // with a ".packages" sub-array, eg: when used with "nohoist" |
| 223 | + // See: https://yarnpkg.com/blog/2018/02/15/nohoist |
| 224 | + if (workspaces && !Array.isArray(workspaces) && Reflect.has(workspaces, 'packages')) { |
| 225 | + workspacesList.push(...workspaces.packages); |
| 226 | + } |
| 227 | + |
| 228 | + if (workspacesList.length === 0) { |
| 229 | + return config; |
| 230 | + } |
| 231 | + console.log('Yarn Workspaces paths detected.'); |
| 232 | + config.root = root; |
| 233 | + |
| 234 | + const appSettings = loadAppSettings(paths.appPackageJson); |
| 235 | + |
| 236 | + if (Reflect.has(appSettings.workspaces, 'development')) { |
| 237 | + config.development = appSettings.workspaces.development ? true : false; |
| 238 | + } |
| 239 | + |
| 240 | + if (Reflect.has(appSettings.workspaces, 'production')) { |
| 241 | + config.production = appSettings.workspaces.production ? true : false; |
| 242 | + } |
| 243 | + |
| 244 | + if (Reflect.has(appSettings.workspaces, 'package-entry')) { |
| 245 | + config.packageEntry = appSettings.workspaces['package-entry']; |
| 246 | + } |
| 247 | + |
| 248 | + const babelSrcPaths = resolveBabelLoaderPaths( |
| 249 | + {root, workspacesList}, |
| 250 | + config.packageEntry |
| 251 | + ); |
| 252 | + |
| 253 | + buildDepsTable(babelSrcPaths); |
| 254 | + |
| 255 | + const applicableSrcPaths = filterSrcPaths( |
| 256 | + babelSrcPaths, |
| 257 | + appSettings.dependencies, |
| 258 | + paths.appPath |
| 259 | + ); |
| 260 | + |
| 261 | + console.log( |
| 262 | + `Found ${babelSrcPaths.length} path(s) with "${ |
| 263 | + config.packageEntry |
| 264 | + }" entry.` |
| 265 | + ); |
| 266 | + |
| 267 | + if (applicableSrcPaths.length > 0) { |
| 268 | + config.paths.push(...applicableSrcPaths); |
| 269 | + } |
| 270 | + |
| 271 | + console.log('Exporting Workspaces config to Webpack.'); |
| 272 | + console.log(config); |
| 273 | + return config; |
| 274 | +}; |
| 275 | + |
| 276 | +module.exports = { |
| 277 | + init |
| 278 | +}; |
0 commit comments