-
Notifications
You must be signed in to change notification settings - Fork 474
/
Copy pathbuild.mjs
135 lines (122 loc) · 4.98 KB
/
build.mjs
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
import fs from 'node:fs'
import path from 'node:path'
import * as esbuild from 'esbuild'
import esbuildPluginLicense from 'esbuild-plugin-license'
const CORE_LICENSE = `MIT License
Copyright (c) 2021-present vuejs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`
await esbuild.build({
bundle: true,
entryPoints: ['index.ts'],
external: ['locales/*'],
outfile: 'outfile.cjs',
format: 'cjs',
platform: 'node',
target: 'node14',
plugins: [
{
name: 'alias',
setup({ onResolve, resolve }) {
onResolve({ filter: /^prompts$/, namespace: 'file' }, async ({ importer, resolveDir }) => {
// we can always use non-transpiled code since we support 14.16.0+
const result = await resolve('prompts/lib/index.js', {
importer,
resolveDir,
kind: 'import-statement'
})
return result
})
}
},
{
name: '@vue/create-eslint-config fix',
setup(build) {
// Update esbuild to support the import attributes syntax in this PR is too risky.
// TODO: update esbuild and remove the hack.
build.onLoad({ filter: /@vue.create-eslint-config.index.js$/ }, (args) => {
const text = fs.readFileSync(args.path, 'utf8')
return {
contents: text.replace(`with { type: 'json' }`, ''),
loader: 'js'
}
})
// The renderEjsFile.js module uses file system APIs therefore after bundling it will not work.
// So we need to preprocess it to remove the file system APIs.
build.onLoad({ filter: /@vue.create-eslint-config.renderEjsFile\.js$/ }, (args) => {
const pkgDir = path.dirname(args.path)
const templatesDir = path.resolve(pkgDir, './templates')
const allTemplateFileNames = fs.readdirSync(templatesDir)
const templateFiles = Object.fromEntries(
allTemplateFileNames.map((fileName) => {
const content = fs.readFileSync(path.resolve(templatesDir, fileName), 'utf8')
return [`./templates/${fileName}`, content]
})
)
return {
contents: `
import ejs from 'ejs'
const templates = ${JSON.stringify(templateFiles)}
export default function renderEjsFile(filePath, data) {
return ejs.render(templates[filePath], data, {})
}
`,
loader: 'js'
}
})
}
},
esbuildPluginLicense({
thirdParty: {
includePrivate: false,
output: {
file: 'LICENSE',
template(allDependencies) {
// There's a bug in the plugin that it also includes the `create-vue` package itself
const dependencies = allDependencies.filter((d) => d.packageJson.name !== 'create-vue')
const licenseText =
`# create-vue core license\n\n` +
`create-vue is released under the MIT license:\n\n` +
CORE_LICENSE +
`\n## Licenses of bundled dependencies\n\n` +
`The published create-vue artifact additionally contains code with the following licenses:\n` +
[...new Set(dependencies.map((dependency) => dependency.packageJson.license))].join(
', '
) +
'\n\n' +
`## Bundled dependencies\n\n` +
dependencies
.map((dependency) => {
return (
`## ${dependency.packageJson.name}\n\n` +
`License: ${dependency.packageJson.license}\n` +
`By: ${dependency.packageJson.author.name}\n` +
`Repository: ${dependency.packageJson.repository.url}\n\n` +
dependency.licenseText
.split('\n')
.map((line) => (line ? `> ${line}` : '>'))
.join('\n')
)
})
.join('\n\n')
return licenseText
}
}
}
})
]
})