Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade rollup plugins #263

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions package.json
Expand Up @@ -37,15 +37,15 @@
"@babel/plugin-transform-react-jsx": "^7.3.0",
"@babel/preset-env": "^7.3.1",
"@babel/preset-typescript": "^7.1.0",
"@rollup/plugin-buble": "^0.20.0",
"@rollup/plugin-json": "^4.0.0",
"babel-plugin-transform-async-to-promises": "^0.8.4",
"chalk": "^2.4.2",
"ora": "^3.0.0",
"rollup": "^1.1.2",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-buble": "^0.19.6",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-hashbang": "^2.2.2",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-resolve": "^4.2.3",
"rollup-plugin-postcss": "^2.0.3",
"rollup-plugin-replace": "^2.1.0",
Expand All @@ -56,7 +56,6 @@
"@types/babel__generator": "^7.0.1",
"@types/babel__template": "^7.0.1",
"@types/babel__traverse": "^7.0.4",
"@types/boxen": "^2.1.0",
"@types/builtin-modules": "^2.0.0",
"@types/elegant-spinner": "^2.0.1",
"@types/figures": "^2.0.0",
Expand All @@ -75,7 +74,7 @@
"@types/slash": "^2.0.0",
"@types/string-width": "^2.0.0",
"babel-plugin-alter-object-assign": "^1.0.2",
"boxen": "^2.1.0",
"boxen": "^4.1.0",
"builtin-modules": "^3.0.0",
"cac": "^6.4.2",
"commitizen": "^3.0.5",
Expand Down
24 changes: 13 additions & 11 deletions src/index.ts
Expand Up @@ -22,6 +22,7 @@ import nodeResolvePlugin from './plugins/node-resolve'
import configLoader from './config-loader'
import isExternal from './utils/is-external'
import getBanner from './utils/get-banner'
import normalizePluginName from './utils/normalize-plugin-name'
import {
Options,
Config,
Expand Down Expand Up @@ -187,7 +188,8 @@ export class Bundler {
config.plugins.progress
),

json: config.plugins.json !== false && merge({}, config.plugins.json),
'@rollup/json':
config.plugins.json !== false && merge({}, config.plugins.json),

hashbang:
config.plugins.hashbang !== false && merge({}, config.plugins.hashbang),
Expand Down Expand Up @@ -250,7 +252,7 @@ export class Bundler {
config.plugins.babel
),

buble:
'@rollup/buble':
(config.plugins.buble || config.babel.minimal) &&
merge(
{
Expand Down Expand Up @@ -322,21 +324,21 @@ export class Bundler {
return config.resolvePlugins[name]
}

const isBuiltIn = require('../package').dependencies[
`rollup-plugin-${name}`
]
name = normalizePluginName(name)

const isBuiltIn = require('../package').dependencies[name]
const plugin =
name === 'babel'
name === 'rollup-plugin-babel'
? import('./plugins/babel').then(res => res.default)
: name === 'node-resolve'
: name === 'rollup-plugin-node-resolve'
? nodeResolvePlugin
: name === 'progress'
: name === 'rollup-plugin-progress'
? progressPlugin
: isBuiltIn
? require(`rollup-plugin-${name}`)
: this.localRequire(`rollup-plugin-${name}`)
? require(name)
: this.localRequire(name)

if (name === 'terser') {
if (name === 'rollup-plugin-terser') {
return plugin.terser
}

Expand Down
11 changes: 11 additions & 0 deletions src/utils/normalize-plugin-name.ts
@@ -0,0 +1,11 @@
export default function(name: string): string {
// @rollup/alias => @rollup/plugin-alias
// @foo/baz => @foo/rollup-plugin-baz
if (/^@[^/]+\//.test(name)) {
return name.replace(/^@([^/]+)\/(rollup-)?(plugin-)?/, (_, m1) => {
return m1 === 'rollup' ? `@rollup/plugin-` : `@${m1}/rollup-plugin-`
})
}

return name.replace(/^(rollup-plugin-)?/, 'rollup-plugin-')
}
2 changes: 2 additions & 0 deletions test/fixtures/package.json
@@ -1,6 +1,8 @@
{
"private": true,
"dependencies": {
"@foo/rollup-plugin-bar": "../stubs/@foo/rollup-plugin-bar",
"@rollup/plugin-baz": "../stubs/@rollup/plugin-baz",
"rollup-plugin-prettier": "^0.5.0",
"rollup-plugin-strip": "^1.2.0",
"rollup-plugin-typescript2": "^0.18.0",
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/yarn.lock
Expand Up @@ -2,6 +2,12 @@
# yarn lockfile v1


"@foo/rollup-plugin-bar@../stubs/@foo/rollup-plugin-bar":
version "0.0.0"

"@rollup/plugin-baz@../stubs/@rollup/plugin-baz":
version "0.0.0"

"@vue/component-compiler-utils@^2.1.0":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@vue/component-compiler-utils/-/component-compiler-utils-2.3.0.tgz#4f580f1b28fc7685859d87ea0e92a1c0271c93da"
Expand Down
29 changes: 29 additions & 0 deletions test/index.test.ts
@@ -1,5 +1,6 @@
import path from 'path'
import { Bundler, Config, Options } from '../src'
import { RollupConfig } from '../src/types'

process.env.BABEL_ENV = 'anything-not-test'

Expand Down Expand Up @@ -43,6 +44,34 @@ function snapshot(
})
}

test('resolve scoped plugins', async () => {
const getConfig = jest.fn()
await generate(
{
input: 'index.js',
plugins: {
'@foo/bar': { getConfig }
}
},
{ rootDir: fixture('defaults') }
)
expect(getConfig).toHaveBeenCalledWith('@foo/bar')
})

test('resolve @rollup plugins', async () => {
const getConfig = jest.fn()
await generate(
{
input: 'index.js',
plugins: {
'@rollup/baz': { getConfig }
}
},
{ rootDir: fixture('defaults') }
)
expect(getConfig).toHaveBeenCalledWith('baz')
})

snapshot({
title: 'defaults',
input: 'index.js',
Expand Down
6 changes: 6 additions & 0 deletions test/stubs/@foo/rollup-plugin-bar/index.js
@@ -0,0 +1,6 @@
const name = '@foo/bar'

module.exports = ({ getConfig } = {}) => {
getConfig(name)
return { name }
}
6 changes: 6 additions & 0 deletions test/stubs/@foo/rollup-plugin-bar/package.json
@@ -0,0 +1,6 @@
{
"private": true,
"name": "@foo/rollup-plugin-bar",
"version": "0.0.0",
"main": "index.js"
}
6 changes: 6 additions & 0 deletions test/stubs/@rollup/plugin-baz/index.js
@@ -0,0 +1,6 @@
const name = 'baz'

module.exports = ({ getConfig } = {}) => {
getConfig(name)
return { name }
}
6 changes: 6 additions & 0 deletions test/stubs/@rollup/plugin-baz/package.json
@@ -0,0 +1,6 @@
{
"private": true,
"name": "@rollup/plugin-baz",
"version": "0.0.0",
"main": "index.js"
}