Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for ESM tailwind.config.js files #3544

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/docs
/__tests__/fixtures/cli-utils.js
/stubs/*
/src/util/requireOrImportConfig.js
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
}
}
]
],
"exclude": [
"src/util/requireOrImportConfig.js"
]
},
"jest": {
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import processTailwindFeatures from './processTailwindFeatures'
import formatCSS from './lib/formatCSS'
import resolveConfig from './util/resolveConfig'
import getAllConfigs from './util/getAllConfigs'
import requireOrImportConfig from './util/requireOrImportConfig'
import { supportedConfigFiles } from './constants'
import defaultConfig from '../stubs/defaultConfig.stub.js'

Expand Down Expand Up @@ -45,7 +46,7 @@ function resolveConfigPath(filePath) {
return undefined
}

const getConfigFunction = (config) => () => {
const getConfigFunction = (config) => async () => {
if (_.isUndefined(config)) {
return resolveConfig([...getAllConfigs(defaultConfig)])
}
Expand All @@ -59,7 +60,9 @@ const getConfigFunction = (config) => () => {
}
}

const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config)
const configObject = _.isObject(config)
? _.get(config, 'config', config)
: await requireOrImportConfig(config)

return resolveConfig([...getAllConfigs(configObject)])
}
Expand Down
4 changes: 2 additions & 2 deletions src/processTailwindFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ let processedPlugins = null
let getProcessedPlugins = null

export default function (getConfig) {
return function (css) {
const config = getConfig()
return async function (css) {
const config = await getConfig()
const configChanged = hash(previousConfig) !== hash(config)
previousConfig = config

Expand Down
26 changes: 26 additions & 0 deletions src/util/requireOrImportConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";

// Node v12.17+ exposes `import` within CJS files
// in order to `require` ESM files.

// This file is intentionally excluded from `babel` (and `eslint`)
// to avoid transpiling away the `import` statement

Object.defineProperty(exports, "__esModule", {
value: true
});

exports.default = requireOrImportConfig;

function requireOrImportConfig(config) {
try {
return require(config)
} catch (e) {
if (e.code === 'ERR_REQUIRE_ESM') {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it is a different exception? Right now you will return null, but I think you should throw the original exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, that makes total sense. I will update this!

try {
return import(config).then(mdl => mdl.default)
} catch (e) {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my previous comment, maybe just throw the original exception in here if import also failed, instead of an empty catch?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@natemoo-re Might you update the pull request, please?

}
}
return null;
}