-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,204 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
#!/usr/bin/env node | ||
const argv = require('minimist')(process.argv.slice(2)) | ||
const server = require('../dist').createServer(argv) | ||
|
||
let port = argv.port || 3000 | ||
if (argv._[0] === 'build') { | ||
require('../dist').build(argv) | ||
} else { | ||
const server = require('../dist').createServer(argv) | ||
|
||
server.on('error', (e) => { | ||
if (e.code === 'EADDRINUSE') { | ||
console.log(`port ${port} is in use, trying another one...`) | ||
setTimeout(() => { | ||
server.close() | ||
server.listen(++port) | ||
}, 100) | ||
} else { | ||
console.error(e) | ||
} | ||
}) | ||
let port = argv.port || 3000 | ||
|
||
server.on('listening', () => { | ||
console.log(`Running at http://localhost:${port}`) | ||
}) | ||
server.on('error', (e) => { | ||
if (e.code === 'EADDRINUSE') { | ||
console.log(`port ${port} is in use, trying another one...`) | ||
setTimeout(() => { | ||
server.close() | ||
server.listen(++port) | ||
}, 100) | ||
} else { | ||
console.error(e) | ||
} | ||
}) | ||
|
||
server.listen(port) | ||
server.on('listening', () => { | ||
console.log(`Running at http://localhost:${port}`) | ||
}) | ||
|
||
server.listen(port) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,183 @@ | ||
export function build() {} | ||
import path from 'path' | ||
import { promises as fs } from 'fs' | ||
import { rollup as Rollup, Plugin } from 'rollup' | ||
import { resolveVue } from './resolveVue' | ||
import { hmrClientPublicPath } from './serverPluginHmr' | ||
import chalk from 'chalk' | ||
|
||
export interface BuildOptions { | ||
root?: string | ||
inlineVue?: boolean | ||
} | ||
|
||
export async function build({ | ||
root = process.cwd(), | ||
inlineVue = resolveVue(root).hasLocalVue | ||
}: BuildOptions = {}) { | ||
const start = Date.now() | ||
|
||
// lazy require rollup so that we don't load it when only using the dev server | ||
// importing it just for the types | ||
const rollup = require('rollup').rollup as typeof Rollup | ||
|
||
const outDir = path.resolve(root, 'dist') | ||
const indexPath = path.resolve(root, 'index.html') | ||
const scriptRE = /<script\b[^>]*>([\s\S]*?)<\/script>/gm | ||
const indexContent = await fs.readFile(indexPath, 'utf-8') | ||
|
||
const cssFilename = 'style.css' | ||
|
||
// make sure to use the same verison of vue as the one bundled with | ||
// this version of vite. | ||
const vueVersion = require('vue/package.json').version | ||
const cdnLink = `https://unpkg.com/vue@${vueVersion}/dist/vue.esm-browser.prod.js` | ||
|
||
const vitePlugin: Plugin = { | ||
name: 'vite', | ||
resolveId(id: string) { | ||
if (id.startsWith('/')) { | ||
if (id === hmrClientPublicPath) { | ||
return hmrClientPublicPath | ||
} else { | ||
return id.startsWith(root) ? id : path.resolve(root, id.slice(1)) | ||
} | ||
} else if (id === 'vue') { | ||
if (inlineVue) { | ||
return resolveVue(root, true).vue | ||
} else { | ||
return { | ||
id: cdnLink, | ||
external: true | ||
} | ||
} | ||
} | ||
}, | ||
load(id: string) { | ||
if (id === hmrClientPublicPath) { | ||
return `export function hot() {}` | ||
} else if (id === indexPath) { | ||
let script = '' | ||
let match | ||
while ((match = scriptRE.exec(indexContent))) { | ||
// TODO handle <script type="module" src="..."/> | ||
// just add it as an import | ||
script += match[1] | ||
} | ||
return script | ||
} | ||
} | ||
} | ||
|
||
const styles: Map<string, string> = new Map() | ||
|
||
const cssExtractPlugin: Plugin = { | ||
name: 'vite-css', | ||
transform(code: string, id: string) { | ||
if (id.endsWith('.css')) { | ||
styles.set(id, code) | ||
return '/* css extracted by vite */' | ||
} | ||
} | ||
} | ||
|
||
const bundle = await rollup({ | ||
// TODO | ||
// parse index.html | ||
// find entry file or script | ||
// if inline script, create a temp main file next to it before bundling | ||
input: path.resolve(root, 'index.html'), | ||
plugins: [ | ||
vitePlugin, | ||
require('rollup-plugin-vue')(), | ||
require('@rollup/plugin-node-resolve')({ | ||
rootDir: root | ||
}), | ||
require('@rollup/plugin-replace')({ | ||
'process.env.NODE_ENV': '"production"' | ||
}), | ||
cssExtractPlugin, | ||
require('rollup-plugin-terser').terser() | ||
] | ||
}) | ||
|
||
const { output } = await bundle.generate({ | ||
dir: outDir, | ||
format: 'es' | ||
}) | ||
|
||
await fs.rmdir(outDir, { recursive: true }) | ||
await fs.mkdir(outDir) | ||
|
||
let generatedIndex = indexContent.replace(scriptRE, '').trim() | ||
|
||
// TODO handle public path for injections? | ||
// this would also affect paths in templates and css. | ||
|
||
// inject css link | ||
generatedIndex = injectCSS(generatedIndex, cssFilename) | ||
// write css | ||
let css = '' | ||
styles.forEach((s) => { | ||
css += s | ||
}) | ||
const cssFilepath = path.join(outDir, cssFilename) | ||
console.log( | ||
`write ${chalk.magenta(path.relative(process.cwd(), cssFilepath))}` | ||
) | ||
await fs.writeFile( | ||
cssFilepath, | ||
// minify with cssnano | ||
( | ||
await require('postcss')([require('cssnano')]).process(css, { | ||
from: undefined | ||
}) | ||
).css | ||
) | ||
|
||
if (!inlineVue) { | ||
// if not inlining vue, inject cdn link so it can start the fetch early | ||
generatedIndex = injectScript(generatedIndex, cdnLink) | ||
} | ||
|
||
// inject chunks | ||
for (const chunk of output) { | ||
if (chunk.type === 'chunk') { | ||
if (chunk.isEntry) { | ||
// inject chunk to html | ||
generatedIndex = injectScript(generatedIndex, chunk.fileName) | ||
} | ||
// write chunk | ||
const filepath = path.join(outDir, chunk.fileName) | ||
console.log(`write ${chalk.cyan(path.relative(process.cwd(), filepath))}`) | ||
await fs.writeFile(filepath, chunk.code) | ||
} | ||
} | ||
|
||
// write html | ||
const indexOutPath = path.join(outDir, 'index.html') | ||
console.log( | ||
`write ${chalk.green(path.relative(process.cwd(), indexOutPath))}` | ||
) | ||
await fs.writeFile(indexOutPath, generatedIndex) | ||
|
||
console.log(`done in ${((Date.now() - start) / 1000).toFixed(2)}s.`) | ||
} | ||
|
||
function injectCSS(html: string, filename: string) { | ||
const tag = `<link rel="stylesheet" href="/${filename}">` | ||
if (/<\/head>/.test(html)) { | ||
return html.replace(/<\/head>/, `${tag}\n</head>`) | ||
} else { | ||
return tag + '\n' + html | ||
} | ||
} | ||
|
||
function injectScript(html: string, filename: string) { | ||
filename = /^https?:\/\//.test(filename) ? filename : `/${filename}` | ||
const tag = `<script type="module" src="${filename}"></script>` | ||
if (/<\/body>/.test(html)) { | ||
return html.replace(/<\/body>/, `${tag}\n</body>`) | ||
} else { | ||
return html + '\n' + tag | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './server' | ||
export * from './build' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.