ckeditor / ckeditor5-package-generator

A tool for creating a new package for CKEditor 5.
Other
6 stars 9 forks source link

`getThemePath()` fails when a generated package is located in a mono-repo #132

Open pomek opened 1 year ago

pomek commented 1 year ago

📝 Provide detailed reproduction steps (if any)

  1. Create a mono-repo (and use workspaces).
  2. Inside the mono-repo, generate a new package.
  3. Then, install dependencies.
  4. Then, execute yarn start

✔️ Expected result

It does not throw any errors.

❌ Actual result

$ ckeditor5-package-tools start
node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module '/Users/pomek/Projects/.../packages/ckeditor5-foo/node_modules/@ckeditor/ckeditor5-theme-lark/package.json'

❓ Possible solution

const packagePath = require.resolve( '@ckeditor/ckeditor5-theme-lark/package.json' );
const packageJson = require( packagePath );
pomek commented 1 year ago

The same applies to https://github.com/ckeditor/ckeditor5-package-generator/blob/8f40903157ece7a32c16722645b672d91f8b8c03/packages/ckeditor5-package-tools/lib/utils/get-webpack-config-dll.js#L30-L31

Fix:

// An absolute path to the manifest file that the `DllReferencePlugin` plugin uses for mapping dependencies.
const ckeditor5manifestPath = require.resolve( 'ckeditor5/build/ckeditor5-dll.manifest.json' );
klonwar commented 7 months ago

TLDR: Can be fixed using yarn patch and changing getThemePath function from lib/utils:

const packageJsonPath = require.resolve( '@ckeditor/ckeditor5-theme-lark/package.json');
const packageJson = require(packageJsonPath);
const packagePath = path.join(packageJsonPath, '..');

There are two workarounds:

  1. The first is to disable hoisting for the package:

"packages/ckeditor5-whatever/package.json

"installConfig": {
    "hoistingLimits": "dependencies"
}

But this is very bad as dependencies are duplicated during installation

  1. The second is to use yarn patch

Tried to solve this issue using yarn patch command and applying your fixes. It really works. but still somethig is wrong: image

Seems like some theme deps still not loaded correctly

UPD: The reason is that original path.join( cwd, 'node_modules', '@ckeditor', 'ckeditor5-theme-lark' ); results in package dir path, while require.resolve( '@ckeditor/ckeditor5-theme-lark/package.json' ); results in package.json path.

The correct fix will be

module.exports = function getThemePath( cwd ) {
  const packageJsonPath = require.resolve( '@ckeditor/ckeditor5-theme-lark/package.json');
  const packageJson = require(packageJsonPath);
  const packagePath = path.join(packageJsonPath, '..');

  return path.join( packagePath, packageJson.main );
};

P.S. Trying to resolve require.resolve( '@ckeditor/ckeditor5-theme-lark'); gives .../node_modules/@ckeditor/ckeditor5-theme-lark/theme/theme.css so this is not an option

Hope this will be in release someday