forked from seia-soto/snowpack-plugin-import-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (27 loc) · 938 Bytes
/
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
const { transformAsync } = require('@babel/core');
const babelPlugin = require('./babel-plugin.js');
const generateImports = require('./generate-imports.js');
const snowpackPlugin = (_, pluginOptions) => {
const extensions = pluginOptions.extensions || ['.js', '.jsx', '.tsx', '.ts'];
const enableInDevMode = pluginOptions.enableInDevMode || false;
const imports = generateImports(pluginOptions);
return {
async transform(options) {
const { contents, filePath, isDev, fileExt } = options;
if (
((isDev && enableInDevMode) || !isDev) &&
extensions.includes(fileExt.toLowerCase())
) {
const result = await transformAsync(contents, {
filename: filePath,
plugins: [babelPlugin({ imports })],
cwd: process.cwd(),
ast: false,
});
return result.code;
}
return contents;
},
};
};
module.exports = snowpackPlugin;