Skip to content

Commit

Permalink
fix: ESM bundle for Node.js, #739
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 27, 2024
1 parent 7c4bd7e commit ce84cd6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion benchmark/engines/liquid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { createEngine } = require('./create-liquid')

module.exports = createEngine(require('../../dist/liquid.node.cjs'))
module.exports = createEngine(require('../../dist/liquid.cjs'))
6 changes: 3 additions & 3 deletions bin/perf-diff.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash

VERSION_LATEST=$(cat package.json | grep '"version":' | head -1 | awk -F'"' '{print $4}')
FILE_LOCAL=dist/liquid.node.cjs.js
FILE_LATEST=dist/liquid.node.cjs.$VERSION_LATEST.js
URL_LATEST=https://unpkg.com/liquidjs@$VERSION_LATEST/dist/liquid.node.cjs.js
FILE_LOCAL=dist/liquid.node.js
FILE_LATEST=dist/liquid.node.$VERSION_LATEST.js
URL_LATEST=https://unpkg.com/liquidjs@$VERSION_LATEST/dist/liquid.node.js

if [ ! -f "$FILE_LATEST" ]; then
curl $URL_LATEST > $FILE_LATEST
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
"name": "liquidjs",
"version": "10.16.4",
"description": "A simple, expressive and safe Shopify / Github Pages compatible template engine in pure JavaScript.",
"main": "dist/liquid.node.cjs.js",
"module": "dist/liquid.node.esm.js",
"es2015": "dist/liquid.browser.esm.js",
"main": "dist/liquid.node.js",
"module": "dist/liquid.node.mjs",
"es2015": "dist/liquid.browser.mjs",
"browser": {
"./dist/liquid.node.cjs.js": "./dist/liquid.browser.umd.js",
"./dist/liquid.node.esm.js": "./dist/liquid.browser.esm.js"
"./dist/liquid.node.js": "./dist/liquid.browser.umd.js",
"./dist/liquid.node.mjs": "./dist/liquid.browser.mjs"
},
"exports": {
"import": "./dist/liquid.node.mjs",
"require": "./dist/liquid.node.js"
},
"types": "dist/index.d.ts",
"engines": {
Expand All @@ -19,6 +23,7 @@
"test": "jest",
"test:coverage": "jest --coverage src test/integration",
"test:e2e": "jest test/e2e",
"test:demo": "./test/demo/check.sh",
"perf:diff": "bin/perf-diff.sh",
"perf:engines": "cd benchmark && npm run engines",
"version": "npm run build && npm test",
Expand Down
8 changes: 4 additions & 4 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ const browserStream = {
'./streamed-emitter': '../build/streamed-emitter-browser'
}
const esmRequire = {
include: './src/fs/node.ts',
include: './src/fs/fs-impl.ts',
delimiters: ['', ''],
'./node-require': '../build/node-require.mjs'
}

const nodeCjs = {
output: [{
file: 'dist/liquid.node.cjs.js',
file: 'dist/liquid.node.js',
format: 'cjs',
banner
}],
Expand All @@ -70,7 +70,7 @@ const nodeCjs = {

const nodeEsm = {
output: [{
file: 'dist/liquid.node.esm.js',
file: 'dist/liquid.node.mjs',
format: 'esm',
banner
}],
Expand All @@ -86,7 +86,7 @@ const nodeEsm = {

const browserEsm = {
output: [{
file: 'dist/liquid.browser.esm.js',
file: 'dist/liquid.browser.mjs',
format: 'esm',
banner
}],
Expand Down
6 changes: 1 addition & 5 deletions src/build/node-require.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { createRequire } from 'module'

export function requireResolve (file) {
/**
* createRequire() can throw,
* when import.meta.url not begin with "file://".
*/
const require = createRequire(import.meta.url)
const require = createRequire(process.cwd() + '/')
return require.resolve(file)
}
4 changes: 2 additions & 2 deletions test/e2e/render-to-node-stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { drainStream } from '../stub/stream'

describe('.renderToNodeStream()', function () {
it('should render to stream in Node.js', done => {
const cjs = require('../../dist/liquid.node.cjs')
const cjs = require('../../dist/liquid.node')
const engine = new cjs.Liquid()
const tpl = engine.parseFileSync(resolve(__dirname, '../stub/root/foo.html'))
const stream = engine.renderToNodeStream(tpl)
Expand All @@ -28,7 +28,7 @@ describe('.renderToNodeStream()', function () {

describe('.renderFileToNodeStream()', function () {
it('should render to stream in Node.js', async () => {
const cjs = require('../../dist/liquid.node.cjs')
const cjs = require('../../dist/liquid.node')
const engine = new cjs.Liquid({
root: resolve(__dirname, '../stub/root/')
})
Expand Down

0 comments on commit ce84cd6

Please sign in to comment.