Skip to content

Commit 957c7cd

Browse files
amirehmatyasf
authored andcommitted
fix(esm): don't mark commonjs as esm
refs FOO-1331 when package.json specifies { "type": "module" }, files inside lib/ are treated by Node, starting from 12.5, as ESM when they are in fact commonjs. This makes it impossible to require() them. To correctly indicate the format to Node, this patch supplies the lib/ folder with a package.json that specifies the intended format: { "type": "commonjs" } :: test plan :: using node 14, you are able to require a package that has both ESM and CJS output: nvm use v14 node ./packages/canvas-theme Change-Id: Ibb58abf12a706b7fe63fa01723fc048b6913cfd7
1 parent 09c0888 commit 957c7cd

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

packages/command-utils/lib/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,17 @@ function runCommandsConcurrently(commands) {
9191
]
9292

9393
Object.keys(commands).forEach((name) => {
94-
const command = commands[name]
95-
if (command) {
96-
args.push(
97-
`${command.toString()}${
98-
command.args.length > 0 ? ` ${command.args.join(' ')} ` : ''
99-
}`
100-
)
94+
let commandList = commands[name]
95+
96+
if (commandList) {
97+
commandList = Array.isArray(commandList) ? commandList : [commandList]
98+
commandList.forEach(command => {
99+
args.push(
100+
`${command.toString()}${
101+
command.args.length > 0 ? ` ${command.args.join(' ')} ` : ''
102+
}`
103+
)
104+
})
101105
}
102106
})
103107

packages/ui-scripts/lib/build/babel.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
* SOFTWARE.
2323
*/
2424

25+
const path = require('path')
2526
const {
2627
runCommandsConcurrently,
2728
getCommand
2829
} = require('@instructure/command-utils')
30+
const specifyCJSFormat = path.resolve(__dirname, 'specify-commonjs-format.sh')
2931

3032
const {
3133
BABEL_ENV,
@@ -102,11 +104,14 @@ const commands = {
102104
[...babelArgs, '--out-dir', 'es'],
103105
[...envVars, 'ES_MODULES=1']
104106
),
105-
cjs: getCommand(
106-
'babel',
107-
[...babelArgs, '--out-dir', 'lib'],
108-
[...envVars, 'TRANSFORM_IMPORTS=1']
109-
)
107+
cjs: [
108+
getCommand(
109+
'babel',
110+
[...babelArgs, '--out-dir', 'lib'],
111+
[...envVars, 'TRANSFORM_IMPORTS=1']
112+
)
113+
getCommand(specifyCJSFormat, [], []),
114+
]
110115
}
111116

112117
const commandsToRun = modules.reduce(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
if [[ ! -f lib/package.json ]]; then
4+
mkdir -p lib
5+
echo '{"type":"commonjs"}' > lib/package.json
6+
fi

0 commit comments

Comments
 (0)