Skip to content

Commit

Permalink
Support ESM mix config
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Sep 28, 2021
1 parent 43e8f14 commit 61e3066
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
6 changes: 5 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function executeScript(cmd, opts, args = []) {
// containg spaces on Windows (yarn does)
const configPath = path.relative(
process.cwd(),
require.resolve('../setup/webpack.config.js')
require.resolve('../setup/webpack.config.mjs')
);

const script = [
Expand All @@ -77,6 +77,10 @@ async function executeScript(cmd, opts, args = []) {
].join(' ');

const scriptEnv = {
// Allow dynamic ESM imports
// This is an unfortunate workaround but it's the simplest way
// to get dynamic ESM imports & webpack-cli to play together
DISABLE_V8_COMPILE_CACHE: '1',
NODE_ENV: env,
MIX_FILE: opts.mixConfig
};
Expand Down
16 changes: 3 additions & 13 deletions setup/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
const { assertSupportedNodeVersion } = require('../src/Engine');
const Mix = require("../src/Mix")

module.exports = async () => {
module.exports = () => {
assertSupportedNodeVersion();

const mix = Mix.primary;
const config = await import('./webpack.config.mjs');

// Load the user's mix config
await mix.load();

// Install any missing dependencies
await mix.installDependencies();

// Start running
await mix.init();

// Turn everything into a config
return await mix.build();
return await config.default();
};
17 changes: 17 additions & 0 deletions setup/webpack.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Mix from '../src/Mix';

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

// Load the user's mix config
await mix.load();

// Install any missing dependencies
await mix.installDependencies();

// Start running
await mix.init();

// Turn everything into a config
return await mix.build();
}
6 changes: 4 additions & 2 deletions src/Mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ class Mix {
*/
async load() {
// 1. Pull in the user's mix config file
const mod = require(this.paths.mix())
// An ESM import here allows a user's mix config
// to be an ESM module and use top-level await
const mod = await import(this.paths.mix());

// Allow the user to export a default `function (mix) { … }` from their config file
// Allow the user to `export default function (mix) { … }` from their config file
if (typeof mod.default === 'function') {
await mod.default(this.api)
}
Expand Down
4 changes: 4 additions & 0 deletions src/Paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ class Paths {
if (process.env.MIX_FILE) {
return [
process.env.MIX_FILE,
`${process.env.MIX_FILE}.mjs`,
`${process.env.MIX_FILE}.cjs`,
`${process.env.MIX_FILE}.js`,
]
}

return [
"webpack.mix.mjs",
"webpack.mix.cjs",
"webpack.mix.js",
]
}
Expand Down

0 comments on commit 61e3066

Please sign in to comment.