vite-plugin-elm
Advanced tools
Comparing version 2.7.0-beta.1 to 2.7.0-beta.2
@@ -19,2 +19,13 @@ "use strict"; | ||
const viteProjectPath = (dependency) => `/${(0, path_1.relative)(process.cwd(), dependency)}`; | ||
const parseImportId = (id) => { | ||
const parsedId = new URL(id, 'file://'); | ||
const pathname = parsedId.pathname; | ||
const valid = pathname.endsWith('.elm'); | ||
const withParams = parsedId.searchParams.getAll('with'); | ||
return { | ||
valid, | ||
pathname, | ||
withParams, | ||
}; | ||
}; | ||
const plugin = (opts) => { | ||
@@ -28,3 +39,4 @@ const compilableFiles = new Map(); | ||
handleHotUpdate({ file, server, modules }) { | ||
if (!file.endsWith('.elm')) | ||
const { valid } = parseImportId(file); | ||
if (!valid) | ||
return; | ||
@@ -52,8 +64,7 @@ const modulesToCompile = []; | ||
async load(id) { | ||
const parsedId = new URL(id, 'file://'); | ||
if (!parsedId.pathname.endsWith('.elm')) | ||
const { valid, pathname, withParams } = parseImportId(id); | ||
if (!valid) | ||
return; | ||
const accompanyCandidates = parsedId.searchParams.getAll('with'); | ||
const accompanies = await (() => { | ||
if (accompanyCandidates.length > 0) { | ||
if (withParams.length > 0) { | ||
const importTree = this.getModuleIds(); | ||
@@ -66,4 +77,4 @@ let importer = ''; | ||
} | ||
const resolveAcoompany = async (accompany) => { var _a; return (_a = (await this.resolve(accompany, importer))) === null || _a === void 0 ? void 0 : _a.id; }; | ||
return Promise.all(accompanyCandidates.map(resolveAcoompany)); | ||
const resolveAcoompany = async (accompany) => { var _a, _b; return (_b = (_a = (await this.resolve(accompany, importer))) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : ''; }; | ||
return Promise.all(withParams.map(resolveAcoompany)); | ||
} | ||
@@ -74,9 +85,9 @@ else { | ||
})(); | ||
const targets = [parsedId.pathname, ...accompanies]; | ||
compilableFiles.delete(parsedId.pathname); | ||
const isBuild = process.env.NODE_ENV === 'production'; | ||
const dependencies = await node_elm_compiler_1.default.findAllDependencies(parsedId.pathname); | ||
compilableFiles.set(parsedId.pathname, new Set(dependencies)); | ||
const targets = [pathname, ...accompanies].filter((target) => target !== ''); | ||
compilableFiles.delete(id); | ||
const dependencies = (await Promise.all(targets.map((target) => node_elm_compiler_1.default.findAllDependencies(target)))).flat(); | ||
compilableFiles.set(id, new Set([...accompanies, ...dependencies])); | ||
const releaseLock = await (0, mutex_1.acquireLock)(); | ||
try { | ||
const isBuild = process.env.NODE_ENV === 'production'; | ||
const compiled = await node_elm_compiler_1.default.compileToString(targets, { | ||
@@ -88,2 +99,3 @@ output: '.js', | ||
}); | ||
const esm = (0, assetsInjector_1.injectAssets)((0, elm_esm_1.toESModule)(compiled)); | ||
// Apparently `addWatchFile` may not exist: https://github.com/hmsk/vite-plugin-elm/pull/36 | ||
@@ -93,3 +105,2 @@ if (this.addWatchFile) { | ||
} | ||
const esm = (0, assetsInjector_1.injectAssets)((0, elm_esm_1.toESModule)(compiled)); | ||
return { | ||
@@ -102,3 +113,3 @@ code: isBuild ? esm : trimDebugMessage((0, hmrInjector_1.injectHMR)(esm, dependencies.map(viteProjectPath))), | ||
if (e instanceof Error && e.message.includes('-- NO MAIN')) { | ||
const message = `${viteProjectPath(parsedId.pathname)}: NO MAIN .elm file is requested to transform by vite. Probably, this file is just a depending module`; | ||
const message = `${viteProjectPath(pathname)}: NO MAIN .elm file is requested to transform by vite. Probably, this file is just a depending module`; | ||
throw message; | ||
@@ -105,0 +116,0 @@ } |
{ | ||
"name": "vite-plugin-elm", | ||
"version": "2.7.0-beta.1", | ||
"version": "2.7.0-beta.2", | ||
"description": "Compile Elm with vite", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -107,2 +107,24 @@ # vite-plugin-elm | ||
## Combine multiple main files (Experimental from `2.7.0-beta.1`) | ||
By passing importing path via `with` URL-ish parameter(s), the plugin compiles multiple main files in a single compilation process. That generates a single `Elm` export which has multiple properties for each given main files. This way reduces bundle size comparing to a total size of importing each file separately since common modules/Elm core codes are not repeated. | ||
```ts | ||
// `Elm.App` and `Elm.Another`, both can work as like importing individually. | ||
import { Elm } from './App.elm?with=./Another.elm' | ||
Elm.App.init({ | ||
node: document.getElementById('rootForApp'), | ||
}) | ||
Elm.Another.init({ | ||
node: document.getElementById('rootForAnother'), | ||
}) | ||
``` | ||
For 3+ main files: | ||
```ts | ||
import { Elm } from './App.elm?with=./Another.elm&./YetAnother.elm' | ||
``` | ||
## Acknowledgement | ||
@@ -109,0 +131,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42822
577
137