sormy / rollup-plugin-smart-asset

Rollup plugin to rebase, inline or copy assets referenced from the code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The deep path is incorrect when set preserveModules to be true

ihupoo opened this issue · comments

I use the keep-imports-with-preserved-modules example and add a folder in src/components 。

The demo is here

See the dist/esm/components/cc/index.js , the import path is incorrect and is same as the import path in dist/esm/components/image.js

Please justify what is the actual value and what is expected.

I add src/components/cc/index.js base on the keep-imports-with-preserved-modules example

import imageSrc from "../../assets/image.txt"

export const imageX = function () {
  return imageSrc
}

The dist directory structure is like this

dist
├─cjs
│  │  index.js
│  └─components
│      │  image.js
│      └─cc
│          index.js
├─esm
│  │  index.js
│  └─components
│      │  image.js
│      └─cc
│         index.js
└─public
    └─assets
        image.txt

The import path in dist/esm/components/cc/index.js is

import imageSrc from '../../public/assets/image.txt';

but the import path I expected is

import imageSrc from '../../../public/assets/image.txt';

The rollupConfig is same as the keep-imports-with-preserved-modules example

import smartAsset from "rollup-plugin-smart-asset"

export default {
  input: "src/index.js",
  preserveModules: true,
  plugins: [
    smartAsset({
      url: "copy",
      keepImport: true,
      useHash: false,
      keepName: true,
      outputDir: "dist/x",
      assetsPath: "../public/assets",
      extensions: [".txt"]
    })
  ],
  output: [
    { dir: "dist/cjs", format: "cjs" },
    { dir: "dist/esm", format: "esm" }
  ]
}

Version: "rollup": "^2.38.0", "rollup-plugin-smart-asset": "^2.1.1"

The workaround is to put assets folder inside src

The plugin doesn't work well when assets are in parent folder from main entry point file that is src/index.js in this example.