-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·91 lines (77 loc) · 2.26 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
/* eslint-disable camelcase */
/* eslint-disable global-require */
/* eslint-disable consistent-return */
/* eslint-disable import/no-dynamic-require */
const path = require('path');
const relative = require('require-relative');
const { createFilter } = require('@rollup/pluginutils');
const compile = require('./compile');
const pkgExportErrors = new Set();
function plugin(options = {}) {
const { emitCss = false } = options;
const filter = createFilter(options.include, options.exclude);
const cacheEmit = new Map();
return {
/**
* Resolve an import's full filepath.
*/
resolveId(importee, importer) {
if (cacheEmit.has(importee)) return importee;
if (
!importer ||
importee[0] === '.' ||
importee[0] === '\0' ||
path.isAbsolute(importee)
)
return null;
// if this is a bare import, see if there's a valid pkg.svelte
const parts = importee.split('/');
let dir;
let pkg;
let name = parts.shift();
if (name && name[0] === '@') {
name += `/${parts.shift()}`;
}
try {
const file = `${name}/package.json`;
const resolved = relative.resolve(file, path.dirname(importer));
dir = path.dirname(resolved);
pkg = require(resolved);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return null;
if (err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
pkgExportErrors.add(name);
return null;
}
throw err;
}
// use pkg.svelte
if (parts.length === 0 && pkg.svelte) {
return path.resolve(dir, pkg.svelte);
}
},
/**
* Returns CSS contents for a file, if ours
*/
load(id) {
return cacheEmit.get(id) || null;
},
/**
* Transforms a `.f7.html` or `.f7` file into a `.js` file.
* NOTE: If `emitCss`, append static `import` to virtual CSS file.
*/
async transform(source, id) {
if (!filter(id)) return null;
if (
!id.includes('.f7.js') &&
!id.includes('.f7.ts') &&
!id.includes('.f7.html') &&
!id.includes('.f7')
)
return null;
const code = compile({ source, emitCss, id, cacheEmit });
return code;
},
};
}
module.exports = plugin;