Skip to content

Commit

Permalink
feat: add prototype Vite packager
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Jul 1, 2021
1 parent 2bf646c commit 392232c
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/packages/hbs-loader/**/*.d.ts
/packages/rollup-plugin-hbs/**/*.js
/packages/rollup-plugin-hbs/**/*.d.ts
/packages/vite/**/*.js
/packages/vite/**/*.d.ts
/test-packages/support/**/*.js
/test-packages/**/*.d.ts
/tests/scenarios/**/*.js
Expand Down
7 changes: 7 additions & 0 deletions packages/vite/.gitignore
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
42 changes: 42 additions & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@embroider/vite",
"version": "0.42.3",
"private": false,
"description": "Builds EmberJS apps with 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": "0.42.3",
"@rollup/plugin-babel": "^5.3.0",
"fs-extra": "^7.0.0"
},
"peerDependencies": {
"@embroider/core": "^0.39.1",
"vite": "^2.0.0"
},
"devDependencies": {
"@embroider/core": "0.42.3",
"typescript": "~4.0.0",
"vite": "^2.3.8"
},
"engines": {
"node": "12.* || 14.* || >= 16"
},
"volta": {
"extends": "../../package.json"
}
}
136 changes: 136 additions & 0 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { realpathSync } from 'fs';
import { copyFile } from 'fs-extra';
import { join } from 'path';
import {
HTMLEntrypoint,
Packager,
PackagerConstructor,
Variant,
applyVariantToBabelConfig,
getAppMeta,
getPackagerCacheDir,
} from '@embroider/core';
import { build } from 'vite';
import templateCompilerPlugin from '@embroider/rollup-plugin-hbs';
import { babel } from '@rollup/plugin-babel';
import { AllowedViteConfig, Options } from './options';

const Vite: PackagerConstructor<Options> = class Vite implements Packager {
static annotation = '@embroider/vite';

private pathToVanillaApp: string;
private variant: Variant;
private viteConfig: AllowedViteConfig;

constructor(
inputPath: string,
private outputPath: string,
variants: Variant[],
_consoleWrite: (msg: string) => void,
options?: Options
) {
this.pathToVanillaApp = realpathSync(inputPath);

// 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 = getAppMeta(this.pathToVanillaApp);
const entrypoints: HTMLEntrypoint[] = [];
const otherAssets: string[] = [];
const rootURL = meta['root-url'];

for (let relativePath of meta.assets) {
if (/\.html/i.test(relativePath)) {
entrypoints.push(new HTMLEntrypoint(this.pathToVanillaApp, rootURL, '/', relativePath));
} else {
otherAssets.push(relativePath);
}
}

await build({
// Options we want to override the defaults for, but users can override themselves, too
logLevel: 'error',

// User options
...this.viteConfig,

// Options we *don't* want to allow users to override
base: meta['root-url'],
cacheDir: getPackagerCacheDir('vite'),
configFile: false,
mode: this.variant.optimizeForProduction ? 'production' : 'development',
resolve: {
...this.viteConfig.resolve,
extensions: meta['resolvable-extensions'],
},
root: this.pathToVanillaApp,

build: {
...this.viteConfig.build,
outDir: this.outputPath,
rollupOptions: {
...this.viteConfig.build?.rollupOptions,
input: entrypoints.map(entry => join(this.pathToVanillaApp, entry.filename)),
},
commonjsOptions: {
...this.viteConfig.build?.commonjsOptions,
extensions: meta['resolvable-extensions'],
include: [/.*/],
},
},

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 file defining a filtering function
// eslint-disable-next-line @typescript-eslint/no-require-imports
filter: require(join(this.pathToVanillaApp, meta.babel.fileFilter)),

// Add the Babel config produced by Embroider
...this.getBabelConfig(meta.babel.filename),
}),

...(this.viteConfig.plugins ?? []),
],
});

await Promise.all([
// Vite does not process non-module scripts, so we need to copy them over
...entrypoints
.reduce((acc, entrypoint) => [...acc, ...entrypoint.scripts], [] as string[])
.map(script => this.copyThrough(script)),

// Copy over other assets
// This more-or-less mimics what Vite does for `public` files
...otherAssets.map(relativePath => this.copyThrough(relativePath)),
]);
}

private copyThrough(path: string) {
const source = join(this.pathToVanillaApp, path);
const dest = join(this.outputPath, path);

return copyFile(source, dest);
}

private getBabelConfig(configFileName: string) {
const appBabelConfigPath = join(this.pathToVanillaApp, configFileName);

// eslint-disable-next-line @typescript-eslint/no-require-imports
return applyVariantToBabelConfig(this.variant, require(appBabelConfigPath));
}
};

export { Vite };
26 changes: 26 additions & 0 deletions packages/vite/src/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { BuildOptions, InlineConfig } from 'vite';

type CommonJSOptions = Required<BuildOptions['commonjsOptions']>;
type ResolveOptions = Required<InlineConfig['resolve']>;
type RollupOptions = Required<BuildOptions['rollupOptions']>;

type AllowedCommonJSOptions = Omit<CommonJSOptions, 'extensions' | 'include'>;
type AllowedResolveOptions = Omit<ResolveOptions, 'extensions'>;
type AllowedRollupOptions = Omit<RollupOptions, 'input'>;

type AllowedBuildOptions = Omit<BuildOptions, 'commonjsOptions' | 'outDir' | 'rollupOptions'> & {
commonjsOptions?: AllowedCommonJSOptions;
rollupOptions?: AllowedRollupOptions;
};

export type AllowedViteConfig = Omit<
InlineConfig,
'base' | 'build' | 'cacheDir' | 'configFile' | 'mode' | 'resolve' | 'root'
> & {
build?: AllowedBuildOptions;
resolve?: AllowedResolveOptions;
};

export interface Options {
viteConfig?: AllowedViteConfig;
}
59 changes: 56 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
dependencies:
"@babel/types" "^7.14.5"

"@babel/helper-module-imports@^7.0.0", "@babel/helper-module-imports@^7.12.13", "@babel/helper-module-imports@^7.14.5", "@babel/helper-module-imports@^7.8.3":
"@babel/helper-module-imports@^7.0.0", "@babel/helper-module-imports@^7.10.4", "@babel/helper-module-imports@^7.12.13", "@babel/helper-module-imports@^7.14.5", "@babel/helper-module-imports@^7.8.3":
version "7.14.5"
resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.14.5.tgz#6d1a44df6a38c957aa7c312da076429f11b422f3"
integrity sha512-SwrNHu5QWS84XlHwGYPDtCxcA0hrSlL2yhWYLgeOc0w7ccOl2qv4s/nARI0aYZW+bSwAL5CukeXA47B/1NKcnQ==
Expand Down Expand Up @@ -1890,6 +1890,23 @@
dependencies:
"@octokit/openapi-types" "^7.2.3"

"@rollup/plugin-babel@^5.3.0":
version "5.3.0"
resolved "https://registry.yarnpkg.com/@rollup/plugin-babel/-/plugin-babel-5.3.0.tgz#9cb1c5146ddd6a4968ad96f209c50c62f92f9879"
integrity sha512-9uIC8HZOnVLrLHxayq/PTzw+uS25E14KPUBh5ktF+18Mjo5yK0ToMMx6epY0uEgkjwJw0aBW4x2horYXh8juWw==
dependencies:
"@babel/helper-module-imports" "^7.10.4"
"@rollup/pluginutils" "^3.1.0"

"@rollup/pluginutils@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
integrity sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==
dependencies:
"@types/estree" "0.0.39"
estree-walker "^1.0.1"
picomatch "^2.2.2"

"@simple-dom/document@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@simple-dom/document/-/document-1.4.0.tgz#af60855f957f284d436983798ef1006cca1a1678"
Expand Down Expand Up @@ -2164,6 +2181,11 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.48.tgz#18dc8091b285df90db2f25aa7d906cfc394b7f74"
integrity sha512-LfZwXoGUDo0C3me81HXgkBg5CTQYb6xzEl+fNmbO4JdRiSKQ8A0GD1OBBvKAIsbCUgoyAty7m99GqqMQe784ew==

"@types/estree@0.0.39":
version "0.0.39"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/estree@^0.0.47":
version "0.0.47"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.47.tgz#d7a51db20f0650efec24cd04994f523d93172ed4"
Expand Down Expand Up @@ -9314,6 +9336,11 @@ es6-promisify@^5.0.0:
dependencies:
es6-promise "^4.0.3"

esbuild@^0.12.8:
version "0.12.13"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.12.13.tgz#666e1e6c50dbde40c02824cc60296574ebd588c5"
integrity sha512-fnKinmXcW1DMYnf1Ol3ZO0yU7dBDCuPcE4XDwceIy2zqTvB4G0NfonqgdvPMJi/IXRoVi/w9r9O5xxg/SqiAxA==

escalade@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
Expand Down Expand Up @@ -9737,6 +9764,11 @@ estree-walker@^0.6.1:
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==

estree-walker@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-1.0.1.tgz#31bc5d612c96b704106b477e6dd5d8aa138cb700"
integrity sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==

esutils@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
Expand Down Expand Up @@ -14862,7 +14894,7 @@ performance-now@^2.1.0:
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=

picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3:
picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.2, picomatch@^2.2.3:
version "2.3.0"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
integrity sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==
Expand Down Expand Up @@ -15010,6 +15042,15 @@ postcss@^8.2.15:
nanoid "^3.1.23"
source-map-js "^0.6.2"

postcss@^8.3.4:
version "8.3.5"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.3.5.tgz#982216b113412bc20a86289e91eb994952a5b709"
integrity sha512-NxTuJocUhYGsMiMFHDUkmjSKT3EdH4/WbGF6GCi1NDGk+vbcUTun4fpbOqaPtD8IIsztA2ilZm2DhYCuyN58gA==
dependencies:
colorette "^1.2.2"
nanoid "^3.1.23"
source-map-js "^0.6.2"

prelude-ls@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
Expand Down Expand Up @@ -16022,7 +16063,7 @@ rollup@^1.12.0:
"@types/node" "*"
acorn "^7.1.0"

rollup@^2.52.6:
rollup@^2.38.5, rollup@^2.52.6:
version "2.52.6"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.52.6.tgz#7c7546d170dead0e7db0b6c709f7f34398498a8e"
integrity sha512-H+Xudmwf8KO+xji8njQNoIQRp8l+iQge/NdUR20JngTxVYdEEnlpkMvQ71YGLl3+xZcPecmdj4q2lrClKaPdRA==
Expand Down Expand Up @@ -18053,6 +18094,18 @@ verror@1.10.0:
core-util-is "1.0.2"
extsprintf "^1.2.0"

vite@^2.3.8:
version "2.3.8"
resolved "https://registry.yarnpkg.com/vite/-/vite-2.3.8.tgz#42e3e03953859fd410e4e6ab3d1cca0aab2adc3c"
integrity sha512-QiEx+iqNnJntSgSF2fWRQvRey9pORIrtNJzNyBJXwc+BdzWs83FQolX84cTBo393cfhObrtWa6180dAa4NLDiQ==
dependencies:
esbuild "^0.12.8"
postcss "^8.3.4"
resolve "^1.20.0"
rollup "^2.38.5"
optionalDependencies:
fsevents "~2.3.2"

vm-browserify@^1.0.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0"
Expand Down

0 comments on commit 392232c

Please sign in to comment.