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

update for dart 2.0 and fix some bugs #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ node_modules
*.tgz

example/bundle.js

# idea
.DS_Store
node_modules
.idea
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ module.exports = {
}
```

```js
// webpack-chain
module.exports = {
chainWebpack: config => {
config.module.rule('dart')
.test(/\.dart$/)
.use('dart-loader')
.loader('dart-loader')
.options({
minimize: '-m'
})
.end()
}
}
```

Make sure you have the `dart2js` binary somewhere in your `PATH`.

### Example
Expand Down
74 changes: 61 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,67 @@
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const colors = require('colors');
const loaderUtils = require('loader-utils');

module.exports = function(source) {
const callback = this.async();
const tmp = path.join(__dirname, 'tmp');
const fname = path.basename(this.resourcePath, '.dart');
const cmd = `dart2js -o '${path.join(tmp, `${fname}.js`)}' '${this.resourcePath}'`;
child_process.execSync(`rm -rf '${tmp}'`);
child_process.execSync(`mkdir -p '${tmp}'`);

child_process.exec(cmd, function(error, stdout, stderr) {
if (error) { return callback(error, null); }
const out = fs.readFileSync(path.join(tmp, `${fname}.js`), 'utf8');
callback(null, out);
});
/**
* @summary
* The map between the options passed to the loader
* and the command-line `dart2js` utility flags.
* Option '--enable-checked-mode' is not needed in Dart 2.0
*/
const optsMap = {
'minimize': '-m'
};

/**
* @summary
* Construct a command-line `dart2js` utility options string
* from a given loader options object.
*
* @return {string}
*/
const constructDart2JsOptions = (options) => {
if (options instanceof Object) {
const optsStr = Object.keys(options).reduce((res, key) => {
const hasKey = Object.keys(optsMap).indexOf(key) !== -1;
const mapping = optsMap[key];
return hasKey ? `${res} ${mapping}` : res;
}, '');
return optsStr;
} else {
return '';
}
}


module.exports = function(source) {
// Retrieve the options that were passed to the loader
const options = loaderUtils.getOptions(this);

// Construct the command-line options to the `dart2js` utility
const dart2JsOptionsStr = constructDart2JsOptions(options);

const callback = this.async();
const tmp = path.join(__dirname, 'tmp');
const fname = path.basename(this.resourcePath, '.dart');
const cmd = `dart2js ${dart2JsOptionsStr} -o '${path.join(tmp, `${fname}.js`)}' '${this.resourcePath}'`;
child_process.execSync(`rm -rf '${tmp}'`);
child_process.execSync(`mkdir -p '${tmp}'`);

child_process.exec(cmd, (error, stdout, stderr) => {
// If there was an error
if (error) {
// Output it into the console
const msg = stdout.red;
console.log('\n');
console.error(' ERROR '.black.bgRed, 'Failed to compile dart to js with errors');
console.log(msg);

// Return from the function and call the callback
return callback(error, null);
}
const out = fs.readFileSync(path.join(tmp, `${fname}.js`), 'utf8');
callback(null, out);
});
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"author": "Matt Dziuban",
"description": "Dart loader module for webpack",
"dependencies": {
"colors": "^1.4.0",
"loader-utils": "^1.2.3"
},
"repository": {
"type": "git",
Expand Down