-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Fix/improve hot reloading 🚀, support SCSS exports again, and ad…
…d local alias overrides to webpack.dev-stage.config.js (#191) * fix: support js imports of scss exports again * feat: fix hot module replacement, add fast refresh webpack plugin * fix: add local aliases to webpack.dev-stage.config.js
- Loading branch information
1 parent
c3187db
commit 33cab17
Showing
6 changed files
with
143 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
/* | ||
This function reads in a 'module.config.js' file if it exists and uses its contents to define | ||
a set of webpack resolve.alias aliases for doing local development of application dependencies. | ||
It reads the package.json file of the dependency to determine if it has any peer dependencies, and | ||
then forces those peer dependencies to be resolved with the application's version. Primarily, this | ||
is useful for making sure there's only one version of those dependencies loaded at once, which is a | ||
problem with both react and react-intl. | ||
The module.config.js file should have the form: | ||
{ | ||
localModules: [ | ||
{ moduleName: 'nameOfPackage', dir: '../path/to/repo', dist: '/path/to/dist/in/repo' }, | ||
... others... | ||
], | ||
} | ||
Some working examples, as of the time of this writing: | ||
{ moduleName: '@edx/paragon/scss', dir: '../paragon', dist: 'scss' } | ||
{ moduleName: '@edx/paragon', dir: '../paragon', dist: 'dist' } | ||
{ moduleName: '@edx/frontend-platform', dir: '../frontend-platform', dist: 'dist' } | ||
*/ | ||
function getLocalAliases() { | ||
const aliases = {}; | ||
|
||
try { | ||
const moduleConfigPath = path.resolve(process.cwd(), 'module.config.js'); | ||
if (!fs.existsSync(moduleConfigPath)) { | ||
console.log('No local module configuration file found. This is fine.'); | ||
return aliases; | ||
} | ||
// eslint-disable-next-line import/no-dynamic-require, global-require | ||
const { localModules } = require(moduleConfigPath); | ||
|
||
let allPeerDependencies = []; | ||
const excludedPeerPackages = []; | ||
if (localModules.length > 0) { | ||
console.info('Resolving modules from local directories via module.config.js.'); | ||
} | ||
localModules.forEach(({ moduleName, dir, dist = '' }) => { | ||
console.info(`Using local version of ${moduleName} from ${dir}/${dist}.`); | ||
// eslint-disable-next-line import/no-dynamic-require, global-require | ||
const { peerDependencies = {}, name } = require(path.resolve(process.cwd(), dir, 'package.json')); | ||
allPeerDependencies = allPeerDependencies.concat(Object.keys(peerDependencies)); | ||
aliases[moduleName] = path.resolve(process.cwd(), dir, dist); | ||
excludedPeerPackages.push(name); | ||
}); | ||
|
||
allPeerDependencies = allPeerDependencies.filter((dep) => !excludedPeerPackages.includes(dep)); | ||
|
||
allPeerDependencies.forEach((dep) => { | ||
aliases[dep] = path.resolve(process.cwd(), 'node_modules', dep); | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
console.error('Error in module.config.js parsing. module.config.js will be ignored.'); | ||
return {}; | ||
} | ||
return aliases; | ||
} | ||
|
||
module.exports = getLocalAliases; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters