Skip to content

Commit

Permalink
Rework booting
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Oct 21, 2021
1 parent a9f960a commit 096f6f2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
5 changes: 4 additions & 1 deletion setup/webpack.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Mix from '../src/Mix.js';

export default async function () {
const mix = Mix.primary;
const mix = Mix.shared;

// Boot mix
await mix.boot();

// Load the user's mix config
await mix.load();
Expand Down
34 changes: 11 additions & 23 deletions src/Mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let { Resolver } = require('./Resolver');

class Mix {
/** @type {Mix|null} */
static _primary = null;
static #instance = null;

/** @type {Record<string, boolean>} */
static _hasWarned = {};
Expand Down Expand Up @@ -47,8 +47,6 @@ class Mix {
/** @type {Task[]} */
this.tasks = [];

this.booted = false;

this.bundlingJavaScript = false;

/**
Expand All @@ -73,8 +71,13 @@ class Mix {
/**
* @internal
*/
static get primary() {
return Mix._primary || (Mix._primary = new Mix());
static get shared() {
if (Mix.#instance) {
return Mix.#instance;
}

// @ts-ignore
return (Mix.#instance = new Mix());
}

/**
Expand All @@ -86,16 +89,9 @@ class Mix {
// to be an ESM module and use top-level await
const mod = await import(this.paths.mix());

// If the user is exporting a function then the user has likely not
// imported mix and it has not "booted" because of this
// We work around this by explicitly booting mix (if it hasn't been) after loading the config

// Because this can run in common js environments we cannot make this process async (nor should we ever need to)
this.boot();

// Allow the user to `export default function (mix) { … }` from their config file
if (typeof mod.default === 'function') {
await mod.default(this.api)
await mod.default(this.api);
}
}

Expand All @@ -109,25 +105,17 @@ class Mix {
/**
* @internal
*/
boot() {
if (this.booted) {
return this;
}

this.booted = true;

async boot() {
// Load .env
Dotenv.config();

// If we're using Laravel set the public path by default
if (this.sees('laravel')) {
if (File.exists('./artisan')) {
this.config.publicPath = 'public';
}

this.listen('init', () => this.hot.record());
this.makeCurrent();

return this;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ require('./helpers');
|
*/

let mix = Mix.primary;

mix.boot();

module.exports = mix.api;
module.exports = Mix.shared.api;
5 changes: 3 additions & 2 deletions test/helpers/mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export let Mix;
/** @type {import('../../src/Mix')['api']} */
export let mix;

test.beforeEach(() => {
Mix = new MixClass().boot();
test.beforeEach(async () => {
Mix = new MixClass();
await Mix.boot();
mix = Mix.api;

fs.ensureDirSync(`test/fixtures/app/dist`);
Expand Down

0 comments on commit 096f6f2

Please sign in to comment.