roullup 的 @rollup/plugin-alias 无法生效,打包后需要的模块代码也没有引入

使用roullup 打包想使用路径别名, 配置了@rollup/plugin-alias, 想通过‘@’访问到 src , 但配置后使用了别名的模块代码在打包后却丢失了, 具体如下

roullup.config.js

`


import json from 'rollup-plugin-json';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import resolve from 'rollup-plugin-node-resolve';
import alias from '@rollup/plugin-alias';
const path = require('path')


const customResolver = resolve({
  extensions: ['.mjs', '.js', '.jsx', '.json', '.sass', '.scss']
});

export default [
  {
    // 入口文件
    input: 'src/index.js',
    output: [
      {
        file: 'dist/bundle-iife.js',
        format: 'iife',
        name: 'iife'
      }, 
      {
        file: 'dist/bundle-cjs.js',
        format: 'cjs',
        name: "cjs"
      },
      {
        file: 'dist/bundle-umd.js',
        format: 'umd',
        name: "umd"
      }
    ],
    watch: {
      include: 'src/**'
    },
    plugins: [ 
      // 路径别名
      alias({
        entries: [
          { find: '@', replacement: path.resolve(__dirname, '../src') },
        ],
        customResolver
      }),
      resolve(),
      json(),
      commonjs(),
      babel({
        exclude: 'node_modules/**', transpile our source code
        runtimeHelpers: true 
      }),
      typescript({
        declarationDir: true,
        declarationDir: './declare'
      })
    ],
  }
]

`

需要打包的源入口文件:
index.js

`

import { testFn } from '@/module/test.js'

testFn(1, 2)

`

module/test.js
`
export const testFn = (a, b) => a + b
`

打包后结果:

(function (factory) {
    typeof define === 'function' && define.amd ? define(factory) :
    factory();
}((function () { 'use strict';



})));
阅读 197
评论
    0 个回答
    撰写回答

    登录后参与交流、获取后续更新提醒