-
Notifications
You must be signed in to change notification settings - Fork 137
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
1 parent
796fef4
commit cc532b3
Showing
6 changed files
with
244 additions
and
5 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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/node_modules | ||
/src/**/*.js | ||
/src/**/*.d.ts | ||
/src/**/*.map | ||
/*/tests/**/*.js | ||
/*/tests/**/*.d.ts | ||
/*/tests/**/*.map |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "@embroider/vite", | ||
"version": "0.39.1", | ||
"private": false, | ||
"description": "An Embroider Packager for Vite", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/embroider-build/embroider.git", | ||
"directory": "packages/vite" | ||
}, | ||
"license": "MIT", | ||
"author": "Alex LaFroscia", | ||
"main": "src/index.js", | ||
"files": [ | ||
"src/**/*.js", | ||
"src/**/*.d.ts", | ||
"src/**/*.js.map" | ||
], | ||
"scripts": { | ||
"prepare": "tsc" | ||
}, | ||
"dependencies": { | ||
"@embroider/rollup-plugin-hbs": "*", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"@rollup/plugin-commonjs": "^18.0.0", | ||
"vite": "^2.0.0" | ||
}, | ||
"peerDependencies": { | ||
"@embroider/core": "^0.39.1" | ||
}, | ||
"devDependencies": { | ||
"@embroider/core": "^0.39.1", | ||
"typescript": "~4.0.0" | ||
}, | ||
"engines": { | ||
"node": "10.* || 12.* || >= 14" | ||
}, | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { realpathSync, readFileSync } from 'fs'; | ||
import { copyFile } from 'fs/promises'; | ||
import { join, parse } from 'path'; | ||
import { AppMeta, PackagerInstance, Variant } from '@embroider/core'; | ||
import { build, BuildOptions, InlineConfig } from 'vite'; | ||
import templateCompilerPlugin from '@embroider/rollup-plugin-hbs'; | ||
import { babel } from '@rollup/plugin-babel'; | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
|
||
type RollupOptions = Required<BuildOptions['rollupOptions']>; | ||
type AllowedRollupOptions = Omit<RollupOptions, 'input'>; | ||
|
||
type AllowedBuildOptions = Omit<BuildOptions, 'outDir' | 'rollupOptions'> & { | ||
rollupOptions?: AllowedRollupOptions; | ||
}; | ||
|
||
type AllowedViteConfig = Omit<InlineConfig, 'base' | 'build' | 'mode' | 'resolve' | 'root'> & { | ||
build?: AllowedBuildOptions; | ||
}; | ||
|
||
interface Options { | ||
viteConfig: AllowedViteConfig; | ||
} | ||
|
||
export class VitePackager implements PackagerInstance { | ||
static annotation = '@embroider/vite'; | ||
|
||
private viteConfig: AllowedViteConfig; | ||
private variant: Variant; | ||
|
||
pathToVanillaApp: string; | ||
|
||
constructor( | ||
pathToVanillaApp: string, | ||
private outputPath: string, | ||
variants: Variant[], | ||
_consoleWrite: (msg: string) => void, | ||
options?: Options | ||
) { | ||
this.pathToVanillaApp = realpathSync(pathToVanillaApp); | ||
|
||
// For now we're not worried about building each variant | ||
// Let's just assume we have one | ||
this.variant = variants[0]; | ||
|
||
this.viteConfig = options?.viteConfig ?? {}; | ||
} | ||
|
||
async build(): Promise<void> { | ||
const meta = this.getAppMeta(); | ||
|
||
const entryPoints = meta.assets | ||
.map(asset => parse(asset)) | ||
.filter(asset => asset.ext === '.html') | ||
.map(asset => join(this.pathToVanillaApp, asset.dir, asset.base)); | ||
|
||
await build({ | ||
...this.viteConfig, | ||
mode: this.variant.optimizeForProduction ? 'production' : 'development', | ||
logLevel: 'error', | ||
plugins: [ | ||
templateCompilerPlugin({ | ||
templateCompilerFile: join(this.pathToVanillaApp, meta['template-compiler'].filename), | ||
variant: this.variant, | ||
}), | ||
babel({ | ||
// Embroider includes the Runtime plugin in the generated Babel config | ||
babelHelpers: 'runtime', | ||
|
||
// Path to the Embroider-generated Babel config | ||
configFile: join(this.pathToVanillaApp, meta.babel.filename), | ||
|
||
// Path to the Embroider-generated file defining a filtering function | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports | ||
filter: require(join(this.pathToVanillaApp, meta.babel.fileFilter)), | ||
}), | ||
commonjs(), | ||
...(this.viteConfig.plugins ?? []), | ||
], | ||
root: this.pathToVanillaApp, | ||
base: meta['root-url'], | ||
resolve: { | ||
extensions: meta['resolvable-extensions'], | ||
}, | ||
build: { | ||
...this.viteConfig.build, | ||
outDir: this.outputPath, | ||
rollupOptions: { | ||
...this.viteConfig.build?.rollupOptions, | ||
input: entryPoints, | ||
}, | ||
}, | ||
}); | ||
|
||
// Copy over the `vendor.js` file that is ignored by Vite | ||
await this.passThrough('assets/vendor.js'); | ||
await this.passThrough('assets/vendor.map'); | ||
} | ||
|
||
private getAppMeta(): AppMeta { | ||
return JSON.parse(readFileSync(join(this.pathToVanillaApp, 'package.json'), 'utf8'))['ember-addon'] as AppMeta; | ||
} | ||
|
||
private async passThrough(path: string) { | ||
const source = join(this.pathToVanillaApp, path); | ||
const dest = join(this.outputPath, path); | ||
|
||
await copyFile(source, dest); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@tsconfig/node14/tsconfig.json" | ||
} |
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