-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
169 lines (148 loc) · 4.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* rollup-plugin-jscc v2.0.0
* @license MIT
*/
/* eslint-disable */
'use strict';
const rollupPluginutils = require('rollup-pluginutils');
const path = require('path');
const getPackageVersion = require('@jsbits/get-package-version');
const fs = require('fs');
const jscc = require('jscc');
/**
* Creates a filter for the options `include`, `exclude`, and `extensions`.
*
* Since `extensions` is not a rollup option, I think is widely used.
*
* @param {import('..').Options} opts The user options
* @param {string|string[]} exts Default extensions
* @returns {function} Filter function that returns true if a given file
* matches the filter.
*/
const makeFilter = (opts, exts) => {
const _filt = rollupPluginutils.createFilter(opts.include, opts.exclude);
exts = opts.extensions || exts;
if (!exts || exts === '*') {
return _filt // do not filter extensions
}
if (!Array.isArray(exts)) {
exts = [exts];
}
exts = exts.map(e => (e[0] !== '.' ? `.${e}` : e));
return id => _filt(id) && exts.includes(path.extname(id))
};
/**
* @param {import('..').Options} options -
* @returns {import('jscc').Options}
*/
const parseOptions = options => {
options = Object.assign(
{
prefixes: [/\/[/*] ?/, /<!-- ?/],
},
options
);
options.values = Object.assign(
{
_VERSION: getPackageVersion(),
},
options.values
);
options.sourceMap = options.sourcemap !== false && options.sourceMap !== false;
return options
};
/**
* Returns a shallow copy of the jscc options.
*
* @param {jscc.Options} opts
* @returns {jscc.Options}
*/
const _getJsccOpts = opts => ({
escapeQuotes: opts.escapeQuotes,
keepLines: opts.keepLines,
mapHires: opts.mapHires,
prefixes: opts.prefixes,
sourceMap: opts.sourceMap,
mapContent: opts.mapContent !== false,
values: Object.assign({}, opts.values),
});
/**
* Simple wrapper for the async `fs.readFile` that returns a Promise that
* resolves to a string with the content decoded as utf8.
*
* @param {string} fname Absolute or relative to cwd
* @returns {Promise<string>}
*/
const _getSource = fname =>
new Promise((resolve, reject) => {
fs.readFile(fname, 'utf8', (error, data) => {
// istanbul ignore if
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
/**
* Call jscc and returns a Promise that is resolved with a {code,map} object
* or a string if the buffer did not change or `options.sourceMap:false`.
*
* @param {string} fname Absolute or relative to cwd
* @param {jscc.Options} options jscc options
* @param {string} [code] Source
* @returns {Promise<string>}
*/
const procFile = (fname, options, code) => {
// Supports transform
const promise = code != null ? Promise.resolve(code) : _getSource(fname);
return promise
.then(source => {
// Supports buffer
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(source)) {
source = source.toString();
}
// Ignore non-string sources
return typeof source === 'string'
? jscc(source, fname, _getJsccOpts(options))
: source
})
.then(ret => {
/*
change the relative source path in the source map to the input file path
explanation:
rollup will resolve source path through the file's directory absolute path
and the source path in the returned source map by the plugin
new Source(resolve(dirname(module.id), originalSourcemap.sourceRoot || '.', source), sourcesContent[i])
@see https://github.com/rollup/rollup/blob/v1.15.6/src/utils/collapseSourcemaps.ts#L196
*/
if (ret.map) {
ret.map.sources[0] = fname;
}
return ret
})
};
const DEFAULT_EXTENSIONS = ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.tag'];
/**
* rollup-plugin-jscc entry point
*
* @param {import('..').Options} options User options
* @returns {import('rollup').Plugin}
*/
function jsccPlugin (options) {
// Get the jscc options from the plugin options
options = parseOptions(options);
const filter = makeFilter(options, DEFAULT_EXTENSIONS);
if (options.asloader !== false) {
return {
name: 'jscc',
load: id => (filter(id) ? procFile(id, options) : null),
}
}
return {
name: 'jscc',
transform: (code, id) => (filter(id) ? procFile(id, options, code) : null),
}
}
module.exports = jsccPlugin;
//# sourceMappingURL=index.js.map