Skip to content

Commit

Permalink
Chore: Annotate the CommonJS export names for ESM import in Node
Browse files Browse the repository at this point in the history
  * we need to build commonjs module as `browser` to avoid nanoid using
    crypto module which is native node.js module
  * instead of that we want to use web crypto api
  * using `platform:browser` bundler finds right export from nanoid and
    use it
  * but for this platform bundler did not annotate the commonjs export
    so we must do it manually
  • Loading branch information
literat authored and OndraM committed Dec 14, 2021
1 parent 82f2f9a commit 09aa8e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"browser": {
"./LmcCookieConsentManager.js": "./LmcCookieConsentManager.js",
"./LmcCookieConsentManager.cjs": "./LmcCookieConsentManager.cjs",
"./LmcCookieConsentManager.mjs": "./LmcCookieConsentManager.mjs",
"crypto": false
"./LmcCookieConsentManager.mjs": "./LmcCookieConsentManager.mjs"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -47,7 +46,7 @@
"prebuild": "npm-run-all --serial build:clean build:copy",
"build:clean": "rm -rf dist && mkdir -p dist/scss",
"build:copy": "cp package.json README.md src/LmcCookieConsentManager.scss dist/ && cp -r src/scss/* dist/scss/",
"build": "npm-run-all --serial js:compile css",
"build": "npm-run-all --serial js css",
"start": "node scripts/serve.js",
"format": "yarn format:check",
"format:check": "prettier --check 'src/**/*.{js,jsx,ts,tsx,scss}' 'scripts/*'",
Expand Down
39 changes: 17 additions & 22 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
const { build } = require('esbuild');
const replace = require('replace-in-file');
const fs = require('fs');

// iife
build({
Expand Down Expand Up @@ -31,30 +31,25 @@ build({
bundle: true,
target: 'es6',
outfile: 'dist/LmcCookieConsentManager.cjs',
platform: 'node',
platform: 'browser',
format: 'cjs',
/**
* the "main" field was ignored (main fields must be configured manually when using the "neutral" platform)
* because vanilla-cookie-consent is set as `main` in package.json
*/
mainFields: ['main'],
})
.then(() => {
const options = {
files: './dist/LmcCookieConsentManager.cjs',
from: /0 && \(module.exports = {}\);/gm,
to: 'module.exports = LmcCookieConsentManager_default;',
};

replace(options)
.then((results) => {
console.log('Replacement results:', results);
})
.catch((error) => {
console.error('Error occurred:', error);
});
})
.catch((error) => {
console.error(error);
process.exit(1);
});
}).then(() => {
// Annotate the CommonJS export names for ESM import in node
fs.appendFile(
'./dist/LmcCookieConsentManager.cjs',
`// Annotate the CommonJS export names for ESM import in node:
module.exports = LmcCookieConsentManager_default;
`,
(error) => {
if (error) {
console.log(error);
process.exit(1);
}
},
);
});

0 comments on commit 09aa8e6

Please sign in to comment.