Why node can't resolve the absolut path to a folder after bundling the code with Webpack

Viewed 894

I'm trying to build an AWS-CDK library with typescript. To build and bundle my library I use Webpack.

Because it needs some static files which I'm providing in my Project root the Copy-Plugin is also used in the webpack config to put it in the dist folder.

Then I try to reference the imported folder the exact absolute path is not omitted. The console log shows just /swagger-ui and CDK is throwing the following exception:

Error: Cannot find asset at /swagger-ui

This is the code to reference the path to the folder

const swaggerUiPath = path.resolve(__dirname, './swagger-ui')

new s3deploy.BucketDeployment(this, 'swagger-ui', {
  sources: [s3deploy.Source.asset(swaggerUiPath)],
  destinationBucket: apiDocBucket
})

After playing around with different methods of getting the full path to the folder I'm still getting the same issue. Maybe I miss something and Webpack does some magic behind the scenes which I'm not aware of.

Thank you!

webpack.config.js:

const path = require('path');
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
const CopyPlugin = require("copy-webpack-plugin")
const nodeExternals = require('webpack-node-externals')

module.exports = {
  entry: './lib/index.ts',
  target: 'node',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  output: {
    filename: 'index.js',
    path: path.resolve(__dirname, 'dist'),
    libraryTarget: 'commonjs',
    globalObject: 'this',
  },
  plugins: [
    new NodePolyfillPlugin(),
    new CopyPlugin({
        patterns: [
          { from: "swagger-ui", to: "swagger-ui" }
        ],
      }),
  ],
  // in order to ignore all modules in node_modules folder 
  externals: [nodeExternals()],
};

package.json:

{
  "name": "aws-openapi-ui",
  "version": "0.1.0",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "build": "webpack",
    "watch": "tsc -w"
  },
  "devDependencies": {
    "@aws-cdk/assert": "1.114.0",
    "@types/jest": "^26.0.10",
    "@types/jszip": "^3.4.1",
    "@types/node": "10.17.27",
    "@types/swagger-ui": "^3.47.1",
    "copy-webpack-plugin": "^9.0.1",
    "node-polyfill-webpack-plugin": "^1.1.4",
    "ts-loader": "^9.2.3",
    "typescript": "~3.9.7",
    "webpack": "^5.45.1",
    "webpack-cli": "^4.7.2",
    "webpack-node-externals": "^3.0.0"
  },
  "peerDependencies": {
    "@aws-cdk/core": "1.114.0"
  },
  "dependencies": {
    "@aws-cdk/aws-s3": "1.114.0",
    "@aws-cdk/aws-s3-deployment": "1.114.0",
    "@aws-cdk/core": "1.114.0",
    "jszip": "^3.6.0"
  },
  "files": [
    "dist"
  ]
}
1 Answers

Try changing

{ from: "swagger-ui", to: "swagger-ui" }

to

{ from: "swagger-ui", to: path.resolve(__dirname, "dist", "swagger-ui") }

(or similar) for the CopyPlugin.

It looks like it's erroring because you need to copy the swagger-ui folder to the same folder that your output index.js is located at.

Related