Skip to content

Commit

Permalink
Dynamic import support
Browse files Browse the repository at this point in the history
[dynamic import](https://github.com/tc39/proposal-dynamic-import) support has just landed in [Rollup 0.55.0](https://github.com/rollup/rollup/blob/master/CHANGELOG.md#upcoming-0550).
When testing a project that uses nodent via rollup-plugin-nodent, I noticed that it doesn't support dynamic import yet, resulting in the error

```
SyntaxError: Unexpected token in ...
```

Looking at the [rollup source code](https://github.com/rollup/rollup/blob/089c9e06190b894d766fc44571752fa91dc1eaa5/src/Graph.ts#L3), I see that they are using [acorn-dynamic-import](https://github.com/kesne/acorn-dynamic-import).
This patches the acorn version that is used in nodent to add the acorn-dynamic-import plugin as done in rollup itself.
That way, the nodent compiler is able to parse code that uses dynamic import.

This is quite similar to what has been done for [buble](bublejs/buble#102).

Also switch from xo to eslint and eslint-config/plugin-*
  • Loading branch information
oligot committed Jan 25, 2018
1 parent efc0b42 commit 18017ad
Show file tree
Hide file tree
Showing 4 changed files with 1,359 additions and 7 deletions.
17 changes: 17 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"plugins": [
"unicorn",
"import"
],
"extends": [
"xo/esnext",
"plugin:unicorn/recommended",
"plugin:import/recommended"
],
"rules": {
"import/no-unresolved": ["error", { commonjs: true }]
},
"parserOptions": {
"sourceType": "script"
}
}
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
const nodent = require('nodent')();
const path = require('path');
const importFrom = require('import-from');
// Patch acorn used in nodent-compiler to support dynamic import
const acorn = importFrom(path.dirname(require.resolve('nodent-compiler')), 'acorn');
// eslint-disable-next-line import/no-extraneous-dependencies
require('acorn-dynamic-import/lib/inject').default(acorn);
const NodentCompiler = require('nodent-compiler');
const createFilter = require('rollup-pluginutils').createFilter;

module.exports = function (options) {
options = options || {};
const filter = createFilter(options.include, options.exclude);
const compiler = new NodentCompiler();

return {
name: 'nodent',
options(opts) {
options.sourcemap = 'sourcemap' in options ? options.sourcemap : // Plugin options
'sourcemap' in opts ? opts.sourcemap : // Rollup options
opts.sourceMap !== false; // Old style Rollup options
'sourcemap' in opts ? opts.sourcemap : // Rollup options
opts.sourceMap !== false; // Old style Rollup options
Object.assign(options, {
parser: {
plugins: {
dynamicImport: true
}
}
});
},
transform(code, id) {
if (!filter(id)) {
return;
}
const res = nodent.compile(code, id, null, options);
const res = compiler.compile(code, id, options);
return {
code: res.code,
map: options.sourcemap ? res.sourcemap : {mappings: ''}
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"node": ">=4"
},
"scripts": {
"test": "xo"
"test": "eslint index.js"
},
"keywords": [
"rollup-plugin",
Expand All @@ -18,10 +18,15 @@
"license": "MIT",
"repository": "oligot/rollup-plugin-nodent",
"dependencies": {
"nodent": "^3.0.17",
"acorn-dynamic-import": "^3.0.0",
"import-from": "^2.1.0",
"nodent-compiler": "^3.1.5",
"rollup-pluginutils": "^2.0.1"
},
"devDependencies": {
"xo": "^0.18.2"
"eslint": "^4.16.0",
"eslint-config-xo": "^0.19.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-unicorn": "^3.0.1"
}
}

0 comments on commit 18017ad

Please sign in to comment.