forked from endorphinjs/rollup-plugin-endorphin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
281 lines (231 loc) · 9.41 KB
/
index.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import * as fs from 'fs';
import * as path from 'path';
import { createFilter } from 'rollup-pluginutils';
import { SourceNode, SourceMapConsumer, RawSourceMap, SourceMapGenerator } from 'source-map';
import { ParsedTemplate, CompileOptions } from '@endorphinjs/template-compiler';
const mkdirp = require('mkdirp');
type TransformedResource = string | Buffer | {
code?: string | Buffer,
css?: string | Buffer,
map?: any
};
type TransformedResult = string | CodeWithMap;
type CodeWithMap = { code: string, map?: any };
interface ResourceTransformer {
(type: string, code: string, filename: string): TransformedResource | Promise<TransformedResource>;
}
interface CSSBundleHandler {
(code: string, map?: SourceMapGenerator): void;
}
interface EndorphinPluginOptions {
/**
* List of file extensions which should be treated as Endorphin template.
* Default are `.html` and `.end`
*/
extensions?: string[],
include?: string | string[],
exclude?: string | string[],
/** Mapping of type attributes of style and script tags to extension */
types: { [type: string]: string };
/** Additional options for template compiler */
template?: CompileOptions;
/** Options for CSS processing */
css?: {
/** A function to transform stylesheet code. */
preprocess?: ResourceTransformer;
/** A function that returns a CSS scope token from given component file path */
scope?: (fileName: string) => string;
/**
* Path where CSS bundle should be saved or function which accepts generated
* CSS bundle and its source map
*/
bundle?: string | CSSBundleHandler;
};
}
const defaultScriptType = 'text/javascript';
const defaultStyleType = 'text/css';
const defaultOptions: EndorphinPluginOptions = {
extensions: ['.html', '.end'],
types: {
[defaultScriptType]: '.js',
[defaultStyleType]: '.css',
'typescript': '.ts',
'ts': '.ts',
'javascript': '.js',
'sass': '.sass',
'scss': '.scss'
}
};
const defaultCSSOptions = {
scope(filePath: string): string {
// A simple function for calculation of has (Adler32) from given string
let s1 = 1, s2 = 0;
for (let i = 0, len = filePath.length; i < len; i++) {
s1 = (s1 + filePath.charCodeAt(i)) % 65521;
s2 = (s2 + s1) % 65521;
}
return 'e' + ((s2 << 16) + s1).toString(36);
}
}
export default function endorphin(options?: EndorphinPluginOptions): object {
options = {
...defaultOptions,
...options
};
if (!options.css) {
options.css = defaultCSSOptions;
} else {
options.css = {
...defaultCSSOptions,
...options.css
};
}
const filter = createFilter(options.include, options.exclude);
const jsResources = {};
const cssResources: Map<string, SourceNode> = new Map();
const endorphin = require('endorphin/compiler');
return {
name: 'endorphin',
load(id: string) {
return id in jsResources ? jsResources[id] : null;
},
resolveId(id: string) {
return id in jsResources ? id : null;
},
async transform(this: any, source: string, id: string) {
if (!filter(id) || !options.extensions.includes(path.extname(id))) {
return null;
}
const cssScope = options.css.scope(id);
// Parse Endorphin template into AST
const parsed = endorphin.parse(source, id, options.template) as ParsedTemplate;
const { scripts, stylesheets } = parsed.ast;
// For inline scripts, emit source code as external module so we can
// hook on it’s processing
scripts.forEach((script, i) => {
if (script.content) {
let assetUrl = createAssetUrl(parsed.url, options.types[script.mime] || options.types[defaultScriptType], i);
if (assetUrl[0] !== '.' && assetUrl[0] !== '/' && assetUrl[0] !== '@') {
assetUrl = `./${assetUrl}`;
}
jsResources[assetUrl] = script.transformed || script.content;
script.url = assetUrl;
script.transformed = script.content = void 0;
}
});
// Process stylesheets: apply custom transform (if required) and scope
// CSS selectors
await Promise.all(stylesheets.map(async (stylesheet) => {
const isExternal = stylesheet.url !== id;
// XXX when resolved via `this.resolveId()`, a file name could lead
// to Node module resolve, e.g. `my-file.css` can be resolved as
// `node_modules/my-file.css/index.js`
// let fullId = await this.resolveId(stylesheet.url, id);
let fullId = path.resolve(path.dirname(id), stylesheet.url);
let content = '';
if (isExternal) {
// Watch for external stylesheets
this.addWatchFile(fullId);
content = fs.readFileSync(fullId, 'utf8');
} else {
content = stylesheet.content;
}
let transformed: CodeWithMap = { code: content };
// Apply custom CSS preprocessing
if (typeof options.css.preprocess === 'function') {
const result = await transformResource(stylesheet.mime, content, fullId, options.css.preprocess);
if (result != null) {
transformed = result;
}
}
// Isolate CSS selector with scope token
if (cssScope) {
let filename = fullId;
let map = transformed.map;
if (typeof map === 'string') {
map = JSON.parse(map);
}
if (map && map.file) {
filename = map.file;
}
const scoped = await endorphin.scopeCSS(transformed.code, cssScope, { filename, map });
transformed = typeof scoped === 'string' ? { code: scoped } : scoped;
}
const node = await nodeFromTransformed(transformed, content, fullId);
cssResources.set(fullId, node);
}));
// Generate JavaScript code from template AST
return endorphin.generate(parsed, {
module: 'endorphin',
cssScope,
warn: (msg: string, pos?: number) => this.warn(msg, pos),
...options.template
});
},
async generateBundle(outputOptions: any) {
if (!options.css.bundle) {
return;
}
const output = new SourceNode();
for (const node of cssResources.values()) {
output.add(node);
}
let code: string, map: SourceMapGenerator;
if (outputOptions.sourcemap) {
const result = output.toStringWithSourceMap();
code = result.code;
map = result.map;
} else {
code = output.toString();
}
if (typeof options.css.bundle === 'function') {
return options.css.bundle(code, map);
} else {
const dir = path.dirname(options.css.bundle);
mkdirp.sync(dir);
if (map) {
const sourceMapPath = path.basename(options.css.bundle) + '.map';
const fullSourceMapPath = path.join(dir, sourceMapPath);
code += `\n/*# sourceMappingURL=${sourceMapPath} */`;
fs.writeFileSync(fullSourceMapPath, map.toString());
}
fs.writeFileSync(options.css.bundle, code);
}
}
};
}
async function transformResource(type: string, content: string, url: string, transformer: ResourceTransformer): Promise<CodeWithMap> {
const transformed: TransformedResource = await transformer(type, content, url);
const result: TransformedResource = typeof transformed === 'string' || Buffer.isBuffer(transformed)
? { code: transformed }
: transformed;
let code = result.css || result.code;
let map = result.map;
if (Buffer.isBuffer(code)) {
code = code.toString()
}
if (Buffer.isBuffer(map)) {
map = map.toString();
}
if (map && map.addMapping) {
// Source map is a SourceMapGenerator
result.map = result.map.toJSON();
}
return { code, map };
}
async function nodeFromTransformed(data: TransformedResult, source: string, fileName?: string): Promise<SourceNode> {
let node: SourceNode;
if (typeof data === 'object' && data.map) {
const consumer = await new SourceMapConsumer(data.map as RawSourceMap);
node = SourceNode.fromStringWithSourceMap(data.code, consumer);
} else {
node = new SourceNode();
node.add(typeof data === 'string' ? data : data.code);
}
node.setSourceContent(fileName, source);
return node;
}
function createAssetUrl(baseUrl: string, ext: string, index: number = 0): string {
const baseName = baseUrl.slice(0, -path.extname(baseUrl).length);
return `${baseName}_${index}${ext}`;
}