-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.js
More file actions
160 lines (140 loc) · 4.47 KB
/
browser.js
File metadata and controls
160 lines (140 loc) · 4.47 KB
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
const fs = require('fs');
const { writeFile } = require('fs/promises');
const path = require('path');
const {
getRootDir,
cleanDir,
watchInputDir,
writeDemoFile,
copyAssets,
watchAssets,
} = require('@ixon-cdk/core');
const minimist = require('minimist');
const { rename } = require('fs/promises');
const Service = require('@vue/cli-service/lib/Service');
// Generate our own demo, because --target lib generates an unusable demo.html (Related: https://github.com/vuejs/vue-cli/issues/3291)
async function generateDemo(module, outPath) {
const demo = `<!DOCTYPE html><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${module} demo</title>
<script src="./${module}.min.js"></script></head>
<html>
<body>
<${module}></${module}>
</body></html>`;
try {
await writeFile(path.join(outPath, 'demo.html'), demo);
} catch (e) {
console.error(`Error writing demo file for ${tag} | ${e}`);
}
}
async function _build(inputFile, outputFile, tag, assets, production, watch) {
const inputDir = path.dirname(path.join(getRootDir(), inputFile));
const outputDir = path.dirname(path.join(getRootDir(), outputFile));
// temporary entry file
const entryFileName = path.join(__dirname, `__temp_entry-${tag}.js`);
const relativeInputPath = path.relative(
path.dirname(entryFileName),
path.join(getRootDir(), inputFile),
);
const entryFileContent = `import Vue from 'vue';
import VueCustomElement from 'vue-custom-element';
import Widget from '${relativeInputPath}';
Vue.config.ignoredElements = ['${tag}'];
Vue.use(VueCustomElement);
const e = Vue.customElement('${tag}', Widget, {
// Otherwise modal doesn't work???
shadow: false,
beforeCreateVueInstance(root) {
const rootNode = root.el.getRootNode();
if (rootNode instanceof ShadowRoot) {
root.shadowRoot = rootNode;
} else {
root.shadowRoot = document.head;
}
return root;
}
// Additional Options: https://github.com/karol-f/vue-custom-element#options
});
// in production the vue-custom-element definition gets concatenated in so we don't want this.
if (process.env.NODE_ENV === 'development') {
customElements.get('${tag}') || customElements.define('${tag}', e)
}
`;
fs.writeFileSync(entryFileName, entryFileContent, {
encoding: 'utf8',
flag: 'w',
});
cleanDir(outputDir);
// watch source files
if (watch) {
watchAssets(assets, inputDir, outputDir);
await watchInputDir(inputDir, (isAsset) => {
if (!isAsset) {
vueBuild(inputDir, outputDir, entryFileName, outputFile, tag, assets);
}
});
process.on('SIGINT', () => {
fs.unlinkSync(entryFileName);
process.exit();
});
} else {
await vueBuild(inputDir, outputDir, entryFileName, outputFile, tag, assets);
}
}
async function vueBuild(inputDir, outputDir, entryFileName, outputFile, tag, assets) {
writeDemoFile(tag, outputDir, outputFile);
const rawArgs = [
'build',
'--target',
'lib',
'--inline-vue',
'--dest',
outputDir,
'--name',
tag,
entryFileName
];
const args = minimist(rawArgs, {
boolean: [
// build
'modern',
'report',
'report-json',
'inline-vue',
'watch',
// serve
'open',
'copy',
'https',
// inspect
'verbose',
],
});
const s = new Service(getRootDir());
const command = args._[0];
try {
await s.run(command, args, rawArgs)
} catch (e) {
console.error(e)
}
// // build
// await build(config);
copyAssets(assets, inputDir, outputDir);
const umdNames = [`${tag}.umd.js`,
`${tag}.umd.min.js`];
for (let i = 0; i < umdNames.length; i++) {
await rename(path.join(outputDir, umdNames[i]), path.join(outputDir, umdNames[i].replace(".umd", "")));
}
await generateDemo(tag, outputDir);
}
module.exports = function runBuild(
inputFile,
outputFile,
tag,
assets,
production = true,
watch = false,
) {
return _build(inputFile, outputFile, tag, assets, production, watch);
};