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 Apr 10, 2021
1 parent 796fef4 commit b6487fe
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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
/packages/test-setup/**/*.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
41 changes: 41 additions & 0 deletions packages/vite/package.json
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"
}
}
110 changes: 110 additions & 0 deletions packages/vite/src/index.ts
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);
}
}
86 changes: 81 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
dependencies:
"@babel/types" "^7.13.12"

"@babel/helper-module-imports@^7.0.0", "@babel/helper-module-imports@^7.12.13", "@babel/helper-module-imports@^7.13.12", "@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.13.12", "@babel/helper-module-imports@^7.8.3":
version "7.13.12"
resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.13.12.tgz#c6a369a6f3621cb25da014078684da9196b61977"
integrity sha512-4cVvR2/1B693IuOvSI20xqqa/+bl7lqAMR59R4iu39R9aOX8/JoYY1sFaNvUMyMBGnHdwvJgUrzNLoUZxXypxA==
Expand Down Expand Up @@ -1686,6 +1686,36 @@
dependencies:
"@octokit/openapi-types" "^6.0.0"

"@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/plugin-commonjs@^18.0.0":
version "18.0.0"
resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-18.0.0.tgz#50dc7518b5aa9e66a270e529ea85115d269825c4"
integrity sha512-fj92shhg8luw7XbA0HowAqz90oo7qtLGwqTKbyZ8pmOyH8ui5e+u0wPEgeHLH3djcVma6gUCUrjY6w5R2o1u6g==
dependencies:
"@rollup/pluginutils" "^3.1.0"
commondir "^1.0.1"
estree-walker "^2.0.1"
glob "^7.1.6"
is-reference "^1.2.1"
magic-string "^0.25.7"
resolve "^1.17.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 @@ -1929,6 +1959,11 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.47.tgz#d7a51db20f0650efec24cd04994f523d93172ed4"
integrity sha512-c5ciR06jK8u9BstrmJyO97m+klJrrhCf9u3rLu3DEAJBirxRqSCvDQoYKmxuYwQI5SZChAWu+tq9oVlGRuzPAg==

"@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/express-serve-static-core@^4.17.18":
version "4.17.19"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.19.tgz#00acfc1632e729acac4f1530e9e16f6dd1508a1d"
Expand Down Expand Up @@ -5769,7 +5804,7 @@ color-name@~1.1.4:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

colorette@^1.2.1:
colorette@^1.2.1, colorette@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.2.tgz#cbcc79d5e99caea2dbf10eb3a26fd8b3e6acfa94"
integrity sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==
Expand Down Expand Up @@ -8724,6 +8759,11 @@ es6-promisify@^5.0.0:
dependencies:
es6-promise "^4.0.3"

esbuild@^0.9.3:
version "0.9.7"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.9.7.tgz#ea0d639cbe4b88ec25fbed4d6ff00c8d788ef70b"
integrity sha512-VtUf6aQ89VTmMLKrWHYG50uByMF4JQlVysb8dmg6cOgW8JnFCipmz7p+HNBl+RR3LLCuBxFGVauAe2wfnF9bLg==

escalade@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
Expand Down Expand Up @@ -9166,6 +9206,16 @@ estree-walker@^0.6.0, 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==

estree-walker@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac"
integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==

esutils@^2.0.0, esutils@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
Expand Down Expand Up @@ -11506,7 +11556,7 @@ is-redirect@^1.0.0:
resolved "https://registry.yarnpkg.com/is-redirect/-/is-redirect-1.0.0.tgz#1d03dded53bd8db0f30c26e4f95d36fc7c87dc24"
integrity sha1-HQPd7VO9jbDzDCbk+V02/HyH3CQ=

is-reference@^1.1.0:
is-reference@^1.1.0, is-reference@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/is-reference/-/is-reference-1.2.1.tgz#8b2dac0b371f4bc994fdeaba9eb542d03002d0b7"
integrity sha512-U82MsXXiFIrjCK4otLT+o2NA2Cd2g5MLoOVXUZjIOhLurrRxpEXzI8O0KZHr3IjLvlAH1kTPYSuqer5T9ZVBKQ==
Expand Down Expand Up @@ -13486,6 +13536,11 @@ nan@^2.12.1:
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.2.tgz#f5376400695168f4cc694ac9393d0c9585eeea19"
integrity sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==

nanoid@^3.1.22:
version "3.1.22"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.22.tgz#b35f8fb7d151990a8aebd5aa5015c03cf726f844"
integrity sha512-/2ZUaJX2ANuLtTvqTlgqBQNJoQO398KyJgZloL0PZkC0dpysjncRUPsFe3DUPzz/y3h+u7C46np8RMuvF3jsSQ==

nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
Expand Down Expand Up @@ -14371,7 +14426,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.0.5, picomatch@^2.2.1:
picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.2.1, picomatch@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad"
integrity sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==
Expand Down Expand Up @@ -14531,6 +14586,15 @@ postcss@^7.0.14, postcss@^7.0.32, postcss@^7.0.5, postcss@^7.0.6:
source-map "^0.6.1"
supports-color "^6.1.0"

postcss@^8.2.1:
version "8.2.9"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.2.9.tgz#fd95ff37b5cee55c409b3fdd237296ab4096fba3"
integrity sha512-b+TmuIL4jGtCHtoLi+G/PisuIl9avxs8IZMSmlABRwNz5RLUUACrC+ws81dcomz1nRezm5YPdXiMEzBEKgYn+Q==
dependencies:
colorette "^1.2.2"
nanoid "^3.1.22"
source-map "^0.6.1"

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 @@ -15624,7 +15688,7 @@ rollup@^1.12.0:
"@types/node" "*"
acorn "^7.1.0"

rollup@^2.45.0:
rollup@^2.38.5, rollup@^2.45.0:
version "2.45.1"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.45.1.tgz#eae2b94dc2088b4e0a3b7197a5a1ee0bdd589d5c"
integrity sha512-vPD+JoDj3CY8k6m1bLcAFttXMe78P4CMxoau0iLVS60+S9kLsv2379xaGy4NgYWu+h2WTlucpoLPAoUoixFBag==
Expand Down Expand Up @@ -17601,6 +17665,18 @@ verror@1.10.0:
core-util-is "1.0.2"
extsprintf "^1.2.0"

vite@^2.0.0:
version "2.1.5"
resolved "https://registry.yarnpkg.com/vite/-/vite-2.1.5.tgz#4857da441c62f7982c83cbd5f42a00330f20c9c1"
integrity sha512-tYU5iaYeUgQYvK/CNNz3tiJ8vYqPWfCE9IQ7K0iuzYovWw7lzty7KRYGWwV3CQPh0NKxWjOczAqiJsCL0Xb+Og==
dependencies:
esbuild "^0.9.3"
postcss "^8.2.1"
resolve "^1.19.0"
rollup "^2.38.5"
optionalDependencies:
fsevents "~2.3.1"

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 b6487fe

Please sign in to comment.