arcanis / pnp-webpack-plugin

Transparently adds support for Plug'n'Play to Webpack
263 stars 20 forks source link

"pnpMode: loose" doesn't work #25

Open MrEfrem opened 4 years ago

MrEfrem commented 4 years ago

.yarnrc.yml contains pnpMode: loose. I checked Webpack@4 with this plugin. Moreover, I checked webpack@next without it. In both cases I saw the same errors:

ERROR in ./.yarn/cache/pg-npm-7.18.2-f1ee9eb2c6-3.zip/node_modules/pg/lib/native/client.js
Module not found: Error: pg tried to access pg-native, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: pg-native (via "pg-native")
Required by: pg@npm:7.18.2 (via /Users/aleksandr/WebProjects/myproject/.yarn/cache/pg-npm-7.18.2-f1ee9eb2c6-3.zip/node_modules/pg/lib/native/client.js)

 @ ./.yarn/cache/pg-npm-7.18.2-f1ee9eb2c6-3.zip/node_modules/pg/lib/native/client.js 11:13-33
 @ ./.yarn/cache/pg-npm-7.18.2-f1ee9eb2c6-3.zip/node_modules/pg/lib/native/index.js
 @ ./.yarn/cache/pg-npm-7.18.2-f1ee9eb2c6-3.zip/node_modules/pg/lib/index.js
 @ ./.yarn/$$virtual/knex-virtual-69b867801e/0/cache/knex-npm-0.20.13-4602ef31f9-3.zip/node_modules/knex/lib/dialects/postgres/index.js
 @ ./.yarn/$$virtual/knex-virtual-69b867801e/0/cache/knex-npm-0.20.13-4602ef31f9-3.zip/node_modules/knex/lib/dialects sync ^\.\/.*\/index\.js$
 @ ./.yarn/$$virtual/knex-virtual-69b867801e/0/cache/knex-npm-0.20.13-4602ef31f9-3.zip/node_modules/knex/lib/knex.js
 @ ./.yarn/$$virtual/knex-virtual-69b867801e/0/cache/knex-npm-0.20.13-4602ef31f9-3.zip/node_modules/knex/lib/index.js
 @ ./.yarn/$$virtual/knex-virtual-69b867801e/0/cache/knex-npm-0.20.13-4602ef31f9-3.zip/node_modules/knex/knex.js
 @ ./src/index.js

The webpack config:

import fs from 'fs';

import webpack from 'webpack';
import nodeExternals from 'webpack-node-externals';
import PnpWebpackPlugin from 'pnp-webpack-plugin';

export default (env) => {
  return new Promise((resolve, reject) => {
    const config = {
      mode: env.WATCH ? 'development' : 'production',
      target: 'node',
      output: {
        path: env.DIST,
        filename: 'index.js',
        libraryTarget: 'commonjs2',
      },
      optimization: {
        minimize: false,
      },
      resolve: {
        plugins: [PnpWebpackPlugin],
      },
      resolveLoader: {
        plugins: [PnpWebpackPlugin.moduleLoader(module)],
      },
      externals: [nodeExternals()],
      module: {
        rules: [
          {
            test: /\.js$/,
            include: [env.SRC, `${env.ROOT}/config`],
            use: [
              {
                loader: 'babel-loader',
                options: {
                  babelrc: false,
                  presets: [
                    [
                      '@babel/env',
                      {
                        targets: {
                          node: 10,
                        },
                        modules: false,
                        useBuiltIns: 'usage',
                        corejs: 3,
                      },
                    ],
                  ],
                },
              },
            ],
          },
        ],
      },
    };

    return webpack(/** @type {import('webpack').Configuration} */ (config)).run(
      (/** @type {Error & { details?: string }} */ error, stats) => {
        if (error) {
          if (error.details) {
            console.error(error.details);
          }
          return reject(error.stack || error);
        }
        if (stats.hasErrors() || stats.hasWarnings()) {
          return reject(
            stats.toString({
              colors: true,
            })
          );
        }
        return resolve();
      }
    );
  });
};

But if I do transpilation via yarn babel src -d <directory> I don't see these errors. I checked with the latest version from sources (yarn set version from sources).

MrEfrem commented 4 years ago

If I set in the webpack config mode: none or mode: development I can compile sources but I still see those messages as ERROR (not as WARNING). And I tried with and without pnpMode: loose. Results are the same. I.e. this option doesn't change anything.

MrEfrem commented 4 years ago

@arcanis KnexJs contains the same packages in both sections: peerDependencies (https://github.com/knex/knex/blob/master/package.json#L51) and peerDependenciesMeta (https://github.com/knex/knex/blob/master/package.json#L58).

"peerDependencies": {
    "mssql": "^6.2.0",
    "mysql": "^2.18.1",
    "mysql2": "^2.1.0",
    "pg": "^8.0.3",
    "sqlite3": "^4.1.1"
  },
  "peerDependenciesMeta": {
    "mssql": {
      "optional": true
    },
    "mysql": {
      "optional": true
    },
    "mysql2": {
      "optional": true
    },
    "pg": {
      "optional": true
    },
    "sqlite3": {
      "optional": true
    }
  }

Can it be the reason of so errors?