-
Notifications
You must be signed in to change notification settings - Fork 0
/
stencil.config.ts
109 lines (96 loc) · 2.8 KB
/
stencil.config.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
import fs from 'fs';
import path from 'path';
import { Config } from '@stencil/core';
import { sass } from '@stencil/sass';
import { postcss } from '@stencil/postcss';
import postCSSPresetEnv from 'postcss-preset-env';
import cssnano from 'cssnano';
import { createFilter } from 'rollup-pluginutils';
import replace from '@rollup/plugin-replace';
const pkgManifest = JSON.parse(fs.readFileSync('package.json', 'utf8'));
function resolveGqlIncludes(code, id) {
// Parse @include path
const regex = /# *?@include *"(?<path>.*)"\n/g;
let resolvedCode = code;
let result;
// Loop over @include statements until there's none left
// eslint-disable-next-line no-cond-assign
while ((result = regex.exec(resolvedCode)) !== null) {
// Resolve absole path from the file path and @include paths
const dir = path.dirname(id);
const absPath = path.normalize(path.join(dir, result.groups.path));
// Replace the @include statement with the file contents
const includedFile = fs.readFileSync(absPath, 'utf8');
resolvedCode = resolvedCode.replace(result[0], includedFile);
}
return resolvedCode;
}
interface Options {
include?: string;
exclude?: string;
}
function gql(opts: Options = {}) {
if (!opts.include) {
opts.include = 'src/**/*.graphql'; // eslint-disable-line no-param-reassign
}
const filter = createFilter(opts.include, opts.exclude);
return {
name: 'gql',
// eslint-disable-next-line consistent-return
transform(code, id) {
if (filter(id)) {
const resolvedCode = resolveGqlIncludes(code, id);
return {
code: `export default ${JSON.stringify(resolvedCode)}`,
};
}
},
};
}
function svg(opts: Options = {}) {
const filter = createFilter(opts.include || '**/*.svg', opts.exclude);
return {
name: 'svg',
// eslint-disable-next-line consistent-return
transform(code, id) {
if (filter(id)) {
// Rollup by default returns a base64 URL. Decode that back into HTML for SVGs
const transformed = code.replace(/'[^']+'/, data =>
JSON.stringify(
Buffer.from(data.replace('data:image/svg+xml;base64,', ''), 'base64').toString('utf8')
)
);
return { code: transformed };
}
},
};
}
export const config: Config = {
namespace: 'manifold-subscription',
globalStyle: 'src/styles/index.scss',
outputTargets: [
{
type: 'dist',
esmLoaderPath: '../loader',
},
{
type: 'www',
serviceWorker: null, // disable service workers
},
],
plugins: [
gql(),
svg(),
sass(),
postcss({
plugins: [cssnano(), postCSSPresetEnv()],
}),
replace({
exclude: 'node_modules/**',
delimiters: ['<@', '@>'],
values: {
NPM_PACKAGE_VERSION: pkgManifest.version,
},
}),
],
};