Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use virtual module prefix to play nice with other plugins #3

Merged
merged 2 commits into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"mocha": "^2.4.5",
"rimraf": "^2.5.4",
"rollup": "^0.25.4",
"rollup-plugin-babel": "^2.4.0"
"rollup-plugin-babel": "^2.4.0",
"rollup-plugin-node-resolve": "^2.0.0"
},
"repository": {
"type": "git",
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const PROCESS_PATH = require.resolve('process-es6');
const BUFFER_PATH = require.resolve('buffer-es6');
const GLOBAL_PATH = join(__dirname, '..', 'src', 'global.js');

const DIRNAME = '\0node-globals:dirname';
const FILENAME = '\0node-globals:filename';

function clone(obj) {
var out = {};
Object.keys(obj).forEach(function(key) {
Expand All @@ -27,8 +30,8 @@ var _mods2 = {
process: PROCESS_PATH,
Buffer: [BUFFER_PATH, 'Buffer'],
global: GLOBAL_PATH,
__filename: '__filename',
__dirname: '__dirname'
__filename: FILENAME,
__dirname: DIRNAME
};
var mods1 = new Map();
var mods2 = new Map();
Expand Down Expand Up @@ -60,12 +63,12 @@ export default options => {
}
},
resolveId(importee, importer) {
if (importee === '__dirname') {
if (importee === DIRNAME) {
let id = randomBytes(15).toString('hex');
dirs.set(id, dirname('/' + relative(basedir, importer)));
return id;
}
if (importee === '__filename') {
if (importee === FILENAME) {
let id = randomBytes(15).toString('hex');
dirs.set(id, '/' + relative(basedir, importer));
return id;
Expand Down
23 changes: 23 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

var rollup = require( 'rollup' );
var nodeResolve = require( 'rollup-plugin-node-resolve' );
var globals = require( '../dist/rollup-plugin-node-globals.cjs' );
var path = require('path');
var vm = require('vm');
Expand Down Expand Up @@ -44,4 +45,26 @@ describe( 'rollup-plugin-node-globals', function () {
});
});
})

it( 'works with rollup-plugin-node-resolve', function () {
return rollup.rollup({
entry: 'test/examples/dirname.js',
plugins: [
nodeResolve(),
globals()
]
}).then( function ( bundle ) {
var generated = bundle.generate();
var code = generated.code;
var script = new vm.Script(code);
var context = vm.createContext({
dirname: path.join(__dirname, 'examples'),
filename: path.join(__dirname, 'examples', 'dirname.js'),
setTimeout: setTimeout,
clearTimeout: clearTimeout,
});
context.self = context;
script.runInContext(context);
});
});
});