问题
I'm trying to create an example boilerplate for a library of reusable components in TypeScript, using Ant Design for UI elements and Rollup for bundling.
The Ant Design documentation although useful, doesn't give specific details for configuring Rollup and I've not had any luck finding an example using the same technology stack.
Using information from various online sources I've put together an outline boilerplate and published it to the following GitHub repository
However, the build output is showing a number of warnings from Rollup relating to rewriting references to 'this'. Can anyone suggest changes to my build config to resolve this issue?
(!) `this` has been rewritten to `undefined`
https://rollupjs.org/guide/en#error-this-is-undefined
node_modules\antd\es\affix\index.js
6: import _inherits from "babel-runtime/helpers/inherits";
7: import _typeof from "babel-runtime/helpers/typeof";
8: var __decorate = this && this.__decorate || function (decorators, target, key, desc) {
^
9: var c = arguments.length,
10: r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
...
回答1:
@Steve, I don't know if you already solved this problem, but I found a solution that worked for me using rollup-plugin-babel
:
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import postcss from 'rollup-plugin-postcss';
import typescript from 'rollup-plugin-typescript2';
import url from 'rollup-plugin-url';
import pkg from './package.json';
const antdVars = require('./src/antd-vars');
export default {
input: 'src/index.tsx',
output: [
{
file: pkg.main,
format: 'cjs',
exports: 'named',
sourcemap: true,
},
{
file: pkg.module,
format: 'es',
exports: 'named',
sourcemap: true,
},
],
plugins: [
peerDepsExternal(),
url(),
nodeResolve({
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
}),
typescript({
exclude: ['*.d.ts', '**/*.d.ts', '**/*.story.tsx', '**/*.spec.tsx'],
rollupCommonJSResolveHack: true,
clean: true,
}),
babel({
babelrc: false,
plugins: [['import', { libraryName: 'antd', style: true }]],
extensions: ['.js', '.jsx', '.ts', '.tsx'],
exclude: /\**node_modules\**/,
}),
commonjs({
include: /\**node_modules\**/,
}),
postcss({
extensions: ['.css', '.scss', '.less'],
use: ['sass', ['less', { javascriptEnabled: true, modifyVars: antdVars }]],
}),
],
};
回答2:
The issue was resolve by updating the rollup config to mark the antd dependencies as external using rollup-plugin-peer-deps-external
The updated code can be seen in the Repo on GitHub
来源:https://stackoverflow.com/questions/53338345/how-to-build-a-component-library-using-typescript-ant-design-and-rollup