1 |
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const path = require('path')
|
6 |
|
7 |
|
8 | function req (name) {
|
9 | return require(name)
|
10 | }
|
11 |
|
12 | function babelRequire (type, name) {
|
13 | let mod
|
14 | if (!name.match(/^babel-(preset|plugin)-/)) {
|
15 | try {
|
16 | mod = req('babel-' + type + '-' + name)
|
17 | }
|
18 | catch (err) {}
|
19 | }
|
20 | return mod
|
21 | }
|
22 |
|
23 |
|
24 | module.exports = function (presetInput, modifications) {
|
25 | modifications = modifications || {}
|
26 | const nameDrops = modifications.nameDrops !== false
|
27 |
|
28 | let preset
|
29 |
|
30 | if (typeof presetInput === 'string') {
|
31 | if (!presetInput.match(/(^babel-preset-|\/)/)) {
|
32 | try {
|
33 | preset = require.resolve('babel-preset-' + presetInput)
|
34 | }
|
35 | catch (err) {
|
36 | console.log(err)
|
37 | }
|
38 | }
|
39 | if (!preset) {
|
40 | preset = require.resolve(presetInput)
|
41 | }
|
42 | }
|
43 |
|
44 | const originalConfig = require(preset)
|
45 | const pathRoot = path.dirname(preset)
|
46 | const config = {}
|
47 |
|
48 | for (const key in originalConfig) {
|
49 | if (originalConfig.hasOwnProperty(key)) {
|
50 | config[key] = originalConfig[key]
|
51 | }
|
52 | }
|
53 |
|
54 | const plugins = config.plugins = (config.plugins || []).slice()
|
55 |
|
56 | function getPlugin (name) {
|
57 | let mod
|
58 | try {
|
59 | mod = babelRequire('plugin', name, pathRoot)
|
60 | }
|
61 | catch (err) {}
|
62 |
|
63 | if (!mod) {
|
64 | try {
|
65 | mod = babelRequire('plugin', name)
|
66 | }
|
67 | catch (err2) {}
|
68 | }
|
69 |
|
70 | return mod
|
71 | }
|
72 |
|
73 | function isSameName (a, b) {
|
74 | if (typeof a !== 'string' || typeof b !== 'string') return false
|
75 | return a.replace(/^babel-plugin-/i, '').toLowerCase() === b.replace(/^babel-plugin-/i, '').toLowerCase()
|
76 | }
|
77 |
|
78 | function indexOf (list, name) {
|
79 | const mod = getPlugin(name)
|
80 | if (!mod && process.env.NODE_ENV === 'development') {
|
81 | console.warn('no module found for: ' + name)
|
82 | }
|
83 |
|
84 | for (let i = 0; i < list.length; i++) {
|
85 | const p = Array.isArray(list[i]) ? list[i][0] : list[i]
|
86 | if ((mod && p === mod) || isSameName(p, name) || (p._original_name && isSameName(p._original_name, name))) {
|
87 | return i
|
88 | }
|
89 | }
|
90 | return -1
|
91 | }
|
92 |
|
93 | Object.keys(modifications).forEach(function (key) {
|
94 | if (key === 'nameDrops') return
|
95 |
|
96 | const value = modifications[key]
|
97 | const index = indexOf(plugins, key)
|
98 |
|
99 | if (value === false) {
|
100 | if (index < 0 && process.env.NODE_ENV === 'development') {
|
101 | console.warn(key + ' not found', __dirname)
|
102 | }
|
103 |
|
104 | plugins.splice(index, 1)
|
105 | }
|
106 | else {
|
107 | let p = getPlugin(key)
|
108 | if (nameDrops) {
|
109 | p._original_name = key
|
110 | }
|
111 | if (value !== true) {
|
112 | p = [p].concat(value)
|
113 | }
|
114 | if (index < 0) {
|
115 | plugins.push(p)
|
116 | }
|
117 | else {
|
118 | plugins[index] = p
|
119 | }
|
120 | }
|
121 | })
|
122 |
|
123 | return config
|
124 | }
|