-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvue-sfc-compiler-pipeline.js
69 lines (59 loc) · 2.85 KB
/
vue-sfc-compiler-pipeline.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
const del = require('del');
const fs = require('fs');
const json = require('@rollup/plugin-json');
const alias = require('@rollup/plugin-alias');
const path = require('path');
const replace = require('@rollup/plugin-replace');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const rollupPipeline = require('./rollup-pipeline');
const vuePlugin = require('./rollup-plugin-vue-sfc-orchard-core');
const { getVueComponents } = require('./get-vue-files');
const { executeFunctionByCommandLineArgument, leaveNodeModule } = require('./process-helpers');
const tryOpenJson = require('./try-open-json');
// If this script is invoked from "npm explore lombiq-vuejs" then we have to navigate back to the current project root.
leaveNodeModule();
const defaultOptions = {
sfcRootPath: path.join('Assets', 'Scripts', 'VueComponents'),
sfcDestinationPath: path.join('wwwroot', 'vue'),
vueJsNodeModulesPath: path.resolve(__dirname, '..', '..', '..', 'node_modules'),
rollupAlias: {},
isProduction: false,
};
function compile(options) {
const fileOptions = tryOpenJson('vue-sfc-compiler-pipeline.json');
const opts = options ? { ...defaultOptions, ...fileOptions, ...options } : defaultOptions;
if (!fs.existsSync(opts.sfcRootPath)) return Promise.resolve([]);
const components = getVueComponents(opts.sfcRootPath);
if (components.length === 0) return Promise.resolve([]);
process.stdout.write(`vue component files: ${components.join(', ')}\n`);
if (!fs.existsSync(opts.vueJsNodeModulesPath)) {
throw new Error(`The vueJsNodeModulesPath option's path "${opts.vueJsNodeModulesPath}" does not exist!`);
}
if (!fs.lstatSync(opts.vueJsNodeModulesPath).isDirectory()) { // #spell-check-ignore-line
throw new Error(`The vueJsNodeModulesPath option's path "${opts.vueJsNodeModulesPath}" is not a directory!`);
}
return rollupPipeline(
opts.sfcDestinationPath,
components.map((appName) => ({ fileName: appName, entryPath: path.join(opts.sfcRootPath, appName) })),
[
vuePlugin(),
json(),
alias(opts.rollupAlias),
nodeResolve({ preferBuiltins: true, browser: true, mainFields: ['module', 'jsnext:main'] }), // #spell-check-ignore-line
replace({
values: {
'process.env.NODE_ENV': JSON.stringify(opts.isProduction ? 'production' : 'development'),
'process.env.BUILD': JSON.stringify('web'),
},
preventAssignment: true,
}),
],
null,
(fileName) => fileName.split('.')[0]);
}
async function clean(options) {
const opts = options ? { ...defaultOptions, ...options } : defaultOptions;
return del(opts.sfcDestinationPath, { force: true });
}
module.exports = { compile, clean };
executeFunctionByCommandLineArgument(module.exports);