Skip to content

Commit

Permalink
Fix ES modules importing (#70)
Browse files Browse the repository at this point in the history
* Fix ES modules importing (backport of #68)
* Add trunk branches to GH Workflow (backport of #71)
  • Loading branch information
ogonkov committed Jun 28, 2020
1 parent aa48536 commit bf6e92a
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 43 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/nodejs.yml → .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI
name: Tests

on:
push:
branches: [ master ]
branches:
- master
- trunk-v*
pull_request:
branches: [ master ]
branches:
- master
- trunk-v*

jobs:
tests:
Expand Down
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![npm package][npm-image]][npm-url]
[![Coverage Status][coverage-image]][coverage-url]
[![node][node]][node-url]
![Build Status][build-image]
[![Build Status][build-image]][build-url]
[![Dependencies Status][david-image]][david-url]

# Nunjucks templates loader for Webpack
Expand Down Expand Up @@ -341,9 +341,9 @@ module.exports = {

**app/global-env.js**
```js
module.exports = function(foo, bar) {
export default function globalFn(foo, bar) {
return `Do anything with ${foo} and ${bar}`;
};
}
```

### extensions
Expand Down Expand Up @@ -376,13 +376,13 @@ module.exports = {
**lib/extensions/custom-extension.js**
```js
// You should use slim bundle to make it work in browser
const nunjucks = require('nunjucks/browser/nunjucks-slim');
import nunjucks from 'nunjucks/browser/nunjucks-slim';

// See example in docs
// https://mozilla.github.io/nunjucks/api.html#custom-tags
class CustomExtension {}

module.exports = new CustomExtension();
export default new CustomExtension();
```

Loader trying to guess which extensions are really used, and keep only required
Expand Down Expand Up @@ -419,9 +419,9 @@ module.exports = {
**foo.js**

```js
module.exports = function(val, param) {
export default function filter(val, param) {
return `${val + param}`;
};
}
```

**template.njk**
Expand All @@ -435,13 +435,13 @@ To mark filter as async, filter module should export `async` flag:
**async-filter.js**

```js
module.exports = function(val, param, callback) {
export default function asyncFilter(val, param, callback) {
setTimeout(function() {
callback(null, val + param);
}, 1000);
};
}

module.exports.async = true;
asyncFilter.async = true;
```

[nunjucks-github]:https://github.com/mozilla/nunjucks
Expand All @@ -457,10 +457,11 @@ module.exports.async = true;

[npm-image]:https://img.shields.io/npm/v/simple-nunjucks-loader.svg
[npm-url]:http://npmjs.org/package/simple-nunjucks-loader
[coverage-image]:https://coveralls.io/repos/github/ogonkov/nunjucks-loader/badge.svg?branch=master
[coverage-url]:https://coveralls.io/github/ogonkov/nunjucks-loader?branch=master
[coverage-image]:https://coveralls.io/repos/github/ogonkov/nunjucks-loader/badge.svg?branch=trunk-v1
[coverage-url]:https://coveralls.io/github/ogonkov/nunjucks-loader?branch=trunk-v1
[node]: https://img.shields.io/node/v/simple-nunjucks-loader.svg
[node-url]: https://nodejs.org
[build-image]:https://github.com/ogonkov/nunjucks-loader/workflows/Node.js%20CI/badge.svg
[build-image]:https://github.com/ogonkov/nunjucks-loader/workflows/Tests/badge.svg?branch=trunk-v1
[build-url]:https://github.com/ogonkov/nunjucks-loader/actions?query=branch%3Atrunk-v1+workflow%3ATests
[david-image]:https://david-dm.org/ogonkov/nunjucks-loader/status.svg
[david-url]:https://david-dm.org/ogonkov/nunjucks-loader
24 changes: 13 additions & 11 deletions src/output/get-extensions.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import {stringifyRequest} from 'loader-utils';
import {getModuleOutput} from './get-module-output';

export function getExtensions(extensions) {
function imports(loaderContext) {
return extensions.map(([name, importPath]) => (
`
var _extension_${name} = require(${stringifyRequest(
loaderContext,
importPath
)});
__nunjucks_module_dependencies__.extensions['${name}'] = {
module: _extension_${name}
};
`
)).join('');
return extensions.map(([name, importPath]) => {
const importVar = `_extension_${name}`;

return `
var ${importVar} = require(${stringifyRequest(
loaderContext,
importPath
)});
__nunjucks_module_dependencies__.extensions['${name}'] = {
module: ${getModuleOutput(importVar)}
};`;
}).join('');
}

return {
Expand Down
14 changes: 9 additions & 5 deletions src/output/get-filters.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import {stringifyRequest} from 'loader-utils';
import {getModuleOutput} from './get-module-output';

export function getFilters(filters) {
function imports(loaderContext) {
return filters.map(([filterName, importPath, filterInstance]) => (`
var _filter_${filterName} = require(${stringifyRequest(
return filters.map(([filterName, importPath, filterInstance]) => {
const importVar = `_filter_${filterName}`;

return `
var ${importVar} = require(${stringifyRequest(
loaderContext,
importPath
)});
__nunjucks_module_dependencies__.filters['${filterName}'] = {
module: _filter_${filterName},
module: ${getModuleOutput(importVar)},
async: ${JSON.stringify(filterInstance.async === true)}
};
`)).join('');
};`;
}).join('');
}

return {
Expand Down
22 changes: 12 additions & 10 deletions src/output/get-globals.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import {toVar} from '../utils/to-var';
import {stringifyRequest} from 'loader-utils';
import {getModuleOutput} from './get-module-output';

export function getGlobals(globals) {
function imports(loaderContext) {
return globals.map(([globalImport, globalPath]) => (
`
var _global_${toVar(globalImport)} = require(${
stringifyRequest(loaderContext,globalPath)
});
__nunjucks_module_dependencies__.globals['${globalImport}'] = {
module: _global_${toVar(globalImport)}
};
`
)).join('')
return globals.map(([globalImport, globalPath]) => {
const importVar = `_global_${toVar(globalImport)}`;

return `
var ${importVar} = require(${
stringifyRequest(loaderContext, globalPath)
});
__nunjucks_module_dependencies__.globals['${globalImport}'] = {
module: ${getModuleOutput(importVar)}
};`;
}).join('')
}

return {
Expand Down
3 changes: 3 additions & 0 deletions src/output/get-module-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getModuleOutput(importedSymbol) {
return `${importedSymbol} && ${importedSymbol}.default || ${importedSymbol}`;
}
4 changes: 3 additions & 1 deletion src/precompile/get-addons-meta.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {getModule} from '../utils/get-module';

/**
* @param {[string, string]} addonEntry
* @return {Promise<[string, string, function]>}
*/
function loadAddon([name, importPath]) {
return import(importPath).then(function(instance) {
return [name, importPath, instance];
return [name, importPath, getModule(instance)];
});
}

Expand Down

0 comments on commit bf6e92a

Please sign in to comment.