-
Notifications
You must be signed in to change notification settings - Fork 28k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sharing code between multiple websites #1706
Comments
We tried to transpile inside Next.js with this PR: #1095 So, the current option is to use option 1 and use npm link to link components. |
Thanks for your response. As you can imagine option 1 is too much trouble and completely disrupts the development flow. If anybody else has the same problem, here's my temporary solution, until option 4 (using simple symlinks) is supported or a better solution comes along:
The
Not a huge fan of this solution, but works better than other options. Please let me know if you have any other suggestions. |
I was wondering the exact same thing yesterday. Symlink of shared However, it looks like hard links work. @sean-shirazi have you tried that? Anyway from a developer experience point of view, it should just be possible to |
Yeah actually I thought of using hard links just to find out that we cannot hard link a directory. Hard links are only for files. Also if we commit hard links to git we get multiple copies instead of actual links.
Yeah, I agree. Symlinks might help keep things organized though. |
@sean-shirazi here is another option... const path = require("path");
const webpack = require("webpack");
const moduleAlias = require("module-alias");
const paths = [
{ name: "@react-joi-forms", path: path.resolve(__dirname, "../src") }
];
paths.forEach(module => {
moduleAlias.addAlias(module.name, module.path);
});
module.exports = {
webpack: (config, { dev }) => {
config.resolve.alias = config.resolve.alias || {};
paths.forEach(module => {
config.resolve.alias[module.name] = module.path;
// realign for hot-loader
if (dev) {
config.module.rules[1].include = [module.path];
config.module.rules[3].include.push(module.path);
config.module.rules[5].include.push(module.path);
} else {
config.module.rules[1].include.push(module.path);
config.module.rules[3].include.push(module.path);
}
});
return config;
}
}; where paths is an array of the additional directories you want to include |
@arunoda ˆˆ |
Interesting, thanks. Though I never ran the client code bundled by webpack because the server code always crashed. I think some extra babel configuration is necessary too. Also the files must be watched for HMR. |
Yes this does watch for hmr but you are correct that extra babel work is needed |
@sean-shirazi's unison solution is the most bearable I've found... feels a bit dirty but actually works great. Because unison is two-way you can't even accidentally edit the wrong file. You can even use "scripts": {
"build": "unison ../common common && NODE_ENV=production next build",
"dev": "concurrently \"unison -auto -repeat watch ../common common\" \"next\"",
} |
I'm working on 2 websites that share some code. The 2 websites are using Next and all the code including the shared code must be transpiled and for the client also bundled by webpack. I was wondering what would be the simplest way to do so.
The solutions I can think of are:
file:../shared-lib
). But then after each change in the shared code we have tonpm install
it again. I'm not sure HMR would work either.node_modules/shared-lib
and transpile it. Using npm link would probably solve some of the hassles mentioned above. But Next doesn't seem to support this Add support to transpile modules inside node_modules #706where
website1/pages/index.js
is:and
shared-lib/lib.js
is:With this, I can
npm run build
and run the server but once I open the page I get this on the server:And if I use some es7 features like async function in
shared-lib/lib.js
, thennpm run build
fails with:So clearly the shared code is not being transpiled. Is there a change I can apply in
next.config.js
to fixed this? or do you perhaps recommend a different solution than symlinks?EDIT: possibly related: Tips for local developing of React modules with babel and webpack
The text was updated successfully, but these errors were encountered: